<s> """<STR_LIT>""" <EOL> import re <EOL> import markdown <EOL> HTML_PLACEHOLDER_PREFIX = markdown . STX + "<STR_LIT>" <EOL> HTML_PLACEHOLDER = HTML_PLACEHOLDER_PREFIX + "<STR_LIT>" + markdown . ETX <EOL> class Processor : <EOL> def __init__ ( self , markdown_instance = None ) : <EOL> if markdown_instance : <EOL> self . markdown = markdown_instance <EOL> class Preprocessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HtmlStash : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . html_counter = <NUM_LIT:0> <EOL> self . rawHtmlBlocks = [ ] <EOL> def store ( self , html , safe = False ) : <EOL> """<STR_LIT>""" <EOL> self . rawHtmlBlocks . append ( ( html , safe ) ) <EOL> placeholder = HTML_PLACEHOLDER % self . html_counter <EOL> self . html_counter += <NUM_LIT:1> <EOL> return placeholder <EOL> def reset ( self ) : <EOL> self . html_counter = <NUM_LIT:0> <EOL> self . rawHtmlBlocks = [ ] <EOL> class HtmlBlockPreprocessor ( Preprocessor ) : <EOL> """<STR_LIT>""" <EOL> right_tag_patterns = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def _get_left_tag ( self , block ) : <EOL> return block [ <NUM_LIT:1> : ] . replace ( "<STR_LIT:>>" , "<STR_LIT:U+0020>" , <NUM_LIT:1> ) . split ( ) [ <NUM_LIT:0> ] . lower ( ) <EOL> def _get_right_tag ( self , left_tag , block ) : <EOL> for p in self . right_tag_patterns : <EOL> tag = p % left_tag <EOL> i = block . rfind ( tag ) <EOL> if i > <NUM_LIT:2> : <EOL> return tag . lstrip ( "<STR_LIT:<>" ) . rstrip ( "<STR_LIT:>>" ) , i + len ( p ) - <NUM_LIT:2> + len ( left_tag ) <EOL> return block . rstrip ( ) [ - len ( left_tag ) - <NUM_LIT:2> : - <NUM_LIT:1> ] . lower ( ) , len ( block ) <EOL> def _equal_tags ( self , left_tag , right_tag ) : <EOL> if left_tag == '<STR_LIT>' or left_tag [ <NUM_LIT:0> ] in [ '<STR_LIT:?>' , '<STR_LIT:@>' , '<STR_LIT:%>' ] : <EOL> return True <EOL> if ( "<STR_LIT:/>" + left_tag ) == right_tag : <EOL> return True <EOL> if ( right_tag == "<STR_LIT>" and left_tag == "<STR_LIT>" ) : <EOL> return True <EOL> elif left_tag == right_tag [ <NUM_LIT:1> : ] and right_tag [ <NUM_LIT:0> ] != "<STR_LIT:<>" : <EOL> return True <EOL> else : <EOL> return False <EOL> def _is_oneliner ( self , tag ) : <EOL> return ( tag in [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def run ( self , lines ) : <EOL> text = "<STR_LIT:\n>" . join ( lines ) <EOL> new_blocks = [ ] <EOL> text = text . split ( "<STR_LIT>" ) <EOL> items = [ ] <EOL> left_tag = '<STR_LIT>' <EOL> right_tag = '<STR_LIT>' <EOL> in_tag = False <EOL> while text : <EOL> block = text [ <NUM_LIT:0> ] <EOL> if block . startswith ( "<STR_LIT:\n>" ) : <EOL> block = block [ <NUM_LIT:1> : ] <EOL> text = text [ <NUM_LIT:1> : ] <EOL> if block . startswith ( "<STR_LIT:\n>" ) : <EOL> block = block [ <NUM_LIT:1> : ] <EOL> if not in_tag : <EOL> if block . startswith ( "<STR_LIT:<>" ) : <EOL> left_tag = self . _get_left_tag ( block ) <EOL> right_tag , data_index = self . _get_right_tag ( left_tag , block ) <EOL> if block [ <NUM_LIT:1> ] == "<STR_LIT:!>" : <EOL> left_tag = "<STR_LIT>" <EOL> right_tag , data_index = self . _get_right_tag ( left_tag , block ) <EOL> if data_index < len ( block ) and markdown . isBlockLevel ( left_tag ) : <EOL> text . insert ( <NUM_LIT:0> , block [ data_index : ] ) <EOL> block = block [ : data_index ] <EOL> if not ( markdown . isBlockLevel ( left_tag ) or block [ <NUM_LIT:1> ] in [ "<STR_LIT:!>" , "<STR_LIT:?>" , "<STR_LIT:@>" , "<STR_LIT:%>" ] ) : <EOL> new_blocks . append ( block ) <EOL> continue <EOL> if self . _is_oneliner ( left_tag ) : <EOL> new_blocks . append ( block . strip ( ) ) <EOL> continue <EOL> if block . rstrip ( ) . endswith ( "<STR_LIT:>>" ) and self . _equal_tags ( left_tag , right_tag ) : <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( block . strip ( ) ) ) <EOL> continue <EOL> else : <EOL> if markdown . isBlockLevel ( left_tag ) or left_tag == "<STR_LIT>" and not block . rstrip ( ) . endswith ( "<STR_LIT:>>" ) : <EOL> items . append ( block . strip ( ) ) <EOL> in_tag = True <EOL> else : <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( block . strip ( ) ) ) <EOL> continue <EOL> new_blocks . append ( block ) <EOL> else : <EOL> items . append ( block . strip ( ) ) <EOL> right_tag , data_index = self . _get_right_tag ( left_tag , block ) <EOL> if self . _equal_tags ( left_tag , right_tag ) : <EOL> in_tag = False <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( '<STR_LIT>' . join ( items ) ) ) <EOL> items = [ ] <EOL> if items : <EOL> new_blocks . append ( self . markdown . htmlStash . store ( '<STR_LIT>' . join ( items ) ) ) <EOL> new_blocks . append ( '<STR_LIT:\n>' ) <EOL> new_text = "<STR_LIT>" . join ( new_blocks ) <EOL> return new_text . split ( "<STR_LIT:\n>" ) <EOL> class ReferencePreprocessor ( Preprocessor ) : <EOL> """<STR_LIT>""" <EOL> RE = re . compile ( r'<STR_LIT>' , re . DOTALL ) <EOL> def run ( self , lines ) : <EOL> new_text = [ ] ; <EOL> for line in lines : <EOL> m = self . RE . match ( line ) <EOL> if m : <EOL> id = m . group ( <NUM_LIT:2> ) . strip ( ) . lower ( ) <EOL> t = m . group ( <NUM_LIT:4> ) . strip ( ) <EOL> if not t : <EOL> self . markdown . references [ id ] = ( m . group ( <NUM_LIT:3> ) , t ) <EOL> elif ( len ( t ) >= <NUM_LIT:2> <EOL> and ( t [ <NUM_LIT:0> ] == t [ - <NUM_LIT:1> ] == "<STR_LIT>" <EOL> or t [ <NUM_LIT:0> ] == t [ - <NUM_LIT:1> ] == "<STR_LIT>" <EOL> or ( t [ <NUM_LIT:0> ] == "<STR_LIT:(>" and t [ - <NUM_LIT:1> ] == "<STR_LIT:)>" ) ) ) : <EOL> self . markdown . references [ id ] = ( m . group ( <NUM_LIT:3> ) , t [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> else : <EOL> new_text . append ( line ) <EOL> else : <EOL> new_text . append ( line ) <EOL> return new_text </s>
