o
    ðEþgéo ã                   @   s:  d Z dZdZd eeeƒ¡ZdZddlZddl	Z	ddl
Z
ddlZddlZddlmZmZ ddlmZmZ ddlZdd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# ddlm$Z$ ddl%m&Z&m'Z' ddl(m)Z) ed Z*e+eef Z,e#e-e e,f Z.ee/ej0e#eeej1gef f f  Z2dZ3e
 4d¡Z5dZ6e)dƒZ7dedefdd„Z8dd„ dD ƒZ9e :d¡Z;G dd„ de<ƒZ=dde6ddddddf	d ed!ed"e>d#e?d$ee* d%ee. d&ee2 d'ee d(ee d)e>dd*fd+d,„Z@de6dddddddf	d-ed"e>d#e?d$ee* d%ee. d&ee2 d'ee d(ee d)e>d.e>dd*fd/d„ZAG d0d1„ d1e&ƒZBd2eBfd3d4„ZCG d5d6„ d6ƒZDG d7d8„ d8eDƒZEG d9d:„ d:eƒZFG d;d<„ d<eFƒZGG d=d>„ d>eFƒZHG d?d@„ d@eFƒZIG dAdB„ dBe"ddCZJG dDdE„ dEeFƒZKG dFdG„ dGeGƒZLG dHdI„ dIeFƒZMG dJdK„ dKeFƒZNG dLdM„ dMeFƒZOG dNdO„ dOeFƒZPG dPdQ„ dQeMƒZQG dRdS„ dSeGƒZRG dTdU„ dUeFƒZSG dVdW„ dWeFƒZTG dXdY„ dYeFƒZUG dZd[„ d[eFƒZVG d\d]„ d]eFƒZWG d^d_„ d_eFƒZXG d`da„ daeFƒZYG dbdc„ dce"ddCZZG ddde„ deeFƒZ[G dfdg„ dgeFƒZ\eH ]¡  eI ]¡  eK ]¡  eL ]¡  eM ]¡  eN ]¡  eO ]¡  eP ]¡  eR ]¡  eQ ]¡  eS ]¡  eT ]¡  eU ]¡  eV ]¡  eW ]¡  eX ]¡  eY ]¡  e[ ]¡  e\ ]¡  dhe#e-e/e?eef  df dee fdidj„Z^G dkd*„ d*eƒZ_e :dl¡Z`e :dm¡Zadnedefdodp„Zbdqedefdrds„Zcdedej0fdtdu„Zdd–dwe-e dxe?dye>de-e fdzd{„Zed–d-edxe?dye>defd|d}„ZfG d~d„ dƒZgd#e?dej0fd€d„ZhegehƒZhd#e?dej0fd‚dƒ„ZiegeiƒZid—d…ed†e>defd‡dˆ„Zjd‰edefdŠd‹„Zk		d˜d…ed$e#e*e>df dŒee fddŽ„ZlG dd„ dejmƒZnd‘d’„ Zod™d“d”„Zpeqd•kre repejsƒ¡ dS dS )ša¢  A fast and complete Python implementation of Markdown.

[from http://daringfireball.net/projects/markdown/]
> Markdown is a text-to-HTML filter; it translates an easy-to-read /
> easy-to-write structured text format into HTML.  Markdown's text
> format is most similar to that of plain text email, and supports
> features such as headers, *emphasis*, code blocks, blockquotes, and
> links.
>
> Markdown's syntax is designed not as a generic markup language, but
> specifically to serve as a front-end to (X)HTML. You can use span-level
> HTML tags anywhere in a Markdown document, and you can use block level
> HTML tags (like <div> and <table> as well).

Module usage:

    >>> import markdown2
    >>> markdown2.markdown("*boo!*")  # or use `html = markdown_path(PATH)`
    u'<p><em>boo!</em></p>\n'

    >>> markdowner = Markdown()
    >>> markdowner.convert("*boo!*")
    u'<p><em>boo!</em></p>\n'
    >>> markdowner.convert("**boom!**")
    u'<p><strong>boom!</strong></p>\n'

This implementation of Markdown implements the full "core" syntax plus a
number of extras (e.g., code syntax coloring, footnotes) as described on
<https://github.com/trentm/python-markdown2/wiki/Extras>.
a(  A fast and complete Python implementation of Markdown, a
text-to-HTML conversion tool for web writers.

Supported extra syntax options (see -x|--extras option below and
see <https://github.com/trentm/python-markdown2/wiki/Extras> for details):

* admonitions: Enable parsing of RST admonitions.
* breaks: Control where hard breaks are inserted in the markdown.
  Options include:
  - on_newline: Replace single new line characters with <br> when True
  - on_backslash: Replace backslashes at the end of a line with <br>
* break-on-newline: Alias for the on_newline option in the breaks extra.
* code-friendly: Disable _ and __ for em and strong.
* cuddled-lists: Allow lists to be cuddled to the preceding paragraph.
* fenced-code-blocks: Allows a code block to not have to be indented
  by fencing it with '```' on a line before and after. Based on
  <http://github.github.com/github-flavored-markdown/> with support for
  syntax highlighting.
* footnotes: Support footnotes as in use on daringfireball.net and
  implemented in other Markdown processors (tho not in Markdown.pl v1.0.1).
* header-ids: Adds "id" attributes to headers. The id value is a slug of
  the header text.
* highlightjs-lang: Allows specifying the language which used for syntax
  highlighting when using fenced-code-blocks and highlightjs.
* html-classes: Takes a dict mapping html tag names (lowercase) to a
  string to use for a "class" tag attribute. Currently only supports "img",
  "table", "thead", "pre", "code", "ul" and "ol" tags. Add an issue if you require
  this for other tags.
* link-patterns: Auto-link given regex patterns in text (e.g. bug number
  references, revision number references).
* link-shortrefs: allow shortcut reference links, not followed by `[]` or
  a link label.
* markdown-in-html: Allow the use of `markdown="1"` in a block HTML tag to
  have markdown processing be done on its contents. Similar to
  <http://michelf.com/projects/php-markdown/extra/#markdown-attr> but with
  some limitations.
* metadata: Extract metadata from a leading '---'-fenced block.
  See <https://github.com/trentm/python-markdown2/issues/77> for details.
* middle-word-em: Allows or disallows emphasis syntax in the middle of words,
  defaulting to allow. Disabling this means that `this_text_here` will not be
  converted to `this<em>text</em>here`.
* nofollow: Add `rel="nofollow"` to add `<a>` tags with an href. See
  <http://en.wikipedia.org/wiki/Nofollow>.
* numbering: Support of generic counters.  Non standard extension to
  allow sequential numbering of figures, tables, equations, exhibits etc.
* pyshell: Treats unindented Python interactive shell sessions as <code>
  blocks.
* smarty-pants: Replaces ' and " with curly quotation marks or curly
  apostrophes.  Replaces --, ---, ..., and . . . with en dashes, em dashes,
  and ellipses.
* spoiler: A special kind of blockquote commonly hidden behind a
  click on SO. Syntax per <http://meta.stackexchange.com/a/72878>.
* strike: text inside of double tilde is ~~strikethrough~~
* tag-friendly: Requires atx style headers to have a space between the # and
  the header text. Useful for applications that require twitter style tags to
  pass through the parser.
* tables: Tables using the same format as GFM
  <https://help.github.com/articles/github-flavored-markdown#tables> and
  PHP-Markdown Extra <https://michelf.ca/projects/php-markdown/extra/#table>.
* toc: The returned HTML string gets a new "toc_html" attribute which is
  a Table of Contents for the document. (experimental)
* use-file-vars: Look for an Emacs-style markdown-extras file variable to turn
  on Extras.
* wiki-tables: Google Code Wiki-style tables. See
  <http://code.google.com/p/support/wiki/WikiSyntax#Tables>.
* wavedrom: Support for generating Wavedrom digital timing diagrams
* xml: Passes one-liner processing instructions and namespaced XML tags.
)é   é   é   Ú.z
Trent Mické    N)ÚdefaultdictÚOrderedDict)ÚABCÚabstractmethod)ÚIterable)Úsha256)Úrandom)
ÚAnyÚCallableÚDictÚListÚLiteralÚOptionalÚTupleÚTypeÚ	TypedDictÚUnion)Ú
Collection)ÚIntEnumÚauto)Úurandom©ÚreplaceÚescapeFÚmarkdowné   é   ÚsÚreturnc                 C   s"   dt t|  d¡ ƒ ¡ dd …  S )Nzmd5-úutf-8é    )r   ÚSECRET_SALTÚencodeÚ	hexdigest©r!   © r)   úc/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/markdown2.pyÚ
_hash_text”   s   "r+   c                 C   s   i | ]}|t |ƒ“qS r)   )r+   ©Ú.0Úchr)   r)   r*   Ú
<dictcomp>˜   s    ÿr/   z\`*_{}[]()>#+-.!z!&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)c                   @   s   e Zd ZdS )ÚMarkdownErrorN)Ú__name__Ú
__module__Ú__qualname__r)   r)   r)   r*   r0   ¡   s    r0   r#   ÚpathÚencodingÚ	html4tagsÚ	tab_widthÚ	safe_modeÚextrasÚlink_patternsÚfootnote_titleÚfootnote_return_symbolÚuse_file_varsÚUnicodeWithAttrsc
              
   C   s<   t  | d|¡}
|
 ¡ }|
 ¡  t||||||||	d |¡S )NÚr)r6   r7   r8   r9   r:   r;   r<   r=   )ÚcodecsÚopenÚreadÚcloseÚMarkdownÚconvert)r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   ÚfpÚtextr)   r)   r*   Úmarkdown_path§   s   ûûrH   rG   Úclic
           
      C   s    t |||||||||	d	 | ¡S )N)	r6   r7   r8   r9   r:   r;   r<   r=   rI   )rD   rE   )
rG   r6   r7   r8   r9   r:   r;   r<   r=   rI   r)   r)   r*   r   ¾   s   ûûc                   @   sl   e Zd Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ Z	eƒ Z
eƒ Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ Zeƒ ZdS )ÚStageN)r1   r2   r3   r   Ú
PREPROCESSÚ	HASH_HTMLÚ	LINK_DEFSÚBLOCK_GAMUTÚHEADERSÚLISTSÚCODE_BLOCKSÚBLOCK_QUOTESÚ
PARAGRAPHSÚ
SPAN_GAMUTÚ
CODE_SPANSÚESCAPE_SPECIALÚLINKSÚITALIC_AND_BOLDÚPOSTPROCESSÚUNHASH_HTMLr)   r)   r)   r*   rJ   Ò   s"    
rJ   Ústagec                    s   ‡ fdd„}|S )zd
    Decorator that handles executing relevant `Extra`s before and after this `Stage` executes.
    c                    s   t  ˆ ¡d‡ ‡fdd„ƒ}|S )NÚmdrD   c                    sÔ   ˆ| _ ˆd | _ˆtjv r.tjˆ d D ]}|j| jvrq| j|j }| |¡r-| |¡}qˆ| _ˆ | |g|¢R i |¤Ž}ˆd | _ˆtjv rhtjˆ d D ]}|j| jvrWqN| j|j }| |¡rg| |¡}qN|S )Ng      à?r   é   )r[   ÚorderÚExtraÚ_exec_orderÚnameÚextra_classesÚtestÚrun)r\   rG   ÚargsÚkwargsÚklassÚextra)Úfuncr[   r)   r*   Úinnerí   s,   



€



€z*mark_stage.<locals>.wrapper.<locals>.inner)r\   rD   )Ú	functoolsÚwraps)ri   rj   ©r[   )ri   r*   Úwrapperì   s   zmark_stage.<locals>.wrapperr)   )r[   rn   r)   rm   r*   Ú
mark_stageè   s   ro   c                   @   s.
  e Zd ZU eed< eedf ed< eeef ed< eeef ed< eeef ed< eeef ed< dZeed	< d
Zeed< e	e
 ed< eeeeef  ed< dZeed< 	 eed< 	 e dej¡Zdedddddddf	dedede	e
 de	e de	e de	e de	e dedefdd„Zdd„ Zdd „ Ze d!ejejB ¡Zd"ed#d$fd%d&„Ze ej!ƒd"ed#efd'd(„ƒZ"e ej#ƒd"ed#efd)d*„ƒZ$e d+ %d,d-¡ej&ejB ¡Z'e d.ej&¡Z(e d/ej&¡Z)e d0ej&¡Z*e d1ej&¡Z+d"ed#efd2d3„Z,e d4ej-¡Z.e d5ejej&B ej/B ejB ¡Z0d6ej1d#efd7d8„Z2d"ed#eeef fd9d:„Z3d;ed#efd<d=„Z4d"ed#efd>d?„Z5d@Z6dAZ7e7e67 Z7e dBe7 ej8ejB ¡Z9dCZ:e:e67 Z:dDZ;e dEe: ej8ejB ¡Z<e dF¡Z=	dùd6e>ej1ef dGed#efdHdI„Z?e ej@ƒdùd"edGed#efdJdK„ƒZA	dùd"edLedMeBegef dNed#ef
dOdP„ZCdQed"ed#efdRdS„ZDe ejEƒd"ed#efdTdU„ƒZFd6ej1d#efdVdW„ZGd6ej1d#efdXdY„ZHd"ed#efdZd[„ZIe d\ej¡ZJe ejKƒd"ed#efd]d^„ƒZLe ejMƒd"ed#efd_d`„ƒZNe daej8¡ZOe ejPƒd"ed#efdbdc„ƒZQe ej@ƒd"ed#efddde„ƒZRdúd"ed#efdgdh„ZSdied#efdjdk„ZTe dlej8ejUB ¡ZVe dmej8ejUB ¡ZWe dn¡ZXe do¡ZYd"edped#efdqdr„ZZd"edpedsedted#ef
dudv„Z[d"edped#e>eeeef edw f fdxdy„Z\e dzej8¡Z]d{ed#efd|d}„Z^d~Z_e`dd€„ ƒZae ejbƒd"ed#efdd‚„ƒZc	dûd"edƒed„e	e d#efd…d†„Zdd"ed#efd‡dˆ„Zed‰edŠed‹ed#dfdŒd„ZfdŽZge egd ej8ejB ¡Zhe egd ej8ejB ¡Zid6ej1d#efd‘d’„Zje d“ej8ejB ¡Zkd6ej1d#efd”d•„Zle ejmƒd"ed#efd–d—„ƒZnd˜Zod™eo Zpdšeo Zqd›Zrd6ej1d#efdœd„Zse ejtƒd"ed#efdždŸ„ƒZue d  %epep¡ejej8B ejUB ¡Zve d¡ejej8B ejUB ¡Zwd¢Zxd6ej1d#efd£d¤„ZydZzd6ej1d#efd¥d¦„Z{d§ed#efd¨d©„Z|dªefd«d¬„Z}d­ed#efd®d¯„Z~d6ej1d#efd°d±„Zd²ed#efd³d´„Z€e ejƒd"ed#efdµd¶„ƒZ‚e d·ej8ejUB ¡Zƒd6ej1d#efd¸d¹„Z„e ej…ƒd"ed#efdºd»„ƒZ†d"ed#efd¼d½„Z‡e d¾ejU¡Zˆe d¿ejU¡Z‰e ejŠƒd"ed#efdÀdÁ„ƒZ‹dÂZŒe eŒdÃ ejej8B ¡Ze eŒdÄ ejej8B ¡ZŽe dÅej¡Ze dÆej¡Ze dÇej¡Z‘e dÈejU¡Z’d6ej1d#efdÉdÊ„Z“d6ej1d#efdËdÌ„Z”e ej•ƒd"ed#efdÍdÎ„ƒZ–e ej—ƒd"ed#efdÏdÐ„ƒZ˜d"ed#efdÑdÒ„Z™e dÓejš¡Z›e dÔejš¡Zœd"ed#efdÕdÖ„Ze d×¡Zžd"ed#efdØdÙ„ZŸd"ed#efdÚdÛ„Z e dÜejš¡Z¡d6ej1d#efdÝdÞ„Z¢e dßejšej8B ej£B ¡Z¤d6ej1d#efdàdá„Z¥d"ed#efdâdã„Z¦däed#efdådæ„Z§d"ed#efdçdè„Z¨d"ed#efdédê„Z©d"ed#efdëdì„Zªe«		düd"edíe	e dîe	e d#eeef fdïdð„ƒZ¬e«		dýd"edñedòedóed#ef
dôdõ„ƒZ­e«d6ej1döed#efd÷dø„ƒZ®dS )þrD   r9   r_   rb   ÚurlsÚtitlesÚhtml_blocksÚ
html_spansz
{(#HTML#)}Úhtml_removed_textz[HTML_REMOVED]Úhtml_removed_text_compatr8   Ú_tocr   r[   r^   z^[ \t]+$FNr6   r7   r:   r;   r<   r=   rI   c
           
      C   sô  |rd| _ nd| _ || _|d | _|du rd| _n|| _t| dd ƒd u r(i | _nt| jtƒs7dd„ | jD ƒ| _|rKt|tƒsEd	d„ |D ƒ}| j |¡ t| jtƒsSJ ‚d
| jv rwd| jvrbd | jd< | jd
 d u rmd| _	n
| jd
  
dd¡| _	d| jv rt| jd tƒsd| jd ddœ| jd< d| jv r£| j di ¡ d| jd d< d| jv r¼|p®| jd }|d u r·tdƒ‚|| jd< | j ¡ | _|| _|| _|| _|| _t d| tj¡| _|	| _t ¡ | _i | _d| jv røtdƒ| jd< tdƒ| jd< d S d S )Nú>z />ú Tr   r9   c                 S   ó   i | ]}|d “qS ©Nr)   ©r-   Úer)   r)   r*   r/   S  ó    z%Markdown.__init__.<locals>.<dictcomp>c                 S   ry   rz   r)   r{   r)   r)   r*   r/   W  r}   Útocú
header-idsé   ÚdepthF)ÚmixedÚprefixúreset-countzbreak-on-newlineÚbreaksÚ
on_newlineúlink-patternszQIf the 'link-patterns' extra is used, an argument for 'link_patterns' is requiredz^(\t|[ ]{1,%d})úsmarty-pantsú"ú')Úempty_element_suffixr7   Útabr8   Úgetattrr9   Ú
isinstanceÚdictÚupdateÚ
_toc_depthÚgetÚ
setdefaultr0   ÚcopyÚ_instance_extrasr:   r;   r<   r=   ÚreÚcompileÚMÚ_outdent_rerI   Úg_escape_tableÚ_escape_tableÚ_code_tabler+   )
Úselfr6   r7   r8   r9   r:   r;   r<   r=   rI   r)   r)   r*   Ú__init__2  sd   





ý




þzMarkdown.__init__c                 C   s<   i | _ i | _i | _i | _d| _| j ¡ | _|  ¡  g | _	d S ©Nr   )
rp   rq   rr   rs   Ú
list_levelr•   r”   r9   Ú_setup_extrasrv   ©r   r)   r)   r*   Úresetˆ  s   
zMarkdown.resetc                 C   s¦   d| j v rtƒ | _g | _tdƒ| _d| j v r)t| dƒr$| j d  dd¡r)tt	ƒ| _
d| j v r1i | _i | _tj ¡ D ]\}}|| j vrCq9|| | j  |i ¡ƒ| j|< q9d S )NÚ	footnotesz<<footnote>>r   Ú_count_from_header_idr„   FÚmetadata)r9   r   r¤   Úfootnote_idsr+   Ú_footnote_markerÚhasattrr’   r   Úintr¥   r¦   rb   r_   Ú	_registryÚitems)r   ra   rg   r)   r)   r*   r¡   ’  s   





ýzMarkdown._setup_extrasa  
        <(a)
        (
            [^>]*
            href=   # href is required
            ['"]?   # HTML5 attribute values do not have to be quoted
            [^#'"]  # We don't want to match href values that start with # (like footnotes)
        )
        rG   r"   r>   c              	      sÚ  ˆ   ¡  tˆtƒstˆdƒ‰ˆ jr[ˆ j ˆ jˆ¡‰ˆ  ˆ¡}d|v rWt 	d¡}| 
|d ¡D ](}d|v rL| 
dd¡\}}zt|ƒ}W n tyK   Y nw |d}}|ˆ j|< q.ˆ  ¡  ˆ dd¡‰ˆ d	d¡‰ˆd
7 ‰ˆ  ˆ¡‰ˆ j dˆ¡‰dˆ jv rˆ  ˆ¡‰ˆ  ˆ¡‰ˆ jrŽˆ  ˆ¡‰ˆ jˆdd‰dˆ jv rŸˆ  ˆ¡‰ˆ  ˆ¡‰ˆ  ˆ¡‰dˆ jv rÃ‡ fdd„}t dˆ j |ˆ¡‰ˆ  ˆ¡‰ˆ  ˆ¡‰ˆ  ˆ¡‰ˆ  ˆ¡‰ˆ jrÝˆ ˆ jˆ j ¡‰dˆ jv }dˆ jv }	|ró|	róˆ j! dˆ¡‰n|rýˆ j! dˆ¡‰n
|	rˆ j! dˆ¡‰dˆ jv rKˆ j"rKˆ jd  #d¡r'‡fdd„}
ˆ j"j$|
d t%ˆ j"ƒˆ _&ˆ j'sCˆ jd durKˆ jd  #dd¡rKˆ j&› dˆ› ‰ˆd7 ‰t(ˆƒ}dˆ jv raˆ j"raˆ j&|_)dˆ jv rkˆ j*|_*|S ) zConvert the given text.r#   zmarkdown-extrasz[ ,]+ú=r]   Nú
Ú
úú

Ú r¦   T©Úrawr¤   c                    s$   |   d¡}ˆ j |¡ ttˆ jƒƒS ©Nr]   )Úgroupr§   ÚappendÚstrÚlen)ÚmatchÚ	normed_idr¢   r)   r*   Úfootnote_sub  s   
z&Markdown.convert.<locals>.footnote_subz%s-(.*?)(?=</a></sup>)ztarget-blank-linksÚnofollowz-<\1 rel="nofollow noopener" target="_blank"\2z$<\1 rel="noopener" target="_blank"\2z<\1 rel="nofollow"\2r~   r   r‚   c              	      s<   t  d| d | d t  | d ¡f ˆ t j¡}|r| ¡ S dS )z+Sort the TOC by order of appearance in textz#^<(h%d).*?id=(["\'])%s\2.*>%s</\1>$r   r]   r   )r–   Úsearchr   r˜   Ústart)Úentryrº   ©rG   r)   r*   Útoc_sort   s
   ýz"Markdown.convert.<locals>.toc_sort©ÚkeyÚprependF)+r£   rŽ   r¸   r=   Ú_emacs_oneliner_vars_patÚsubÚ_emacs_vars_oneliner_subÚ_get_emacs_varsr–   r—   Úsplitrª   Ú
ValueErrorr9   r¡   r   Ú_detabÚ_ws_only_line_reÚ_extract_metadataÚ
preprocessr8   Ú_hash_html_spansÚ_hash_html_blocksÚ_strip_footnote_definitionsÚ_strip_link_definitionsÚ_run_block_gamutr¨   Ú_add_footnotesÚpostprocessÚ_unescape_special_charsÚ_unhash_html_spansrt   ru   Ú_a_nofollow_or_blank_linksrv   r’   ÚsortÚcalculate_toc_htmlÚ	_toc_htmlrI   r>   Útoc_htmlr¦   )r   rG   Ú
emacs_varsÚsplitterr|   ÚenameÚeargr¼   Údo_target_blank_linksÚdo_nofollow_linksrÂ   Úrvr)   ©r   rG   r*   rE   ·  s‚   



ÿ
















	,zMarkdown.convertc                 C   ó   |S )z´A hook for subclasses to do some postprocessing of the html, if
        desired. This is called before unescaping of special chars and
        unhashing of raw HTML spans.
        r)   rå   r)   r)   r*   rÖ   <  ó   zMarkdown.postprocessc                 C   ræ   )zÌA hook for subclasses to do some preprocessing of the Markdown, if
        desired. This is called after basic formatting of the text, but prior
        to any extras, safe mode, etc. processing.
        r)   rå   r)   r)   r*   rÏ   D  rç   zMarkdown.preprocessaí  
        ^{0}(  # optional opening fence
            (?:
                {1}:(?:\n+[ \t]+.*)+  # indented lists
            )|(?:
                (?:{1}:\s+>(?:\n\s+.*)+?)  # multiline long descriptions
                (?=\n{1}:\s*.*\n|\s*\Z)  # match up until the start of the next key:value definition or the end of the input text
            )|(?:
                {1}:(?! >).*\n?  # simple key:value pair, leading spaces allowed
            )
        ){0}  # optional closing fence
        z(?:---[\ \t]*\n)?z[\S \t]*\w[\S \t]*\s*zK^-(?:[ \t]*([^\n]*)(?:[ \t]*[:-][ \t]*(\S+))?)(?:\n((?:[ \t]+[^\n]+\n?)+))?z9^([^:\n]+)[ \t]*:[ \t]*([^\n]*)(?:((?:\n[ \t]+[^\n]+)+))?z^---[\ \t]*\nz^
c           	         s,  |  d¡rtjˆj|dd}|d }|d }ntjˆj|dd}|d }|d }t ˆj|¡}|s3|S dtdtt	t
 ttt
f f f‡ ‡‡fdd	„‰ |D ]G}| d
d¡\}‰ˆd d… dkrntˆdd … ƒ ¡ ˆj| ¡ < qLˆdkrzdˆj| ¡ < qLˆd dkrŠˆ ˆƒˆj| ¡ < qLˆ ¡ ˆj| ¡ < qL|S )Nú---r   )Úmaxsplitr]   r   Úvaluer"   c                    s,  |   ¡ }|  ˆd t| ƒt|ƒ … d¡dd … }| d¡rˆg }t ˆj|¡D ]_}|d r>|d s>|d s>| |d  ¡ ¡ q&|d dkrV|d sV|d rV| |d  ¡ ¡ q&|d rn|d rn| |d  ¡ |d  ¡ i¡ q&|d s„|d s„|d r„| ˆ |d ƒ¡ q&	 q&|S ‡ fdd„t ˆj	|¡D ƒS )	Nr¯   r]   ú-r   r   rw   c                    s6   i | ]}|d    ¡ |d r|d   ¡ nˆ |d ƒ“qS )r   r]   r   ©Ústrip)r-   rº   )Úparse_structured_valuer)   r*   r/   ›  s    
ûÿ
üzNMarkdown._extract_metadata.<locals>.parse_structured_value.<locals>.<dictcomp>)
Úlstripr   r¹   Ú
startswithr–   ÚfindallÚ_key_val_list_patr·   rí   Ú_key_val_dict_pat)rê   Úvsr?   rº   ©rî   r   Úvr)   r*   rî   €  s$   (
 
úz:Markdown._extract_metadata.<locals>.parse_structured_valueú:r   z >
r¯   r²   )rð   r–   rÊ   Ú_meta_data_fence_patternÚ_meta_data_newlinerñ   Ú_meta_data_patternr¸   r   Úlistr   r   Ú_dedentrí   r¦   )	r   rG   Úfence_splitsÚmetadata_contentÚtailÚmetadata_splitrº   ÚitemÚkr)   rõ   r*   rÎ   p  s*   

.$ zMarkdown._extract_metadatazD((?:<!--)?\s*-\*-)\s*(?:(\S[^\r\n]*?)([\r\n]\s*)?)?(-\*-\s*(?:-->)?)zŸ^
        (?P<prefix>(?:[^\r\n|\n|\r])*?)
        [\ \t]*Local\ Variables:[\ \t]*
        (?P<suffix>.*?)(?:\r\n|\n|\r)
        (?P<content>.*?\1End:)
        rº   c                 C   s†   |  d¡ ¡ dkr6|  d¡ ¡ dkr6t d|  d¡¡d }t d|  d¡¡d }d |d|  d¡ ¡ d|¡S | ¡ \}}|j||… S )	Nr]   ú-*-r   z^\s*r   z\s*$z{}<!-- {} {} {} -->{}r   )r¶   rí   r–   rñ   ÚformatÚspanÚstring)r   rº   Úlead_wsÚtail_wsr¿   Úendr)   r)   r*   rÈ   Ê  s   $z!Markdown._emacs_vars_oneliner_subc              	   C   sð  i }t ddƒ}|d|… }d|v rl| j |¡}|rl| d¡}d|vs$J ‚dd„ | d¡D ƒ}t|ƒd	krCd
|d vrC|d  ¡ |d< n)|D ]&}z| ¡  d
d	¡\}	}
W n tyb   t 	d|¡ Y qEw |
 ¡ ||	 
¡ < qE|| d… }d|v rC| j |¡}|rC| d¡}| d¡}| d¡ d¡}t|ƒD ]0\}}| |¡s­t 	d||f ¡ i   S |t|ƒd	 krÇ| |¡sÇt 	d||f ¡ i   S q—d}|dd… D ]r}|rÜ|t|ƒd… }|rç|dt|ƒ … }| ¡ }|r|}	| d¡rþ|dd…  ¡ }nd}||	  d| 7  < qÐz
| d
d	¡\}	}
W n ty&   t 	d| ¡ Y qÐw |
 ¡ }
|
 d¡r<|
dd…  ¡ }
|	}nd}|
||	< qÐt| ¡ ƒD ],\}}t|ƒd	krt| d¡r`| d¡sl| d¡rt| d¡rt|d	d… ||< qI|S )a0  Return a dictionary of emacs-style local variables.

        Parsing is done loosely according to this spec (and according to
        some in-practice deviations from this):
        http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables
        r   é   Nr  r¯   c                 S   s   g | ]
}|  ¡ r|  ¡ ‘qS r)   rì   )r-   r!   r)   r)   r*   Ú
<listcomp>ä  s    ÿz,Markdown._get_emacs_vars.<locals>.<listcomp>ú;r]   r÷   r   Úmodez-emacs variables error: malformed -*- line: %rzLocal Variablesrƒ   ÚsuffixÚcontentFz@emacs variables error: line '%s' does not use proper prefix '%s'z@emacs variables error: line '%s' does not use proper suffix '%s'éÿÿÿÿú\rx   zClocal variables error: missing colon in local variables entry: '%s'r‰   )ÚpowrÆ   r¾   r¶   rÊ   r¹   rí   rË   ÚlogÚdebugÚlowerÚ_emacs_local_vars_patÚ
splitlinesÚ	enumeraterð   ÚendswithÚrstriprû   r¬   )r   rG   rÞ   ÚSIZEÚheadrº   Úemacs_vars_strÚemacs_var_strsÚemacs_var_strÚvariablerê   rÿ   rƒ   r  ÚlinesÚiÚlineÚcontinued_forÚvarÚvalr)   r)   r*   rÉ   Ó  s˜   

ÿý



þþ€
ÿý
&ÿÿ€zMarkdown._get_emacs_varsr#  c                 C   sJ   d|vr|S |  dd¡\}}|d| jt|ƒ| j   7 }|| }|  |¡S )zRRecusively convert tabs to spaces in a single line.

        Called from _detab().ú	r]   rx   )rÊ   r7   r¹   Ú_detab_line)r   r#  Úchunk1Úchunk2Úoutputr)   r)   r*   r(  :  s   
zMarkdown._detab_linec                 C   s8   d|vr|S g }|  ¡ D ]
}| |  |¡¡ qd |¡S )a“  Iterate text line by line and convert tabs to spaces.

            >>> m = Markdown()
            >>> m._detab("\tfoo")
            '    foo'
            >>> m._detab("  \tfoo")
            '    foo'
            >>> m._detab("\t  foo")
            '      foo'
            >>> m._detab("  foo")
            '  foo'
            >>> m._detab("  foo\n\tbar\tblam")
            '  foo\n    bar blam'
        r'  r¯   )r  r·   r(  Újoin)r   rG   r+  r#  r)   r)   r*   rÌ   E  s   
zMarkdown._detabzA|article|aside|header|hgroup|footer|nav|section|figure|figcaptionzqp|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del|style|html|head|bodyaÞ  
        (                       # save in \1
            ^                   # start of line  (with re.M)
            <(%s)               # start tag = \2
            \b                  # word break
            (.*\n)*?            # any number of lines, minimally matching
            </\2>               # the matching end tag
            [ \t]*              # trailing spaces/tabs
            (?=\n+|\Z)          # followed by a newline or end of document
        )
        zTp|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|mathzŸa|abbr|acronym|b|bdo|big|br|button|cite|code|dfn|em|i|img|input|kbd|label|map|object|output|q|samp|script|select|small|span|strong|sub|sup|textarea|time|tt|varaÞ  
        (                       # save in \1
            ^                   # start of line  (with re.M)
            <(%s)               # start tag = \2
            \b                  # word break
            (.*\n)*?            # any number of lines, minimally matching
            .*</\2>             # the matching end tag
            [ \t]*              # trailing spaces/tabs
            (?=\n+|\Z)          # followed by a newline or end of document
        )
        z\s+markdown=("1"|'1')r´   c              	   C   sÞ  t |tƒr
|}d }n| d¡}z| d¡}W n ty!   d }Y nw |s5t d|¡}|d us0J ‚| d¡}|r@| jr@|  |¡}n d| jv rÈd|v rÈ| 	dd¡d }| j
 |¡}|rÇ| 	d¡}ttd t 	d| |d ¡ƒƒ|dd …  }|d d	… ttd t 	d
| |d	 ¡ƒƒ }|d }d |dd	… ¡}|d	 }	|d | ¡ … || ¡ d …  }t|ƒ}
|| j|
< t|	ƒ}|	| j|< d d|
d|d|dg¡S n| j di ¡ d¡rà| j |¡rà| j | j|¡}t|ƒ}|| j|< d| d S )Nr]   r   z.*?<(\S).*?\s*>úmarkdown-in-htmlz	markdown=r¯   r   z(.*?<%s.*markdown=.*?>)r  z(\s*?</%s>.*?$)r²   r±   r   r‚   )rŽ   r¸   r¶   Ú
IndexErrorr–   rº   r8   Ú_sanitize_htmlr9   rÊ   Ú_html_markdown_attr_rer¾   rû   Úfilterr,  r¿   r	  r+   rr   r’   Ú	_h_tag_rerÇ   Ú
_h_tag_sub)r   rº   r´   ÚhtmlÚtagÚmÚ
first_liner!  ÚmiddleÚ	last_lineÚf_keyÚl_keyrÄ   r)   r)   r*   Ú_hash_html_block_sub†  sP   

ÿ


** 

þï 
zMarkdown._hash_html_block_subc                    s  d|vr|S t ˆj|d‰ ˆ |ˆjˆ ¡}ˆj ˆ |¡}ˆ |ˆj‡ ‡fdd„¡}d|v r8tˆjƒ}| ˆ |¡}d|v ród}	 z| 	d|¡}W n	 t
yP   Y n£w z
| 	d	|¡d
 }W n	 t
yd   Y nw |}|r¥tˆjd ƒD ]}||d  dkr| n|d8 }|dkr† nqp|dkrŒn|dkr™|d dkr™d}n||d |… dkr¤nnN|t|ƒk r¼|| dvr²n
|d7 }|t|ƒk s«|||d … dvrÇq>|||… }|r×ˆjr×ˆ |¡}t|ƒ}	|ˆj|	< |d|… d |	 d ||d…  }q?dˆjv rtˆjƒ}
|
 ˆ |¡}|S )aò  Hashify HTML blocks

        We only want to do this for block-level HTML tags, such as headers,
        lists, and tables. That's because we still want to wrap <p>s around
        "paragraphs" that are wrapped in non-block-level tags, such as anchors,
        phrase emphasis, and spans. The list of tags we're looking for is
        hard-coded.

        @param raw {boolean} indicates if these are raw HTML blocks in
            the original source. It makes a difference in "safe" mode.
        ú<r³   c                    s   ˆ ˆ  | ¡ƒS rz   )Ú_run_span_gamut)Út©Úhash_html_block_subr   r)   r*   Ú<lambda>â  s    z,Markdown._hash_html_blocks.<locals>.<lambda>ú<hrz<!--r   Tú-->r   r]   rx   r¯   r   r±   ú 	)r²   r¯   r±   NÚxml)Ú_curryr<  Ú_strict_tag_block_subÚ_block_tags_aÚ_liberal_tag_block_rerÇ   Ú
_span_tagsÚ_hr_tag_re_from_tab_widthr7   ÚindexrË   Úranger¹   r8   r/  r+   rr   r9   Ú_xml_oneliner_re_from_tab_width)r   rG   r´   Ú
_hr_tag_rer¿   Ú	start_idxÚend_idxr"  r4  rÄ   Ú_xml_oneliner_rer)   r@  r*   rÑ   º  sv   ý
ÿÿÿý


$Î4
zMarkdown._hash_html_blocksÚhtml_tags_reÚcallbackÚallow_indentc                 C   sÆ   d}|}d}d}|  d¡D ]O}	t d |rdnd|¡|	¡}
||	7 }|
rG|	 d|
 d¡ ¡r2|d8 }n|  |
 d¡|	¡r>d	}
n	|d7 }|
 d¡}|dkr\|
rT|| d
¡ƒ}|}||7 }d}q||7 }|S )a¿  
        Finds and substitutes HTML blocks within blocks of text

        Args:
            text: the text to search
            html_tags_re: a regex pattern of HTML block tags to match against.
                For example, `Markdown._block_tags_a`
            callback: callback function that receives the found HTML text block and returns a new str
            allow_indent: allow matching HTML blocks that are not completely outdented
        r   r²   Tz0^(\s{{0,{}}})(?:</code>(?=</pre>))?(</?({})\b>?)Ú0z%s</r]   r   Nr¯   )r  r–   rº   r  rð   r¶   Ú_tag_is_closedr  )r   rG   rT  rU  rV  Ú	tag_countÚcurrent_tagÚblockÚresultÚchunkÚ	is_markupr)   r)   r*   rH  /  s2   ÿ

€zMarkdown._strict_tag_block_subÚtag_namec                 C   s(   t t d| |¡ƒt t d| |¡ƒkS )Nz<%s(?:.*?)>z</%s>)r¹   r–   rñ   )r   r_  rG   r)   r)   r*   rX  b  s   (zMarkdown._tag_is_closedc                 C   s6   | j d }t d| tjtjB tjB ¡}| | j|¡S )Nr]   a  
            ^[ ]{0,%d}\[(.+)\]: # id = \1
              [ \t]*
              \n?               # maybe *one* newline
              [ \t]*
            <?(.+?)>?           # url = \2
              [ \t]*
            (?:
                \n?             # maybe one newline
                [ \t]*
                (?<=\s)         # lookbehind for whitespace
                ['"(]
                ([^\n]*)        # title = \3
                ['")]
                [ \t]*
            )?  # title is optional
            (?:\n+|\Z)
            )r7   r–   r—   ÚXr˜   ÚUrÇ   Ú_extract_link_def_sub)r   rG   Úless_than_tabÚ_link_def_rer)   r)   r*   rÓ   f  s   
ïïz Markdown._strip_link_definitionsc                 C   s8   |  ¡ \}}}| ¡ }|  |¡| j|< |r|| j|< dS ©Nr²   )Úgroupsr  Ú_encode_amps_and_anglesrp   rq   )r   rº   ÚidÚurlÚtitlerÄ   r)   r)   r*   rb  ‚  s   
zMarkdown._extract_link_def_subc                 C   sD   |  ¡ \}}t|| d¡ d ¡ }t dd|¡}|d | j|< dS )Nr¯   )Úskip_first_lineú\Wrë   r±   r²   )rf  rü   rð   rí   r–   rÇ   r¤   )r   rº   rh  rG   r»   r)   r)   r*   Ú_extract_footnote_def_subŠ  s
   z"Markdown._extract_footnote_def_subc                 C   s:   | j d }t d|| j | j f tjtjB ¡}| | j|¡S )a˜  A footnote definition looks like this:

            [^note-id]: Text of the note.

                May include one or more indented paragraphs.

        Where,
        - The 'note-id' can be pretty much anything, though typically it
          is the number of the footnote.
        - The first paragraph may start on the next line, like so:

            [^note-id]:
                Text of the note.
        r]   aÓ  
            ^[ ]{0,%d}\[\^(.+)\]:   # id = \1
            [ \t]*
            (                       # footnote text = \2
              # First line need not start with the spaces.
              (?:\s*.*\n+)
              (?:
                (?:[ ]{%d} | \t)  # Subsequent lines must be indented.
                .*\n+
              )*
            )
            # Lookahead for non-space at line-start, or end of doc.
            (?:(?=^[ ]{0,%d}\S)|\Z)
            )r7   r–   r—   r`  r˜   rÇ   rm  )r   rG   rc  Úfootnote_def_rer)   r)   r*   rÒ   “  s   
ó
òz$Markdown._strip_footnote_definitionsz)^[ ]{0,3}([-_*])[ ]{0,2}(\1[ ]{0,2}){2,}$c                 C   s^   |   |¡}d| j d }t | j||¡}|  |¡}|  |¡}|  |¡}|  |¡}|  	|¡}|S )Nz
<hrr¯   )
Ú_do_headersr‹   r–   rÇ   Ú_hr_reÚ	_do_listsÚ_do_code_blocksÚ_do_block_quotesrÑ   Ú_form_paragraphs)r   rG   Úhrr)   r)   r*   rÔ   ¶  s   





zMarkdown._run_block_gamutc                 C   sT   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}t dd| j |¡}|S )Nz  {2,}\n(?!\<(?:\/?(ul|ol|li))\>)ú<br%s
)	Ú_do_code_spansÚ_escape_special_charsÚ	_do_linksÚ_do_auto_linksrg  Ú_do_italics_and_boldr–   rÇ   r‹   rå   r)   r)   r*   r>  Õ  s   





zMarkdown._run_span_gamuta.  
        (
            \\*  # escapes
            (?:
                # tag
                </?
                (?:\w+)         # tag name
                (?:             # attributes
                    \s+                           # whitespace after tag
                    (?:[^\t<>"'=/]+:)?
                    [^<>"'=/]+=                   # attr name
                    (?:".*?"|'.*?'|[^<>"'=/\s]+)  # value, quoted or unquoted. If unquoted, no spaces allowed
                )*
                \s*/?>
                |
                # auto-link (e.g., <http://www.activestate.com/>)
                <[\w~:/?#\[\]@!$&'\(\)*+,;%=\.\\-]+>
                |
                <!--.*?-->      # comment
                |
                <\?.*?\?>       # processing instruction
            )
        )
        c              	   C   s²   t  d¡}g }d}| j |¡D ]D}|rD| |¡rD| |¡dd … p$d|f\}}| | d| jd ¡| d| jd ¡ d| jd ¡ ¡ n| |  | d	d
¡¡¡ | }qd 	|¡S )Nz^((?:\\\\)*(?!\\))Fr]   r²   z\\r  Ú*Ú_z\<ú&lt;)
r–   r—   Ú_sorta_html_tokenize_rerÊ   rº   r·   r   r›   Ú_encode_backslash_escapesr,  )r   rG   Úlead_escape_reÚescapedÚis_html_markupÚtokenÚ
escape_seqr)   r)   r*   rx  	  s   
þÿ
zMarkdown._escape_special_charsc           
   
      sô   ‡ fdd„}‡fdd„}‡ fdd„}g }ˆ j  |¡‰d}tˆƒD ]T\}}|ri||ƒsi|||ƒsi||ƒ}	|	r]| ˆ  ˆ  |	 d¡¡¡¡ | ˆ  |	 d	¡¡¡ | ˆ  ˆ  |	 d
¡¡¡¡ n| ˆ  ˆ  |¡¡¡ n| ˆ  |¡¡ | }q d |¡S )Nc                    s4   d| v rˆ j  | ¡rdS d| v rˆ j | ¡rdS dS )Nr÷   Tú@F)Ú_auto_link_rerº   Ú_auto_email_link_rer(   r¢   r)   r*   Ú_is_auto_link-  s
   z0Markdown._hash_html_spans.<locals>._is_auto_linkc                    sj   z!|dkrˆ | | d … }n|dkrˆ | d | d … }nW dS W n
 t y+   Y dS w t dd |¡¡S )	Nú<code>r   ú</code>r   r]   Fz <code>md5-[A-Fa-f0-9]{32}</code>r²   )r.  r–   rº   r,  )rM  r„  Úpeek_tokens)Úsplit_tokensr)   r*   Ú_is_code_span4  s   €ÿz0Markdown._hash_html_spans.<locals>._is_code_spanc                    s   ˆ j dkrd S t d| ¡S )Nr   z(<!--)(.*)(-->))r8   r–   rº   )r„  r¢   r)   r*   Ú_is_commentA  s   
z.Markdown._hash_html_spans.<locals>._is_commentFr]   r   r   r²   )	r  rÊ   r  r·   Ú
_hash_spanr/  r¶   Ú_encode_incomplete_tagsr,  )
r   rG   r‰  rŽ  r  Útokensrƒ  rM  r„  Ú
is_commentr)   )r   r  r*   rÐ   )  s"   
zMarkdown._hash_html_spansTc                 C   sl   d}||kr4|rt | j ¡ ƒD ]
\}}| ||¡}q|r.t | j ¡ ƒD ]
\}}| ||¡}q#|}||ks|S )z¢
        Recursively unhash a block of text

        Args:
            spans: unhash anything from `self.html_spans`
            code: unhash code blocks
        r²   )rû   rs   r¬   r   rœ   )r   rG   ÚspansÚcodeÚorigrÄ   Ú	sanitizedr)   r)   r*   rØ   Y  s   ùzMarkdown._unhash_html_spansr!   c                 C   sN   | j dkr| jS | j dkr g d¢}|D ]
\}}| ||¡}q|S td| j  ƒ‚)Nr   r   ©)ú&ú&amp;)r=  r~  )rw   ú&gt;zAinvalid value for 'safe_mode': %r (must be 'escape' or 'replace'))r8   rt   r   r0   )r   r!   ÚreplacementsÚbeforeÚafterr)   r)   r*   r/  l  s   

ÿzMarkdown._sanitize_htmlzå
            (                   # \1
              [ \t]+
              (['"])            # quote char = \2
              (?P<title>.*?)
              \2
            )?                  # title is optional
          \)$
        zÐ
          # Match tail of: [text][id]
          [ ]?          # one optional space
          (?:\n[ ]*)?   # one optional newline followed by spaces
          \[
            (?P<id>.*?)
          \]
        z\s*z<(.*)>.*r¿   c                 C   s"   | j  ||¡}|r| ¡ S t|ƒS )zlReturns the index of the first non-whitespace character in text
        after (and including) start
        )Ú_whitespacerº   r	  r¹   )r   rG   r¿   rº   r)   r)   r*   Ú_find_non_whitespace’  s   zMarkdown._find_non_whitespaceÚopen_cÚclose_cc                 C   sf   |}t |ƒ}d}|dkr1||k r1|| |kr|d7 }n
|| |kr%|d8 }|d7 }|dkr1||k s|S )zàReturns the index where the open_c and close_c characters balance
        out - the same number of open_c and close_c are encountered - or the
        end of string if it's reached before the balance point is found.
        r]   r   ©r¹   )r   rG   r¿   r¡  r¢  r"  ÚlÚcountr)   r)   r*   Ú_find_balanced™  s   
ûzMarkdown._find_balanced©NNNc           	      C   s¨   |   ||d ¡}|t|ƒkrdS |}|| dk}|r$|  ||d dd¡}|  ||dd¡}| j |||¡}|s8dS ||| ¡ … | d¡}}|rO| j d|¡}|||fS )	z=Extracts the url and (optional) title from the tail of a linkr]   r§  r=  rw   ú(ú)rj  z\1)	r   r¹   r¦  Ú_inline_link_titler¾   r¿   r¶   Ú_strip_anglebracketsrÇ   )	r   rG   r¿   ÚidxrR  Úhas_anglebracketsrº   ri  rj  r)   r)   r*   Ú_extract_url_and_title©  s   
zMarkdown._extract_url_and_titlezÍ
        data:
        # in format type/subtype;parameter=optional
        (?P<mime>\w+/[\w+\.-]+(?:;\w+=[\w+\.-]+)?)?
        # optional base64 token
        (?P<token>;base64)?
        ,(?P<data>.*)
    ri  c                 C   sh   | j  |¡}d}|dur!| d¡pd}| d¡r!| d¡dkr!d}t|| j|d}t|ƒ}|| j|< |S )	zù
        Function that passes a URL through `_html_escape_url` to remove any nasty characters,
        and then hashes the now "safe" URL to prevent other safety mechanisms from tampering
        with it (eg: escaping "&" in URL parameters)
        NÚmimer²   zimage/r„  z;base64Úbase64)r8   Úcharset)Ú_data_url_rerº   r¶   rð   Ú_html_escape_urlr8   r+   r›   )r   ri  Údata_urlr±  r¯  rÄ   r)   r)   r*   Ú_protect_urlÇ  s   
zMarkdown._protect_urlz#(?:https?|ftp):\/\/|(?:mailto|tel):c                 C   s<   d}d}d  ||¡}d||  }t d  | j|||¡tj¡S )aZ  
        _safe_href is adapted from pagedown's Markdown.Sanitizer.js
        From: https://github.com/StackExchange/pagedown/blob/master/LICENSE.txt
        Original Showdown code copyright (c) 2007 John Fraser
        Modifications and bugfixes (c) 2009 Dana Robinson
        Modifications and bugfixes (c) 2009-2014 Stack Exchange Inc.
        z-\wz$#/\.!#$%&\(\)\+,/:;=\?@\[\]^`\{\}\|~z5(?:[{}]+(?:\.[{}]+)*)(?:(?<!tel):\d+/?)?(?![^:/]*:/*)z[%s]*z%^(?:({})?({})({})|(#|\.{{,2}}/)({}))$)r  r–   r—   Ú_safe_protocolsÚI)r   ÚsafeÚ	less_safeÚdomainÚfragmentr)   r)   r*   Ú
_safe_hrefÚ  s
   	zMarkdown._safe_hrefc              	   C   s  d}d}d}	 z|  d|¡}W n
 ty   Y |S w t|ƒ}d}t|d t|| |ƒƒD ]}|| }	|	dkrA|d8 }|dk r@ nq,|	dkrI|d7 }q,|d }q||d |… }
| jrd|  |
¡}
|  |
¡}
d| jv r¤|
 	d¡r¤t
 d	d
|
dd… ¡}|| jv rŸd|› d|› d| j› d
|› d	}|d|… | ||d d…  }n|d }q|d7 }|||d … dkrÌ|  ||¡\}}}|du rÄ|d }q| j|dd}n…d}d| jv r|
rt
 d||d… ¡r| j |d|… › d||d… › |¡}|r| d¡ ¡ p|
 ¡ | jv r|d|… › d||d… › }nd}|p$| j ||¡}|s-|d }q| d¡ ¡ p8|
 ¡ }|| jvrB|}q| j| }| j |¡}| ¡ }| d| jd ¡ d| jd ¡}|r}t|ƒ d| jd ¡ d| jd ¡}d|› d}nd}|dko‹||d  dk}|r³|d8 }|  d¡}d|  |¡› d|  t|
ƒ¡› d|› |› | j›  }}n0||krÞ| jrÊ| j |¡sÊd|› d }nd!|  |¡› d|› d }|› |
› d"}n|d }qd#| jv rò| d| jd ¡}|t|ƒ }|t|ƒ }|d|… | ||d…  }q)$a‘  Turn Markdown link shortcuts into XHTML <a> and <img> tags.

        This is a combination of Markdown.pl's _DoAnchors() and
        _DoImages(). They are done together because that simplified the
        approach. It was necessary to use a different approach than
        Markdown.pl because of the lack of atomic matching support in
        Python's regex engine used in $g_nested_brackets.
        i¸  r   Tú[r]   ú]r¤   ú^rl  rë   Nz$<sup class="footnote-ref" id="fnref-z"><a href="#fn-ú">z
</a></sup>r¨  )r•  zlink-shortrefsz[ ]?(?:\n[ ]*)?(?!\[)z[]rh  r|  r}  z title="r‰   r²   ú!Úimgz
<img src="z" alt="z<a href="#"rw   z	<a href="ú</a>rˆ   )rM  rË   r¹   rN  Úminr8   rÐ   rØ   r9   rð   r–   rÇ   r¤   r¨   r®  rº   Ú_tail_of_reference_link_rer¶   r  rp   rq   r’   r	  r   r›   Ú_xml_escape_attrÚ_html_class_str_from_tagrµ  r  r‹   r¼  )r   rG   ÚMAX_LINK_TEXT_SENTINELÚanchor_allowed_posÚcurr_posrQ  Útext_lengthÚbracket_depthÚpr.   Ú	link_textr»   r\  ri  rj  Úurl_end_idxrº   Úlink_idÚ	title_strÚis_imgÚimg_class_strÚresult_headr)   r)   r*   ry  ì  sà   
  ìþÿ€


þþþÿ"(  
ýý
ÿþþþÿ
 ÜzMarkdown._do_linksrƒ   Únc                 C   sd   t |ƒ}|rt|tƒr|d | }| j|  d7  < dt|ƒks'| j| dkr0|d| j|  7 }|S )aÈ  Generate a header id attribute value from the given header
        HTML content.

        This is only called if the "header-ids" extra is enabled.
        Subclasses may override this for different header ids.

        @param text {str} The text of the header tag
        @param prefix {str} The requested prefix for header ids. This is the
            value of the "header-ids" extra key, if any. Otherwise, None.
        @param n {int} (unused) The <hN> tag number, i.e. `1` for an <h1> tag.
        @returns {str} The value for the header tag's "id" attribute. Return
            None to not have an id attribute and to exclude this header from
            the TOC (if the "toc" extra is specified).
        rë   r]   r   z-%s)Ú_slugifyrŽ   r¸   r¥   r¹   )r   rG   rƒ   rÕ  Ú	header_idr)   r)   r*   Úheader_id_from_text¦  s   zMarkdown.header_id_from_textc                 C   sP   t |ƒ}| jd  d¡}|rt|tƒr|d | }|| jv p'|tdd„ | jƒv S )Nr   rƒ   rë   c                 S   s   | d S rµ   r)   )Úxr)   r)   r*   rB  È  s    z,Markdown._header_id_exists.<locals>.<lambda>)rÖ  r9   r’   rŽ   r¸   r¥   Úmaprv   )r   rG   r×  rƒ   r)   r)   r*   Ú_header_id_existsÃ  s
   zMarkdown._header_id_existsÚlevelrh  ra   c                 C   s:   || j krd S | jd u rg | _| j |||  |¡f¡ d S rz   )r‘   rv   r·   r×   )r   rÜ  rh  ra   r)   r)   r*   Ú_toc_add_entryÊ  s
   

zMarkdown._toc_add_entrya=  
        (^(.+)[ \t]{0,99}\n(=+|-+)[ \t]*\n+)
        |
        (^(\#{1,6})  # \1 = string of #'s
        [ \t]%s
        (.+?)       # \2 = Header text
        [ \t]{0,99}
        (?<!\\)     # ensure not an escaped trailing '#'
        \#*         # optional closing #'s (not counted)
        \n+
        )
        r|  ú+c                 C   sø   |  d¡dur|  d¡dkr|  d¡S |  d¡dur,dddœ|  d¡d  }|  d¡}nt|  d¡ƒ}|  d	¡}| j d
¡}|rGt|| d	ƒ}d}d| jv ra|  || jd  d¡|¡}|rad| }|  |¡}d| jv rt|rt|  |||¡ d||||f S )z#Handles processing markdown headersr]   Nr   rë   r   )r­   rë   r   r   r€   zdemote-headersr²   r   rƒ   ú id="%s"r~   z<h%d%s>%s</h%d>

)r¶   r¹   r9   r’   rÄ  rØ  r>  rÝ  )r   rº   rÕ  Úheader_groupÚdemote_headersÚheader_id_attrr×  r4  r)   r)   r*   Ú_h_subá  s*   


ÿ
zMarkdown._h_subz\
        ^<h([1-6])(.*)>  # \1 tag num, \2 attrs
        (.*)  # \3 text
        </h\1>
    c                 C   sØ   |j | ¡ | ¡ … }t| d¡ƒ}t d| d¡pd¡pd}|r(| d¡p'd}| d¡}| d¡}|r;|  |¡r;|S |pI|  	|| j
d  d¡|¡}d	| j
v rV|  |||¡ |rj|sj|d
d… d|  |dd
…  S |S )zIDifferent to `_h_sub` in that this function handles existing HTML headersr]   z.*?id=(\S+)?.*r   r²   z'" r   r   rƒ   r~   Nrß  )r  r¿   r	  rª   r¶   r–   rº   rí   rÛ  rØ  r9   r’   rÝ  )r   rº   rG   Úh_levelÚid_attrÚh_textr×  r)   r)   r*   r3    s   


 zMarkdown._h_tag_subc                 C   s*   d| j v r| j | j|¡S | j | j|¡S )Nztag-friendly)r9   Ú_h_re_tag_friendlyrÇ   rã  Ú_h_rerå   r)   r)   r*   ro    s   
zMarkdown._do_headersz*+-z(?:[%s]|\d+\.)z(?:[%s])z	(?:\d+\.)c                 C   s’   |  d¡}|  d¡| jv rdpd}|dkr(|  d¡dkr(d|  d¡d d…  }nd}||  |¡ }|  |¡}| jrAd	 ||||¡S d
 ||||¡S )Nr]   r   ÚulÚolz1.z start="%s"r  r²   z<{}{}>
{}</{}>
z<{}{}>
{}</{}>

)r¶   Ú_marker_ul_charsrÇ  Ú_process_list_itemsr    r  )r   rº   ÚlstÚlst_typeÚlst_optsr\  r)   r)   r*   Ú	_list_sub4  s   

zMarkdown._list_subc                 C   s  d}	 g }| j | jfD ]M}| jd }|| jkr| j n| j}d||||f }| jr8t d| tjtjB tjB ¡}nt d| tjtjB tjB ¡}| 	||¡}	|	rX| 
|	 ¡ |	f¡ q|s^	 |S | ¡  |d d }	|	 ¡ \}
}|  |	¡}|d |
… | ||d …  }|
t|ƒ }q)Nr   Tr]   a]  
                    (                   # \1 = whole list
                      (                 # \2
                        ([ ]{0,%d})     # \3 = the indentation level of the list item marker
                        (%s)            # \4 = first list item marker
                        [ \t]+
                        (?!\ *\4\ )     # '- - - ...' isn't a list. See 'not_quite_a_list' test case.
                      )
                      (?:.+?)
                      (                 # \5
                          \Z
                        |
                          \n{2,}
                          (?=\S)
                          (?!           # Negative lookahead for another list item marker
                            [ \t]*
                            %s[ \t]+
                          )
                        |
                          \n+
                          (?=
                            \3          # lookahead for a different style of list item marker
                            %s[ \t]+
                          )
                      )
                    )
                r¿  z(?:(?<=\n\n)|\A\n?))Ú
_marker_ulÚ
_marker_olr7   r    r–   r—   r`  r˜   ÚSr¾   r·   r¿   rÚ   r  rð  r¹   )r   rG   ÚposÚhitsÚ
marker_patrc  Úother_marker_patÚ
whole_listÚlist_rerº   r¿   r	  r8  r)   r)   r*   rq  F  s8   

æ 
ÿ€ù
ÎzMarkdown._do_listsaD  
        (\n)?                   # leading line = \1
        (^[ \t]*)               # leading whitespace = \2
        (?P<marker>{}) [ \t]+   # list marker = \3
        ((?:.+?)                # list item text = \4
        (\n{{1,2}}))              # eols = \5
        (?= \n* (\Z | \2 (?P<next_marker>{}) [ \t]+))
        zr
        (\[[\ xX]\])[ \t]+       # tasklist marker = \1
        (.*)                   # list item text = \2
    zE<input type="checkbox" class="task-list-item-checkbox" %sdisabled> %sc                 C   sD   |  d¡}|  d¡}|dv r| jd|f S |dkr | jd|f S dS )Nr]   r   )z[x]z[X]zchecked z[ ]r²   )r¶   Ú_task_list_warpper_str)r   rº   ÚmarkerÚ	item_textr)   r)   r*   Ú_task_list_item_sub“  s   

zMarkdown._task_list_item_subc                 C   s¶   |  d¡}|  d¡}|sd|v s| jr$| j|d| jdd }|  |¡}n|  | j|ddd ¡}| d¡r;|d d… }|  |¡}t|  d	¡ƒd
k| _d| j	v rW| j
 | j|¡}d| S )Nr   r]   r±   rx   )Úmin_outdentÚmax_outdent©rþ  r¯   r  r   r   Ú	task_listz<li>%s</li>
)r¶   Ú_last_li_endswith_two_eolsÚ_uniform_outdentrŒ   rÔ   rq  r  r>  r¹   r9   Ú_task_list_item_rerÇ   rý  )r   rº   r  Úleading_liner)   r)   r*   Ú_list_item_subŸ  s   




zMarkdown._list_item_subÚlist_strc                 C   sD   |  j d7  _ d| _| d¡d }| j | j|¡}|  j d8  _ |S )Nr]   Fr¯   )r    r  r  Ú_list_item_rerÇ   r  )r   r  r)   r)   r*   rì  ²  s   zMarkdown._process_list_itemsÚ
lexer_namec                 C   sN   z
ddl m}m} W n
 ty   Y dS w z| |¡W S  |jy&   Y dS w )zy
        Returns:
            `pygments.Lexer` or None if a lexer matching `lexer_name` is
            not found
        r   )ÚlexersÚutilN)Úpygmentsr
  r  ÚImportErrorÚget_lexer_by_nameÚClassNotFound)r   r	  r
  r  r)   r)   r*   Ú_get_pygments_lexerÑ  s   ÿÿzMarkdown._get_pygments_lexerÚ	codeblockc                 K   sL   ddl }ddl}G dd„ d|jjƒ}| dd¡ |di |¤Ž}| |||¡S )a-  
        TODO: this function is only referenced by the `FencedCodeBlocks`
        extra. May be worth moving over there

        Args:
            codeblock: the codeblock to highlight
            lexer (pygments.Lexer): lexer to use
            formatter_opts: pygments HtmlFormatter options
        r   Nc                   @   s&   e Zd Zdd„ Zdd„ Zddd„ZdS )	z8Markdown._color_with_pygments.<locals>.HtmlCodeFormatterc                 s   s    dV  |E dH  dV  dS )zgA function for use in a Pygments Formatter which
                wraps in <code> tags.
                )r   rŠ  N)r   r‹  r)   ©r   rj   r)   r)   r*   Ú
_wrap_codeó  s   €

zCMarkdown._color_with_pygments.<locals>.HtmlCodeFormatter._wrap_codec                 s   s    dV  |E d H  dV  d S )N)r   r¯   r)   r  r)   r)   r*   Ú_add_newlineû  s   €

zEMarkdown._color_with_pygments.<locals>.HtmlCodeFormatter._add_newlineNc              	   S   s:   |du r|   |  |  |¡¡¡S |  |   |  |  |¡¡¡¡S )z,Return the source with a code, pre, and div.N)r  Ú	_wrap_prer  Ú	_wrap_div)r   ÚsourceÚoutfiler)   r)   r*   Úwrap  s   z=Markdown._color_with_pygments.<locals>.HtmlCodeFormatter.wraprz   )r1   r2   r3   r  r  r  r)   r)   r)   r*   ÚHtmlCodeFormatterò  s    r  ÚcssclassÚ
codehiliter)   )r  Úpygments.formattersÚ
formattersÚHtmlFormatterr“   Ú	highlight)r   r  ÚlexerÚformatter_optsr  r  Ú	formatterr)   r)   r*   Ú_color_with_pygmentsà  s   zMarkdown._color_with_pygmentsc                 C   s\   |  d¡}|  |¡}|  |¡}| d¡}| ¡ }|  d¡}|  d¡}|  |¡}d |||¡S )Nr]   r¯   Úprer•  z!
<pre{}><code{}>{}
</code></pre>
)r¶   Ú_outdentrÌ   rï   r  rÇ  Ú_encode_coder  )r   rº   r  Úpre_class_strÚcode_class_strr)   r)   r*   Ú_code_block_sub  s   






ÿzMarkdown._code_block_subr5  c                 C   sT   d| j vrdS z| j d }W n
 ty   Y dS w t|tƒr(||v r(d||  S dS )zoGet the appropriate ' class="..."' string (note the leading
        space), if any, for the given tag.
        zhtml-classesr²   z class="%s")r9   Ú	TypeErrorrŽ   r   )r   r5  Úhtml_classes_from_tagr)   r)   r*   rÇ    s   
ÿ
z!Markdown._html_class_str_from_tagc                 C   s.   t  d| j| jf t jt jB ¡}| | j|¡S )z&Process Markdown `<pre><code>` blocks.a<  
            (?:\n\n|\A\n?)
            (               # $1 = the code block -- one or more lines, starting with a space/tab
              (?:
                (?:[ ]{%d} | \t)  # Lines must start with a tab or a tab-width of spaces
                .*\n+
              )+
            )
            ((?=^[ ]{0,%d}\S)|\Z)   # Lookahead for non-space at line-start, or end of doc
            # Lookahead to make sure this block isn't already in a code block.
            # Needed when syntax highlighting is being used.
            (?!([^<]|<(/?)span)*\</code\>)
            )r–   r—   r7   r˜   r`  rÇ   r*  )r   rG   Úcode_block_rer)   r)   r*   rr  -  s   
ô
ózMarkdown._do_code_blocksa
  
            (?<!\\)
            (`+)        # \1 = Opening run of `
            (?!`)       # See Note A test/tm-cases/escapes.text
            (.+?)       # \2 = The code block
            (?<!`)
            \1          # Matching closer
            (?!`)
        c                 C   s,   |  d¡ d¡}|  |¡}d |  d¡|¡S )Nr   rE  z<code{}>{}</code>r•  )r¶   rí   r'  r  rÇ  )r   rº   Úcr)   r)   r*   Ú_code_span_subR  s   
zMarkdown._code_span_subc                 C   s   | j  | j|¡S rz   )Ú_code_span_rerÇ   r/  rå   r)   r)   r*   rw  W  s   zMarkdown._do_code_spansc                 C   s8   g d¢}|D ]
\}}|  ||¡}qt|ƒ}|| j|< |S )z¸Encode/escape certain characters inside Markdown code runs.
        The point is that in code, these characters are literals,
        and lose their special Markdown meanings.
        r˜  )r   r+   rœ   )r   rG   rœ  r  rž  Úhashedr)   r)   r*   r'  q  s   
zMarkdown._encode_codez"(\*\*|__)(?=\S)(.+?[*_]?)(?<=\S)\1z(\*|_)(?=\S)(.*?\S)\1c                 C   s    | j  d|¡}| j d|¡}|S )Nz<strong>\2</strong>z<em>\2</em>)Ú
_strong_rerÇ   Ú_em_rerå   r)   r)   r*   r{  ‡  s   zMarkdown._do_italics_and_bolda&  
        (                           # Wrap whole match in \1
          (
            ^[ \t]*>%s[ \t]?        # '>' at the start of a line
              .+\n                  # rest of the first line
            (.+\n)*                 # subsequent consecutive lines
          )+
        )
    r²   z[ 	]*?!?z^[ 	]*>[ 	]?z^[ 	]*>[ 	]*?![ 	]?z"\A(?:^[ \t]*>[ \t]*?!.*[\n\r]*)+\Zz(\s*<pre>.+?</pre>)c                 C   s   t  dd| d¡¡S )Nz(?m)^  r²   r]   )r–   rÇ   r¶   ©r   rº   r)   r)   r*   Ú_dedent_two_spaces_sub  s   zMarkdown._dedent_two_spaces_subc                 C   sŒ   |  d¡}d| jv o| j |¡}|r| j d|¡}n| j d|¡}| j d|¡}|  |¡}t	 dd|¡}| j
 | j|¡}|rBd| S d| S )Nr]   Úspoilerr²   z(?m)^ú  z/<blockquote class="spoiler">
%s
</blockquote>

z<blockquote>
%s
</blockquote>

)r¶   r9   Ú_bq_all_lines_spoilersrº   Ú_bq_one_level_re_spoilerrÇ   Ú_bq_one_level_rerÍ   rÔ   r–   Ú_html_pre_block_rer5  )r   rº   ÚbqÚ
is_spoilerr)   r)   r*   Ú_block_quote_sub   s   

zMarkdown._block_quote_subc                 C   s6   d|vr|S d| j v r| j | j|¡S | j | j|¡S )Nrw   r6  )r9   Ú_block_quote_re_spoilerrÇ   r>  Ú_block_quote_rerå   r)   r)   r*   rs  µ  s
   
zMarkdown._do_block_quotesc                 C   s2  |  d¡}g }tt d|¡ƒD ]„\}}|| jv r!| | j| ¡ qd }d| jv rv| j |d ¡}|rvt	| 
d¡ƒdkrv| 
d¡rN| 
d¡d | 
d¡d ksU| 
d¡d u rv| ¡ }|  ||d … ¡ d¡}t d	|¡rr|d |… }n|}d }|  |¡}| d
|  d¡ | d¡ d ¡ |r“| |¡ qd |¡S )Nr¯   z\n{2,}zcuddled-listsr   r   Únext_markerrû  r  z^<(?:ul|ol).*?>z<p%s>rÍ  rE  ú</p>r±   )rí   r  r–   rÊ   rr   r·   r9   r  r¾   r¹   r¶   r¿   rq  r  rº   r>  rÇ  rï   r,  )r   rG   Úgrafsr"  ÚgrafÚcuddled_listÚlir¿   r)   r)   r*   rt  ¾  s2   


þ
"
€
zMarkdown._form_paragraphsc              	      s\  ˆ j r¬ddˆ j dg}ˆ jsdˆ _ˆ jsdˆ _ˆ jj‡ fdd„d tˆ jƒD ]q\}}|d	kr4| d
¡ | d| ¡ | ˆ  ˆ j | ¡¡ zdˆ j d ˆ j d ||d f }W n t	yp   t
 d¡ d||d f }Y nw |d  d¡rŒ|d d tdƒ … d | d |d< n| d| ¡ | d¡ q'| d¡ | d¡ |d d |¡ S |S )Nz<div class="footnotes">rC  z<ol>z%Jump back to footnote %d in the text.z&#8617;c                    s   t ˆ j ¡ ƒ | ¡S rz   )rû   r¤   ÚkeysrM  )Úar¢   r)   r*   rB  þ  ó    z)Markdown._add_footnotes.<locals>.<lambda>rÃ   r   r²   z<li id="fn-%s">z4<a href="#fnref-%s" class="footnoteBackLink" title="rÀ  rÃ  r]   zHFootnote error. `footnote_title` must include parameter. Using defaults.zf<a href="#fnref-%s" class="footnoteBackLink" title="Jump back to footnote %d in the text.">&#8617;</a>r  rB  z&#160;z

<p>%s</p>ú</li>z</ol>z</div>r±   r¯   )r¤   r‹   r;   r<   r§   rÚ   r  r·   rÔ   r+  r  r  r  r¹   r,  )r   rG   Úfooterr"  rh  Úbacklinkr)   r¢   r*   rÕ   î  s^   ý
þþýü
ü

ýýÿÿ
ÿ

zMarkdown._add_footnotesz<(?![a-z/?\$!])z(?<![a-z0-9?!/'"-])>c                 C   s,   t  d|¡}| j d|¡}| j d|¡}|S )Nrš  r~  r›  )Ú_AMPERSAND_RErÇ   Ú_naked_lt_reÚ_naked_gt_rerå   r)   r)   r*   rg  !	  s   z Markdown._encode_amps_and_anglesz'<(!--|/?\w+?(?!\w)\s*?.+?(?:[\s/]+?|$))c                 C   s2   | j dvr|S | d¡r|S dd„ }| j ||¡S )Nr   rw   c                 S   s   |   ¡  dd¡S )Nr=  r~  )r¶   r   )rº   r)   r)   r*   Úincomplete_tags_sub8	  ó   z=Markdown._encode_incomplete_tags.<locals>.incomplete_tags_sub)r8   r  Ú_incomplete_tags_rerÇ   )r   rG   rP  r)   r)   r*   r‘  1	  s   

z Markdown._encode_incomplete_tagsc                 C   s,   t | j ¡ ƒD ]\}}| d| |¡}q|S )Nr  )rû   r›   r¬   r   )r   rG   r.   r   r)   r)   r*   r€  =	  s   z"Markdown._encode_backslash_escapesz<((https?|ftp):[^\'">\s]+)>c                 C   s   |  d¡}d |  |¡|¡S )Nr]   ú<a href="{}">{}</a>)r¶   r  rµ  )r   rº   Úg1r)   r)   r*   Ú_auto_link_subC	  s   
zMarkdown._auto_link_subz 
          <
           (?:mailto:)?
          (
              [-.\w]+
              \@
              [-\w]+(\.[-\w]+)*\.[a-z]+
          )
          >
        c                 C   s   |   |  | d¡¡¡S rµ   )Ú_encode_email_addressr×   r¶   r4  r)   r)   r*   Ú_auto_email_link_subQ	  s   ÿzMarkdown._auto_email_link_subc                 C   s$   | j  | j|¡}| j | j|¡}|S rz   )r‡  rÇ   rU  rˆ  rW  rå   r)   r)   r*   rz  U	  s   zMarkdown._do_auto_linksÚaddrc                 C   s6   dd„ d| D ƒ}dd  |¡d  |dd … ¡f }|S )Nc                 S   s   g | ]}t |ƒ‘qS r)   )Ú _xml_encode_email_char_at_randomr,   r)   r)   r*   r  g	  s    ÿz2Markdown._encode_email_address.<locals>.<listcomp>zmailto:z<a href="%s">%s</a>r²   é   )r,  )r   rX  Úcharsr)   r)   r*   rV  Z	  s   ÿÿzMarkdown._encode_email_addressc                 C   sh   t | j ¡ ƒt | j ¡ ƒ }|t dd„ | j ¡ D ƒƒ7 }	 |}|D ]
\}}| ||¡}q!||kr3	 |S q)Nc                 s   s    | ]	}t t|ƒƒV  qd S rz   )ÚtupleÚreversed©r-   r"  r)   r)   r*   Ú	<genexpr>r	  s   € z3Markdown._unescape_special_chars.<locals>.<genexpr>)r\  r›   r¬   rœ   rr   r   )r   rG   ÚhashmapÚ	orig_textr.   Úhashr)   r)   r*   r×   n	  s   úz Markdown._unescape_special_charsc                 C   ó   | j  d|¡S re  )r™   rÇ   rå   r)   r)   r*   r&  {	  s   zMarkdown._outdentc                 C   s   t |ƒ}|| j|< |S )z 
        Wrapper around `_hash_text` that also adds the hash to `self.hash_spans`,
        meaning it will be automatically unhashed during conversion.
        )r+   rs   )r   rG   rÄ   r)   r)   r*   r  	  s   
zMarkdown._hash_spanrþ  rÿ  c           	         sæ   dd„ |   ¡ D ƒ}dd„ |D ƒ}|sd| fS t|ƒ}ˆ dur,t‡ fdd„|D ƒp*ˆ gƒ}|dur5t||ƒ}g }t||   d¡ƒD ],\}}| |¡rS| | |dd¡¡ q?|durf||k rf| | |dd¡¡ q?| |¡ q?|d |¡fS )	aS  
        Removes the smallest common leading indentation from each (non empty)
        line of `text` and returns said indent along with the outdented text.

        Args:
            min_outdent: make sure the smallest common whitespace is at least this size
            max_outdent: the maximum amount a line can be outdented by
        c                 S   s$   g | ]}|rt  d |¡d nd‘qS )z^[ \t]*r   N)r–   rñ   )r-   r#  r)   r)   r*   r  ˜	  s    ÿÿz-Markdown._uniform_outdent.<locals>.<listcomp>c                 S   s   g | ]}|d ur|‘qS rz   r)   r^  r)   r)   r*   r  œ	  ó    r²   Nc                    s   g | ]}|ˆ kr|‘qS r)   r)   r^  r   r)   r*   r  ¦	  rd  Tr]   )r  rÄ  Úziprð   r·   r   r,  )	rG   rþ  rÿ  Ú
whitespaceÚwhitespace_not_emptyÚoutdentÚ	outdentedÚline_wsr#  r)   r   r*   r  ˆ	  s&   þ

zMarkdown._uniform_outdentÚindentÚinclude_empty_linesÚindent_empty_linesc                 C   sT   g }|   d¡D ]}| ¡ s|r| || ¡ q|r| |¡ q| d¡ qd |¡S )aO  
        Uniformly indent a block of text by a fixed amount

        Args:
            text: the text to indent
            indent: a string containing the indent to apply
            include_empty_lines: don't remove whitespace only lines
            indent_empty_lines: indent whitespace only lines with the rest of the text
        Tr²   )r  rí   r·   r,  )rG   rk  rl  rm  Úblocksr#  r)   r)   r*   Ú_uniform_indent·	  s   
zMarkdown._uniform_indentÚsubstrc                 C   sf   t  t  |¡| ¡D ]'}| ¡ \}}|| ¡   kr|kr  dS  || ¡   kr.|kr0 dS  q	q	dS )zV
        Checks if a regex match overlaps with a substring in the given text.
        TF)r–   Úfinditerr   r  r¿   r	  )rG   rº   rp  Úinstancer¿   r	  r)   r)   r*   Ú_match_overlaps_substrÑ	  s   ÿÿzMarkdown._match_overlaps_substr)F)TFrz   ©NN)FF)¯r1   r2   r3   Ú_extras_dictÚ__annotations__r   r¸   rt   ru   r   Ú
_safe_moderû   r\  rª   r    rJ   Úfloatr–   r—   r˜   rÍ   ÚDEFAULT_TAB_WIDTHÚboolÚ_extras_paramÚ_link_patternsrž   r£   r¡   Ú
IGNORECASEÚVERBOSErÙ   rE   ro   rY   rÖ   rK   rÏ   r  Ú	MULTILINErú   rò   ró   rø   rù   rÎ   ÚUNICODErÆ   ÚDOTALLr  ÚMatchrÈ   rÉ   r(  rÌ   Ú
_html5tagsrI  r`  Ú_strict_tag_block_reÚ_block_tags_brK  rJ  r0  r   r<  rL   rÑ   r   rH  rX  rM   rÓ   rb  rm  rÒ   rp  rN   rÔ   rT   r>  r  rV   rx  rÐ   rØ   r/  ró  rª  rÅ  rŸ  r«  r   r¦  r®  r²  rµ  r¶  Úpropertyr¼  rW   ry  rØ  rÛ  rÝ  Ú
_h_re_baserè  rç  rã  r2  r3  rO   ro  rë  Ú_marker_anyrñ  rò  rð  rP   rq  r  r  rú  rý  r  r  rì  r  r$  r*  rÇ  rQ   rr  r0  r/  rU   rw  r'  r2  r3  rX   r{  Ú_block_quote_baser@  r?  r:  r9  r8  r;  r5  r>  rR   rs  rS   rt  rÕ   r·  rN  rO  rg  rR  r‘  r€  r‡  rU  ra  rˆ  rW  rz  rV  r×   r&  r  Ústaticmethodr  ro  rs  r)   r)   r)   r*   rD     s2  
 öþýüûúùø	÷

öV

	÷ õþÿJ	û	g
ö
õÿ
ö
õÿýþý
ü4yûþýüû
ú3	!é/
ø	
ù
	
,ù	
 =ýÿþý
ü
ü;ø
ýþ
û.
ø
	/0
	÷
	ýÿþý
ü.üÿþýüûrD   c                   @   s   e Zd ZdZddgZdS )ÚMarkdownWithExtrasa‚  A markdowner class that enables most extras:

    - footnotes
    - fenced-code-blocks (only highlights code if 'pygments' Python module on path)

    These are not included:
    - pyshell (specific to Python-related documenting)
    - code-friendly (because it *disables* part of the syntax)
    - link-patterns (because you need to specify some actual
      link-patterns anyway)
    r¤   úfenced-code-blocksN)r1   r2   r3   Ú__doc__r9   r)   r)   r)   r*   r‹  ß	  s    r‹  c                   @   sä   e Zd ZU i Zeeed  f ed< i Zee	e
eed   eed   f f ed< eed< 	 e
eee	ed  f  eee	ed  f  f ed< 	 dedee fdd„Zed	d
„ ƒZedd„ ƒZededefdd„ƒZdedefdd„ZdS )r_   r«   r`   ra   r^   r\   Úoptionsc                 C   s"   || _ |dur|| _dS i | _dS )zˆ
        Args:
            md: An instance of `Markdown`
            options: a dict of settings to alter the extra's behaviour
        N)r\   rŽ  ©r   r\   rŽ  r)   r)   r*   rž   
  s   zExtra.__init__c                 C   sN   | j | jv r| j| j = tj ¡ D ]}|D ]}| |v r#| | ¡ | |v sqqdS )z\
        Removes the class from the extras registry and unsets its execution order.
        N)ra   r«   r_   r`   ÚvaluesÚremove)ÚclsÚ
exec_orderÚsectionr)   r)   r*   Ú
deregister
  s   

ÿ€ÿþzExtra.deregisterc                 C   s  | | j | j< tg | jd ¢| jd ¢R ƒD ]i\}}|t| jd ƒk }t|tƒsOt|tƒrOtj	 
¡ D ]}|D ]}||v rL| |¡}|sF|d7 }| || ¡ q5q1qtj	 |g g f¡ | tj	| |radnd v rfq|rttj	| d  d| ¡ qtj	| d  | ¡ qdS )zŽ
        Registers the class for use with `Markdown` and calculates its execution order based on
        the `order` class attribute.
        r   r]   N)r«   ra   r  r^   r¹   rŽ   rJ   Ú
issubclassr_   r`   r  rM  Úinsertr“   r·   )r’  rM  r  r  Úexec_ordersr”  Úto_indexr)   r)   r*   Úregister
  s*   &
€ûþ
ëzExtra.registerrG   r"   c                 C   ó   dS )z„
        Run the extra against the given text.

        Returns:
            The new text after being modified by the extra
        Nr)   rå   r)   r)   r*   rd   :
  s   z	Extra.runc                 C   r›  )zä
        Check a section of markdown to see if this extra should be run upon it.
        The default implementation will always return True but it's recommended to override
        this behaviour to improve performance.
        Tr)   rå   r)   r)   r*   rc   D
  rç   z
Extra.testN)r1   r2   r3   r«   r   r¸   Útyperv  r`   rJ   r\  rû   r   r   rD   r   rž   Úclassmethodr•  rš  r	   rd   rz  rc   r)   r)   r)   r*   r_   õ	  s   
 ,0	

	r_   c                       s†   e Zd ZdZdZejfejffZej	Z
ejZdedef‡ fdd„Zdd„ Zed	ejd
efdd„ƒZd	ejd
efdd„Zdd„ Z‡  ZS )ÚItalicAndBoldProcessora•  
    An ABC that provides hooks for dealing with italics and bold syntax.
    This class is set to trigger both before AND after the italics and bold stage.
    This allows any child classes to intercept instances of bold or italic syntax and
    change the output or hash it to prevent it from being processed.

    After the I&B stage any hashes in the `hash_tables` instance variable are replaced.
    zitalic-and-bold-processorr\   rŽ  c                    s   t ƒ  ||¡ i | _d S rz   )Úsuperrž   Ú
hash_tabler  ©Ú	__class__r)   r*   rž   \
  s   
zItalicAndBoldProcessor.__init__c                 C   sn   | j jtjk r| j | j|¡}| j | j|¡}|S d}||kr5|}| j ¡ D ]
\}}| 	||¡}q&||ks|S re  )
r\   r^   rJ   rX   Ú	strong_rerÇ   Úem_rer   r¬   r   )r   rG   ra  rÄ   rp  r)   r)   r*   rd   `
  s   ûýzItalicAndBoldProcessor.runrº   r"   c                 C   s   |j | ¡ | ¡ … S rz   )r  r¿   r	  r4  r)   r)   r*   rÇ   m
  s   zItalicAndBoldProcessor.subc                 C   s,   |j | ¡ | ¡ … }t|ƒ}|| j|< |S rz   )r  r¿   r	  r+   r   )r   rº   rp  rÄ   r)   r)   r*   Úsub_hashr
  s   
zItalicAndBoldProcessor.sub_hashc                 C   s0   | j jtjk rd|v pd|v S | jot d|¡S )Nr|  r}  zmd5-[0-9a-z]{32})r\   r^   rJ   rX   r   r–   r¾   rå   r)   r)   r*   rc   x
  s   zItalicAndBoldProcessor.test)r1   r2   r3   r  ra   rJ   rX   r^   rD   r2  r£  r3  r¤  r   rž   rd   r	   r–   r‚  r¸   rÇ   r¥  rc   Ú__classcell__r)   r)   r¡  r*   rž  M
  s    rž  c                   @   sj   e Zd ZdZdZejejfdfZdZ	e
 de	 e
je
jB e
jB ¡Zdd„ Zde
jd	efd
d„Zdd„ ZdS )ÚAdmonitionsz+
    Enable parsing of RST admonitions
    Úadmonitionsr)   zIadmonition|attention|caution|danger|error|hint|important|note|tip|warningaP  
        ^(\ *)\.\.\ (%s)::\ *                # $1 leading indent, $2 the admonition
        (.*)?                                # $3 admonition title
        ((?:\s*\n\1\ {3,}.*)+?)              # $4 admonition body (required)
        (?=\s*(?:\Z|\n{4,}|\n\1?\ {0,2}\S))  # until EOF, 3 blank lines or something less indented
        c                 C   s   | j  |¡d uS rz   )Úadmonitions_rer¾   rå   r)   r)   r*   rc   ”
  rQ  zAdmonitions.testrº   r"   c           	      C   s˜   |  ¡ \}}}}d| }| ¡ dkrd}nd| ¡  }|r!d| }| j d| j |¡d  ¡}| j d |||¡| jjd¡}d	 ||¡}| j ||d¡S )
Nz<strong>%s</strong>Ú
admonitionzadmonition %sú<em>%s</em>ú
%s
r]   z
{}
{}

{}
Fz<aside class="{}">
{}</aside>)rf  r  r\   rÔ   r  ro  r  rŒ   )	r   rº   Úlead_indentÚadmonition_namerj  ÚbodyÚadmonition_typeÚadmonition_classrª  r)   r)   r*   rÇ   —
  s   þzAdmonitions.subc                 C   ó   | j  | j|¡S rz   )r©  rÇ   rå   r)   r)   r*   rd   ³
  rQ  zAdmonitions.runN)r1   r2   r3   r  ra   rJ   rN   rM   r^   r¨  r–   r—   r}  r  r~  r©  rc   r‚  r¸   rÇ   rd   r)   r)   r)   r*   r§  
  s    ûú	r§  c                   @   sR   e Zd ZdZdZdejffZe 	dej
¡Zdd„ Zdejdefd	d
„Zdd„ ZdS )ÚAlertsz¾
    Markdown Alerts as per
    https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
    Úalertsr)   zÀ
        <blockquote>\s*
        <p>
        \[!(?P<type>NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]
        (?P<closing_tag></p>[ \t]*\n?)?
        (?P<contents>[\s\S]+?)
        </blockquote>
    c                 C   ó   d|v S )Nz<blockquote>r)   rå   r)   r)   r*   rc   Ê
  ó   zAlerts.testrº   r"   c                 C   sd   |d   ¡ }d|d  ¡ › d}|d  ¡ }|d r&d|› d|› d|› d	S d|› d|› d
|› d	S )Nrœ  z<em>z</em>ÚcontentsÚclosing_tagz<div class="alert z">
r¯   ú
</div>z
<p>)r  rj  rí   )r   rº   ÚtypÚheadingr·  r)   r)   r*   rÇ   Í
  s   z
Alerts.subc                 C   r²  rz   )Úalert_rerÇ   rå   r)   r)   r*   rd   Ö
  rQ  z
Alerts.runN)r1   r2   r3   r  ra   rJ   rR   r^   r–   r—   r`  r¼  rc   r‚  r¸   rÇ   rd   r)   r)   r)   r*   r³  ·
  s    ù
	r³  c                   @   s$   e Zd ZU dZeed< 	 eed< dS )Ú_BreaksExtraOptszOptions for the `Breaks` extraÚon_backslashr†   N©r1   r2   r3   r  rz  rv  r)   r)   r)   r*   r½  Ú
  s   
 r½  )Útotalc                   @   s.   e Zd ZU dZdejffZeed< dd„ Z	dS )ÚBreaksr…   r)   rŽ  c                 C   sd   | j  dd¡}| j  dd¡}|r|rd}n|rd}n|rd}nd}d| jj }t |d	 ||¡}|S )
Nr¾  Fr†   z *\\?z(?: *\\| {2,})z *z {2,}rv  z\n(?!\<(?:\/?(ul|ol|li))\>))rŽ  r’   r\   r‹   r–   rÇ   )r   rG   r¾  r†   ÚpatternÚ	break_tagr)   r)   r*   rd   ç
  s   z
Breaks.runN)
r1   r2   r3   ra   rJ   rX   r^   r½  rv  rd   r)   r)   r)   r*   rÁ  â
  s
   
 rÁ  c                       s0   e Zd ZdZdZdejdef‡ fdd„Z‡  Z	S )ÚCodeFriendlyz-
    Disable _ and __ for em and strong.
    zcode-friendlyrº   r"   c                    s†   |  d¡}|j| ¡ | ¡ … }d|v rt|ƒ}|| j|< |S d|v r=|t|ƒt|ƒ … }t|ƒ}|| j|< || | S tƒ  |¡S )Nr]   r}  )	r¶   r  r¿   r	  r+   r   r¹   rŸ  rÇ   )r   rº   ÚsyntaxrG   rÄ   r¡  r)   r*   rÇ      s   


zCodeFriendly.sub)
r1   r2   r3   r  ra   r–   r‚  r¸   rÇ   r¦  r)   r)   r¡  r*   rÄ  ú
  s     rÄ  c                   @   s–   e Zd ZdZdZejejfejffZ	e
 de
je
jB e
jB ¡Zdd„ Zdededefd	d
„Zdedeeef fdd„Zde
jdefdd„Zdd„ ZdS )ÚFencedCodeBlockszÝ
    Allows a code block to not have to be indented
    by fencing it with '```' on a line before and after. Based on
    <http://github.github.com/github-flavored-markdown/> with support for
    syntax highlighting.
    rŒ  a"  
        (?:\n+|\A\n?|(?<=\n))
        (^[ \t]*`{3,})\s{0,99}?([\w+-]+)?\s{0,99}?\n  # $1 = opening fence (captured for back-referencing), $2 = optional lang
        (.*?)                             # $3 = code block content
        \1[ \t]*\n                      # closing fence
        c                 C   sN   d|vrdS | j jtjkr| j jsdS | j jtjkr | j jr dS | j jtjkS )Nz```FT)r\   r[   rJ   rK   r8   rM   rN   rå   r)   r)   r*   rc   $  s   zFencedCodeBlocks.testr  Úleading_indentr"   c                    sb   ˆ j jd pi }‡ fdd„}ˆ j j||d\}}||ƒ}ˆ j j||fi |¤Ž}dˆ j  ||d¡ S )zÀ
        Args:
            codeblock: the codeblock to format
            leading_indent: the indentation to prefix the block with
            lexer (pygments.Lexer): the lexer to use
        rŒ  c                    sL   t ˆ jj ¡ ƒD ]
\}}|  ||¡} qg d¢}|D ]
\}}|  ||¡} q| S )N))rš  r™  )r~  r=  )r›  rw   )rû   r\   rs   r¬   r   )r  rÄ   r—  rœ  ÚoldÚnewr¢   r)   r*   Úunhash_code;  s   z@FencedCodeBlocks._code_block_with_lexer_sub.<locals>.unhash_code©rÿ  r¬  T)r\   r9   r  r$  ro  )r   r  rÇ  r!  r"  rÊ  r}  Úcoloredr)   r¢   r*   Ú_code_block_with_lexer_sub-  s   ÿz+FencedCodeBlocks._code_block_with_lexer_subr	  c                 C   sF   | j  d¡}d| j jv r|rd ||¡}n| j  d¡}d ||¡dfS )aT  
        Returns the tags that the encoded code block will be wrapped in, based
        upon the lexer name.

        This function can be overridden by subclasses to piggy-back off of the
        fenced code blocks syntax (see `Mermaid` extra).

        Returns:
            The opening and closing tags, as strings within a tuple
        r%  úhighlightjs-langz class="{} language-{}"r•  z<pre{}><code{}>z</code></pre>)r\   rÇ  r9   r  )r   r	  Ú	pre_classÚ
code_classr)   r)   r*   ÚtagsP  s
   zFencedCodeBlocks.tagsrº   c                 C   sÞ   |  d¡}|  d¡}|d d… }|r9d| jjvr9| j |¡}|r9dt|  d¡ƒt|  d¡ ¡ ƒ  }|  |||¡S dt|  d¡ƒt|  d¡ ¡ ƒ  }|rW| jj||d\}}| j |¡}|  	|¡}d 
||d	 |||d ¡S )
Nr   r   r  rÎ  rx   r]   rË  z
{}{}{}
{}{}
r   )r¶   r\   r9   r  r¹   rï   rÍ  r  r'  rÑ  r  )r   rº   r	  r  r!  rÇ  rÑ  r)   r)   r*   rÇ   b  s   

$$
zFencedCodeBlocks.subc                 C   r²  rz   )Úfenced_code_block_rerÇ   rå   r)   r)   r*   rd   z  rQ  zFencedCodeBlocks.runN)r1   r2   r3   r  ra   rJ   rM   rN   rK   r^   r–   r—   r˜   r`  ró  rÒ  rc   r¸   rÍ  r\  rÑ  r‚  rÇ   rd   r)   r)   r)   r*   rÆ    s$    û	þý
û#rÆ  c                   @   sˆ   e Zd ZdZdZejefdfZe	 
d¡Ze	 
de	j¡Ze	 
de	j¡Ze	 
de	j¡Ze	 
d¡Zd	Zi Zd
d„ Zdd„ Zdd„ Zdd„ Zd	S )ÚLatexzP
    Convert $ and $$ to <math> and </math> tags for inline and block math.
    Úlatexr)   z(?<!\$)\$(?!\$)(.*?)\$z\$\$(.*?)\$\$z<pre>(.*?)</pre>z```(.*?)```z(?<!`)(`)(.*?)(?<!`)\1(?!`)Nc                 C   s   | j  | d¡¡S rµ   )Ú	converterrE   r¶   r4  r)   r)   r*   Ú_convert_single_match  s   zLatex._convert_single_matchc                 C   s   | j j| d¡ dd¡ddS )Nr]   z\nr²   r[  )Údisplay)rÕ  rE   r¶   r   r4  r)   r)   r*   Ú_convert_double_match“  s   zLatex._convert_double_matchc                 C   s&   dt | jƒ› d}| d¡| j|< |S )Nz<!--CODE_BLOCK_rD  r   )r¹   Úcode_blocksr¶   )r   rº   Úplaceholderr)   r)   r*   Úcode_placeholder–  s   zLatex.code_placeholderc                 C   s    z
dd l }|j| _W n ty   tdƒ‚w | j | j|¡}| j | j|¡}| j | j|¡}| j | j	|¡}| j
 | j|¡}| j ¡ D ]
\}}| ||¡}qC|S )Nr   zFThe "latex" extra requires the "latex2mathml" package to be installed.)Úlatex2mathml.converterrÕ  r  Ú_pre_code_block_rerÇ   rÛ  Ú
_single_reÚ
_triple_reÚ_single_dollar_rerÖ  Ú_double_dollar_rerØ  rÙ  r¬   r   )r   rG   Úlatex2mathmlrÚ  Ú
code_blockr)   r)   r*   rd   ›  s   ÿz	Latex.run)r1   r2   r3   r  ra   rJ   rQ   rÆ  r^   r–   r—   rà  r  rá  rÝ  rß  rÞ  rÕ  rÙ  rÖ  rØ  rÛ  rd   r)   r)   r)   r*   rÓ  ~  s    

rÓ  c                   @   sD   e Zd ZU dZdZejfdfZee	d< e
 d¡Zdd„ Zdd	„ Zd
S )ÚLinkPatternszn
    Auto-link given regex patterns in text (e.g. bug number
    references, revision number references).
    r‡   r)   rŽ  z!?\[.*?\]\(.*?\)c                    s  i }ˆj D ]ò\}}g }| ˆ¡D ]'‰t‡‡‡fdd„|D ƒƒr qt|ƒr)|ˆƒ}nˆ |¡}| ˆ ¡ |f¡ qt|ƒD ]º\\‰‰ }ˆˆd ˆ… dkrWˆˆ ˆ d … dkrWq<ˆˆd ˆ… dkskˆˆ ˆ d … dkrlq<ˆˆd	 ˆ… d
kr—ˆˆ ˆ d	 … d
kr—ˆd ˆd	 … ˆˆˆ …  ˆˆ d	 d …  ‰q<d}ˆjj	ˆj
fD ]}| ˆ¡D ]‰t‡ ‡fdd„ˆjD ƒƒrºd} nq§q  |rÀq<| dd¡ dˆjjd ¡ dˆjjd ¡}	d |	ˆˆˆ … ¡}
t|
ƒ}|
||< ˆd ˆ… | ˆˆ d …  ‰q<qt| ¡ ƒD ]
\}}
ˆ ||
¡‰qþˆS )Nc                 3   s     | ]}ˆj  ˆˆ |¡V  qd S rz   )r\   rs  )r-   Úh)rº   r   rG   r)   r*   r_  Á  s   € z#LinkPatterns.run.<locals>.<genexpr>r]   r½  r¾  r   z](z")r   z"""Fc                 3   s(    | ]}|d  ˆkoˆ |d kV  qdS )r   r]   Nr)   )r-   r?   )r	  r¿   r)   r*   r_  Ü  s   €& Tr‰   ú&quot;r|  r}  rS  )rŽ  rq  ÚanyÚcallableÚexpandr·   r  r]  r\   r‡  Ú_basic_link_reÚregsr   r›   r  r+   rû   r¬   )r   rG   Úlink_from_hashÚregexÚreplrœ  ÚhrefÚis_inside_linkÚlink_reÚescaped_hrefÚlinkrb  r)   )r	  rº   r   r¿   rG   r*   rd   ¼  sR   

(((,ü
üÙ(zLinkPatterns.runc                 C   r›  ©NTr)   rå   r)   r)   r*   rc   õ  ó   zLinkPatterns.testN)r1   r2   r3   r  ra   rJ   rW   r^   r|  rv  r–   r—   rê  rd   rc   r)   r)   r)   r*   rä  ±  s   
 
9rä  c                   @   s0   e Zd ZdZdZdejffZdd„ Zdd„ Z	dS )	ÚMarkdownInHTMLzä
    Allow the use of `markdown="1"` in a block HTML tag to
    have markdown processing be done on its contents. Similar to
    <http://michelf.com/projects/php-markdown/extra/#markdown-attr> but with
    some limitations.
    r-  r)   c                    s"   ‡ fdd„}ˆ j  |ˆ j j|d¡S )Nc                    s4   ˆ j  | ¡\}} ˆ j  | ¡} ˆ j j| |ddd} | S )NTF)rl  rm  )r\   r  r<  ro  )r[  rk  r¢   r)   r*   rU    s   z$MarkdownInHTML.run.<locals>.callbackT)r\   rH  rI  )r   rG   rU  r)   r¢   r*   rd     s   zMarkdownInHTML.runc                 C   r›  rô  r)   rå   r)   r)   r*   rc     rõ  zMarkdownInHTML.testN)
r1   r2   r3   r  ra   rJ   rL   r^   rd   rc   r)   r)   r)   r*   rö  ù  s    	rö  c                       s*   e Zd ZdZefdfZ‡ fdd„Z‡  ZS )ÚMermaidÚmermaidr)   c                    s   |dkrdS t ƒ  |¡S )Nrø  )z.<pre class="mermaid-pre"><div class="mermaid">z</div></pre>)rŸ  rÑ  )r   r	  r¡  r)   r*   rÑ    s   zMermaid.tags)r1   r2   r3   ra   rÆ  r^   rÑ  r¦  r)   r)   r¡  r*   r÷    s    
r÷  c                       sh   e Zd ZdZdZefejffZde	de
eef f‡ fdd„Z‡ fdd„Zd	ejd
ef‡ fdd„Z‡  ZS )ÚMiddleWordEmz¿
    Allows or disallows emphasis syntax in the middle of words,
    defaulting to allow. Disabling this means that `this_text_here` will not be
    converted to `this<em>text</em>here`.
    zmiddle-word-emr\   rŽ  c                    st   t |tƒr	d|i}| dd¡ tƒ  ||¡ | j| _|d s8t d| jj	 | jj
¡| _t dtjtjB ¡| _dS dS )aq  
        Args:
            md: the markdown instance
            options: can be bool for backwards compatibility but will be converted to a dict
                in the constructor. All options are:
                - allowed (bool): whether to allow emphasis in the middle of a word.
                    If `options` is a bool it will be placed under this key.
        ÚallowedTz(?<=\b)%s(?=\b)a¥  
                    (                # \1 - must be a single em char in the middle of a word
                        (?<![*_\s])  # cannot be preceeded by em character or whitespace (must be in middle of word)
                        [*_]         # em character
                        (?![*_])     # cannot be followed by another em char
                    )
                    (?=\S)           # em opening must be followed by non-whitespace text
                    (.*?\S)          # the emphasized text
                    \1               # closing char
                    (?!\s|$)         # must not be followed by whitespace (middle of word) or EOF
                N)rŽ   rz  r“   rŸ  rž   r¤  Úliberal_em_rer–   r—   rÂ  Úflagsró  r`  r  r¡  r)   r*   rž   #  s   
	

ôþzMiddleWordEm.__init__c                    s>   | j d r|S tƒ  |¡}| jj| jjk r| j | j|¡}|S )Nrú  )	rŽ  rŸ  rd   r\   r^   r[   rû  rÇ   r¥  rå   r¡  r)   r*   rd   B  s   
zMiddleWordEm.runrº   r"   c                    s0   |  d¡}t|ƒdkrtƒ  |¡S d|  d¡ S )Nr]   r«  r   )r¶   r¹   rŸ  rÇ   )r   rº   rÅ  r¡  r)   r*   rÇ   O  s   
zMiddleWordEm.sub)r1   r2   r3   r  ra   rÄ  rJ   rX   r^   rD   r   r   rz  rž   rd   r–   r‚  r¸   rÇ   r¦  r)   r)   r¡  r*   rù    s     rù  c                   @   s(   e Zd ZdZdZejfdfZdd„ ZdS )Ú	Numberingz
    Support of generic counters.  Non standard extension to
    allow sequential numbering of figures, tables, equations, exhibits etc.
    Ú	numberingr)   c                 C   s´  t  dt j¡}t  d¡}i }i }g }d}d}| |¡D ]H}	t|	 ¡ ƒdkr&q|	 d¡}
|	 d¡ ¡ }|	 d¡}|	 d¡}| |
d¡}||
f||< | 	|	 
d	¡| |
||||¡|	 d	¡f¡ |d ||
< qt|ƒD ]}|d |d	 … |d  ||d d …  }qhtt| |¡ƒƒD ]O}	| |	 d¡d
¡\}}
|d ur¤| |
|	 d¡|¡}n| |	 d¡dd|	 d¡ d ¡}d| jjv rÅ| d| jjd ¡}|d |	 
¡ … | ||	 ¡ d …  }qˆ|S )Na(  
            \[\#(\w+) # the counter.  Open square plus hash plus a word \1
            ([^@]*)   # Some optional characters, that aren't an @. \2
            @(\w+)       # the id.  Should this be normed? \3
            ([^\]]*)\]   # The rest of the text up to the terminating ] \4
            z\[@(\w+)\s*\]z><figcaption class="{}" id="counter-ref-{}">{}{}{}</figcaption>z+<a class="{}" href="#counter-ref-{}">{}</a>r   r]   r   r   r   rt  Úcountererrorú?rˆ   r‰   )r–   r—   r~  rq  r¹   rf  r¶   rí   r’   r·   r¿   r  r	  r]  rû   r\   r9   r   r›   )r   rG   Úregex_defnsÚ
regex_subsÚcountersÚ
referencesrœ  Údefinition_htmlÚreference_htmlrº   ÚcounterÚtext_beforeÚref_idÚ
text_afterÚnumberrî  r)   r)   r*   rd   `  sZ   û



üú*þþ&zNumbering.runN)	r1   r2   r3   r  ra   rJ   rM   r^   rd   r)   r)   r)   r*   rý  W  s
    rý  c                   @   sD   e Zd ZdZdZdejffZdd„ Zde	j
defdd	„Zd
d„ ZdS )ÚPyShellzS
    Treats unindented Python interactive shell sessions as <code>
    blocks.
    Úpyshellr)   c                 C   rµ  )Nz>>>r)   rå   r)   r)   r*   rc   ¡  r¶  zPyShell.testrº   r"   c                 C   sv   d| j jv rt| d¡ƒ}| j jd  d| d ¡S | d¡ d¡}t|ƒ d| j j }d| d|  	|¡ d }|S )NrŒ  r   z	```pycon
z```
rx   r¯   )
r\   r9   rü   r¶   rb   rd   r  Ú_dedentlinesr7   r,  )r   rº   Údedentedr!  rk  r!   r)   r)   r*   rÇ   ¤  s   ÿÿþzPyShell.subc                 C   ó2   | j jd }t d| tjtjB ¡}| | j|¡S )Nr]   zï
            ^([ ]{0,%d})>>>[ ].*\n  # first line
            ^(\1[^\S\n]*\S.*\n)*    # any number of subsequent lines with at least one character
            (?=^\1?\n|\Z)           # ends with a blank line or end of document
            ©r\   r7   r–   r—   r˜   r`  rÇ   )r   rG   rc  Ú_pyshell_block_rer)   r)   r*   rd   ±  s   ü
üzPyShell.runN)r1   r2   r3   r  ra   rJ   rP   r^   rc   r–   r‚  r¸   rÇ   rd   r)   r)   r)   r*   r  ˜  s    r  c                   @   s|   e Zd ZdZdZdejffZe 	d¡Z
e 	d¡Ze 	d¡Ze 	d¡Ze 	d¡Zg d	¢Zd
edefdd„Zdd„ Zdd„ ZdS )ÚSmartyPantszž
    Replaces ' and " with curly quotation marks or curly
    apostrophes.  Replaces --, ---, ..., and . . . with en dashes, em dashes,
    and ellipses.
    rˆ   r)   z(?<!\S)'(?=\S)z(?<!\S)"(?=\S)z(?<=\S)'z(?<=\S)"(?=(\s|,|;|\.|\?|!|$))z'(\d\d)(?=(\s|,|;|\.|\?|!|$)))ÚtisÚtwasÚtwerÚneathÚorÕ  ÚroundÚboutÚtwixtÚnuffÚfraidÚsuprG   r"   c                 C   sN   | j  d|¡}| jD ]}| d| d| ¡}| d| ¡  d| ¡  ¡}q
|S )Nz	&#8217;\1z'%sz	&#8217;%s)Ú_apostrophe_year_rerÇ   Ú_contractionsr   Ú
capitalize)r   rG   r.  r)   r)   r*   ÚcontractionsÒ  s   

ÿzSmartyPants.contractionsc                 C   s²   d|v r|   |¡}| j d|¡}| j d|¡}d|v r)| j d|¡}| j d|¡}| dd¡}| d	d
¡}| dd¡}| dd¡}| dd¡}d| jjv rWd|v rW| dd¡}|S )aÑ  Fancifies 'single quotes', "double quotes", and apostrophes.
        Converts --, ---, and ... into en dashes, em dashes, and ellipses.

        Inspiration is: <http://daringfireball.net/projects/smartypants/>
        See "test/tm-cases/smarty_pants.text" for a full discussion of the
        support here and
        <http://code.google.com/p/python-markdown2/issues/detail?id=42> for a
        discussion of some diversion from the original SmartyPants.
        rŠ   z&#8216;z&#8217;r‰   z&#8220;z&#8221;rè   z&#8212;ú--z&#8211;ú...z&#8230;z . . . ú. . .r¤   zfootnote-refzclass="footnote-ref&#8221;zclass="footnote-ref")	r"  Ú_opening_single_quote_rerÇ   Ú_closing_single_quote_reÚ_opening_double_quote_reÚ_closing_double_quote_rer   r\   r9   rå   r)   r)   r*   rd   Ú  s   

zSmartyPants.runc                    s   t ‡ fdd„dD ƒƒS )Nc                 3   s    | ]}|ˆ v V  qd S rz   r)   r^  rÁ   r)   r*   r_  ü  s   € z#SmartyPants.test.<locals>.<genexpr>)rŠ   r‰   r#  r$  r%  )rç  rå   r)   rÁ   r*   rc   û  s   zSmartyPants.testN)r1   r2   r3   r  ra   rJ   rT   r^   r–   r—   r&  r(  r'  r)  r  r   r¸   r"  rd   rc   r)   r)   r)   r*   r  ¼  s    




!r  c                   @   ó>   e Zd ZdZdZejfdfZe 	dej
¡Zdd„ Zdd„ Zd	S )
ÚStrikez:
    Text inside of double tilde is ~~strikethrough~~
    Ústriker)   z~~(?=\S)(.+?)(?<=\S)~~c                 C   rc  )Nz	<s>\1</s>)Ú
_strike_rerÇ   rå   r)   r)   r*   rd     ó   z
Strike.runc                 C   rµ  )Nz~~r)   rå   r)   r)   r*   rc     r¶  zStrike.testN)r1   r2   r3   r  ra   rJ   rX   r^   r–   r—   ró  r-  rd   rc   r)   r)   r)   r*   r+    ó    r+  c                   @   s<   e Zd ZdZdZdejffZdd„ Zde	j
defdd	„Zd
S )ÚTableszÈ
    Tables using the same format as GFM
    <https://help.github.com/articles/github-flavored-markdown#tables> and
    PHP-Markdown Extra <https://michelf.ca/projects/php-markdown/extra/#table>.
    Útablesr)   c                 C   s8   | j jd }t d|||f tjtjB ¡}| | j|¡S )z Copying PHP-Markdown and GFM table syntax. Some regex borrowed from
        https://github.com/michelf/php-markdown/blob/lib/Michelf/Markdown.php#L2538
        r]   a¤  
                (?:(?<=\n)|\A\n?)             # leading blank line

                ^[ ]{0,%d}                      # allowed whitespace
                (.*[|].*)[ ]*\n                   # $1: header row (at least one pipe)

                ^[ ]{0,%d}                      # allowed whitespace
                (                               # $2: underline row
                    # underline row with leading bar
                    (?:  \|\ *:?-+:?\ *  )+  \|? \s?[ ]*\n
                    |
                    # or, underline row without leading bar
                    (?:  \ *:?-+:?\ *\|  )+  (?:  \ *:?-+:?\ *  )? \s?[ ]*\n
                )

                (                               # $3: data rows
                    (?:
                        ^[ ]{0,%d}(?!\ )         # ensure line begins with 0 to less_than_tab spaces
                        .*\|.*[ ]*\n
                    )*
                )
            r  )r   rG   rc  Útable_rer)   r)   r*   rd     s   ë
ëz
Tables.runrº   r"   c                    s  d}d}d}d‰ |  ¡ \}}}‡ fdd„t |t |dt |d|¡¡¡D ƒ}i }	t|ƒD ]*\}
}|d d	krA|d
 d	krAd|	|
< q,|d d	krLd|	|
< q,|d
 d	krVd|	|
< q,d| j d¡ d| j d¡ dg}‡ fdd„t |t |dt |d|¡¡¡D ƒ}t|ƒD ]\}
}| d |	 	|
d¡| j 
|¡¡¡ qƒ| d¡ | d¡ | d¡}|r÷| d¡ | d¡D ]=}| d¡ ‡ fdd„t |t |dt |d|¡¡¡D ƒ}t|ƒD ]\}
}| d |	 	|
d¡| j 
|¡¡¡ qÖ| d¡ q´| d¡ | d¡ d |¡d S )Nz^[ 	
]+|[ 	
]+$z^\||\|$z^\||(?<![\`\\])\|z\\\|c                    ó   g | ]}t  ˆ d | ¡ ¡‘qS ©ú|©r–   rÇ   rí   ©r-   Úcell©Úescape_bar_rer)   r*   r  D  ó    zTables.sub.<locals>.<listcomp>r²   r   r÷   r  z style="text-align:center;"z style="text-align:left;"z style="text-align:right;"ú	<table%s>Útableú	<thead%s>Útheadú<tr>c                    r3  r4  r6  r7  r9  r)   r*   r  P  r;  z  <th{}>{}</th>ú</tr>ú</thead>r¯   ú<tbody>c                    r3  r4  r6  r7  r9  r)   r*   r  _  r;  z  <td{}>{}</td>ú</tbody>ú</table>)rf  r–   rÊ   rÇ   r  r\   rÇ  r·   r  r’   r>  rí   r,  )r   rº   Útrim_space_reÚtrim_bar_reÚsplit_bar_rer  Ú	underliner¯  ÚcolsÚalign_from_col_idxÚcol_idxÚcolÚhlinesr#  r)   r9  r*   rÇ   ;  sN   .

€".

þ




.

þ

z
Tables.subN)r1   r2   r3   r  ra   rJ   rP   r^   rd   r–   r‚  r¸   rÇ   r)   r)   r)   r*   r0    s    r0  c                   @   s:   e Zd ZdZdejffZe dej	¡Z
dd„ Zdd„ ZdS )	ÚTelegramSpoilerz
tg-spoilerr)   z\|\|\s?(.+?)\s?\|\|c                 C   rc  )Nz<tg-spoiler>\1</tg-spoiler>)Ú_tg_spoiler_rerÇ   rå   r)   r)   r*   rd   r  r.  zTelegramSpoiler.runc                 C   rµ  ©Nz||r)   rå   r)   r)   r*   rc   u  r¶  zTelegramSpoiler.testN)r1   r2   r3   ra   rJ   rX   r^   r–   r—   ró  rP  rd   rc   r)   r)   r)   r*   rO  l  s    rO  c                   @   r*  )
Ú	Underlinez7
    Text inside of double dash is --underlined--.
    rI  r)   z.(?<!<!)--(?!>)(?=\S)(.+?)(?<=\S)(?<!<!)--(?!>)c                 C   rc  )Nz	<u>\1</u>)Ú_underline_rerÇ   rå   r)   r)   r*   rd   ‚  r.  zUnderline.runc                 C   rµ  )Nr#  r)   rå   r)   r)   r*   rc   …  r¶  zUnderline.testN)r1   r2   r3   r  ra   rJ   rX   r^   r–   r—   ró  rS  rd   rc   r)   r)   r)   r*   rR  y  r/  rR  c                   @   s   e Zd ZU dZeed< dS )Ú_WavedromExtraOptsz Options for the `Wavedrom` extraÚprefer_embed_svgNr¿  r)   r)   r)   r*   rT  ‰  s   
 rT  c                   @   sP   e Zd ZU dZdZejefdfZe	e
d< dd„ Zdejdefd	d
„Zdd„ ZdS )ÚWavedromzA
    Support for generating Wavedrom digital timing diagrams
    Úwavedromr)   rŽ  c                 C   s"   t j |¡}|d u p| d¡dkS )Nr   rW  )rÆ  rÒ  r¾   r¶   )r   rG   rº   r)   r)   r*   rc   œ  s   zWavedrom.testrº   r"   c                 C   sš   | j  | d¡¡\}}d\}}| j dd¡}|r3zdd l}| |¡ ¡ }d\}}W n	 ty2   Y nw t	|ƒ| j j
|< | j jd || j j
| |¡|ddS )	Nr   )z<script type="WaveDrom">
z	</script>rU  Tr   )z<div>r¹  z
{}{}{}
)rl  )r\   r  r¶   rŽ  r’   rW  ÚrenderÚtostringr  r+   r›   ro  r  )r   rº   r­  ÚwavesÚopen_tagÚ	close_tagÚ	embed_svgrW  r)   r)   r*   rÇ      s    ÿþzWavedrom.subc                 C   s   t j | j|¡S rz   )rÆ  rÒ  rÇ   rå   r)   r)   r*   rd   ¹  rQ  zWavedrom.runN)r1   r2   r3   r  ra   rJ   rQ   rÆ  r^   rT  rv  rc   r–   r‚  r¸   rÇ   rd   r)   r)   r)   r*   rV  ”  s   
 rV  c                   @   sB   e Zd ZdZdZefdfZdd„ Zdej	de
fdd	„Zd
d„ ZdS )Ú
WikiTableszk
    Google Code Wiki-style tables. See
    <http://code.google.com/p/support/wiki/WikiSyntax#Tables>.
    zwiki-tablesr)   c                 C   r  )Nr]   zÍ
            (?:(?<=\n\n)|\A\n?)            # leading blank line
            ^([ ]{0,%d})\|\|.+?\|\|[ ]*\n  # first line
            (^\1\|\|.+?\|\|\n)*        # any number of subsequent lines
            r  )r   rG   rc  Úwiki_table_rer)   r)   r*   rd   Å  s   ü
üzWikiTables.runrº   r"   c                    s|  |  d¡ ¡ }g }| d¡D ]}| ¡ dd…  ¡ }dd„ t d|¡D ƒ}| |¡ qg ‰d‡‡fdd„	}‡ ‡fd	d
„}|dˆj d¡ ƒ |rˆ|d rˆt d|d d ¡rˆ|dˆj d¡ dƒ |ddƒ |d D ]‰ |d|ˆ ƒ› ddƒ qj|ddƒ |ddƒ |dd … }|r³|ddƒ |D ]}|ddƒ |D ]‰ |d|ˆ ƒ› ddƒ qš|ddƒ q‘|ddƒ |dƒ d 	ˆ¡d S )Nr   r   éþÿÿÿc                 S   s   g | ]}|  ¡ ‘qS r)   rì   )r-   r.  r)   r)   r*   r  Ó  rI  z"WikiTables.sub.<locals>.<listcomp>z(?<!\\)\|\|c                    s   ˆ   ˆjj| |  ¡ d S rz   )r·   r\   rŒ   )r#  Úindents)rN  r   r)   r*   Ú	add_hlineØ  s   z!WikiTables.sub.<locals>.add_hlinec                    s   ˆj  t ddˆ ¡ d¡¡S )Nú^\s*~r²   rx   )r\   r>  r–   rÇ   rí   rÁ   )r8  r   r)   r*   Úformat_cellÛ  s   z#WikiTables.sub.<locals>.format_cellr<  r=  rc  r>  r?  r]   r@  z<th>z</th>r   rA  rB  rC  z<td>z</td>rD  rE  r¯   )r   )
r¶   rí   r  r–   rÊ   r·   r\   rÇ  rº   r,  )r   rº   ÚttextÚrowsr#  Úrowrb  rd  r)   )r8  rN  r   r*   rÇ   Î  s8    





zWikiTables.subc                 C   rµ  rQ  r)   rå   r)   r)   r*   rc   õ  r¶  zWikiTables.testN)r1   r2   r3   r  ra   r0  r^   rd   r–   r‚  r¸   rÇ   rc   r)   r)   r)   r*   r^  ½  s    
	'r^  r~   c                    s8  | du rdS ‡ fdd„}g }dg‰ | D ]Z\}}}|ˆ d kr,|  d|ƒ  ¡ ˆ   |¡ n6|ˆ d kr;|d  d7  < n'|ˆ d k rbˆ  ¡  |d  d¡sT|d  d7  < |  d|ƒ  ¡ |ˆ d k sA|  d	 |ƒ ||¡¡ qtˆ ƒd
kr•ˆ  ¡  |d  d¡s‡|d  d7  < |  d|ƒ  ¡ tˆ ƒd
kstd |¡d S )zsReturn the HTML for the current TOC.

    This expects the `_toc` attribute to have been set on this instance.
    Nc                      s   dt ˆ ƒd  S )Nr7  r]   r£  r)   ©Úh_stackr)   r*   rk    rQ  z"calculate_toc_html.<locals>.indentr   r  z%s<ul>rJ  z%s</ul></li>z{}<li><a href="#{}">{}</a>r]   z%s</ul>r¯   )r·   Úpopr  r  r¹   r,  )r~   rk  r!  rÜ  rh  ra   r)   rh  r*   rÛ     s6   üÿürÛ   c                   @   s:   e Zd ZU dZdZeeeef  ed< dZ	ee ed< dS )r>   zªA subclass of unicode used for the return value of conversion to
    possibly attach some attributes. E.g. the "toc_html" attribute when
    the "toc" extra is used.
    Nr¦   rÝ   )
r1   r2   r3   r  r¦   r   r   r¸   rv  rÝ   r)   r)   r)   r*   r>   7  s   
 z[^\w\s-]z[-\s]+rê   c                 C   s@   ddl }| d| ¡ dd¡ ¡ } t d| ¡ ¡  ¡ } t d| ¡S )z­
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.

    From Django's "django/template/defaultfilters.py".
    r   NÚNFKDr#   Úignorer²   rë   )	ÚunicodedataÚ	normalizer&   ÚdecodeÚ_slugify_strip_rerÇ   rí   r  Ú_slugify_hyphenate_re)rê   rm  r)   r)   r*   rÖ  B  s   rÖ  Úfunctionc                    s   ‡ ‡‡fdd„}|S )Nc                     s$   ˆ  ¡ }| |¡ ˆˆ |  i |¤ŽS rz   )r”   r   )ÚrestÚkwrestÚcombined©re   rr  rf   r)   r*   r\  R  s   
z_curry.<locals>.resultr)   )rr  re   rf   r\  r)   rv  r*   rG  Q  s   rG  c                 C   sÊ   |   d¡r]|  d¡dkr]|  d¡}| d|… | |d d… }}tjtjtjtjtjdœ}d}|D ]!}z||| O }W q1 tyR   t	d|| d 
t| ¡ ƒ¡f ƒ‚w t | d|… |¡S t t | ¡¡S )z}'foo'    -> re.compile(re.escape('foo'))
       '/foo/'  -> re.compile('foo')
       '/foo/i' -> re.compile('foo', re.I)
    ú/r   r]   N)r"  r¤  r!   r6  Úuz:unsupported regex flag: '%s' in '%s' (must be one of '%s')r²   )rð   Úrfindr–   r}  ÚLOCALEr  r  r€  ÚKeyErrorrË   r,  rû   rG  r—   r   )r!   r¬  r}  Ú	flags_strÚflag_from_charrü  Úcharr)   r)   r*   Ú_regex_from_encoded_patternZ  s(   
ûþÿr  é   r!  Útabsizerk  c                 C   sü  d}|rt d||f ƒ d}t| ƒD ]E\}}|dkr|rqd}|D ]}|dkr,|d7 }q!|dkr9||||  7 }q!|dv r>q! nq|rKt d	||f ƒ |du rR|}qt||ƒ}q|r`t d
| ƒ |durü|dkrüt| ƒD ]\}}|dkrw|rwqld}	t|ƒD ]q\}
}|dkrŠ|	d7 }	n.|dkr—|	||	|  7 }	n!|dv r¯|r£t d| ƒ | | |
d… | |<  nLtd|||f ƒ‚|rÄt d|||	|f ƒ |	|krÖ| | |
d d… | |<  n%|	|krîd|	|  | | |
d d…  | |<  nq}|	rû| | |	d… | |< ql| S )a  _dedentlines(lines, tabsize=8, skip_first_line=False) -> dedented lines

        "lines" is a list of lines to dedent.
        "tabsize" is the tab width to use for indent width calculations.
        "skip_first_line" is a boolean indicating if the first line should
            be skipped for calculating the indent width and for dedenting.
            This is sometimes useful for docstrings and similar.

    Same as dedent() except operates on a sequence of lines. Note: the
    lines list is modified **in-place**.
    Fz3dedent: dedent(..., tabsize=%d, skip_first_line=%r)Nr   rx   r]   r'  r®   zdedent: indent=%d: %rzdedent: margin=%rz"dedent: %r: EOL -> strip up to EOLzKunexpected non-whitespace char %r in line %r while removing %d-space marginzdedent: %r: %r -> removed %d/%d)Úprintr  rÄ  rË   )r!  r  rk  ÚDEBUGÚmarginr"  r#  rk  r.   ÚremovedÚjr)   r)   r*   r  x  sv   ÿ

þ
ÿ$þ€r  c                 C   s"   |   d¡}t|||d d |¡S )aÅ  _dedent(text, tabsize=8, skip_first_line=False) -> dedented text

        "text" is the text to dedent.
        "tabsize" is the tab width to use for indent width calculations.
        "skip_first_line" is a boolean indicating if the first line should
            be skipped for calculating the indent width and for dedenting.
            This is sometimes useful for docstrings and similar.

    textwrap.dedent(s), but don't expand tabs to spaces
    T)r  rk  r²   )r  r  r,  )rG   r  rk  r!  r)   r)   r*   rü   Ã  s   

rü   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	_memoizedzéDecorator that caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned, and
    not re-evaluated.

    http://wiki.python.org/moin/PythonDecoratorLibrary
    c                 C   s   || _ i | _d S rz   )ri   Úcache)r   ri   r)   r)   r*   rž   Ú  s   
z_memoized.__init__c                 G   sR   z| j | W S  ty   | j|Ž  | j |< }| Y S  ty(   | j|Ž  Y S w rz   )rˆ  r{  ri   r+  )r   re   rê   r)   r)   r*   Ú__call__Þ  s   ýz_memoized.__call__c                 C   s   | j jS )z Return the function's docstring.)ri   r  r¢   r)   r)   r*   Ú__repr__é  s   z_memoized.__repr__N)r1   r2   r3   r  rž   r‰  rŠ  r)   r)   r)   r*   r‡  Ó  s
    r‡  c                 C   ó   t  d| d  t j¡S )z,Standalone XML processing instruction regex.a  
        (?:
            (?<=\n\n)       # Starting after a blank line
            |               # or
            \A\n?           # the beginning of the doc
        )
        (                           # save in $1
            [ ]{0,%d}
            (?:
                <\?\w+\b\s+.*?\?>   # XML processing instruction
                |
                <\w+:\w+\b\s+.*?/>  # namespaced single tag
            )
            [ \t]*
            (?=\n{2,}|\Z)       # followed by a blank line or end of document
        )
        r]   ©r–   r—   r`  ©r7   r)   r)   r*   rO  î  s
   ððrO  c                 C   r‹  )Na  
        (?:
            (?<=\n\n)       # Starting after a blank line
            |               # or
            \A\n?           # the beginning of the doc
        )
        (                       # save in \1
            [ ]{0,%d}
            <(hr)               # start tag = \2
            \b                  # word break
            ([^<>])*?           #
            /?>                 # the matching end tag
            [ \t]*
            (?=\n{2,}|\Z)       # followed by a blank line or end of document
        )
        r]   rŒ  r  r)   r)   r*   rL    s
   ññrL  TÚattrÚskip_single_quotec                 C   s<   t  d| ¡}|  dd¡ dd¡ dd¡}|s| dd	¡}|S )
zÉEscape the given string for use in an HTML/XML tag attribute.

    By default this doesn't bother with escaping `'` to `&#39;`, presuming that
    the tag attribute is surrounded by double quotes.
    rš  r‰   ræ  r=  r~  rw   r›  rŠ   ú&#39;)rM  rÇ   r   )rŽ  r  r‚  r)   r)   r*   rÆ    s   ýrÆ  r.   c                 C   sF   t ƒ }|dkr| dvr| S |dk rdtt| ƒƒdd …  S dt| ƒ S )NgÍÌÌÌÌÌì?z@_gÍÌÌÌÌÌÜ?z&#%s;r]   )r   ÚhexÚord)r.   r?   r)   r)   r*   rY  )  s   rY  r±  c                 C   sD   |   dd¡  dd¡  dd¡}|r |dkr|  dd	¡}|  d
d¡}|S )zä
    Replace special characters that are potentially malicious in url string.

    Args:
        charset: don't escape characters from this charset. Currently the only
            exception is for '+' when charset=='base64'
    r‰   ræ  r=  r~  rw   r›  r°  rÞ  rx   rŠ   r  )r   )rŽ  r8   r±  r‚  r)   r)   r*   r³  7  s   ýr³  c                   @   s   e Zd ZdZdd„ ZdS )Ú_NoReflowFormatterz;An argparse formatter that does NOT reflow the description.c                 C   s   |pdS re  r)   )r   Údescriptionr)   r)   r*   Úformat_descriptionR  r¶  z%_NoReflowFormatter.format_descriptionN)r1   r2   r3   r  r•  r)   r)   r)   r*   r“  P  s    r“  c                  C   s   dd l } |  ¡  d S rŸ   )ÚdoctestÚtestmod)r–  r)   r)   r*   Ú_testV  s   r˜  c           !   
   C   s~  | d u rt j} tjjst ¡  tjdtdt	d}|j
dddt› d |j
dd	d
d |j
ddddtjdd |j
ddd |j
ddddd |j
dddddd |j
d d!d"d#d$ |j
d%d&d |j
d'd(d |j
d)dd*d$ |j
d+dd,d$ |j
d-td.d/ |jtjdd0d dd1 | ¡ }|j}t |j¡ |jržtƒ S |jrÜi }|jD ]4}t d2¡}| |¡D ]'}d3|v rÐ| d3d4¡\}}	zt|	ƒ}	W n tyÏ   Y nw |d }}	|	||< q²q¦nd }|jr8g }
t|jƒ}zIt|  ¡ ƒD ]<\}}| !¡ sùqð| "¡  #d5¡rqðz| $¡  %d d4¡\}}W n ty"   t&d6|j|d4 |f ƒ‚w |
 't(|ƒ|f¡ qðW | )¡  n| )¡  w d }
d7d8l*m+}m,}m-}m.} ||||t/ƒƒƒd9d:ƒ}|sXd;g}|D ]â}|d;krgt j0 1¡ }nt2 |d<|j3¡}| 1¡ }| )¡  |j4r²d7d=l5m6}m7} t8d>ƒ |d?| d@||d@dA}|j0 9| :d0¡¡ |j0 )¡  |j; 1¡  <d0¡}t j; 9|¡ t8dBƒ t=||j>|j?||
|j@d@dC}|jArát|jAdDƒ}| 9|¡ W d   ƒ n	1 sÛw   Y  nt j; 9|¡ |rdE|v rt BdFt|jC :t j;j3püd0dG¡ƒ ¡ |j4r;||||t/ƒƒƒd9ƒ}|||dHƒƒr/t jD Ed7|¡ d7dIlFmG} ||ƒ}||ƒ} n|}|} t8dJ| |k ƒ qZd S )KNÚ	markdown2z%(prog)s [PATHS...])Úprogr”  ÚusageÚformatter_classz	--versionÚversionz	%(prog)s )Úactionr  Úpathsr|  zGoptional list of files to convert.If none are given, stdin will be used)ÚnargsÚhelpz-vz	--verboseÚ	log_levelÚstore_constzmore verbose output)Údestrž  Úconstr¡  z
--encodingz specify encoding of text content)r¡  z--html4tagsÚ
store_trueFz'use HTML 4 style for empty element tags)rž  Údefaultr¡  z-sz--safeÚMODEr8   zgsanitize literal HTML: 'escape' escapes HTML meta chars, 'replace' replaces with an [HTML_REMOVED] note)Úmetavarr¤  r¡  z-xz--extrasr·   zPTurn on specific extra features (not part of the core Markdown spec). See above.)rž  r¡  z--use-file-varsz‡Look for and use Emacs-style 'markdown-extras' file var to turn on extras. See <https://github.com/trentm/python-markdown2/wiki/Extras>z--link-patterns-filezpath to a link pattern filez--self-testz'run internal self-tests (some doctests)z	--comparez-run against Markdown.pl as well (for testing)z--outputz"output to a file instead of stdout)rœ  r¡  r#   )r¢  Úcomparer5   r8   r=   z[,;: ]+r­   r]   ú#z$%s:%d: invalid link pattern line: %rr   )ÚabspathÚdirnameÚexistsr,  rc   zMarkdown.plrë   r?   )ÚPIPEÚPopenz==== Markdown.pl ====zperl %sT)ÚshellÚstdinÚstdoutÚ	close_fdsz==== markdown2.py ====)r6   r8   r9   r:   r=   rI   Úwr~   z
toc_html: Úxmlcharrefreplaceztest_markdown2.py)Únorm_html_from_htmlz==== match? %r ====)HÚsysÚargvÚloggingÚrootÚhandlersÚbasicConfigÚargparseÚArgumentParserÚ
cmdln_descr“  Úadd_argumentÚ__version__rƒ  r¸   Úset_defaultsÚINFOÚ
parse_argsrŸ  r  ÚsetLevelr¢  Ú	self_testr˜  r9   r–   r—   rÊ   rª   rË   Úlink_patterns_filerA   r  Ú	readlinesrí   rï   rð   r  Úrsplitr0   r·   r  rC   Úos.pathr¬  r­  r®  r,  Ú__file__r²  rB   r@   r5   rª  Ú
subprocessr¯  r°  r‚  Úwriter&   r³  ro  r   r6   r8   r=   r+  r  rÝ   r4   r—  Útest_markdown2r·  )!r¹  ÚparserÚoptsrŸ  r9   r!   rß   r|   rà   rá   r:   Úfr"  r#  Úpatrï  r¬  r­  r®  r,  Úmarkdown_plr4   rG   rF   r¯  r°  rÍ  Ú	perl_htmlr4  Útest_dirr·  Ú	norm_htmlÚnorm_perl_htmlr)   r)   r*   Úmain[  s  þÿþ
þÿ
ÿÿ
ÿÿÿÿÿ
ÿ

ÿ

÷þ
ÿÿ
ÿöÿ

ûÿ€ÿ
€ØrÙ  Ú__main__)r€  F)T)FNrz   )tr  rÀ  Ú__version_info__r,  rÚ  r¸   rÂ  Ú
__author__r¾  r@   rº  r–   r¸  Úcollectionsr   r   Úabcr   r	   rk   Úcollections.abcr
   Úhashlibr   r   Útypingr   r   r   r   r   r   r   r   r   r   r   Úenumr   r   Úosr   rw  r   ru  rû   r{  r\  ÚPatternr‚  r|  rƒ  Ú	getLoggerr  ry  r%   r+   rš   r—   rM  Ú	Exceptionr0   rz  rª   rH   r   rJ   ro   rD   r‹  r_   rž  r§  r³  r½  rÁ  rÄ  rÆ  rÓ  rä  rö  r÷  rù  rý  r  r  r+  r0  rO  rR  rT  rV  r^  rš  rÛ   r>   rp  rq  rÖ  rG  r  r  rü   r‡  rO  rL  rÆ  rY  r³  ÚRawDescriptionHelpFormatterr“  r˜  rÙ  r1   Úexitr¹  r)   r)   r)   r*   Ú<module>   s‚  J0&
ÿ
öÿþýüûúùø	÷
ö
õöÿþýüûúùø	÷
ö
õ&                 bX46#l3H
=A$IW)=,	"
	
	$Kýÿþ
ý
 

ÿ