#NEWLINE# def mark( src ) : #NEWLINE# #INDENT# global refs #NEWLINE# t0 = time . time ( ) #NEWLINE# refs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# src = src . replace ( '\r\n' , '\n' ) #NEWLINE# src = re . sub ( '(.*?)\\n=+\\n' , '\n# \\1\n' , src ) #NEWLINE# src = re . sub ( '(.*?)\\n-+\\n' , '\n## \\1\n' , src ) #NEWLINE# lines = ( src . split ( '\n' ) + [ '' ] ) #NEWLINE# i = bq = 0 #NEWLINE# ul = ol = 0 #NEWLINE# while ( i < len ( lines ) ) : #NEWLINE# #INDENT# if lines [ i ] . startswith ( '>' ) : #NEWLINE# #INDENT# nb = 1 #NEWLINE# while ( ( nb < len ( lines [ i ] ) ) and ( lines [ i ] [ nb ] == '>' ) ) : #NEWLINE# #INDENT# nb += 1 #NEWLINE# #UNINDENT# lines [ i ] = lines [ i ] [ nb : ] #NEWLINE# if ( nb > bq ) : #NEWLINE# #INDENT# lines . insert ( i , ( '<blockquote>' * ( nb - bq ) ) ) #NEWLINE# i += 1 #NEWLINE# bq = nb #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( nb < bq ) : #NEWLINE# #INDENT# lines . insert ( i , ( '</blockquote>' * ( bq - nb ) ) ) #NEWLINE# i += 1 #NEWLINE# bq = nb #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( bq > 0 ) : #NEWLINE# #INDENT# lines . insert ( i , ( '</blockquote>' * bq ) ) #NEWLINE# i += 1 #NEWLINE# bq = 0 #NEWLINE# #UNINDENT# #UNINDENT# if ( lines [ i ] . strip ( ) and ( lines [ i ] . lstrip ( ) [ 0 ] in '-+*' ) and ( len ( lines [ i ] . lstrip ( ) ) > 1 ) and ( lines [ i ] . lstrip ( ) [ 1 ] == ' ' ) and ( ( i == 0 ) or ul or ( not lines [ ( i - 1 ) ] . strip ( ) ) ) ) : #NEWLINE# #INDENT# nb = ( ( 1 + len ( lines [ i ] ) ) - len ( lines [ i ] . lstrip ( ) ) ) #NEWLINE# lines [ i ] = ( '<li>' + lines [ i ] [ nb : ] ) #NEWLINE# if ( nb > ul ) : #NEWLINE# #INDENT# lines . insert ( i , ( '<ul>' * ( nb - ul ) ) ) #NEWLINE# i += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( nb < ul ) : #NEWLINE# #INDENT# lines . insert ( i , ( '</ul>' * ( ul - nb ) ) ) #NEWLINE# i += 1 #NEWLINE# #UNINDENT# #UNINDENT# ul = nb #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ul and ( not lines [ i ] . strip ( ) ) ) : #NEWLINE# #INDENT# if ( ( i < ( len ( lines ) - 1 ) ) and lines [ ( i + 1 ) ] . strip ( ) and ( not lines [ ( i + 1 ) ] . startswith ( ' ' ) ) ) : #NEWLINE# #INDENT# nline = lines [ ( i + 1 ) ] . lstrip ( ) #NEWLINE# if ( ( nline [ 0 ] in '-+*' ) and ( len ( nline ) > 1 ) and ( nline [ 1 ] == ' ' ) ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# lines . insert ( i , ( '</ul>' * ul ) ) #NEWLINE# i += 1 #NEWLINE# ul = 0 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# mo = re . search ( '^(\\d+\\.)' , lines [ i ] ) #NEWLINE# if mo : #NEWLINE# #INDENT# if ( not ol ) : #NEWLINE# #INDENT# lines . insert ( i , '<ol>' ) #NEWLINE# i += 1 #NEWLINE# #UNINDENT# lines [ i ] = ( '<li>' + lines [ i ] [ len ( mo . groups ( ) [ 0 ] ) : ] ) #NEWLINE# ol = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ol and ( not lines [ i ] . strip ( ) ) and ( i < ( len ( lines ) - 1 ) ) and ( not lines [ ( i + 1 ) ] . startswith ( ' ' ) ) and ( not re . search ( '^(\\d+\\.)' , lines [ ( i + 1 ) ] ) ) ) : #NEWLINE# #INDENT# lines . insert ( i , '</ol>' ) #NEWLINE# i += 1 #NEWLINE# ol = 0 #NEWLINE# #UNINDENT# #UNINDENT# i += 1 #NEWLINE# #UNINDENT# if ul : #NEWLINE# #INDENT# lines . append ( ( '</ul>' * ul ) ) #NEWLINE# #UNINDENT# if ol : #NEWLINE# #INDENT# lines . append ( ( '</ol>' * ol ) ) #NEWLINE# #UNINDENT# if bq : #NEWLINE# #INDENT# lines . append ( ( '</blockquote>' * bq ) ) #NEWLINE# #UNINDENT# t1 = time . time ( ) #NEWLINE# sections = [ ] #NEWLINE# scripts = [ ] #NEWLINE# section = Marked ( ) #NEWLINE# i = 0 #NEWLINE# while ( i < len ( lines ) ) : #NEWLINE# #INDENT# line = lines [ i ] #NEWLINE# if ( line . strip ( ) and line . startswith ( '    ' ) ) : #NEWLINE# #INDENT# if ( isinstance ( section , Marked ) and section . line ) : #NEWLINE# #INDENT# sections . append ( section ) #NEWLINE# #UNINDENT# section = CodeBlock ( line [ 4 : ] ) #NEWLINE# j = ( i + 1 ) #NEWLINE# while ( ( j < len ( lines ) ) and lines [ j ] . startswith ( '    ' ) ) : #NEWLINE# #INDENT# section . lines . append ( lines [ j ] [ 4 : ] ) #NEWLINE# j += 1 #NEWLINE# #UNINDENT# sections . append ( section ) #NEWLINE# section = Marked ( ) #NEWLINE# i = j #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( line . strip ( ) and line . startswith ( '```' ) ) : #NEWLINE# #INDENT# if ( isinstance ( section , Marked ) and section . line ) : #NEWLINE# #INDENT# sections . append ( section ) #NEWLINE# #UNINDENT# section = CodeBlock ( line ) #NEWLINE# j = ( i + 1 ) #NEWLINE# while ( ( j < len ( lines ) ) and ( not lines [ j ] . startswith ( '```' ) ) ) : #NEWLINE# #INDENT# section . lines . append ( lines [ j ] ) #NEWLINE# j += 1 #NEWLINE# #UNINDENT# sections . append ( section ) #NEWLINE# section = Marked ( ) #NEWLINE# i = ( j + 1 ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . lower ( ) . startswith ( '<script' ) : #NEWLINE# #INDENT# if ( isinstance ( section , Marked ) and section . line ) : #NEWLINE# #INDENT# sections . append ( section ) #NEWLINE# section = Marked ( ) #NEWLINE# #UNINDENT# j = ( i + 1 ) #NEWLINE# while ( j < len ( nb ) ) : #NEWLINE# #INDENT# if lines [ j ] . lower ( ) . startswith ( '</script>' ) : #NEWLINE# #INDENT# scripts . append ( '\n' . join ( lines [ ( i + 1 ) : j ] ) ) #NEWLINE# for k in range ( i , ( j + 1 ) ) : #NEWLINE# #INDENT# lines [ k ] = '' #NEWLINE# #UNINDENT# break #NEWLINE# #UNINDENT# j += 1 #NEWLINE# #UNINDENT# i = j #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . startswith ( '#' ) : #NEWLINE# #INDENT# level = 1 #NEWLINE# line = lines [ i ] #NEWLINE# while ( ( level < len ( line ) ) and ( line [ level ] == '#' ) and ( level <= 6 ) ) : #NEWLINE# #INDENT# level += 1 #NEWLINE# #UNINDENT# if ( not line [ ( level + 1 ) : ] . strip ( ) ) : #NEWLINE# #INDENT# if ( level == 1 ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# lines [ i ] = ( '<H%s>%s</H%s>\n' % ( ( level - 1 ) , '#' , ( level - 1 ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# lines [ i ] = ( '<H%s>%s</H%s>\n' % ( level , line [ ( level + 1 ) : ] , level ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# mo = re . search ( ref_pattern , line ) #NEWLINE# if ( mo is not None ) : #NEWLINE# #INDENT# if ( isinstance ( section , Marked ) and section . line ) : #NEWLINE# #INDENT# sections . append ( section ) #NEWLINE# section = Marked ( ) #NEWLINE# #UNINDENT# key = mo . groups ( ) [ 0 ] #NEWLINE# value = URL ( mo . groups ( ) [ 1 ] ) #NEWLINE# refs [ key . lower ( ) ] = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not line . strip ( ) ) : #NEWLINE# #INDENT# line = '<p></p>' #NEWLINE# #UNINDENT# if section . line : #NEWLINE# #INDENT# section . line += '\n' #NEWLINE# #UNINDENT# section . line += line #NEWLINE# #UNINDENT# i += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# t2 = time . time ( ) #NEWLINE# if ( isinstance ( section , Marked ) and section . line ) : #NEWLINE# #INDENT# sections . append ( section ) #NEWLINE# #UNINDENT# res = '' #NEWLINE# for section in sections : #NEWLINE# #INDENT# ( mk , _scripts ) = section . to_html ( ) #NEWLINE# res += mk #NEWLINE# scripts += _scripts #NEWLINE# #UNINDENT# return ( res , scripts )