<s> """<STR_LIT>""" <EOL> import re <EOL> import markdown <EOL> FENCED_BLOCK_RE = re . compile ( r'<STR_LIT>' , re . MULTILINE | re . DOTALL ) <EOL> CODE_WRAP = '<STR_LIT>' <EOL> LANG_TAG = '<STR_LIT>' <EOL> def newline_callback ( matchobj ) : <EOL> if len ( matchobj . group ( <NUM_LIT:1> ) ) == <NUM_LIT:1> : <EOL> return matchobj . group ( <NUM_LIT:0> ) . rstrip ( ) + '<STR_LIT>' <EOL> else : <EOL> return matchobj . group ( <NUM_LIT:0> ) <EOL> class FloydExtension ( markdown . Extension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , configs ) : <EOL> self . config = { } <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . registerExtension ( self ) <EOL> md . parser . blockprocessors . add ( '<STR_LIT>' , DotHeaderProcessor ( md . parser ) , '<STR_LIT>' ) <EOL> md . preprocessors . add ( '<STR_LIT>' , FencedBlockPreprocessor ( md ) , "<STR_LIT>" ) <EOL> md . preprocessors . add ( '<STR_LIT>' , DoubleNewlinePreprocessor ( md ) , "<STR_LIT>" ) <EOL> def reset ( self ) : <EOL> pass <EOL> class FencedBlockPreprocessor ( markdown . preprocessors . Preprocessor ) : <EOL> def run ( self , lines ) : <EOL> text = '<STR_LIT:\n>' . join ( lines ) <EOL> m = FENCED_BLOCK_RE . search ( text ) <EOL> if m : <EOL> lang = '<STR_LIT>' <EOL> if m . group ( '<STR_LIT>' ) : <EOL> lang = LANG_TAG % m . group ( '<STR_LIT>' ) <EOL> code = CODE_WRAP % ( lang , self . _escape ( m . group ( '<STR_LIT:code>' ) ) ) <EOL> placeholder = self . markdown . htmlStash . store ( code , safe = True ) <EOL> text = '<STR_LIT>' % ( text [ : m . start ( ) ] , placeholder , text [ m . end ( ) : ] ) <EOL> return text . split ( '<STR_LIT:\n>' ) <EOL> def _escape ( self , txt ) : <EOL> return txt . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> class DoubleNewlinePreprocessor ( markdown . preprocessors . Preprocessor ) : <EOL> def run ( self , lines ) : <EOL> text = '<STR_LIT:\n>' . join ( lines ) <EOL> text = re . sub ( r'<STR_LIT>' , newline_callback , text ) <EOL> return text . split ( '<STR_LIT:\n>' ) <EOL> class DotHeaderProcessor ( markdown . blockprocessors . HashHeaderProcessor ) : <EOL> """<STR_LIT>""" <EOL> RE = re . compile ( r'<STR_LIT>' ) <EOL> def test ( self , parent , block ) : <EOL> return bool ( self . RE . search ( block ) ) <EOL> def run ( self , parent , blocks ) : <EOL> block = blocks . pop ( <NUM_LIT:0> ) <EOL> m = self . RE . search ( block ) <EOL> if m : <EOL> before = block [ : m . start ( ) ] <EOL> after = block [ m . end ( ) : ] <EOL> if before : <EOL> self . parser . parseBlocks ( parent , [ before ] ) <EOL> h = markdown . etree . SubElement ( parent , m . group ( '<STR_LIT>' ) [ : <NUM_LIT:2> ] ) <EOL> h . text = m . group ( '<STR_LIT>' ) . strip ( ) <EOL> if after : <EOL> blocks . insert ( <NUM_LIT:0> , after ) <EOL> else : <EOL> message ( CRITICAL , "<STR_LIT>" ) <EOL> def makeExtension ( configs = [ ] ) : <EOL> return FloydExtension ( configs = configs ) </s>
