<s> from __future__ import unicode_literals <EOL> from __future__ import absolute_import <EOL> from . import util <EOL> from . import odict <EOL> from . import inlinepatterns <EOL> def build_treeprocessors ( md_instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> treeprocessors = odict . OrderedDict ( ) <EOL> treeprocessors [ "<STR_LIT>" ] = InlineProcessor ( md_instance ) <EOL> treeprocessors [ "<STR_LIT>" ] = PrettifyTreeprocessor ( md_instance ) <EOL> return treeprocessors <EOL> def isString ( s ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( s , util . AtomicString ) : <EOL> return isinstance ( s , util . string_type ) <EOL> return False <EOL> class Treeprocessor ( util . Processor ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , root ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InlineProcessor ( Treeprocessor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , md ) : <EOL> self . __placeholder_prefix = util . INLINE_PLACEHOLDER_PREFIX <EOL> self . __placeholder_suffix = util . ETX <EOL> self . __placeholder_length = <NUM_LIT:4> + len ( self . __placeholder_prefix ) + len ( self . __placeholder_suffix ) <EOL> self . __placeholder_re = util . INLINE_PLACEHOLDER_RE <EOL> self . markdown = md <EOL> def __makePlaceholder ( self , type ) : <EOL> """<STR_LIT>""" <EOL> id = "<STR_LIT>" % len ( self . stashed_nodes ) <EOL> hash = util . INLINE_PLACEHOLDER % id <EOL> return hash , id <EOL> def __findPlaceholder ( self , data , index ) : <EOL> """<STR_LIT>""" <EOL> m = self . __placeholder_re . search ( data , index ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) , m . end ( ) <EOL> else : <EOL> return None , index + <NUM_LIT:1> <EOL> def __stashNode ( self , node , type ) : <EOL> """<STR_LIT>""" <EOL> placeholder , id = self . __makePlaceholder ( type ) <EOL> self . stashed_nodes [ id ] = node <EOL> return placeholder <EOL> def __handleInline ( self , data , patternIndex = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( data , util . AtomicString ) : <EOL> startIndex = <NUM_LIT:0> <EOL> while patternIndex < len ( self . markdown . inlinePatterns ) : <EOL> data , matched , startIndex = self . __applyPattern ( <EOL> self . markdown . inlinePatterns . value_for_index ( patternIndex ) , <EOL> data , patternIndex , startIndex ) <EOL> if not matched : <EOL> patternIndex += <NUM_LIT:1> <EOL> return data <EOL> def __processElementText ( self , node , subnode , isText = True ) : <EOL> """<STR_LIT>""" <EOL> if isText : <EOL> text = subnode . text <EOL> subnode . text = None <EOL> else : <EOL> text = subnode . tail <EOL> subnode . tail = None <EOL> childResult = self . __processPlaceholders ( text , subnode ) <EOL> if not isText and node is not subnode : <EOL> pos = node . getchildren ( ) . index ( subnode ) <EOL> node . remove ( subnode ) <EOL> else : <EOL> pos = <NUM_LIT:0> <EOL> childResult . reverse ( ) <EOL> for newChild in childResult : <EOL> node . insert ( pos , newChild ) <EOL> def __processPlaceholders ( self , data , parent ) : <EOL> """<STR_LIT>""" <EOL> def linkText ( text ) : <EOL> if text : <EOL> if result : <EOL> if result [ - <NUM_LIT:1> ] . tail : <EOL> result [ - <NUM_LIT:1> ] . tail += text <EOL> else : <EOL> result [ - <NUM_LIT:1> ] . tail = text <EOL> else : <EOL> if parent . text : <EOL> parent . text += text <EOL> else : <EOL> parent . text = text <EOL> result = [ ] <EOL> strartIndex = <NUM_LIT:0> <EOL> while data : <EOL> index = data . find ( self . __placeholder_prefix , strartIndex ) <EOL> if index != - <NUM_LIT:1> : <EOL> id , phEndIndex = self . __findPlaceholder ( data , index ) <EOL> if id in self . stashed_nodes : <EOL> node = self . stashed_nodes . get ( id ) <EOL> if index > <NUM_LIT:0> : <EOL> text = data [ strartIndex : index ] <EOL> linkText ( text ) <EOL> if not isString ( node ) : <EOL> for child in [ node ] + node . getchildren ( ) : <EOL> if child . tail : <EOL> if child . tail . strip ( ) : <EOL> self . __processElementText ( node , child , False ) <EOL> if child . text : <EOL> if child . text . strip ( ) : <EOL> self . __processElementText ( child , child ) <EOL> else : <EOL> linkText ( node ) <EOL> strartIndex = phEndIndex <EOL> continue <EOL> strartIndex = phEndIndex <EOL> result . append ( node ) <EOL> else : <EOL> end = index + len ( self . __placeholder_prefix ) <EOL> linkText ( data [ strartIndex : end ] ) <EOL> strartIndex = end <EOL> else : <EOL> text = data [ strartIndex : ] <EOL> if isinstance ( data , util . AtomicString ) : <EOL> text = util . AtomicString ( text ) <EOL> linkText ( text ) <EOL> data = "<STR_LIT>" <EOL> return result <EOL> def __applyPattern ( self , pattern , data , patternIndex , startIndex = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> match = pattern . getCompiledRegExp ( ) . match ( data [ startIndex : ] ) <EOL> leftData = data [ : startIndex ] <EOL> if not match : <EOL> return data , False , <NUM_LIT:0> <EOL> node = pattern . handleMatch ( match ) <EOL> if node is None : <EOL> return data , True , len ( leftData ) + match . span ( len ( match . groups ( ) ) ) [ <NUM_LIT:0> ] <EOL> if not isString ( node ) : <EOL> if not isinstance ( node . text , util . AtomicString ) : <EOL> for child in [ node ] + node . getchildren ( ) : <EOL> if not isString ( node ) : <EOL> if child . text : <EOL> child . text = self . __handleInline ( child . text , <EOL> patternIndex + <NUM_LIT:1> ) <EOL> if child . tail : <EOL> child . tail = self . __handleInline ( child . tail , <EOL> patternIndex ) <EOL> placeholder = self . __stashNode ( node , pattern . type ( ) ) <EOL> return "<STR_LIT>" % ( leftData , <EOL> match . group ( <NUM_LIT:1> ) , <EOL> placeholder , match . groups ( ) [ - <NUM_LIT:1> ] ) , True , <NUM_LIT:0> <EOL> def run ( self , tree ) : <EOL> """<STR_LIT>""" <EOL> self . stashed_nodes = { } <EOL> stack = [ tree ] <EOL> while stack : <EOL> currElement = stack . pop ( ) <EOL> insertQueue = [ ] <EOL> for child in currElement . getchildren ( ) : <EOL> if child . text and not isinstance ( child . text , util . AtomicString ) : <EOL> text = child . text <EOL> child . text = None <EOL> lst = self . __processPlaceholders ( self . __handleInline ( <EOL> text ) , child ) <EOL> stack += lst <EOL> insertQueue . append ( ( child , lst ) ) <EOL> if child . tail : <EOL> tail = self . __handleInline ( child . tail ) <EOL> dumby = util . etree . Element ( '<STR_LIT:d>' ) <EOL> tailResult = self . __processPlaceholders ( tail , dumby ) <EOL> if dumby . text : <EOL> child . tail = dumby . text <EOL> else : <EOL> child . tail = None <EOL> pos = currElement . getchildren ( ) . index ( child ) + <NUM_LIT:1> <EOL> tailResult . reverse ( ) <EOL> for newChild in tailResult : <EOL> currElement . insert ( pos , newChild ) <EOL> if child . getchildren ( ) : <EOL> stack . append ( child ) <EOL> for element , lst in insertQueue : <EOL> if self . markdown . enable_attributes : <EOL> if element . text and isString ( element . text ) : <EOL> element . text = inlinepatterns . handleAttributes ( element . text , <EOL> element ) <EOL> i = <NUM_LIT:0> <EOL> for newChild in lst : <EOL> if self . markdown . enable_attributes : <EOL> if newChild . tail and isString ( newChild . tail ) : <EOL> newChild . tail = inlinepatterns . handleAttributes ( newChild . tail , <EOL> element ) <EOL> if newChild . text and isString ( newChild . text ) : <EOL> newChild . text = inlinepatterns . handleAttributes ( newChild . text , <EOL> newChild ) <EOL> element . insert ( i , newChild ) <EOL> i += <NUM_LIT:1> <EOL> return tree <EOL> class PrettifyTreeprocessor ( Treeprocessor ) : <EOL> """<STR_LIT>""" <EOL> def _prettifyETree ( self , elem ) : <EOL> """<STR_LIT>""" <EOL> i = "<STR_LIT:\n>" <EOL> if util . isBlockLevel ( elem . tag ) and elem . tag not in [ '<STR_LIT:code>' , '<STR_LIT>' ] : <EOL> if ( not elem . text or not elem . text . strip ( ) ) and len ( elem ) and util . isBlockLevel ( elem [ <NUM_LIT:0> ] . tag ) : <EOL> elem . text = i <EOL> for e in elem : <EOL> if util . isBlockLevel ( e . tag ) : <EOL> self . _prettifyETree ( e ) <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> def run ( self , root ) : <EOL> """<STR_LIT>""" <EOL> self . _prettifyETree ( root ) <EOL> brs = root . getiterator ( '<STR_LIT>' ) <EOL> for br in brs : <EOL> if not br . tail or not br . tail . strip ( ) : <EOL> br . tail = '<STR_LIT:\n>' <EOL> else : <EOL> br . tail = '<STR_LIT>' % br . tail <EOL> pres = root . getiterator ( '<STR_LIT>' ) <EOL> for pre in pres : <EOL> if len ( pre ) and pre [ <NUM_LIT:0> ] . tag == '<STR_LIT:code>' : <EOL> pre [ <NUM_LIT:0> ] . text = pre [ <NUM_LIT:0> ] . text . rstrip ( ) + '<STR_LIT:\n>' </s>
