<s> '''<STR_LIT>''' <EOL> import markdown <EOL> import re <EOL> def build_url ( label , base , end ) : <EOL> """<STR_LIT>""" <EOL> clean_label = re . sub ( r'<STR_LIT>' , '<STR_LIT:_>' , label ) <EOL> return '<STR_LIT>' % ( base , clean_label , end ) <EOL> class WikiLinkExtension ( markdown . Extension ) : <EOL> def __init__ ( self , configs ) : <EOL> self . config = { <EOL> '<STR_LIT>' : [ '<STR_LIT:/>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:/>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ build_url , '<STR_LIT>' ] , <EOL> } <EOL> for key , value in configs : <EOL> self . setConfig ( key , value ) <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> self . md = md <EOL> WIKILINK_RE = r'<STR_LIT>' <EOL> wikilinkPattern = WikiLinks ( WIKILINK_RE , self . getConfigs ( ) ) <EOL> wikilinkPattern . md = md <EOL> md . inlinePatterns . add ( '<STR_LIT>' , wikilinkPattern , "<STR_LIT>" ) <EOL> class WikiLinks ( markdown . inlinepatterns . Pattern ) : <EOL> def __init__ ( self , pattern , config ) : <EOL> markdown . inlinepatterns . Pattern . __init__ ( self , pattern ) <EOL> self . config = config <EOL> def handleMatch ( self , m ) : <EOL> if m . group ( <NUM_LIT:2> ) . strip ( ) : <EOL> base_url , end_url , html_class = self . _getMeta ( ) <EOL> label = m . group ( <NUM_LIT:2> ) . strip ( ) <EOL> url = self . config [ '<STR_LIT>' ] ( label , base_url , end_url ) <EOL> a = markdown . util . etree . Element ( '<STR_LIT:a>' ) <EOL> a . text = label <EOL> a . set ( '<STR_LIT>' , url ) <EOL> if html_class : <EOL> a . set ( '<STR_LIT:class>' , html_class ) <EOL> else : <EOL> a = '<STR_LIT>' <EOL> return a <EOL> def _getMeta ( self ) : <EOL> """<STR_LIT>""" <EOL> base_url = self . config [ '<STR_LIT>' ] <EOL> end_url = self . config [ '<STR_LIT>' ] <EOL> html_class = self . config [ '<STR_LIT>' ] <EOL> if hasattr ( self . md , '<STR_LIT:Meta>' ) : <EOL> if self . md . Meta . has_key ( '<STR_LIT>' ) : <EOL> base_url = self . md . Meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if self . md . Meta . has_key ( '<STR_LIT>' ) : <EOL> end_url = self . md . Meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if self . md . Meta . has_key ( '<STR_LIT>' ) : <EOL> html_class = self . md . Meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> return base_url , end_url , html_class <EOL> def makeExtension ( configs = None ) : <EOL> return WikiLinkExtension ( configs = configs ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
