<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from . import util <EOL> from . import odict <EOL> import re <EOL> def build_preprocessors ( md_instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> preprocessors = odict . OrderedDict ( ) <EOL> preprocessors [ '<STR_LIT>' ] = NormalizeWhitespace ( md_instance ) <EOL> if md_instance . safeMode != '<STR_LIT>' : <EOL> preprocessors [ "<STR_LIT>" ] = HtmlBlockPreprocessor ( md_instance ) <EOL> preprocessors [ "<STR_LIT>" ] = ReferencePreprocessor ( md_instance ) <EOL> return preprocessors <EOL> class Preprocessor ( util . Processor ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NormalizeWhitespace ( Preprocessor ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , lines ) : <EOL> source = '<STR_LIT:\n>' . join ( lines ) <EOL> source = source . replace ( util . STX , "<STR_LIT>" ) . replace ( util . ETX , "<STR_LIT>" ) <EOL> source = source . replace ( "<STR_LIT:\r\n>" , "<STR_LIT:\n>" ) . replace ( "<STR_LIT:\r>" , "<STR_LIT:\n>" ) + "<STR_LIT>" <EOL> source = source . expandtabs ( self . markdown . tab_length ) <EOL> source = re . sub ( r'<STR_LIT>' , '<STR_LIT:\n>' , source ) <EOL> return source . split ( '<STR_LIT:\n>' ) <EOL> class HtmlBlockPreprocessor ( Preprocessor ) : <EOL> """<STR_LIT>""" <EOL> right_tag_patterns = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> attrs_pattern = r"""<STR_LIT>""" <EOL> left_tag_pattern = r'<STR_LIT>' % attrs_pattern <EOL> attrs_re = re . compile ( attrs_pattern , re . VERBOSE ) <EOL> left_tag_re = re . compile ( left_tag_pattern , re . VERBOSE ) <EOL> markdown_in_raw = False <EOL> def _get_left_tag ( self , block ) : <EOL> m = self . left_tag_re . match ( block ) <EOL> if m : <EOL> tag = m . group ( '<STR_LIT>' ) <EOL> raw_attrs = m . group ( '<STR_LIT>' ) <EOL> attrs = { } <EOL> if raw_attrs : <EOL> for ma in self . attrs_re . finditer ( raw_attrs ) : <EOL> if ma . group ( '<STR_LIT>' ) : <EOL> if ma . group ( '<STR_LIT:value>' ) : <EOL> attrs [ ma . group ( '<STR_LIT>' ) . strip ( ) ] = ma . group ( '<STR_LIT:value>' ) <EOL> else : <EOL> attrs [ ma . group ( '<STR_LIT>' ) . strip ( ) ] = "<STR_LIT>" <EOL> elif ma . group ( '<STR_LIT>' ) : <EOL> if ma . group ( '<STR_LIT>' ) : <EOL> attrs [ ma . group ( '<STR_LIT>' ) . strip ( ) ] = ma . group ( '<STR_LIT>' ) <EOL> else : <EOL> attrs [ ma . group ( '<STR_LIT>' ) . strip ( ) ] = "<STR_LIT>" <EOL> elif ma . group ( '<STR_LIT>' ) : <EOL> attrs [ ma . group ( '<STR_LIT>' ) . strip ( ) ] = "<STR_LIT>" <EOL> return tag , len ( m . group ( <NUM_LIT:0> ) ) , attrs <EOL> else : <EOL> tag = block [ <NUM_LIT:1> : ] . split ( "<STR_LIT:>>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . lower ( ) <EOL> return tag , len ( tag ) + <NUM_LIT:2> , { } <EOL> def _recursive_tagfind ( self , ltag , rtag , start_index , block ) : <EOL> while <NUM_LIT:1> : <EOL> i = block . find ( rtag , start_index ) <EOL> if i == - <NUM_LIT:1> : <EOL> return - <NUM_LIT:1> <EOL> j = block . find ( ltag , start_index ) <EOL> if ( j > i or j == - <NUM_LIT:1> ) : <EOL> return i + len ( rtag ) <EOL> j = block . find ( '<STR_LIT:>>' , j ) <EOL> start_index = self . _recursive_tagfind ( ltag , rtag , j + <NUM_LIT:1> , block ) <EOL> if start_index == - <NUM_LIT:1> : <EOL> return - <NUM_LIT:1> <EOL> def _get_right_tag ( self , left_tag , left_index , block ) : <EOL> for p in self . right_tag_patterns : <EOL> tag = p % left_tag <EOL> i = self . _recursive_tagfind ( "<STR_LIT>" % left_tag , tag , left_index , block ) <EOL> if i > <NUM_LIT:2> : <EOL> return tag . lstrip ( "<STR_LIT:<>" ) . rstrip ( "<STR_LIT:>>" ) , i <EOL> return block . rstrip ( ) [ - left_index : - <NUM_LIT:1> ] . lower ( ) , len ( block ) <EOL> def _equal_tags ( self , left_tag , right_tag ) : <EOL> if 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 . rsplit ( "<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:<>" ) and len ( block . strip ( ) ) > <NUM_LIT:1> : <EOL> if block [ <NUM_LIT:1> ] == "<STR_LIT:!>" : <EOL> left_tag , left_index , attrs = "<STR_LIT>" , <NUM_LIT:2> , { } <EOL> else : <EOL> left_tag , left_index , attrs = self . _get_left_tag ( block ) <EOL> right_tag , data_index = self . _get_right_tag ( left_tag , <EOL> left_index , <EOL> block ) <EOL> if data_index < len ( block ) and ( util . isBlockLevel ( left_tag ) <EOL> or left_tag == '<STR_LIT>' ) : <EOL> text . insert ( <NUM_LIT:0> , block [ data_index : ] ) <EOL> block = block [ : data_index ] <EOL> if not ( util . 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> if self . markdown_in_raw and '<STR_LIT>' in attrs . keys ( ) : <EOL> start = re . sub ( r'<STR_LIT>' , <EOL> '<STR_LIT>' , block [ : left_index ] ) <EOL> end = block [ - len ( right_tag ) - <NUM_LIT:2> : ] <EOL> block = block [ left_index : - len ( right_tag ) - <NUM_LIT:2> ] <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( start ) ) <EOL> new_blocks . append ( block ) <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( end ) ) <EOL> else : <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( block . strip ( ) ) ) <EOL> continue <EOL> else : <EOL> if util . 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 ) <EOL> right_tag , data_index = self . _get_right_tag ( left_tag , <NUM_LIT:0> , block ) <EOL> if self . _equal_tags ( left_tag , right_tag ) : <EOL> if data_index < len ( block ) : <EOL> items [ - <NUM_LIT:1> ] = block [ : data_index ] <EOL> text . insert ( <NUM_LIT:0> , block [ data_index : ] ) <EOL> in_tag = False <EOL> if self . markdown_in_raw and '<STR_LIT>' in attrs . keys ( ) : <EOL> start = re . sub ( r'<STR_LIT>' , <EOL> '<STR_LIT>' , items [ <NUM_LIT:0> ] [ : left_index ] ) <EOL> items [ <NUM_LIT:0> ] = items [ <NUM_LIT:0> ] [ left_index : ] <EOL> end = items [ - <NUM_LIT:1> ] [ - len ( right_tag ) - <NUM_LIT:2> : ] <EOL> items [ - <NUM_LIT:1> ] = items [ - <NUM_LIT:1> ] [ : - len ( right_tag ) - <NUM_LIT:2> ] <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( start ) ) <EOL> new_blocks . extend ( items ) <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( end ) ) <EOL> else : <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( '<STR_LIT>' . join ( items ) ) ) <EOL> items = [ ] <EOL> if items : <EOL> if self . markdown_in_raw and '<STR_LIT>' in attrs . keys ( ) : <EOL> start = re . sub ( r'<STR_LIT>' , <EOL> '<STR_LIT>' , items [ <NUM_LIT:0> ] [ : left_index ] ) <EOL> items [ <NUM_LIT:0> ] = items [ <NUM_LIT:0> ] [ left_index : ] <EOL> end = items [ - <NUM_LIT:1> ] [ - len ( right_tag ) - <NUM_LIT:2> : ] <EOL> items [ - <NUM_LIT:1> ] = items [ - <NUM_LIT:1> ] [ : - len ( right_tag ) - <NUM_LIT:2> ] <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( start ) ) <EOL> new_blocks . extend ( items ) <EOL> if end . strip ( ) : <EOL> new_blocks . append ( <EOL> self . markdown . htmlStash . store ( end ) ) <EOL> else : <EOL> new_blocks . append ( <EOL> 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> TITLE = r'<STR_LIT>' <EOL> RE = re . compile ( r'<STR_LIT>' % TITLE , re . DOTALL ) <EOL> TITLE_RE = re . compile ( r'<STR_LIT>' % TITLE ) <EOL> def run ( self , lines ) : <EOL> new_text = [ ] ; <EOL> while lines : <EOL> line = lines . pop ( <NUM_LIT:0> ) <EOL> m = self . RE . match ( line ) <EOL> if m : <EOL> id = m . group ( <NUM_LIT:1> ) . strip ( ) . lower ( ) <EOL> link = m . group ( <NUM_LIT:2> ) . lstrip ( '<STR_LIT:<>' ) . rstrip ( '<STR_LIT:>>' ) <EOL> t = m . group ( <NUM_LIT:5> ) or m . group ( <NUM_LIT:6> ) or m . group ( <NUM_LIT:7> ) <EOL> if not t : <EOL> tm = self . TITLE_RE . match ( lines [ <NUM_LIT:0> ] ) <EOL> if tm : <EOL> lines . pop ( <NUM_LIT:0> ) <EOL> t = tm . group ( <NUM_LIT:2> ) or tm . group ( <NUM_LIT:3> ) or tm . group ( <NUM_LIT:4> ) <EOL> self . markdown . references [ id ] = ( link , t ) <EOL> else : <EOL> new_text . append ( line ) <EOL> return new_text </s>
