\+@sSdZddlmZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd lmZmZdd lmZdd lZdZdZejdZejdZGdddeZGdddeZGddde ZGddde ZGddde ZgddZd S)a ========================= FOOTNOTES ================================= This section adds footnote handling to markdown. It can be used as an example for extending python-markdown with relatively complex functionality. While in this case the extension is included inside the module itself, it could just as easily be added from outside the module. Not that all markdown classes above are ignorant about footnotes. All footnote functionality is provided separately and then added to the markdown instance at the run time. Footnote functionality is attached by calling extendMarkdown() method of FootnoteExtension. The method also registers the extension to allow it's state to be reset by a call to reset() method. Example: Footnotes[^1] have a label[^label] and a definition[^!DEF]. [^1]: This is a footnote [^label]: A footnote on "label" [^!DEF]: The footnote for definition )absolute_import)unicode_literals) Extension) Preprocessor)Pattern) Treeprocessor) Postprocessor)etree text_type) OrderedDictNZzz1337820767766393qqZqq3936677670287331zzz[ ]{0,3}\[\^([^\]]*)\]:\s*(.*)z((\t)|( ))(.*)c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)FootnoteExtensionz Footnote Extension. cCsoiddgd6ddgd6ddgd 6|_x%|D]\}}||j|d amp_substituteN) registerExtensionparsermd preprocessorsaddFootnotePreprocessorinlinePatternsFootnotePatterntreeprocessorsFootnoteTreeprocessorpostprocessorsFootnotePostprocessor)rr md_globalsZ FOOTNOTE_RErrrextendMarkdownCs   z FootnoteExtension.extendMarkdowncCst|_|jd7_dS)zD Clear the footnotes on reset, and prepare for a distinct document. rN)r footnotesr)rrrrrXs zFootnoteExtension.resetcs%fdd|}|S)z@ Return ElementTree Element that contains Footnote placeholder. csx|D]}|jrG|jjjddkrG||dfSn|jr|jjjddkr||dfSn|qWdS)NrrTFr-)textfind getConfigtail)elementchild)finderrrrr4_s  ! !z:FootnoteExtension.findFootnotesPlaceholder..finderr)rrootresr)r4rrfindFootnotesPlaceholder]s z*FootnoteExtension.findFootnotesPlaceholdercCs||j|indexFN_BACKLINK_TEXTr. getchildrentagNBSP_PLACEHOLDERappend) rr5rBrEr8rFZbacklinknoderKrrrmakeFootnotesDivs4    z"FootnoteExtension.makeFootnotesDivN) __name__ __module__ __qualname____doc__rr+rr7r9r?r@rArYrrrrr*s         rc@s:eZdZdZddZddZddZdS) r#z7 Find all footnote references and store for later use. cCs ||_dS)N)r,)rr,rrrrszFootnotePreprocessor.__init__cCsg}d}xtj||}|r|j||dd\}}|jd|jd||d7}|jj|jddj|n|j||t ||dkr|d7}qPqW|S)z Loop through lines and find, set, and remove footnote definitions. Keywords: * lines: A list of lines of text Return: A list of lines of text with footnote definitions removed. rrNr ) DEF_REmatch detectTabbedinsertgroupr,r9joinrWlen)rlinesnewlinesimfnZ_irrrruns #( zFootnotePreprocessor.runc Cs2g}d}d}dd}x|D]}|jr||}|rf|j||d7}q%q| rtj| r|j||d7}q%q||dfSq%d}|d7}x<t|t|D]$}||jr||} PqqWP|| r|jdq%q%Pq%W|d7}||fS)z Find indented text and remove indent before further proccesing. Keyword arguments: * lines: an array of strings Returns: a list of post processed items and the index of last line. FrcSs&tj|}|r"|jdSdS)N) TABBED_REr`rc)liner`rrrdetabsz0FootnotePreprocessor.detectTabbed..detabrT)striprWr_r`rangere) rrfitemsZ blank_linerhrornZ detabbed_linejZ next_linerrrras:              z!FootnotePreprocessor.detectTabbedN)rZr[r\r]rrkrarrrrr#s   r#cs4eZdZdZfddZddZS)r%z? InlinePattern for footnote markers in a document's body text. cs#tt|j|||_dS)N)superr%rr,)rpatternr,) __class__rrrszFootnotePattern.__init__cCs|jd}||jjjkrtjd}tj|d}|jd|jj||jdd|jj||jj j dkr|jd d n|jd d t |jjj |d |_ |SdSdS)NrsuprGr8rHrIr:r;ZrelrrCz footnote-refr)zhtml5zxhtml5)rcr,rMr rNrPrOrAr@r r>r rRr.)rrir8rxrGrrr handleMatch s "zFootnotePattern.handleMatch)rZr[r\r]rryrr)rwrr%s r%c@s.eZdZdZddZddZdS)r'z3 Build and append footnote div to end of document. cCs ||_dS)N)r,)rr,rrrrszFootnoteTreeprocessor.__init__cCs|jj|}|r|jj|}|r|\}}}|jj|}|rz|j||j||q|j|d|d|_q|j|ndS)Nr) r,rYr7rTrRremoverbr1rW)rr5Z footnotesDivresultr3parentisTextZindrrrrks  zFootnoteTreeprocessor.runN)rZr[r\r]rrkrrrrr's  r'c@s.eZdZdZddZddZdS)r)z* Replace placeholders with html entities. cCs ||_dS)N)r,)rr,rrrr1szFootnotePostprocessor.__init__cCs.|jt|jjd}|jtdS)Nrz )replacerSr,r0rV)rr.rrrrk4szFootnotePostprocessor.runN)rZr[r\r]rrkrrrrr)/s  r)cCs td|S)z- Return an instance of the FootnoteExtension r)r)rrrr makeExtension8sr)r] __future__rrrprr!rinlinepatternsrr&r r(r utilr r odictr rerSrVcompiler_rmrr#r%r'r)rrrrrs( }[