
    ei                     X   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z ddlZddlZdd	lmZ d
ZdZdZ ej&                  d      Z ej&                  d      Z ej&                  d      Z ej&                  d      ZdZ ej&                  dj3                  ej4                  d   ej4                  dd dz  ej4                  d               Z G d de      Zd Z G d d      Zd Z d Z!d Z"d Z#d*dZ$d+dZ%d*d Z& G d! d"e      Z' G d# d$e      Z( G d% d&e(      Z) G d' d(e      Z*d) Z+y),a9  
SuperFences.

pymdownx.superfences
Nested Fenced Code Blocks

This is a modification of the original Fenced Code Extension.
Algorithm has been rewritten to allow for fenced blocks in blockquotes,
lists, etc.  And also , allow for special UML fences like 'flow' for flowcharts
and `sequence` for sequence diagrams.

Modified: 2014 - 2017 Isaac Muse <isaacmuse@gmail.com>
---

Fenced Code Extension for Python Markdown
=========================================

This extension adds Fenced Code Blocks to Python-Markdown.

See <https://pythonhosted.org/Markdown/extensions/fenced_code_blocks.html>
for documentation.

Original code Copyright 2007-2008 [Waylan Limberg](https://github.com/waylan).


All changes Copyright 2008-2014 The Python Markdown Project

License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
    )	Extension)Preprocessor)CodeBlockProcessor)	get_attrs)utilN   )QuotesExtension)> 	a  (?x)
    (?P<fence>~{3,}|`{3,})
    (?:[ \t]*\.?(?P<lang>[\w#.+-]+)(?=[\t ]|$))?                                           # Language
    (?:
        [ \t]*(\{(?P<attrs>[^\n]*)\}) |                                                    # Optional attributes or
        (?P<options>
            (?:
                (?:[ \t]*[a-zA-Z][a-zA-Z0-9_]*(?:=(?P<quot>"|').*?(?P=quot))?)(?=[\t ]|$)  # Options
            )+
        ) |
        (?P<unrecognized>
            (?:([ \t]*[^\s]+)(?=[\t ]|$))+
        )
    )?[ \t]*$
    z3^(?P<hl_lines>\d+(?:-\d+)?(?:[ \t]+\d+(?:-\d+)?)*)$zS(?P<linestart>[\d]+)(?:[ \t]+(?P<linestep>[\d]+))?(?:[ \t]+(?P<linespecial>[\d]+))?zh(?x)
    (?:
        (?P<key>[a-zA-Z][a-zA-Z0-9_]*)(?:=(?P<quot>"|')(?P<value>.*?)(?P=quot))?
    )
    z	%s[ \t]*$z^([\> ]*){}({}){}$z([0-9]+)c                       e Zd ZdZy)SuperFencesExceptionz=Special exception to ensure one is raised when a fence fails.N)__name__
__module____qualname____doc__     V/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/pymdownx/superfences.pyr   r   S   s    Gr   r   c                 r    | j                  dd      } | j                  dd      } | j                  dd      } | S )zBasic html escaping.&z&amp;<z&lt;r   z&gt;)replace)txts    r   _escaper   W   s9     ++c7
#C
++c6
"C
++c6
"CJr   c                   6    e Zd ZdZd Zd Zd	dZd Zd Zd Z	y)
	CodeStashz
    Stash code for later retrieval.

    Store original fenced code here in case we were
    too greedy and need to restore in an indented code
    block.
    c                     i | _         y)Initialize.Nstashselfs    r   __init__zCodeStash.__init__i        
r   c                 ,    t        | j                        S )zLength of stash.)lenr$   r%   s    r   __len__zCodeStash.__len__n   s     4::r   Nc                 >    | j                   j                  ||      }|S )zGet the code from the key.)r$   get)r&   keydefaultcodes       r   r-   zCodeStash.gets   s     zz~~c7+r   c                     | j                   |= y)zRemove the stashed code.Nr#   )r&   r.   s     r   removezCodeStash.removey   s     JJsOr   c                 &    ||f| j                   |<   y)zStore the code in the stash.Nr#   )r&   r.   r0   indent_levels       r   storezCodeStash.store~   s      .

3r   c                     i | _         yzClear the stash.Nr#   r%   s    r   clear_stashzCodeStash.clear_stash   r(   r   N)
r   r   r   r   r'   r+   r-   r2   r5   r8   r   r   r   r    r    `   s%    


/
r   r    c                 8   |d   }|d   }|d   }|r|j                  d|       |rd| dnd}|r dj                  d	j                  |            nd}|r)d	d	j                  d
 |j                         D              z   nd}dj                  |||t	        |             S )zFormat source as code blocks.classesid_valueattrsr    id=""  class="{}"r   c              3   2   K   | ]  \  }}| d | d  ywz="r?   Nr   .0kvs      r   	<genexpr>z$fence_code_format.<locals>.<genexpr>         BDAqaS1#Q<B   z <pre{}{}{}><code>{}</code></pre>insertformatjoinitemsr   	sourcelanguage
class_nameoptionsmdkwargsr;   r<   r=   s	            r   fence_code_formatrW      s     YGj!H7OEq*%&.xj"BH9@m""388G#45bGFKC#((BEKKMBBBQSE-44XwwW]__r   c                 8   |d   }|d   }|d   }|r|j                  d|       |rd| dnd}|r dj                  d	j                  |            nd}|r)d	d	j                  d
 |j                         D              z   nd}dj                  |||t	        |             S )zFormat source as div.r;   r<   r=   r   r>   r?   r@   rA   r   c              3   2   K   | ]  \  }}| d | d  ywrC   r   rD   s      r   rH   z#fence_div_format.<locals>.<genexpr>   rI   rJ   z<div{}{}{}>{}</div>rK   rP   s	            r   fence_div_formatrZ      s     YGj!H7OEq*%&.xj"BH9@m""388G#45bGFKC#((BEKKMBBBQSE '''5'&/RRr   c                 ,   |j                   d   j                  }|j                         D ]h  \  }}d}|rW|j                  d      r|||<   !dt        fdt
        fdfD ],  \  }	}
||	k(  s|dus|
|
j                  |      %|||<   d} n |rd|||<   j y)zHighlight validator.fenced_code_blockFzdata-hl_lineslinenums)titleNT)preprocessorsuse_pygmentsrO   
startswithRE_HL_LINESRE_LINENUMSmatch)rR   inputsrT   r=   rU   ra   rF   rG   matchedopt	validators              r   highlight_validatorrj      s     ##$78EEL 1||G$a$.#<z;>WYh"i Y8})*;yq?Q?]%&
"& E!H r   c                 >    |j                         D ]
  \  }}|||<    y)zDefault validator.T)rO   )rR   rf   rT   r=   rU   rF   rG   s          r   default_validatorrl      s)      1ar   c                 T    |j                   d   j                           || ||||      S )zValidator wrapper.r\   )r`   get_hl_settings)rR   rf   rT   r=   rU   ri   s         r   
_validatorro      s/     ()99;Xvwr::r   c                      || ||||fi |S )zFormatter wrapper.r   )srcrR   rT   rU   rS   _fmtrV   s          r   
_formatterrs      s     Xz7BA&AAr   c                 &    |du xs |dk(  xs | |k(  S )zTest language.N*r   )rR   test_languages     r   _testrw      s#     D UMS$8UH<UUr   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )SuperFencesCodeExtensionz!SuperFences code block extension.c                 `    g | _         ddgg dgddgddgddgd| _        t        |   |i | y	)
r"   Fz-Disable indented code blocks - Default: Falsez2Specify custom fences. Default: See documentation.r@   zwSet class name for wrapper element. The default of CodeHilite or Highlight will be usedif nothing is set. - Default: ''z(Preserve tabs in fences - Default: Falsez,Relaxed fenced code headers - Default: False)disable_indented_code_blockscustom_fences	css_classpreserve_tabsrelaxed_headersN)superfencesconfigsuperr'   )r&   argsrV   	__class__s      r   r'   z!SuperFencesCodeExtension.__init__   s]     -24c,d "VW $%OP %'UV
 	$)&)r   c                     |t        j                  t        |      ||d}|dk(  r|| j                  d<   y| j                  j	                  |       y)z@Extend SuperFences with the given name, language, and formatter.)rv   nametest	formatterri   ru   r   N)	functoolspartialrw   r   append)r&   r   r   ri   objs        r   extend_super_fencesz,SuperFencesCodeExtension.extend_super_fences   sQ     %%e4@""	
 3;"%DQ##C(r   c           
      |   |j                  |        | j                         }| j                  j                  ddt        dt        j                  t        t              d       |j                  dg       }|D ]  }|j                  d      }|j                  d      }|j                  d	t              }|j                  d
t              }|T|W| j                  |t        j                  t        ||      t        j                  t        |              || _        | j                          t!               | _        y)z7Add fenced block preprocessor to the Markdown instance.r   r   Nri   r   r|   r   classrM   ri   )rS   rr   )registerExtension
getConfigsr   rL   rw   r   r   ro   rj   r-   rW   rl   r   rs   rU   patch_fenced_ruler    r$   )	r&   rU   r   r|   customr   rS   fence_formatri   s	            r   extendMarkdownz'SuperFencesCodeExtension.extendMarkdown  s    	T"" 	%!&..zEXY		
 

?B7# 
	F::f%DG,J!::h0ABL

;0ABIJ$:((%%jZl[%%jIF
	  [
r   c                    | j                         }t        | j                        }||_        | |_        | j
                  d   d   dk(  r|j                  | j
                  d   d<   | j                  j                  j                  |dd       t        | j                  j                        }||_        | |_        | j                  j                  j                  j                  |dd       |d	   rgt        | j                        }||_        | |_        | j                  j                  j                  |d
d       | j                  j                  dgi        | j                  j                  dgdddii       y)a  
        Patch Python Markdown with our own fenced block extension.

        We don't attempt to protect against a user loading the `fenced_code` extension with this.
        Most likely they will have issues, but they shouldn't have loaded them together in the first place :).
        r   r   r   r   r\      r0   P   r~   fenced_raw_blockg?@zpymdownx._bypassnormzpymdownx.highlight_enabledFN)r   SuperFencesBlockPreprocessorrU   r   	extensionr   	highlightr`   registerSuperFencesCodeBlockProcessorparserblockprocessorsSuperFencesRawBlockPreprocessorregisterExtensions)r&   r   fencedindented_code
raw_fenceds        r   r   z*SuperFencesCodeExtension.patch_fenced_rule%  sA    "-dgg6Av&-7/5/?/?DQ,&&v/BBG5dggnnE%"&&&//vrJ/"8AJ &J#'J GG!!**:7I5QGG&&(>'?D 	""$8#9<PS]_dRe;fgr   c                 8    | j                   j                          yr7   )r$   r8   r%   s    r   resetzSuperFencesCodeExtension.resetF  s     	

 r   )
r   r   r   r   r'   r   r   r   r   __classcell__r   s   @r   ry   ry      s"    +*$)"!HhB!r   ry   c                        e Zd ZdZdZ fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Z xZS )r   z
    Preprocessor to find fenced code blocks.

    Because this is done as a preprocessor, it might be too greedy.
    We will stash the blocks code and restore if we mistakenly processed
    text from an indented code block.
    z<pre%s><code%s>%s</code></pre>c                     t         |   |       | j                  j                  | _        d| _        i | _        d| _        d| _        y)r"   FN)	r   r'   rU   
tab_lengthtab_lenchecked_hl_settingscodehilite_confchecked_quotesquotes_logic)r&   rU   r   s     r   r'   z%SuperFencesBlockPreprocessor.__init__W  sC     	ww))#( !#!r   c                 8    |j                  | j                        S )zNormalize whitespace.)
expandtabsr   )r&   texts     r   normalize_wsz)SuperFencesBlockPreprocessor.normalize_wsa  s     t||,,r   c                 d    dj                  |D cg c]  }|| j                  d  c}      S c c}w )zDedent the fenced block lines.
N)rN   ws_virtual_len)r&   lineslines      r   rebuild_blockz*SuperFencesBlockPreprocessor.rebuild_blockf  s/     yyG$t2234GHHGs   -c                     | j                   sFd| _         | j                  j                  D ]&  }t        |t              sd| _         | j
                  S  | j
                  S )z2Check if we are using the Quotes blockquote logic.T)r   rU   registeredExtensions
isinstancer	   r   )r&   exts     r   is_pymdownx_quotes_logicz5SuperFencesBlockPreprocessor.is_pymdownx_quotes_logick  s]     """&Dww33 c?3(,D%   	    r   c                    | j                   s)d| _         d}d| _        | j                  j                  D ]0  }|| _        	 |j                         }|j                         | _         n d| j                  j                  v | _	        | j                  d   }|r|n|d   | _        | j                  j                  dd      | _        |j                  dd      | _        |d   | _        |d	   | _        |d
   | _        |d   | _        |d   | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  di       | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _         yy# t        $ r Y w xY w)z8Check for Highlight extension to get its configurations.TN	attr_listr}   r   Fextend_pygments_lang
guess_langpygments_stylera   	noclassesr^   linenums_styletablelinenums_classlinenums_specialr   language_prefixz	language-code_attr_on_pre
auto_titleauto_title_map
line_spansr@   line_anchorsanchor_linenumspygments_lang_classstripnldefault_lang)!r   highlighterrU   r   highlight_extget_pymdownx_highlight_settingsget_pymdownx_highlighterAttributeErrortreeprocessorsr   r   r}   r-   r   r   r   r   ra   r   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   )r&   r   r   r}   s       r   rn   z,SuperFencesBlockPreprocessor.get_hl_settingsv  s!    '''+D$F#Dww33 %(" @@BF'*'C'C'ED$ )DGG,B,BBDNK0I*3Y9LDN#';;??3De#LD (.

3I4(PD%$\2DO"()9":D &~ 6D#K0DN":.DM"(**-=w"GD"(**-=z"JD$*JJ/A2$FD!#)::.?#MD $*JJ/A5$ID!$jju=DO"(**-=r"BD$jjr:DO &

>2 >D#)::.?#GD '-zz2G'OD$!::i6DL &

>4 @DM ( & s   %H88	IIc                     d| _         d| _        d| _        d| _        d| _        d| _        g | _        d| _        d| _        i | _	        g | _
        d| _        i | _        d| _        y)zReset the class variables.Nr   r@   )wsws_lenr   fencelangquote_levelr0   empty_lines	fence_endrT   r;   idr=   r   r%   s    r   clearz"SuperFencesBlockPreprocessor.clear  si     
		
r   c                    ||z   j                         dk(  r4| xj                  dz  c_        | j                  j                  ||z          yt	        |      | j
                  k7  r|dk7  r| j                          y| j                  j                  |      '|j                  d      s	 | j                  ||||       yd| _        | j                  j                  ||z          y# t        $ r  t        $ r | j                          Y yw xY w)zEvaluate a normal fence.r@   r   N)r   r   r   )stripr   r0   r   r*   r   r   r   re   rb   process_nested_blockr   	Exception)r&   r   contentstartends        r   
eval_fencez'SuperFencesBlockPreprocessor.eval_fence  s     L!R'!IIR'\*W+++2JJL^^!!'*6w?Q?QR]?^))"gucB  !DIIR'\* (  

s   &C! !"DDc                    | j                         }|| j                  kD  r| j                          y|r || j                  k7  r| j                          y|dk(  r4| j                  j	                  ||z          | xj
                  dz  c_        yt        |      | j                  k  r| j                          y| j
                  r || j                  k  r| j                          y| j                  j                  |      	 | j                  ||||       yd| _        | j                  j	                  ||z          y# t        $ r  t        $ r | j                          Y yw xY w)z#Evaluate fence inside a blockquote.Nr@   r   r   )r   r   r   r0   r   r   r*   r   r   re   r   r   r   )r&   r   r   r   r   r   r   s          r   eval_quotedz(SuperFencesBlockPreprocessor.eval_quoted  s#    446)))JJLK4+;+;;JJLb=IIR'\*!Wt{{"JJL+0@0@"@ JJL^^!!'*6))"gucB  !DIIR'\* (  

s   =D8 8"EEc           	         || j                  |      z   | _        d}| j                  ||z
  dz
  | _        | j                  | j	                  | j
                        | j                  | j                  | j                  | j                  | j                  | j                  r| j                  ni       }|?| j                  | j                  dj                  | j
                              dz   |||       | j                          y))Process the contents of the nested block.N   )rq   rR   rU   rT   r;   r<   r=   r   )r   lastr   
line_countr   r0   r   rU   rT   r;   r   r   r=   _storerN   r   r&   r   r   r   r   r0   s         r   r   z1SuperFencesBlockPreprocessor.process_nested_block  s     **733	>>%!EkAoDO>>&&tyy177$(NNdjj " D KK))$))DII*>?$FeUXY

r   c                 j    t        |      }|dk  rd}|S || j                  kD  r| j                  dz   }|S )a  
        Normalize highlight line number.

        Clamp outrages numbers. Numbers out of range will be only one increment out range.
        This prevents people from create massive buffers of line numbers that exceed real
        number of code lines.
        r   r   )intr   )r&   numbers     r   normalize_hl_linez.SuperFencesBlockPreprocessor.normalize_hl_line  sA     VA:F  doo%__q(Fr   c           
         g }|r|j                         D ]  }|j                  d      D cg c]  }| j                  |       }}t        |      dkD  r:|d   |d   k  sJ|j                  t	        t        |d   |d   dz                      xd|d   cxk  r| j                  k  sn |j                  |        |S c c}w )zParse the lines to highlight.-r   r   )splitr   r*   extendlistranger   )r&   r]   r   entrye
line_ranges         r   parse_hl_linesz+SuperFencesBlockPreprocessor.parse_hl_lines  s     !) -AFSAQRAd44Q7R
Rz?Q&!!}
15T%
1z!}q?P*Q%RS*Q-:4??:LL,-  Ss   B>c                      |rt        |      S dS Parse line start.r   r   )r&   	linestarts     r   parse_line_startz-SuperFencesBlockPreprocessor.parse_line_start%  s     "+s9~22r   c                 2    |rt        |      nd}|dkD  r|S dS )r	  r   r   r
  )r&   linestepsteps      r   parse_line_stepz,SuperFencesBlockPreprocessor.parse_line_step*  s"     !)s8}baxt'R'r   c                      |rt        |      S dS r  r
  )r&   linespecials     r   parse_line_specialz/SuperFencesBlockPreprocessor.parse_line_special1  s     $/s;6B6r   c                 <   d}d}g }d}|D ]z  }|| j                   k\  r ni|t        vr n_|dz  }|dk(  r6| j                  || j                  z  z
  }||z  }|j                  d|z         nd}|dz  }|j                  |       ||z  }| dj	                  |      ||d fS )zParse fence line.r   r   r   r   r@   N)r   PREFIX_CHARSr   r   rN   )r&   r   r   r   r   indexctab_sizes           r   parse_fence_linez-SuperFencesBlockPreprocessor.parse_fence_line6  s      	A!4!44$aKFDy<<54<<+?@(*		#.)!#		!XE	  wwr{DM))r   c                     d| _         d| _        g }|D ]2  }|t        vr n(| xj                   dz  c_         |j                  |       4 | j	                  dj                  |            }t        |      | _        |S )z<Parse the whitespace (blockquote syntax is counted as well).r   r   r@   )r   r   r  r   r   rN   r*   )r&   r   r   r  s       r   parse_whitespacez-SuperFencesBlockPreprocessor.parse_whitespaceO  sv      	A$KK1KIIaL		 rwwr{+!"g	r   c                 
   d}|j                  d      r|j                  d      | _        |j                  d      }i | _        i | _        d| _        i }|rEt
        j                  |      D ]-  }|j                  d      }|j                  d      }||}|||<   / t        | j                  j                        D ]  } |d   | j                        si }	i }
|j                  dt        j                  t        t        	            }	  || j                  ||	|
| j                        }|
rd}|st|j                  d
      | _        |	| _         n |s| j$                  r| j'                  |      S |S # t         $ r  t"        $ r Y \w xY w)zGet options.Fr   rT   Nr.   valuer   ri   r   r   )groupr   rT   r=   r   
RE_OPTIONSfinditerreversedr   r   r-   r   r   ro   rl   rU   r   r   r   handle_unrecognized)r&   mokaystringvaluesm2r.   r  r  rT   r=   ri   s               r   parse_optionsz*SuperFencesBlockPreprocessor.parse_options`  sx    776?DI#
 ))&1 $hhuo)=E#s$ dnn889 	EuV}TYY'!IIk93D3DZ[l3mn	$TYYPD
  D%*YY{%;DN#*DL!	$ ,,++A.. ,   s    E--FFc                 b   d}| j                   s|S |j                  d      r|j                  d      | _        i | _        i | _        d| _        t        | j                  j                        D ]  } |d   | j                        si }i }|j                  dt        j                  t        t                    }	  || j                  i ||| j                        }|sp|j                  d      | _        || _        | j"                  r|| _         n |sd| _        y# t        $ r  t         $ r Y Tw xY w)	z!Handle unrecognized code headers.Fr   Nr   ri   r   r   T)r   r  r   rT   r=   r   r!  r   r   r-   r   r   ro   rl   rU   r   r   r   )r&   r#  r$  r  rT   r=   ri   s          r   r"  z0SuperFencesBlockPreprocessor.handle_unrecognized  s    ##K776?DI
 dnn889 	EuV}TYY'!IIk93D3DZ[l3mn	$TYYGUDGGLD
 %*YY{%;DN#*DL~~%*
!	$ DI ,   s   9 DD.-D.c                    d}t        |j                  d      j                  dd| j                  z              }i | _        i | _        d| _        i }|D ]8  \  }}|dk(  r|| _        |dk(  r| j                  j                  |       4|||<   : |j                  d      r|j                  d      | _
        n.| j                  r| j                  j                  d	      nd
| _
        t        | j                  j                        D ]  } |d   | j                        si }i }	|j                  dt!        j"                  t$        t&                    }
	  |
| j                  |||	| j(                        }|sp|j                  d      | _        || _        | j.                  r|	| _         n |s| j0                  r| j3                  |      S |S # t*        $ r  t,        $ r Y kw xY w)zHandle attribute list.Fr=   r   r   Nr   .r   r   r@   r   ri   r   r   )r   r  r   r   rT   r=   r   r   r;   r   r   popr!  r   r   r-   r   r   ro   rl   rU   r   r   r   r   r"  )r&   r#  r$  
attributesr&  rF   rG   r  rT   r=   ri   s              r   handle_attrsz)SuperFencesBlockPreprocessor.handle_attrs  s    qwww/77cDLL>PQR

 	DAqDyc##A&q		 776?DI/3||((+DI dnn889 	EuV}TYY'!IIk93D3DZ[l3mn	$TYYPD
 %*YY{%;DN#*DL~~%*
!	$ ,,++A.. ,   s    GGGc                    d}|D ]  }|j                  d      }| j                  8| j                  |      }t        j	                  || j
                        }|z|j                  d      r| j                  |      }n4|j                  d      r| j                  |      }n| j                  |      }|r|}|| j                  |j                  d            z   | _        || _        | j                  j                  d      | _        d| _        |j                  d      | _        t!        j"                  t$        | j                  z        | _        n| j)                          nv| j+                  |      \  }}|dz   }	|j                  d      }
| j                  r| j-                  |||
|	       n*|
dk(  r| j/                  |||	       n| j)                          |dz  } | j1                  |      S )z Search for nested fenced blocks.r   unrecognizedr=   r   r   r   )rstripr   r  RE_NESTED_FENCE_STARTre   r   r  r"  r.  r(  r   firstr   countr   r   recompileNESTED_FENCE_ENDr   r   r  r   r   
reassemble)r&   r   r5  r   r   r#  r$  r   r   r   r   s              r   search_nestedz*SuperFencesBlockPreprocessor.search_nested  s     9	D ;;t$Dzz!**40 *//dkkB= ww~.#77:)#003#11!4 %%'$*;*;AGGAJ*G%G
"$+/77==+=(+,(%&WWW%5
)+4Dtzz4Q)R 

 #33D9Gai hhsm##$$R+ucJ A%OOB< JJLQJEs9	v u%%r   c                     t        | j                        rC| j                  j                         \  }}}|d| |gz   ||d z   }t        | j                        rC|S )Reassemble text.N)r*   stackr,  r&   r   r   r   r   s        r   r9  z'SuperFencesBlockPreprocessor.reassemble#  sW     $**o!%!1FE3&5MVH,uST{:E $**o r   c                 p   |d   }|d   }|d   }|g }d}	d}
d}d}d}| j                   rd|v r,t        j                  |d         }|j                  d      }|d= d|v rNt        j                  |d         }|j                  d      }
|j                  d      }	|j                  d	      }|d= d
|v r|d
   }|d
= | j                  |	      }	| j                  |
      }
| j                  |      }| j                  |      }| j                  xj                  dz  c_
        | j                  | j                  | j                  | j                   | j                  | j                  | j                   | j"                  | j$                  | j&                  | j(                  | j*                  | j,                  | j.                  | j0                  | j2                  | j4                  | j6                  | j8                  | j:                        j=                  ||| j>                  ||
|	|||||| j                  j                        }|S )z
        Syntax highlight the code block.

        If configuration is not empty, then the CodeHilite extension
        is enabled, so we call into it to highlight the code.
        r;   r<   r=   Nr]   r^   r  r  r  r_   r   )r   r   ra   r   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )	r]   r  r  r  r;   r<   r=   r_   code_block_count) ra   rc   re   r  rd   r  r  r  r  r   pygments_code_blockr   r   r   r   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r}   )r&   rq   rR   rT   rU   rV   r;   r<   r=   r  r  r  r]   r_   r#  els                   r   r   z&SuperFencesBlockPreprocessor.highlight.  sF    #*%w?G 	W$%%gj&9:77:.J'W$%%gj&9:GGK0	77:.ggm4J''!(G$''1)))4	--k:&&x0..!3...**nn]]..!22..!%!:!: 00!22..** 00 $ 8 8LL**'  
( )NN#!//CC  
) 	F 	r   c           	         | j                   j                  j                  |      }| j                  j	                  dj                  | j                  |      ||f       | j                  si| j                  j                  j                  |dd dj                  | j                  | j                  |      | j                        | j                         yy)
        Store the fenced blocks in the stack to be replaced when done iterating.

        Store the original text in case we need to restore if we are too greedy.
        {}{}r   r   {}
{}{}N)rU   	htmlStashr5   r=  r   rM   r   disabled_indentedr   r$   r4  r   r   r   r&   rQ   r0   r   r   placeholders         r   r   z#SuperFencesBlockPreprocessor._store  s     gg''--d3

6==+>sKL%% NN  &&Ab!!!$**d.?.?.GS## &r   c                 h    g }|j                  d      D ]  }||z
  }|j                  ||d         |S z0Reindent the code to where it is supposed to be.r   N)r   r   r&   r   poslevelindentedr   r  s          r   reindentz%SuperFencesBlockPreprocessor.reindent  sC     JJt$ 	*D%KEOODL)	* r   c                    g }|D ]  }t         j                  |      }|r|j                  d      }t        |j                  d            }d}| j                  j
                  j                  |d      \  }}|I| j                  |||      }	|j                  |	       | j                  j
                  j                  |       ||j                  |       |j                  |        |S ),Revert a prematurely converted fenced block.r   r   NNN)FENCED_BLOCK_REre   r  r*   r   r$   r-   rQ  r   r2   r   )
r&   r   	new_linesr   r#  r.   r4   originalrN  r0   s
             r   restore_raw_textz-SuperFencesBlockPreprocessor.restore_raw_text  s     	 	'D%%d+Aggaj"1771: $ 4 4 8 8l K#'==3ED$$T*NN((//4# $$T*  &'	'( r   c                 0   | j                          | j                          g | _        | j                  j	                  dd      | _        | j                  j	                  dd      | _        | j                  r| j                  |      }| j                  |      S )Search for fenced blocks.r{   Fr~   )	rn   r   r=  r   r-   rH  r~   rX  r:  r&   r   s     r   runz SuperFencesBlockPreprocessor.run  sz     	


!%1OQV!W![[___eD))%0E!!%((r   )r@   r@   NN) r   r   r   r   	CODE_WRAPr'   r   r   r   rn   r   r   r   r   r   r  r  r  r  r  r  r(  r"  r.  r:  r9  r   r   rQ  rX  r\  r   r   s   @r   r   r   L  s     1I"-
I
	!)AV$+.%+N, 3
(7
*2",\#J-^?&B	Ob$4)r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   z@Special class for preserving tabs before normalizing whitespace.c                     || j                  |      z   | _        dj                  | j                        }| j	                  |dz   |||       | j                          y)r   r   N)r   r   rN   r0   r   r   r   s         r   r   z4SuperFencesRawBlockPreprocessor.process_nested_block  sL     **733	yy#D4Kuc2

r   c                 p   | j                   j                  j                  d      }| j                  j	                  dj                  | j                  |      ||f       | j                  j                  j                  |dd dj                  | j                  || j                        | j                         y)rD  r@   rE  r   r   rF  N)rU   rG  r5   r=  r   rM   r   r   r$   r4  r   r   rI  s         r   r   z&SuperFencesRawBlockPreprocessor._store  s     gg''--b1

6==+>sKL"""djj&$))<	
r   c                    t        | j                        rp| j                  j                         \  }}}|d| |j                  t        j
                  t        d      dd t        z   gz   ||d z   }t        | j                        rp|S )r<  Nr   r   )r*   r=  r,  r   md_utilSTXSOHEOTr>  s        r   r9  z*SuperFencesRawBlockPreprocessor.reassemble  sz     $**o!%!1FE3&5MV^^GKKa%H"%MPS%S$TTW\]`]aWbbE $**o r   c                     | j                          | j                          g | _        | j                  j	                  dd      | _        | j                  |      S )rZ  r{   F)rn   r   r=  r   r-   rH  r:  r[  s     r   r\  z#SuperFencesRawBlockPreprocessor.run  sJ     	


!%1OQV!W!!%((r   N)r   r   r   r   r   r   r9  r\  r   r   r   r   r     s    J
 	)r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   z_Process indented code blocks to see if we accidentally processed its content as a fenced block.c                      y)z-Test method that is one day to be deprecated.Tr   )r&   parentblocks      r   r   z"SuperFencesCodeBlockProcessor.test  s     r   c                     g }|j                  d      D ]  }||z
  }|j                  ||d         dj                  |      S rL  )r   r   rN   rM  s          r   rQ  z&SuperFencesCodeBlockProcessor.reindent  sM     JJt$ 	*D%KEOODL)	* yy""r   c                    g }|j                  d      D ]  }t        j                  |      }|r|j                  d      }t	        |j                  d            }d}| j
                  j                  j                  |d      \  }}|I| j                  |||      }	|j                  |	       | j
                  j                  j                  |       ||j                  |       |j                  |        dj                  |      S )rS  r   r   r   NrT  )r   rU  re   r  r*   r   r$   r-   rQ  r   r2   rN   )
r&   rj  	new_blockr   r#  r.   r4   rW  rN  r0   s
             r   revert_greedy_fencesz2SuperFencesCodeBlockProcessor.revert_greedy_fences  s     	KK% 	'D%%d+Aggaj"1771: $ 4 4 8 8l K#'==3ED$$T*NN((//4# $$T*  &'	'( yy##r   c                    d}| j                   j                  dd      sht        j                  | ||d         }|rL| j                   j                  dd      r| j	                  |d         |d<   t        j
                  | ||      du}|S )zLook for and parse code block.Fr{   r   nestedT)r   r-   r   r   rn  r\  )r&   ri  blockshandleds       r   r\  z!SuperFencesCodeBlockProcessor.run  s     {{=uE(--dFF1IFG;;??8T2 $ 9 9&) DF1I,00vvFeSr   N)r   r   r   r   r   rQ  rn  r\  r   r   r   r   r     s    i
#$4r   r   c                      t        | i |S )zReturn extension.)ry   )r   rV   s     r   makeExtensionrt  +  s     $T4V44r   r9   )r@   r@   NNr@   N),r   markdown.extensionsr   markdown.preprocessorsr   markdown.blockprocessorsr   markdown.extensions.attr_listr   markdownr   rb  r   r6  quotesr	   rd  re  r  r7  r3  rc   rd   r  r8  rM   HTML_PLACEHOLDERrU  r   r   r   r    rW   rZ   rj   rl   ro   rs   rw   ry   r   r   r   rt  r   r   r   <module>r|     s]  : * / 7 3 $  	 #"

 $ bjjOPbjjopRZZ
   "**    #  2&4  $H9 H& &R`"S".;BVl!y l!^s	)< s	)l-)&B -)`6$6 6r5r   