
    $g                     ,   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	c m
Z	 ddlmZ ddlZddlmZmZ dZd	Zg d
g dg dg dg ddZdZdZdZ ej        d          ZdZdZdZdZdZdZdZd Zde Z de Z! edd          Z" edd          Z# edd          Z$dZ%d Z&d!Z'd"Z(d#Z) ej        d$          Z*d%efd&efd'efd(Z+djd*Z, ej        d+-                     e,dd,           e,dd-           e,dd.                              Z.d/efd0efd1efd(Z/djd2Z0 ej        d+-                     e0dd,           e0dd3           e0dd.                              Z1 ej        d4-                    ee                    Z2d5d6hZ3d7dd8ed9d:d;d<d=d>	d?dd8ed@dAdBdCdDd>	dEdd8ed@dFdGdHdIdDdJ
dKd6d8edLdMd5d8edLdNZ4dO Z5 e5d6dP           e5d5dQ           e5dd.           e5dd-           e5dd,          dRZ6 G dS dTe          Z7 G dU dVe7          Z8 G dW dXe          Z9 G dY dZe          Z: G d[ d\e          Z; G d] d^e          Z< G d_ d`e7          Z= G da dbe7          Z> G dc dde8          Z? G de dfe8          Z@ G dg dhe          ZAdi ZBdS )ka  
Magic Link.

pymdownx.magiclink
An extension for Python Markdown.
Find HTML, FTP links, and email address and turn them to actual links

MIT license.

Copyright (c) 2014 - 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)Treeprocessor)util   )warn_deprecatedN)LinkInlineProcessorInlineProcessor   	dashboardaccountplanssupportrepomarketeplacenotificationsissuespullsponsorssettingsr   r   -explorehelpprojectsimessages	bookmarkshome	bitbucketgithubgitlabtwitterxa}  (?xi)
(?P<mail>
    (?<![-/\+@a-z\d_])(?:[-+a-z\d_]([-a-z\d_+]|\.(?!\.))*)  # Local part
    (?<!\.)@(?:[-a-z\d_]+\.)                                # @domain part start
    (?:(?:[-a-z\d_]|(?<!\.)\.(?!\.))*)[a-z]\b               # @domain.end (allow multiple dot names)
    (?![-@])                                                # Don't allow last char to be followed by these
)
at  (?xi)
(?P<link>
    (?:(?<=\b)|(?<=_))(?:
        (?:ht|f)tps?://[^_\W][-\w]*(?:\.[-\w.]+)*|          # (http|ftp)://
        (?P<www>w{3}\.)[^_\W][-\w]*(?:\.[-\w.]+)*           # www.
    )
    /?[-\w.?,!'(){}\[\]/+&@%$#=:"|~;]*                      # url path, fragments, and query stuff
    (?:[^_\W]|[-/#@$+=])                                    # allowed end chars
)
z(?i)<((?:ht|f)tps?://[^<>]*)>z^[a-zA-Z0-9]+$z\w{1,15}z+[a-zA-Z\d](?:[-a-zA-Z\d_]{0,37}[a-zA-Z\d])?z2[\.a-zA-Z\d_](?:[-a-zA-Z\d_\.]{0,37}[-a-zA-Z\d_])?z[-a-zA-Z\d_]{1,39}z4(?x)
(?P<mention>
    (?<![a-zA-Z])@
    (?:{})
)\b
z(?P<mention>(?<![a-zA-Z])@{})\bc                 r    |dk    r|  dt            S |dk    r|  dt           S |dk    r|  dt           S dS )z*Create external mentions by provider type.r$   :r%   r#   N)RE_GITHUB_USERRE_GITLAB_USERRE_BITBUCKET_USER)nameprovider_types     R/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/pymdownx/magiclink.pycreate_ext_mentionsr0   Y   sj       **.***	(	"	"**.***	+	%	%--+--- 
&	%    ztwitter:zx:r$   r%   r#   zi(?x)
(?P<mention>
    (?<![a-zA-Z])
    @(?:{})
)\b
/(?P<mention_repo>[-._a-zA-Z\d]{{0,99}}[a-zA-Z\d])\b
zZ(?x)
(?P<mention>(?<![a-zA-Z])@{})\b
/(?P<mention_repo>[-._a-zA-Z\d]{{0,99}}[a-zA-Z\d])\b
z(?x)
(?P<all>(?<![@/])(?:(?P<user>\b{})/)
(?P<repo>\b[-._a-zA-Z\d]{{0,99}}[a-zA-Z\d])
(?:(?P<issue>(?:\#|!|\?)[1-9][0-9]*)|(?P<commit>@[a-f\d]{{40}})(?:\.{{3}}(?P<diff>[a-f\d]{{40}}))?))\b
z(?x)
(?P<all>(?<![@/])(?:(?P<user>\b{})/)?
(?P<repo>\b[-._a-zA-Z\d]{{0,99}}[a-zA-Z\d])
(?:(?P<issue>(?:\#|!|\?)[1-9][0-9]*)|(?P<commit>@[a-f\d]{{40}})(?:\.{{3}}(?P<diff>[a-f\d]{{40}}))?))\b
z(?x)
(?P<all>
    (?:(?<![a-zA-Z])(?P<issue>(?:\#|!|\?)[1-9][0-9]*)|(?P<commit>(?<![@/])\b[a-f\d]{40})(?:\.{3}(?P<diff>[a-f\d]{40}))?)
)\b
z(https?://)(?:www\\\.)?(.*)a  
        (?P<github>(?P<github_base>{}/
        (?P<github_user_repo>(?P<github_user>{})/[^/]+))/
            (?:issues/(?P<github_issue>\d+)/?|
                pull/(?P<github_pull>\d+)/?|
                discussions/(?P<github_discuss>\d+)/?|
                commit/(?P<github_commit>[\da-f]{{7,40}})/?|
                compare/(?P<github_diff1>[\da-f]{{7,40}})\.{{3}}
                    (?P<github_diff2>[\da-f]{{7,40}})))a  
        (?P<bitbucket>(?P<bitbucket_base>{}/
        (?P<bitbucket_user_repo>(?P<bitbucket_user>{})/[^/]+))/
            (?:issues/(?P<bitbucket_issue>\d+)(?:/[^/]+)?/?|
                pull-requests/(?P<bitbucket_pull>\d+)(?:/[^/]+(?:/diff)?)?/?|
                commits/commit/(?P<bitbucket_commit>[\da-f]{{7,40}})/?|
                branches/commits/(?P<bitbucket_diff1>[\da-f]{{7,40}})
                    (?:\.{{2}}|%0d)(?P<bitbucket_diff2>[\da-f]{{7,40}})\#diff))a  
        (?P<gitlab>(?P<gitlab_base>{}/
        (?P<gitlab_user_repo>(?P<gitlab_user>{})/[^/]+))/(?:-/)?
            (?:issues/(?P<gitlab_issue>\d+)/?|
                merge_requests/(?P<gitlab_pull>\d+)/?|
                commit/(?P<gitlab_commit>[\da-f]{{8,40}})/?|
                compare/(?P<gitlab_diff1>[\da-f]{{8,40}})\.{{3}}
                    (?P<gitlab_diff2>[\da-f]{{8,40}})))r$   r#   r%   Tc                 j   t           |          }t          j        |                                                    d                    }|rJt
                              |          }|r.|                    d          dz   |                    d          z   }|d                             ||d                   S z Create repository link provider./r   z(?:w{3}\.)?r
   r   )	REPO_LINK_TEMPLATESreescapelowerrstripRE_WWWmatchgroupformatproviderhostwwwtemplatehost_patms         r/   create_repo_link_patternrF           #8,Hy,,S1122H
 @LL"" 	@wwqzzN2QWWQZZ?HA;h444r1   z(?xi)^(?:{}|{}|{})/?$zhttps://github.comzhttps://bitbucket.orgzhttps://gitlab.comz
        (?P<github>(?P<github_base>{}/
            (?P<github_user_repo>(?P<github_user>{})(?:/(?P<github_repo>[^/]+))?)))
        z
        (?P<bitbucket>(?P<bitbucket_base>{}/
            (?P<bitbucket_user_repo>(?P<bitbucket_user>{})(?:/(?P<bitbucket_repo>[^/]+)/?)?)))
        z
        (?P<gitlab>(?P<gitlab_base>{}/
            (?P<gitlab_user_repo>(?P<gitlab_user>{})(?:/(?P<gitlab_repo>[^/]+))?)))
        c                 j   t           |          }t          j        |                                                    d                    }|rJt
                              |          }|r.|                    d          dz   |                    d          z   }|d                             ||d                   S r4   )	USER_LINK_TEMPLATESr7   r8   r9   r:   r;   r<   r=   r>   r?   s         r/   create_user_link_patternrJ      rG   r1   z"https://bitbucket.org"z(?xi)
    ^(?:
        (?P<twitter>(?P<twitter_base>https://(?:w{{3}}\.)?twitter\.com/(?P<twitter_user>{}))) |
        (?P<x>(?P<x_base>https://(?:w{{3}}\.)?x\.com/(?P<x_user>{})))
    )/?$
    r'   r&   GitLabz{}z{}/{{}}/{{}}/-/issues/{{}}z"{}/{{}}/{{}}/-/merge_requests/{{}}z{}/{{}}/{{}}/-/commit/{{}}z"{}/{{}}/{{}}/-/compare/{{}}...{{}}   )	r@   typeurluser_patternissuer   commitcompare	hash_size	Bitbucketz{}/{{}}/{{}}/issues/{{}}z{}/{{}}/{{}}/pull-requests/{{}}z {}/{{}}/{{}}/commits/commit/{{}}z-{}/{{}}/{{}}/branches/commits/{{}}..{{}}#diff   GitHubz{}/{{}}/{{}}/pull/{{}}z{}/{{}}/{{}}/discussions/{{}}z{}/{{}}/{{}}/commit/{{}}z {}/{{}}/{{}}/compare/{{}}...{{}})
r@   rM   rN   rO   rP   r   discussrQ   rR   rS   Twitter)r@   rM   rN   rO   X)r%   r#   r$   r&   r'   c                     t           |                                          }dD ]J}||vr||                             |                                                    d                    ||<   K|S )z+Create the provider with the provided host.)rN   rP   r   rQ   rR   rW   r5   )PROVIDER_TEMPLATEScopyr>   r9   r:   )r@   rA   entrykeys       r/   create_providerr_   8  sq     x(--//EG A Ae3Z&&tzz||':':3'?'?@@c

Lr1   zhttps://twitter.comzhttps://x.com)r&   r'   r%   r#   r$   c                       e Zd ZdZd ZdS )_MagiclinkShorthandPatternzBase shorthand link class.c	                     || _         || _        || _        || _        || _        || j        v r|nd| _        t          j        | ||           dS )Initialize. N)userr   labels	normalizeprovider_infor@   r	   __init__)	selfpatternmdre   r   r@   rf   rg   rh   s	            r/   ri   z#_MagiclinkShorthandPattern.__init__O  s[     		"*$,0B$B$B w33333r1   N)__name__
__module____qualname____doc__ri    r1   r/   ra   ra   L  s)        $$	4 	4 	4 	4 	4r1   ra   c                   $    e Zd ZdZd Zd Zd ZdS )_MagiclinkReferencePatternzfConvert #1, repo#1, user/repo#1, !1, repo!1, user/repo!1, hash, repo@hash, or user/repo@hash to links.c           
         |dd         }|dd         }|dk    r3| j         |         d         }| j                            dd          }	d}
|}n|dk    r<| j         |         d         }| j                            dd	          }	d
}
| j        rdn|}n[| j         |         d         dk    rB|dk    r<| j         |         d         }| j                            dd          }	d}
| j        rdn|}ndS | j        rt          j        | |           |_        nI| j        r t          j        | | |           |_        n"t          j        | d| | |           |_        |	                    d|
                    |||                     |	                    dd| d|
            |	                    dd
                    | j         |         d         |	|||                     dS )zProcess issues.Nr   #rP   Issuemagiclink-issue!r   Pull Requestmagiclink-pullrM   r$   ?rW   
Discussionmagiclink-discussionFr5   hrefclassmagiclink magiclink- titlez{} {}: {}/{} #{}r@   T)rh   rf   getrg   my_repomd_utilAtomicStringtextmy_usersetr>   )rj   elr@   re   r   rP   
issue_typeissue_value
issue_linkissue_label
class_nameicons               r/   process_issuesz)_MagiclinkReferencePattern.process_issues^  s(    2A2Y
ABBi+H5g>J+//'7;;K*JDD3+H5f=J+//&.AAK)J.833jDD)&1X==*PSBSBS+H5i@J+//)\BBK/J.833jDD5< 	P*d+AK+A+ABBBGG\ 	P*d+GD+G++G+GHHBGG*d+N+NT+N4+N+N+NOOBG
vz((t[AABBB
wFxFF*FFGGG
%%"8,Z8 		
 		
 		
 tr1   c           
         |d| j         |         d                  }| j        r|}n| j        r| d| }n
| d| d| }|                    d| j         |         d                             |||                     t          j        |          |_        |                    dd| d	           |                    d
d                    | j         |         d         | j        	                    dd          |||                     dS )Process commit.r   rS   @r5   r~   rQ   r   r   z magiclink-commitr   z{} {}: {}/{}@{}r@   CommitN
rh   r   r   r   r>   r   r   r   rf   r   )rj   r   r@   re   r   rQ   hash_refr   s           r/   process_commitz)_MagiclinkReferencePattern.process_commit  s3    !D.x8EEF< 	/DD\ 	/''X''DD..T..H..D
vt)(3H=DDT4QWXXYYY&t,,
wJxJJJKKK
$$"8,Z8(33 		
 		
 		
 		
 		
r1   c                 ^   |d| j         |         d                  }|d| j         |         d                  }| j        r| d| }	n| j        r| d| d| }	n| d| d| d| }	|                    d| j         |         d                             ||||                     t          j        |	          |_        |                    dd	| d
           |                    dd                    | j         |         d         | j        	                    dd          ||||                     dS )r   r   rS   ...r   r5   r~   rR   r   r   z magiclink-comparer   z{} {}: {}/{}@{}...{}r@   CompareNr   )
rj   r   r@   re   r   commit1commit2	hash_ref1	hash_ref2r   s
             r/   process_comparez*_MagiclinkReferencePattern.process_compare  ss    Ad0:;GGH	Ad0:;GGH	< 	>//I//DD\ 	>66Y66966DD==T==I==)==D
vt)(3I>EEdDRY[bccddd&t,,
wKxKKKLLL
"))"8,Z8	955 
	
 
	
 
	
 
	
 
	
r1   N)rm   rn   ro   rp   r   r   r   rq   r1   r/   rs   rs   [  sH        pp+ + +Z
 
 
2
 
 
 
 
r1   rs   c                       e 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 Zd Zd Zd ZdS )MagicShortenerTreeprocessorzHTree processor that finds repo issue and commit links and shortens them.r   r   r
               c                     || _         || _        || _        || _        || _        || _        || _        |
| _        || _        dddd| _	        |	| _
        t          j        | |           dS )rc   rV   rT   rK   r2   N)baserepo_shortnersocial_shortenercustom_shortners	base_userrepo_labelsrg   r@   rh   rf   excludesr   ri   )rj   rl   base_urlbase_user_urlrf   rg   r   r   r   r   r@   rh   s               r/   ri   z$MagicShortenerTreeprocessor.__init__  s      	* 0 0&!" *$
 

 !tR(((((r1   c           
          |}t          j        |          |_        d|vr|                    d           |                    dd                    || j                            dd          |                     dS )zShorten repo link.zmagiclink-repositoryr   	{} {}: {}
repository
RepositoryNr   r   r   appendr   r>   r   r   )rj   linkr   label	user_repor   s         r/   shorten_repoz(MagicShortenerTreeprocessor.shorten_repo  s     (..	!334555t'++L,GG 	
 	
 	
 	
 	
r1   c           
          t          j        d|           |_        d|vr|                    d           |                    dd                    || j                            dd          |                     dS )zShorten user link.r   zmagiclink-mentionr   r   metionUserNr   )rj   r   r   r   r   s        r/   shorten_userz(MagicShortenerTreeprocessor.shorten_user  s     (Y99	j001222t'++Hf==y 	
 	
 	
 	
 	
r1   c                    | j                             dd          }| j        r$|d         d|          d|d         d|          }nz| j        rMd                    |                    d          d         |d         d|         |d         d|                   }n&| d|d         d|          d|d         d|          }t          j        |          |_        d	|vr|	                    d	           |
                    d
d                    |||                    d          |d         d|         |d         d|                              dS )zShorten diff/compare links.rR   r   r   r   r   z
{}@{}...{}r5   r   zmagiclink-comparer   z{} {}: {}@{}...{}Nr   r   r   r   r>   splitr   r   r   r   r   r:   	rj   r   r   r   r   valuerS   
repo_labelr   s	            r/   shorten_diffz(MagicShortenerTreeprocessor.shorten_diff  s    %)))Y??
< 	TAhq{+GGa90EGGDD\ 	T&&ys';';A'>a9@UW\]^W_`abk`kWlmmDDSS%(1Y;"7SSE!HQy[<QSSD(..	j001222&&z9#3#3C#8#8%(1Y;:OQVWXQYZ[\eZeQf 	
 	
 	
 	
 	
r1   c                    | j                             dd          }| j        r|d|         }nN| j        r8d                    |                    d          d         |d|                   }n| d|d|          }t          j        |          |_        d|vr|	                    d           |
                    d	d
                    |||                    d          |d|                              dS )zShorten commit link.rQ   r   r   z{}@{}r5   r   r   zmagiclink-commitr   z{} {}: {}@{}Nr   r   s	            r/   shorten_commitz*MagicShortenerTreeprocessor.shorten_commit"  s    %))(H==
< 	79%DD\ 	7>>)//#"6"6q"959;MNNDD66%)"466D(..	Z//0111!!%Y5E5Ec5J5JERST]R]L^__	
 	
 	
 	
 	
r1   c           
         | j         |         d         }|| j        k    r7| j                            dd          }	d}
d|vr|                    d           n|| j        k    r@| j                            dd          }	| j        rdnd}
d	|vr|                    d	           nP|d
k    rJ|| j        k    r?| j                            dd          }	| j        rdnd}
d|vr|                    d           | j        rt          j
        |
 |           |_        nn| j        rHt          j
        d                    |                    d          d         |
|                    |_        nt          j
        | |
 |           |_        |                    dd                    ||	|                    d          |                     dS )zShorten issue/pull link.rM   rP   rv   ru   rw   r   ry   rx   rz   r$   rW   r|   r{   r}   z{}{}{}r5   r   r   z{} {}: {} #{}N)rh   ISSUEr   r   r   PULLrg   DISCUSSr   r   r   r   r   r>   r   r   r:   )rj   r@   r   r   r   r   r   	link_typer.   r   r   s              r/   shorten_issuez)MagicShortenerTreeprocessor.shorten_issue7  s    *84V<
"")--gw??JD 
22!!"3444$)##)--fnEEJ.133cDz11!!"2333h&&9+D+D)--iFFJ.133cD%Z77!!"8999< 	J,-=e-=-=>>DII\ 	J,X__Y__S=Q=QRS=TVZ\a-b-bccDII,	-H4-H-H-HIIDI/00
IDTDTUXDYDY[`aabbbbbr1   c           	         | j         |         d         }d| }|                    dd          }	|	r|	                    d          ng }
d|
vr|
                    d           ||
vr|
                    |           || j        u r|                     ||
||||           n?|| j        u r|                     ||
||||           n|                     |||
||||           |	                    dd
                    |
                     dS zShorten URL.r@   z
magiclink-r   rd   r   	magiclinkN)rh   r   r   r   DIFFr   COMMITr   r   r   join)rj   r   r@   r   r   r   rS   r   
prov_class
class_attrr   s              r/   shorten_issue_commitz0MagicShortenerTreeprocessor.shorten_issue_commitU  s6    "8,Z8,(,,
XXgr**
.8@Z%%c***b
j((k***Z''j))) 	!!dJy%SSSS$+%%j%E9UUUUxz5)UT]^^^#((:../////r1   c                    | j         |         d         }d| }|                    dd          }|r|                    d          ng }d|vr|                    d           ||vr|                    |           || j        u r|                     ||||           n|                     ||||           |                    dd                    |                     dS r   )	rh   r   r   r   REPOr   r   r   r   )	rj   r   r@   r   r   r   r   r   r   s	            r/   shorten_user_repoz-MagicShortenerTreeprocessor.shorten_user_repol  s     "8,Z8,(,,
XXgr**
.8@Z%%c***b
j((k***Z''j))) 	!!dJyAAAAdJyAAA#((:../////r1   c                     |                     d          rd}n/|                     d          rd}n|                     d          rd}|S )zGet the provider and hash size.r$   r#   r%   r=   rj   r<   r@   s      r/   get_provider_typez-MagicShortenerTreeprocessor.get_provider_type  sX     ;;x   	 HH[[%% 	 "HH[["" 	 Hr1   c                 d    |                     d          rd}n|                     d          rd}|S )zGet social provider.r&   r'   r   r   s      r/   get_social_providerz/MagicShortenerTreeprocessor.get_social_provider  s=     ;;y!! 	 HH[[ 	Hr1   c                    	 |                     |dz             8|                     |dz             |                     |dz             f}| j        }n|                     |dz              |                     |dz             }| j        }n|                     |dz              |                     |dz             }| j        }n]|dk    r8|                     |dz              |                     |dz             }| j        }n|                     |dz             }| j        }nR# t          $ rE d	}	 |                     |d
z             d}| j        }d}n# t          $ r Y nw xY w|s	d}| j        }Y nw xY w||fS )zGet the link type._diff1N_diff2_commit_pullr$   _discuss_issueF_repoT)	r=   r   r   r   r   r   
IndexErrorr   USER)rj   r@   r<   r   r   founds         r/   get_typez$MagicShortenerTreeprocessor.get_type  s   	&{{8h.//;X%8995;;xRZGZ;[;[\ I		X	122>Hy$899 K		X/00<Hw$677 I		X%%%++h6K*L*L*XHz$9:: L		Hx$788 J	 	& 	& 	&E;;x'122> E $	I E    & I		& is6   DD   E/-#EE/
EE/EE/.E/c                 X    | j         o#|                    |dz             dz   | j         k    S )z2Check if link is from our specified user and repo._baser5   )r   r=   rj   r.   r<   s      r/   
is_my_repoz&MagicShortenerTreeprocessor.is_my_repo  s.     yTU[[)@AACG49TTr1   c                 p    | j         o/|                    |dz                                 | j                   S )z)Check if link is from our specified user.r   )r   r=   
startswithr   s      r/   
is_my_userz&MagicShortenerTreeprocessor.is_my_user  s4     ~a%++mg.E"F"F"Q"QRVR`"a"aar1   c                     |                     |dz             }|                                | j                            |t	                                v S )z Check if user has been excluded._user)r=   r9   r   r   r   )rj   r.   r@   r<   re   s        r/   excludedz$MagicShortenerTreeprocessor.excluded  sC     {{=7233zz||t}00355AAAAr1   c                 
   | j         d         | _        |                    d          }|D ]}t          t	          |                    }|j                            d          }|j                            dd          }|j        }d}|r|j        d= |s|sl||k    s|r| j        rd|z   |k    r| j        rt          
                    |          }	|	r|                     |	          }
|
}|                     |
|	          | _        | j        p|                     |
|	          | _        |                     |
|	          \  }}d}|                     |
||	          sA|                     ||||	                    |
d	z             || j        |         d
                    |s| j        rt*          
                    |          }	|	r|                     |	          }
|
}|                     |
|	          | _        | j        p|                     |
|	          | _        |                     |
|	          \  }}d}|                     |
||	          s.|                     ||||	                    |
d	z                        |s| j        r| j                                        D ]\  }}|d         
                    |          }	|	r|}| j        |         d         }
|                     |
|	          | _        | j        p|                     |
|	          | _        |                     |
|	          \  }}d}|                     |
||	          sA|                     ||||	                    |
d	z             || j        |         d
                    |s|d         
                    |          }	|	r|}| j        |         d         }
|                     |
|	          | _        | j        p|                     |
|	          | _        |                     |
|	          \  }}d}|                     |
||	          s.|                     ||||	                    |
d	z                        |s| j        rt4          
                    |          }	|	r|                     |	          }|dk    rt9          d           |                     ||	          | _        | j        p|                     ||	          | _        |                     ||	          \  }}|                     |||	          s.|                     ||||	                    |dz                        |S )z%Shorten popular git repository links.hide_protocolar   r~   rd   Fzhttps://T
_user_reporS   r   rM   re   r&   IThe 'twitter' social provider has been deprecated, please use 'x' insteadr   )configr   iterlenlistattribr   r   r   RE_REPO_LINKr<   r   r   r   r   r   r   r   r   r=   rh   RE_USER_REPO_LINKr   r   itemsr   RE_SOCIAL_LINKr   r   )rj   rootlinksr   	has_childis_magicr~   r   r   rE   r.   r@   r   r   customr]   s                   r/   runzMagicShortenerTreeprocessor.run  s    "[9		# p	 p	DDJJI{{33H;??62..D9DE -K,    d.@jSWFW\`E`E`% $**400A (,(>(>q(A(A#0'+}a'H'H'+|'Xt}VW7X7X+/==+J+J(y $  $}}]HaHH  55 $ ( ) !(D E E % $ 28 <[ I    !3 )//55A (,(>(>q(A(A#0'+}a'H'H'+|'Xt}VW7X7X+/==+J+J(y $#}}]HaHH  22 $ ( ) !(D E E	    &&!6 &&)-)>)D)D)F)F %& %&!&M//55 "'-H,0,>v,Fv,NM+/??=!+L+LDL+/<+\4??=Z[;\;\DL/3}}]A/N/N,E9$(E $(==!#L#L " $ 9 9$($,$-$%GGML,H$I$I$)$($6x$@$M!" !" !"  % & %f 3 3D 9 9A  &+1040B60J60R/3}a/P/P/3|/`t}^_?`?`37==PQ3R3R 0y(,'+}}]Ha'P'P !&$($:$:(,(0(1()0L(M(M	%& %& %&  !6 &,,T22A #'#;#;A#>#>#y00+,wxxx'+x'C'C'+|'StxQR7S7S+/==1+E+E(y#}}XxCC  22 $ ( ) !7(: ; ;	   r1   N)rm   rn   ro   rp   r   r   r   r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  rq   r1   r/   r   r     s3       RR EDFGDDD) ) )B
 
 
 
 
 

 
 
,
 
 
*c c c<0 0 0.0 0 0*
 
 
  !  !  ! FU U Ub b b
B B Bw w w w wr1   r   c                       e Zd ZdZdZd ZdS )MagiclinkPatternz+Convert html, ftp links to clickable links.r   c                    t          j        d          }t          j        |                    d                    |_        |                    d          r)d                    |                    d                    }nc|                    d          }| j        d         rAt          j        |j        |j                            d          dz   d                   |_        |	                    d	| 
                    |                                                     | j                            d
d          r(|	                    dt          t                               ||                    d          |                    d          fS )zHandle URL matches.r   r   rB   z	http://{}r   ://r   Nr~   repo_url_shortenerFr   r   )etreeElementr   r   r=   r   r>   r   findr   unescapestripr   str
MAGIC_LINKstartend)rj   rE   datar   r~   s        r/   handleMatchzMagiclinkPattern.handleMatchJ  s.    ]3&qwwv77775>> 	R%%aggfoo66DD776??D{?+ R!.rwrw||E7J7JQ7N7O7O/PQQ
vt}}TZZ\\22333;??/77 	1FF;J0001771::quuQxx''r1   Nrm   rn   ro   rp   ANCESTOR_EXCLUDESr  rq   r1   r/   r  r  E  s/        55( ( ( ( (r1   r  c                       e Zd ZdZd ZdS )MagiclinkAutoPatternz@Return a link Element given an auto link `<http://example/com>`.c                 r   t          j        d          }|                    d|                     |                    d                               t          j        |                    d                    |_        | j        d         rAt          j        |j        |j        	                    d          dz   d                   |_        | j        
                    dd	          r(|                    d
t          t                               ||                    d          |                    d          fS )z(Return link optionally without protocol.r   r~   r   r   r  r   Nr  Fr   r   )r  r  r   r  r=   r   r   r   r   r  r   r  MAGIC_AUTO_LINKr  r  )rj   rE   r  r   s       r/   r  z MagiclinkAutoPattern.handleMatch`  s     ]3
vt}}QWWQZZ00111&qwwqzz22;' 	N*2727<<3F3F3J3K3K+LMMBG;??/77 	6FF;O 4 45551771::quuQxx''r1   N)rm   rn   ro   rp   r  rq   r1   r/   r  r  ]  s)        JJ( ( ( ( (r1   r  c                   "    e Zd ZdZdZd Zd ZdS )MagiclinkMailPatternz(Convert emails to clickable email links.r  c                 (    t           j         d|ddS )z=Return entity definition by code, or the code if not defined.ru   d;)r   AMP_SUBSTITUTE)rj   codes     r/   email_encodez!MagiclinkMailPattern.email_encodet  s    (44444444r1   c                     t          j        d          }                     |                    d                    }d| }t	          j        d                     fd|D                                 |_        |                    dd                    d |D                                  ||	                    d          |
                    d          fS )	Handle email link patterns.r   mailzmailto:rd   c                 T    g | ]$}                     t          |                    %S rq   )r(  ord).0crj   s     r/   
<listcomp>z4MagiclinkMailPattern.handleMatch.<locals>.<listcomp>~  s-    /Y/Y/Ya0A0A#a&&0I0I/Y/Y/Yr1   r~   c                 P    g | ]#}t           j        d t          |          ddz   $S )ru   r$  r%  )r   r&  r-  )r.  r/  s     r/   r0  z4MagiclinkMailPattern.handleMatch.<locals>.<listcomp>  s0    WWWQ 6SVV HWWWr1   r   )r  r  r  r=   r   r   r   r   r   r  r  )rj   rE   r  r   emailr~   s   `     r/   r  z MagiclinkMailPattern.handleMatchx  s     ]3aggfoo..   &rww/Y/Y/Y/YSX/Y/Y/Y'Z'Z[[
vrwwWWRVWWWXXYYY1771::quuQxx''r1   N)rm   rn   ro   rp   r  r(  r  rq   r1   r/   r"  r"  o  s>        225 5 5( ( ( ( (r1   r"  c                       e Zd ZdZdZd ZdS )MagiclinkMentionPatternzConvert @mention to links.r  c           
          |                     d          dd         }|                    d          }t          |          dk    r|d         }|d         }n| j        }|d         }|dk    rt	          d           t          j        d          }|                    d	d
                    | j	        |         d         |                     |                    dd                    | j	        |         d         | j
                            dd          |                     |                    dd| d           t          j        d|           |_        ||                    d          |                    d          fS )r*  mentionr   Nr)   r   r&   r   r   r~   z{}/{}rN   r   r   r@   r   r   r   z magiclink-mentionr   )r=   r   r   r@   r   r  r  r   r>   rh   rf   r   r   r   r   r  r  )rj   rE   r  r   partsr@   r6  r   s           r/   r  z#MagiclinkMentionPattern.handleMatch  sa    wwy!!!""%

3u::>>QxHAhGG}HAhGy  ghhh]3
vw~~d&8&B5&I7SSTTT
t1(;JGYbdjIkIkmtuu	
 	
 	
 	wKxKKKLLL&}7}}551771::quuQxx''r1   Nr  rq   r1   r/   r4  r4    s/        $$( ( ( ( (r1   r4  c                       e Zd ZdZdZd ZdS )MagiclinkRepositoryPatternzConvert @user/repo to links.r  c           
         |                     d          dd         }|                    d          }t          |          dk    r|d         }|d         }n| j        }|d         }|                     d          }t	          j        d          }|                    dd	                    | j        |         d
         ||                     |                    dd                    | j        |         d         | j	        
                    dd          ||                     |                    dd| d           t          j        | d|           |_        ||                    d          |                    d          fS )r*  r6  r   Nr)   r   mention_repor   r~   z{}/{}/{}rN   r   z{} {}: {}/{}r@   r   r   r   r   z magiclink-repositoryr5   )r=   r   r   r@   r  r  r   r>   rh   rf   r   r   r   r   r  r  )	rj   rE   r  r   r7  r@   re   r   r   s	            r/   r  z&MagiclinkRepositoryPattern.handleMatch  sl    wwy!!!""%

3u::>>QxH8DD}H8Dww~&&]3
vz(();H)Ee)LdTXYYZZZ
!!"8,Z8$+//,Xd:e:egkmq 	
 	
 	
 	wNxNNNOOO&$'7'7'7'7881771::quuQxx''r1   Nr  rq   r1   r/   r9  r9    s/        &&( ( ( ( (r1   r9  c                       e Zd ZdZdZd ZdS )MagiclinkExternalRefsPatternzXConvert repo#1, user/repo#1, repo!1, user/repo!1, repo@hash, or user/repo@hash to links.r  c                    |                     d          }|                     d          }|r|                     d          dd         n|                     d          }|r|                     d          nd}|                     d          }|                     d          }|s| j        }|                    d          }	t          |	          dk    r|	d	         }
|	d         }n| j        }
|sd
S || j        k    o
|
| j        k    | _        | j        o
|| j        k    | _        t          j	        d          }|r| 
                    ||
||||           ns|r|                     ||
|||           nW|                     ||
|||          s>|                     d	          |                    d	          |                    d	          fS ||                    d	          |                    d	          fS )r*  rQ   diffr   NrP   r   re   r)   r   NNNr   )r=   re   r   r   r@   r   r   r   r  r  r   r   r   r  r  )rj   rE   r  	is_commitis_diffr   value2r   re   r7  r@   r   s               r/   r  z(MagiclinkExternalRefsPattern.handleMatch  s    GGH%%	''&//)2H!!!""%%8H8H$+5wwvwwv 	9D

3u::>>QxH8DD}H  	$##ty(FX-F|9	(9]3 	8  XtT5&IIII 	8HdD%@@@@&&r8T4GG 8wwqzz1771::quuQxx771771::quuQxx''r1   Nr  rq   r1   r/   r=  r=    s/        bb#( #( #( #( #(r1   r=  c                       e Zd ZdZdZd ZdS )MagiclinkInternalRefsPatternz Convert #1, !1, and commit_hash.r  c                    | j         r| j        sdS |                    d          }|                    d          }|r|                    d          n|                    d          }|r|                    d          nd}| j        }| j         }| j        }	d| _        d| _        t          j        d          }
|r|                     |
|	||||           ns|r| 	                    |
|	|||           nW| 
                    |
|	|||          s>|                    d          |                    d          |                    d          fS |
|                    d          |                    d          fS )	r*  r@  rQ   r?  rP   NTr   r   )re   r   r=   r@   r   r   r  r  r   r   r   r  r  )rj   rE   r  rA  rB  r   rC  r   re   r@   r   s              r/   r  z(MagiclinkInternalRefsPattern.handleMatch  sh    y 	$	 	$##GGH%%	''&//%.D!!!AGGG4D4D$+5yy=]3 	8  XtT5&IIII 	8HdD%@@@@&&r8T4GG 8wwqzz1771::quuQxx771771::quuQxx''r1   Nr  rq   r1   r/   rE  rE    s/        **( ( ( ( (r1   rE  c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )MagiclinkExtensionzCAdd auto link and link transformation extensions to Markdown class.c                     ddgddgddgg dg dg dg dg dd	d
gddgddgddgi dgddgddgddgi dgd| _          t                      j        |i | dS )rc   Fz^If 'True', links are displayed without the initial ftp://, http:// or https://- Default: FalsezDIf 'True' repo commit and issue links are shortened - Default: Falsez5If 'True' social links are shortened - Default: Falser   r   r   r   r"   z4A list of user names to exclude from URL shortening.zFIf 'True' repo shorthand syntax is converted to links - Default: FalsezHIf 'True' social shorthand syntax is converted to links - Default: Falser$   zKThe base provider to use (github, gitlab, bitbucket, x) - Default: "github"zTitle labels - Default: {}zLNormalize issue, pull, and discussions symbols all to use # - Default: Falserd   z'The base user name to use - Default: ""z"The base repo to use - Default: ""z&Custom repositories hosts - Default {})r   r  social_url_shortenershortener_user_excluderepo_url_shorthandsocial_url_shorthandr@   rf   normalize_issue_symbolsre   r   r  N)r   superri   )rj   argskwargs	__class__s      r/   ri   zMagiclinkExtension.__init__  s   
 # V#
 G% "V!U!UtttOOOEEE???  G	' X#
 Z%
 ]
 ,
 ^(
 9
 4
 8i8
 8
r 	$)&)))))r1   c                 D   t          t          |          }||_        |j                            |dd           t          t          |          }||_        |j                            |dd           |j                            t          t          |          dd           dS )zSetup auto links.autolinkx   z
magic-linkU   z
magic-mailg9U@N)	r  RE_AUTOLINKr   inlinePatternsregisterr  RE_LINKr"  RE_MAIL)rj   rl   r   auto_link_patternlink_patterns        r/   setup_autolinksz"MagiclinkExtension.setup_autolinksP  s     1bAA#) 
""#4j#FFF'44$
""<rBBB
""#7#D#DlTXYYYYYr1   c           
         dg}t          j        ||           | j        rt          | j        || j        | j        | j        | j        | j	        | j
                  }|j                            |dd           | j        smt          t                              | j                  || j        | j        | j        | j        | j	        | j
                  }|j                            |dd           t"                              d                    | j                            }t)          ||| j        | j        | j        | j        | j	        | j
                  }|j                            |dd           t)          t*                              | j                  || j        | j        | j        | j        | j	        | j
                  }|j                            |d	d
           | j        r t-          | j        || j        | j        | j        | j        | j	        | j
                  }|j                            |dd           | j        st-          t0                              | j                  || j        | j        | j        | j        | j	        | j
                  }|j                            |dd           t3          t4          || j        | j        | j        | j        | j	        | j
                  }	|j                            |	dd           dS dS dS )zSetup shorthand.r   zmagic-repo-ext-mentiongS@zmagic-repo-int-mentiong33333S@|zmagic-ext-mentiongS@zmagic-int-mentiongfffffS@zmagic-ext-refsg     S@zmagic-int-refsgS@zmagic-int-micro-refsg33333S@N)r   escape_chars	git_shortr9  re_git_ext_repo_mentionsre   r   r@   rf   rg   rh   rX  rY  	is_socialRE_GIT_INT_REPO_MENTIONSr>   int_mentionsRE_ALL_EXT_MENTIONSr   ext_mentionsr4  RE_INT_MENTIONSr=  re_git_ext_refsRE_GIT_INT_EXT_REFSrE  RE_GIT_INT_MICRO_REFS)
rj   rl   ra  git_ext_repogit_int_repork   git_mentiongit_ext_refsgit_int_refsgit_int_micro_refss
             r/   setup_shorthandz"MagiclinkExtension.setup_shorthand^  s    u"l+++ > 	Y5-		"	 	L &&|5MtTTT> Y9,33D4EFFIIMKN&	  	  !**<9QSWXXX &,,SXXd6G-H-HII-IIMKN	
 	
 	"";0CTJJJ-""4#455IIMKN	
 	
 	"";0CTJJJ > "	]7$		"	 	L &&|5EtLLL> ];'..t/@AAIIMKN&	  	  !**<9I4PPP%A)IIMKN&	& 	&" !**+=?UW[\\\\\E"	] "	]] ]r1   c                     t          || j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
                  }||_        |j                            |dd           dS )zSetup shortener.zmagic-repo-shortenerg#@N)r   r   r   rf   rg   r   r   r   shortener_exclusionsr@   rh   r   treeprocessorsrY  )rj   rl   r   	shorteners       r/   setup_shortenerz"MagiclinkExtension.setup_shortener  sz     0MKN!!%M
 
	 "	
""9.DcJJJJJr1   c                    d}d}| j         r||fS | j        ro| j        rhd                    | j        | j                 d         | j        | j                  }d                    | j        | j                 d         | j                  }||fS )zGet base URLs.rd   z	{}/{}/{}/rN   z{}/{}/)rd  re   r   r>   rh   r@   )rj   r   r   r   s       r/   get_base_urlsz MagiclinkExtension.get_base_urls  s     > 	+]**9 	a 	a"))$*<T]*KE*RTXT]_c_hiiH$OOD,>t},Me,TVZV_``M&&r1   c                 R
   |                                  }|                    dd          | _        |                    dd          | _        |                    dd          | _        |                    di           | _        |                    dd          | _        | j        t          v | _        |                    d	d          | _	        |                    d
d          | _
        |                    dd          | _        |                    dd          | _        d t                                          D             | _        t                                           | _        |                    di           }|                    di           }i | _        t(          t*          t,          g}|                                D ]x\  }}t.                              |          st3          d| d          || j        vr;t5          |d         |d                   | j        |<   |d         | j        |         d<   t7          j        d                    t=          |d         |d         |                    dd                                        t7          j        d                    t?          |d         |d         |                    dd                                        d| j        |<   ||vr|                    |d         g           ||<   |                     tC          ||d                              ztD                              d#                    |                    | _$        tJ                              d#                    |                    | _&        |                    di                                           D ]D\  }}	|| j        v r6tO          |	tP          tR          tT          f          rd |	D             | j        |<   E| j        | j        vrd| _        | +                    ||           | j	        s| j
        rg | _,        | j	        r| j,        -                    |           | j
        r>| j,                             t\                     | j,                             t^                     | j        | j                 d         | _0        | 1                    |           | j        s| j        r:| 2                    |          \  | _3        | _4        | 5                    ||           dS dS )z;Add support for turning html links and emails to link tags.re   rd   r   r@   r$   rf   rN  FrL  rM  r  rJ  c                 4    i | ]\  }}|t          |          S rq   )r   )r.  kvs      r/   
<dictcomp>z5MagiclinkExtension.extendMarkdown.<locals>.<dictcomp>  s$    $T$T$T41aQA$T$T$Tr1   r  rK  zName 'zB' not allowed, provider name must contain only letters and numbersrM   rA   r   z(?xi)^{}/?$rB   T)r   re   r`  c                 6    h | ]}|                                 S rq   )r9   )r.  r'   s     r/   	<setcomp>z4MagiclinkExtension.extendMarkdown.<locals>.<setcomp>  s     1K1K1K!''))1K1K1Kr1   rO   N)6
getConfigsr   re   r   r@   rf   rg   SOCIAL_PROVIDERSrd  rb  social_shortr   r   DEFAULT_EXCLUDESr  ru  PROVIDER_INFOr\   rh   r   RE_GITHUB_EXT_MENTIONSRE_GITLAB_EXT_MENTIONSRE_BITBUCKET_EXT_MENTIONSRE_CUSTOM_NAMEr<   
ValueErrorr_   r7   compiler>   rF   rJ   r   r0   RE_GIT_EXT_REPO_MENTIONSr   rc  RE_GIT_EXT_REFSrj  
isinstancer   tupler   r^  rh  extendRE_X_EXT_MENTIONSRE_TWITTER_EXT_MENTIONSrf  rs  rz  r   r   rx  )
rj   rl   r   custom_providerr   external_usersr  r]   r^   r   s
             r/   extendMarkdownz!MagiclinkExtension.extendMarkdown  s    "" JJvr**	JJvr**	

:x88jj2..$=uEE*::$8%@@"JJ'=uEE#ZZ(<eDD &

+A5 I I$T$T;K;Q;Q;S;S$T$T$T!*//11 **Xr22::6;; "02HJcd,2244 	R 	RMFE!''//  gVggg   T///-<U6]ERXM-Z-Z"6*9>w"6*:6J&--4U6]E&MSXS\S\]bdhSiSijj  
 J&--4U6]E&MSXS\S\]bdhSiSijj  1 1%f- ))'/||E&M2'F'FHV$%%&9&%-&P&PQQQ(@(G(GQ_H`H`(a(a%.55chh~6N6NOO **%=rBBHHJJ 	L 	LJCd(((ZeS?Q-R-R(1K1KU1K1K1K)#. = 222$DMR(((> 		%T. 		% "D~ 9!((888  B!(():;;;!(()@AAA $ 24= A. QD  $$$  	-!6 	-040B0B60J0J-DM4-  V,,,,,	- 	-r1   )rm   rn   ro   rp   ri   r^  rs  rx  rz  r  __classcell__)rR  s   @r/   rH  rH    s        MM<* <* <* <* <*|Z Z Z^] ^] ^]@K K K.' ' 'J- J- J- J- J- J- J-r1   rH  c                      t          | i |S )zReturn extension.)rH  )rP  rQ  s     r/   makeExtensionr  1  s     t.v...r1   )T)Crp   markdownr   markdown.treeprocessorsr   r   r   r   xml.etree.ElementTreer  ElementTreerd   r7   markdown.inlinepatternsr   r	   r  r   r  r[  rZ  rW  r  r  RE_TWITTER_USER	RE_X_USERr*   r+   r,   rg  ri  r0   r  r  r  r  r  r  re  r  rk  rl  r;   r6   rF   r>   r   rI   rJ   r  r  r  r[   r_   r  ra   rs   r   r  r  r"  r4  r9  r=  rE  rH  r  rq   r1   r/   <module>r     s   2       1 1 1 1 1 1 $ $ $ $ $ $ ! ! ! ! ! ! % % % % % % % % %       				 H H H H H H H H
 FEEddd???555	/	/	/  	 /-.. 	?F)   5. . . 8o77 %)%% ,,Xx@@ ,,Xx@@ //[II      
2	3	3	; 		S 	
	; 	
1# # L	5 	5 	5 	5 rz ''  +?@@  .EFF  +?@@  	 		 		 	  2	5 	5 	5 	5 BJ ''  +?@@  .GHH  +?@@    
 		**  # 
 &-4.7
 
  )+14B
 
 &+(2,5  '	  !	
 
S/ / d   y*?@@	o	.	.oh(<== .EFFoh(<== 4 4 4 4 4 4 4 4b
 b
 b
 b
 b
!; b
 b
 b
JB B B B B- B B BJ( ( ( ( (* ( ( (0( ( ( ( (? ( ( ($( ( ( ( (? ( ( ((( ( ( ( (8 ( ( (@( ( ( ( (!; ( ( (>(( (( (( (( ((#= (( (( ((V( ( ( ( (#= ( ( (D_- _- _- _- _- _- _- _-D	/ / / / /r1   