<s> """<STR_LIT>""" <EOL> import markdown <EOL> import re <EOL> from urlparse import urlparse , urlunparse <EOL> import sys <EOL> if sys . version >= "<STR_LIT>" : <EOL> from html import entities as htmlentitydefs <EOL> else : <EOL> import htmlentitydefs <EOL> """<STR_LIT>""" <EOL> NOBRACKET = r'<STR_LIT>' <EOL> BRK = ( r'<STR_LIT>' <EOL> + ( NOBRACKET + r'<STR_LIT>' ) * <NUM_LIT:6> <EOL> + ( NOBRACKET + r'<STR_LIT>' ) * <NUM_LIT:6> <EOL> + NOBRACKET + r'<STR_LIT>' ) <EOL> NOIMG = r'<STR_LIT>' <EOL> BACKTICK_RE = r'<STR_LIT>' <EOL> ESCAPE_RE = r'<STR_LIT>' <EOL> EMPHASIS_RE = r'<STR_LIT>' <EOL> STRONG_RE = r'<STR_LIT>' <EOL> STRONG_EM_RE = r'<STR_LIT>' <EOL> if markdown . SMART_EMPHASIS : <EOL> EMPHASIS_2_RE = r'<STR_LIT>' <EOL> else : <EOL> EMPHASIS_2_RE = r'<STR_LIT>' <EOL> LINK_RE = NOIMG + BRK + r'''<STR_LIT>''' <EOL> IMAGE_LINK_RE = r'<STR_LIT>' + BRK + r'<STR_LIT>' <EOL> REFERENCE_RE = NOIMG + BRK + r'<STR_LIT>' <EOL> IMAGE_REFERENCE_RE = r'<STR_LIT>' + BRK + '<STR_LIT>' <EOL> NOT_STRONG_RE = r'<STR_LIT>' <EOL> AUTOLINK_RE = r'<STR_LIT>' <EOL> AUTOMAIL_RE = r'<STR_LIT>' <EOL> HTML_RE = r'<STR_LIT>' <EOL> ENTITY_RE = r'<STR_LIT>' <EOL> LINE_BREAK_RE = r'<STR_LIT>' <EOL> LINE_BREAK_2_RE = r'<STR_LIT>' <EOL> def dequote ( string ) : <EOL> """<STR_LIT>""" <EOL> if ( ( string . startswith ( '<STR_LIT:">' ) and string . endswith ( '<STR_LIT:">' ) ) <EOL> or ( string . startswith ( "<STR_LIT:'>" ) and string . endswith ( "<STR_LIT:'>" ) ) ) : <EOL> return string [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> return string <EOL> ATTR_RE = re . compile ( "<STR_LIT>" ) <EOL> def handleAttributes ( text , parent ) : <EOL> """<STR_LIT>""" <EOL> def attributeCallback ( match ) : <EOL> parent . set ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) ) <EOL> return ATTR_RE . sub ( attributeCallback , text ) <EOL> """<STR_LIT>""" <EOL> class Pattern : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern , markdown_instance = None ) : <EOL> """<STR_LIT>""" <EOL> self . pattern = pattern <EOL> self . compiled_re = re . compile ( "<STR_LIT>" % pattern , re . DOTALL ) <EOL> self . safe_mode = False <EOL> if markdown_instance : <EOL> self . markdown = markdown_instance <EOL> def getCompiledRegExp ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . compiled_re <EOL> def handleMatch ( self , m ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . __name__ <EOL> BasePattern = Pattern <EOL> class SimpleTextPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> text = m . group ( <NUM_LIT:2> ) <EOL> if text == markdown . INLINE_PLACEHOLDER_PREFIX : <EOL> return None <EOL> return text <EOL> class SimpleTagPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern , tag ) : <EOL> Pattern . __init__ ( self , pattern ) <EOL> self . tag = tag <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( self . tag ) <EOL> el . text = m . group ( <NUM_LIT:3> ) <EOL> return el <EOL> class SubstituteTagPattern ( SimpleTagPattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> return markdown . etree . Element ( self . tag ) <EOL> class BacktickPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> Pattern . __init__ ( self , pattern ) <EOL> self . tag = "<STR_LIT:code>" <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( self . tag ) <EOL> el . text = markdown . AtomicString ( m . group ( <NUM_LIT:3> ) . strip ( ) ) <EOL> return el <EOL> class DoubleTagPattern ( SimpleTagPattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> tag1 , tag2 = self . tag . split ( "<STR_LIT:U+002C>" ) <EOL> el1 = markdown . etree . Element ( tag1 ) <EOL> el2 = markdown . etree . SubElement ( el1 , tag2 ) <EOL> el2 . text = m . group ( <NUM_LIT:3> ) <EOL> return el1 <EOL> class HtmlPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> rawhtml = m . group ( <NUM_LIT:2> ) <EOL> inline = True <EOL> place_holder = self . markdown . htmlStash . store ( rawhtml ) <EOL> return place_holder <EOL> class LinkPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( "<STR_LIT:a>" ) <EOL> el . text = m . group ( <NUM_LIT:2> ) <EOL> title = m . group ( <NUM_LIT:11> ) <EOL> href = m . group ( <NUM_LIT:9> ) <EOL> if href : <EOL> if href [ <NUM_LIT:0> ] == "<STR_LIT:<>" : <EOL> href = href [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> el . set ( "<STR_LIT>" , self . sanitize_url ( href . strip ( ) ) ) <EOL> else : <EOL> el . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if title : <EOL> title = dequote ( title ) <EOL> el . set ( "<STR_LIT:title>" , title ) <EOL> return el <EOL> def sanitize_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> locless_schemes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> scheme , netloc , path , params , query , fragment = url = urlparse ( url ) <EOL> safe_url = False <EOL> if netloc != '<STR_LIT>' or scheme in locless_schemes : <EOL> safe_url = True <EOL> for part in url [ <NUM_LIT:2> : ] : <EOL> if "<STR_LIT::>" in part : <EOL> safe_url = False <EOL> if self . markdown . safeMode and not safe_url : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return urlunparse ( url ) <EOL> class ImagePattern ( LinkPattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( "<STR_LIT>" ) <EOL> src_parts = m . group ( <NUM_LIT:9> ) . split ( ) <EOL> if src_parts : <EOL> src = src_parts [ <NUM_LIT:0> ] <EOL> if src [ <NUM_LIT:0> ] == "<STR_LIT:<>" and src [ - <NUM_LIT:1> ] == "<STR_LIT:>>" : <EOL> src = src [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> el . set ( '<STR_LIT:src>' , self . sanitize_url ( src ) ) <EOL> else : <EOL> el . set ( '<STR_LIT:src>' , "<STR_LIT>" ) <EOL> if len ( src_parts ) > <NUM_LIT:1> : <EOL> el . set ( '<STR_LIT:title>' , dequote ( "<STR_LIT:U+0020>" . join ( src_parts [ <NUM_LIT:1> : ] ) ) ) <EOL> if markdown . ENABLE_ATTRIBUTES : <EOL> truealt = handleAttributes ( m . group ( <NUM_LIT:2> ) , el ) <EOL> else : <EOL> truealt = m . group ( <NUM_LIT:2> ) <EOL> el . set ( '<STR_LIT>' , truealt ) <EOL> return el <EOL> class ReferencePattern ( LinkPattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> if m . group ( <NUM_LIT:9> ) : <EOL> id = m . group ( <NUM_LIT:9> ) . lower ( ) <EOL> else : <EOL> id = m . group ( <NUM_LIT:2> ) . lower ( ) <EOL> if not id in self . markdown . references : <EOL> return None <EOL> href , title = self . markdown . references [ id ] <EOL> text = m . group ( <NUM_LIT:2> ) <EOL> return self . makeTag ( href , title , text ) <EOL> def makeTag ( self , href , title , text ) : <EOL> el = markdown . etree . Element ( '<STR_LIT:a>' ) <EOL> el . set ( '<STR_LIT>' , self . sanitize_url ( href ) ) <EOL> if title : <EOL> el . set ( '<STR_LIT:title>' , title ) <EOL> el . text = text <EOL> return el <EOL> class ImageReferencePattern ( ReferencePattern ) : <EOL> """<STR_LIT>""" <EOL> def makeTag ( self , href , title , text ) : <EOL> el = markdown . etree . Element ( "<STR_LIT>" ) <EOL> el . set ( "<STR_LIT:src>" , self . sanitize_url ( href ) ) <EOL> if title : <EOL> el . set ( "<STR_LIT:title>" , title ) <EOL> el . set ( "<STR_LIT>" , text ) <EOL> return el <EOL> class AutolinkPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( "<STR_LIT:a>" ) <EOL> el . set ( '<STR_LIT>' , m . group ( <NUM_LIT:2> ) ) <EOL> el . text = markdown . AtomicString ( m . group ( <NUM_LIT:2> ) ) <EOL> return el <EOL> class AutomailPattern ( Pattern ) : <EOL> """<STR_LIT>""" <EOL> def handleMatch ( self , m ) : <EOL> el = markdown . etree . Element ( '<STR_LIT:a>' ) <EOL> email = m . group ( <NUM_LIT:2> ) <EOL> if email . startswith ( "<STR_LIT>" ) : <EOL> email = email [ len ( "<STR_LIT>" ) : ] <EOL> def codepoint2name ( code ) : <EOL> """<STR_LIT>""" <EOL> entity = htmlentitydefs . codepoint2name . get ( code ) <EOL> if entity : <EOL> return "<STR_LIT>" % ( markdown . AMP_SUBSTITUTE , entity ) <EOL> else : <EOL> return "<STR_LIT>" % ( markdown . AMP_SUBSTITUTE , code ) <EOL> letters = [ codepoint2name ( ord ( letter ) ) for letter in email ] <EOL> el . text = markdown . AtomicString ( '<STR_LIT>' . join ( letters ) ) <EOL> mailto = "<STR_LIT>" + email <EOL> mailto = "<STR_LIT>" . join ( [ markdown . AMP_SUBSTITUTE + '<STR_LIT>' % <EOL> ord ( letter ) for letter in mailto ] ) <EOL> el . set ( '<STR_LIT>' , mailto ) <EOL> return el </s>
