
    $gX>                         d Z ddlmZ ddlm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 ddlZdZedz  Zd	Zi Z G d
 de          Z G d de          Z G d de          Zd ZdS )ax  
Snippet ---8<---.

pymdownx.snippet
Inject snippets

MIT license.

Copyright (c) 2017 Isaac Muse <isaacmuse@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
    )	Extension)PreprocessorN   )utili       g      $@c                       e Zd ZdZdS )SnippetMissingErrorzSnippet missing exception.N)__name__
__module____qualname____doc__     Q/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/pymdownx/snippets.pyr	   r	   )   s        $$$$r   r	   c                        e Zd ZdZ ej        d          Z ej        d          Z ej        d          Z ej        d          Z	 fdZ
d Zd Zd	 Zej        d
             ZddZd Z xZS )SnippetPreprocessorz$Handle snippets in Markdown content.a  (?x)
        ^(?P<space>[ \t]*)
        (?P<escape>;*)
        (?P<all>
            (?P<inline_marker>-{1,}8<-{1,}[ \t]+)
            (?P<snippet>(?:"(?:\\"|[^"\n\r])+?"|'(?:\\'|[^'\n\r])+?'))(?![ \t]) |
            (?P<block_marker>-{1,}8<-{1,})(?![ \t])
        )\r?$
        zE(?x)
        ^(?P<space>[ \t]*)
        (?P<snippet>.*?)\r?$
        z(?xi)
        ^(?P<pre>.*?)
        (?P<escape>;*)
        (?P<inline_marker>-{1,}8<-{1,}[ \t]+)
        (?P<section>\[[ \t]*(?P<type>start|end)[ \t]*:[ \t]*(?P<name>[a-z][-_0-9a-z]*)[ \t]*\])
        (?P<post>.*?)$
        z7(?i)(.*?)(?:(:[0-9]*)?(:[0-9]*)?|(:[a-z][-_0-9a-z]*)?)$c                 ^   |                     d          }t          |t          t          j        f          r|g}d |D             | _        |d         | _        |                     d          | _        |                     d          | _        |                     d          | _	        |d         | _
        |d         | _        |d	         | _        |d
         | _        |d         | _        |j        | _        t!                                                       | j                                         dS )Initialize.	base_pathc                 L    g | ]!}t           j                            |          "S r   )ospathabspath).0bs     r   
<listcomp>z0SnippetPreprocessor.__init__.<locals>.<listcomp>U   s&    ;;;"'//!,,;;;r   restrict_base_pathencodingcheck_pathsauto_appendurl_downloadurl_max_sizeurl_timeouturl_request_headersdedent_subsectionsN)get
isinstancestrr   PathLiker   r   r   r   r    r!   r"   r#   r$   r%   
tab_lengthsuper__init__downloadcache_clear)selfconfigmdbase	__class__s       r   r,   zSnippetPreprocessor.__init__O   s    zz+&&dS"+.// 	6D;;d;;;"()=">

:..!::m44!::m44">2">2!-0#)*?#@ "()=">-!!#####r   c                 (   g }d}d}|D ]O}| j                             |          }|r|r|                    d          r|                    d          |                    d                              ddd          z   |                    d          z   |                    d          z   |                    d	          z   }n|w|                    d          |k    r^|s|                    d          dk    rd}d}|r|                    d          dk    r|r|                    d          dk    rd} n! n|r|r8|r|                    |           Q|s| j        rt          d| d          | j        r|                     |          n|S )z-Extract the specified section from the lines.Fescapepre; r   inline_markersectionpostNnametypestartTendzSnippet section 'z' could not be located)	RE_SNIPPET_SECTIONmatchgroupreplaceappendr   r	   r%   dedent)r/   r:   lines	new_linesr>   foundlms           r   extract_sectionz#SnippetPreprocessor.extract_sectiond   s    	 (	$ (	$A '--a00A  U qwwx00 GGENNQWWX%6%6%>%>sB%J%JJQWWUdMeMeeGGI&&')*9  1776??g#=#=  G!;!; E E  
qwwv'99  qwwv%77!EE E  u   $  ### 	[) 	[%&Y'&Y&Y&YZZZ)-)@Ot{{9%%%iOr   c                 v    t          j        d                    |                                        d          S )zDe-indent lines.
)textwraprE   joinsplitr/   rF   s     r   rE   zSnippetPreprocessor.dedent   s.     tyy//0066t<<<r   c                    d}| j         D ]c}t          j                            |          r@t          j                            |          r| j        rTt          j                            t          j                            ||                    }|                    |          sn t          j                            ||          }t          j                            |          r|} nt          j        	                    |          }t          j                            ||          }t          j                            |          r$t          j        
                    ||          r|} ne|S )zGet snippet path.N)r   r   r   existsisdirr   r   rO   
startswithdirnamesamefile)r/   r   snippetr2   filenamerV   s         r   get_snippet_pathz$SnippetPreprocessor.get_snippet_path   s7    N 	 	Dw~~d## 7==&& . <#%7??27<<d3K3K#L#L'22488 %$% $&7<<d#;#;w~~h// "* !good33G!w||GT::Hw~~h// BG4D4DXt4T4T "*r   c                      t           j                            | j                  } j        dk    rdn j        }t           j                            ||          5 }t          j        r|j        n|j	        }|dk    rt          d| d          d}d|j        vrI j        dk    r|                     j                  n|                                }t          |          }nt          |j        d                   } j        dk    r"| j        k    rt!          d	 j                   |dk    rd
gcddd           S ||                                } fd|                                D             cddd           S # 1 swxY w Y   dS )z
        Actually download the snippet pointed to by the passed URL.

        The most recently used files are kept in a cache until the next reset.
        )headersr   N)timeout   zCannot download snippet ''zcontent-lengthz2refusing to read payloads larger than or equal to r8   c                 D    g | ]}|                     j                  S r   )decoder   )r   rI   r/   s     r   r   z0SnippetPreprocessor.download.<locals>.<listcomp>   s'    JJJAHHT]++JJJr   )urllibrequestRequestr$   r#   urlopenr   PY39statuscoder	   r\   r"   readlenint
ValueError
splitlines)r/   urlhttp_requestr]   responserg   contentcontent_lengths   `       r   r-   zSnippetPreprocessor.download   s$    ~--c4;S-TT*a//$$T5E^##L'#BB 	Kh )-	DX__x}F}})*Lc*L*L*LMMM Gx'777 ?C>OST>T>T(--(9:::ZbZgZgZiZi!$W!$X%56F%G!H!H A%%.D<M*M*M !iVZVg!i!ijjj ""t/	K 	K 	K 	K 	K 	K 	K 	K2 "--// KJJJW5G5G5I5IJJJ=	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	Ks   CF5FF
FNFc           	      l	   |r| j                             |           g }d}d}|D ]p}d}| j                            |          }|rt|                    d          r+|                    |                    ddd                     a|r|                    d          ry|                    d          rd}n| }|s|                    |           |r|s| j                            |          }|r|                    d                              | j	                  |r/|                    d	          dd
         
                                n&|                    d	          
                                }	|s|	s|                    d           n|	                    d          rd}
d}d}| j                            |	          }|                    d          
                                }	|	s| j        rt          d|	 d          |                    d          }|r*t          |          dk    rt!          |dd                   }
|                    d          }|r;t          |          dk    r(t#          dt!          |dd                   dz
            }|                    d          }|r
|dd         }|	                                                    d          }|r|s| j        o|}|s|                     |	          n|	}|rf|| j         v r|st+          j        |d| j                  5 }d |D             }||
;t1          ||
          }| j        r|                     ||                   n||         }n|r|                     ||          }ddd           n# 1 swxY w Y   n	 |                     |          }||
;t1          ||
          }| j        r|                     ||                   n||         }n|r|                     ||          }n# t          $ r | j        r g }Y nw xY w|                    fd|                     |||          D                        V| j        rt          d|	 d          r|r| j                             |           |S )zParse snippets snippet.Fr5   r7   r8   r   r9   TspacerX   NzSnippet at path 'z' could not be found      r      )zhttps://zhttp://r)r   c                 8    g | ]}|                     d           S )z
)rstrip)r   rI   s     r   r   z6SnippetPreprocessor.parse_snippets.<locals>.<listcomp>G  s$    &C&C&CAqxx'7'7&C&C&Cr   c                     g | ]}|z   S r   r   )r   l2rt   s     r   r   z6SnippetPreprocessor.parse_snippets.<locals>.<listcomp>]  s*       +-EBJ  r   )is_url) seenaddRE_ALL_SNIPPETSrA   rB   rD   rC   
RE_SNIPPET
expandtabsr*   striprU   RE_SNIPPET_FILEr   r	   rj   rk   maxlowerr!   rZ   codecsopenr   slicer%   rE   rK   r-   extendparse_snippetsremove)r/   rF   	file_namer~   rG   inlineblocklinerJ   r   r?   r>   r:   endingstartingsection_nameis_linkrn   rX   fs_linessrt   s                         @r   r   z"SnippetPreprocessor.parse_snippets   s     	%IMM)$$$	 |	^ |	^DF$**400A 778$$ $$T\\#r1%=%=>>> QWW_55  WW_-- !FF !&IE    &&& 0V 0 O))$// Y^((33DODD;Aaqwwy))!B$/55777qwwyGYGYG_G_GaGa ! !!((,,,  ??3''  (..t44wwqzz'')) !' !12`d2`2`2`aaa  *c&kkAoofQRRj//C771:: :H 1 13x|#4#4q#899E wwqzz /*122.G **,,112IJJ '  '3G=@J$//555d )^ $)++  )#[#NNN QRS&C&C&C&C&CG$0CO$)%$5$5EIE\*l$++gaj*A*A*Abijkbl!( Q*.*>*>w*P*PQ Q Q Q Q Q Q Q Q Q Q Q Q Q Q
)&*mmG&<&<G$0CO$)%$5$5EIE\*l$++gaj*A*A*Abijkbl!( Q*.*>*>w*P*P2 ) ) )#/ & %&(GGG) $$   151D1D ' ''* 2E 2 2      % ^-.\$.\.\.\]]]  	(IY'''s&   ?A$N//N3	6N3	<A,P))Q ?Q c                    t                      | _        | j        rS|                    d                    d                    | j                                                d                     |                     |          S )zProcess snippets.z

-8<-
{}
-8<-
z

rM   )setr   r    r   formatrO   rP   r   rQ   s     r   runzSnippetPreprocessor.runo  sn     EE	 	cLL/66v{{4CS7T7TUU[[\`aabbb""5)))r   )NF)r
   r   r   r   recompiler   r   r@   r   r,   rK   rE   rZ   	functools	lru_cacher-   r   r   __classcell__r3   s   @r   r   r   -   s       .. bj	
 
O 	 J $	  !bj![\\O$ $ $ $ $*3P 3P 3Pj= = =
  2 'K 'K 'KRL L L L\* * * * * * *r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )SnippetExtensionzSnippet extension.c                     dgdgddgddgddgg d	gdd
gt           dgt          dgt          dgddgd
| _         t	                      j        |i | dS )r   .z,Base path for snippet paths - Default: ["."]TzNRestrict snippet paths such that they are under the base paths - Default: Truezutf-8z'Encoding of snippets - Default: "utf-8"FzBMake the build fail if a snippet can't be found - Default: "False"zeA list of snippets (relative to the 'base_path') to auto append to the Markdown content - Default: []z5Download external URLs as snippets - Default: "False"z9External URL max size (0 means no limit)- Default: 32 MiBz:Defualt URL timeout (0 means no timeout) - Default: 10 secz#Extra request Headers - Default: {}z=Dedent subsection extractions e.g. 'sections' and/or 'lines'.)
r   r   r   r   r    r!   r"   r#   r$   r%   N)DEFAULT_URL_SIZEDEFAULT_URL_TIMEOUTDEFAULT_URL_REQUEST_HEADERSr0   r+   r,   )r/   argskwargsr3   s      r   r,   zSnippetExtension.__init__|  s     %!QR`# !"MN!#ijw #$]^-/jk/1mn$?Af#g#(*i"j!
 
& 	$)&)))))r   c                     || _         |                    |            |                                 }t          ||          }|j                            |dd           dS )zRegister the extension.rX   r   N)r1   registerExtension
getConfigsr   preprocessorsregister)r/   r1   r0   rX   s       r   extendMarkdownzSnippetExtension.extendMarkdown  s^     
T"""""%fb11
!!'9b99999r   c                 X    | j         j        d         j                                         dS )zReset.rX   N)r1   r   r-   r.   )r/   s    r   resetzSnippetExtension.reset  s)     	i(1==?????r   )r
   r   r   r   r,   r   r   r   r   s   @r   r   r   y  sc        * * * * *0: : :@ @ @ @ @ @ @r   r   c                      t          | i |S )zReturn extension.)r   )r   r   s     r   makeExtensionr     s     T,V,,,r   )r   markdownr   markdown.preprocessorsr   r   rb   r   r   r   r8   r   rN   MIr   r   r   	Exceptionr	   r   r   r   r   r   r   <module>r      sD   0       / / / / / /      				  				       7    % % % % %) % % %I* I* I* I* I*, I* I* I*X
'@ '@ '@ '@ '@y '@ '@ '@T- - - - -r   