<s> """<STR_LIT>""" <EOL> from markdown import extensions <EOL> from markdown import preprocessors <EOL> from pygments import highlight <EOL> from pygments import lexers <EOL> from pygments . formatters . html import HtmlFormatter <EOL> from pygments . lexers import TextLexer <EOL> from pygments . lexers import get_lexer_by_name <EOL> import re <EOL> class IncludePreprocessor ( preprocessors . Preprocessor ) : <EOL> REGEX = re . compile ( "<STR_LIT>" ) <EOL> def __init__ ( self , pod , markdown_instance ) : <EOL> self . pod = pod <EOL> self . markdown = markdown_instance <EOL> def run ( self , lines ) : <EOL> new_lines = [ ] <EOL> for line in lines : <EOL> pod_paths = IncludePreprocessor . REGEX . findall ( line ) <EOL> if not pod_paths or line . startswith ( '<STR_LIT:U+0020>' ) : <EOL> new_lines . append ( line ) <EOL> for pod_path in pod_paths : <EOL> doc = self . pod . get_doc ( pod_path ) <EOL> included_lines = doc . body . split ( '<STR_LIT:\n>' ) <EOL> new_lines . extend ( included_lines ) <EOL> return new_lines <EOL> class IncludeExtension ( extensions . Extension ) : <EOL> def __init__ ( self , pod ) : <EOL> self . pod = pod <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . registerExtension ( self ) <EOL> self . processor = IncludePreprocessor ( self . pod , md ) <EOL> self . processor . md = md <EOL> md . preprocessors . add ( '<STR_LIT>' , self . processor , '<STR_LIT>' ) <EOL> class UrlPreprocessor ( preprocessors . Preprocessor ) : <EOL> REGEX = re . compile ( "<STR_LIT>" ) <EOL> def __init__ ( self , pod , markdown_instance ) : <EOL> self . pod = pod <EOL> self . markdown = markdown_instance <EOL> def run ( self , lines ) : <EOL> new_lines = [ ] <EOL> for line in lines : <EOL> pod_paths = UrlPreprocessor . REGEX . findall ( line ) <EOL> if not pod_paths or line . startswith ( '<STR_LIT:U+0020>' ) : <EOL> new_lines . append ( line ) <EOL> else : <EOL> for pod_path in pod_paths : <EOL> doc = self . pod . get_doc ( pod_path ) <EOL> line = re . sub ( UrlPreprocessor . REGEX , doc . url . path , line ) <EOL> new_lines . append ( line ) <EOL> return new_lines <EOL> class UrlExtension ( extensions . Extension ) : <EOL> def __init__ ( self , pod ) : <EOL> self . pod = pod <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . registerExtension ( self ) <EOL> self . processor = UrlPreprocessor ( self . pod , md ) <EOL> self . processor . md = md <EOL> md . preprocessors . add ( '<STR_LIT:url>' , self . processor , '<STR_LIT>' ) <EOL> class CodeBlockPreprocessor ( preprocessors . Preprocessor ) : <EOL> pattern = re . compile ( r'<STR_LIT>' , re . S ) <EOL> formatter = HtmlFormatter ( noclasses = True ) <EOL> def run ( self , lines ) : <EOL> def repl ( m ) : <EOL> try : <EOL> lexer = lexers . get_lexer_by_name ( m . group ( <NUM_LIT:1> ) ) <EOL> except ValueError : <EOL> lexer = lexers . TextLexer ( ) <EOL> code = highlight ( m . group ( <NUM_LIT:2> ) , lexer , self . formatter ) <EOL> return '<STR_LIT>' % code <EOL> joined_lines = "<STR_LIT:\n>" . join ( lines ) <EOL> joined_lines = self . pattern . sub ( repl , joined_lines ) <EOL> return joined_lines . split ( "<STR_LIT:\n>" ) <EOL> class CodeBlockExtension ( extensions . Extension ) : <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> md . preprocessors . add ( '<STR_LIT>' , CodeBlockPreprocessor ( ) , '<STR_LIT>' ) </s>
