
    ei                        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+did)Z, ej,                  d*j[                   e,dd+       e,dd,       e,dd-                  Z.d.efd/efd0efd(Z/did1Z0 ej,                  d*j[                   e0dd+       e0dd2       e0dd-                  Z1 ej,                  d3j[                  ee            Z2d4d5hZ3d6dd7ed8d9d:d;d<d=	d>dd7ed?d@dAdBdCd=	dDdd7ed?dEdFdGdHdCdI
dJd5d7edKdLd4d7edKdMZ4dN Z5 e5d5dO       e5d4dP       e5dd-       e5dd,       e5dd+      dQZ6 G dR dSe      Z7 G dT dUe7      Z8 G dV dWe      Z9 G dX dYe      Z: G dZ d[e      Z; G d\ d]e      Z< G d^ d_e7      Z= G d` dae7      Z> G db dce8      Z? G dd dee8      Z@ G df dge      ZAdh ZBy)ja  
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marketplace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                 d    |dk(  r|  dt          S |dk(  r|  dt         S |dk(  r|  dt         S y)z*Create external mentions by provider type.r$   :r%   r#   N)RE_GITHUB_USERRE_GITLAB_USERRE_BITBUCKET_USER)nameprovider_types     T/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/pymdownx/magiclink.pycreate_ext_mentionsr0   Y   sX      &.)**	(	"&.)**	+	%&+,-- 
&    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>[-._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>[-._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%   c                 &   t         |    }t        j                  |j                         j	                  d            }|r=t
        j                  |      }|r&|j                  d      dz   |j                  d      z   }|d   j                  ||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yy,,S12H
LL"wwqzN2QWWQZ?HA;h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                 &   t         |    }t        j                  |j                         j	                  d            }|r=t
        j                  |      }|r&|j                  d      dz   |j                  d      z   }|d   j                  ||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         |    j                         }dD ];  }||vr||   j                  |j                         j	                  d            ||<   = |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  sc     x(--/EG Ae3Z&&tzz|':':3'?@c
A Lr1   zhttps://twitter.comzhttps://x.com)r&   r'   r%   r#   r$   c                       e Zd ZdZd Zy)_MagiclinkShorthandPatternzBase shorthand link class.c	                     || _         || _        || _        || _        || _        || j                  v r|nd| _        t        j                  | ||       y)Initialize. N)userr   labels	normalizeprovider_infor@   r	   __init__)	selfpatternmdre   r   r@   rf   rg   rh   s	            r/   ri   z#_MagiclinkShorthandPattern.__init__O  sP     		"*$,0B0B$B  w3r1   N)__name__
__module____qualname____doc__ri    r1   r/   ra   ra   L  s
    $	4r1   ra   c                   "    e Zd ZdZd Zd Zd Zy)_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                  j                  dd      }	d}
|}n|dk(  rA| j                   |   d   }| j                  j                  dd	      }	d
}
| j                  rdn|}n\| j                   |   d   dk(  rF|dk(  rA| j                   |   d   }| j                  j                  dd      }	d}
| j                  rdn|}ny| j                  rt        j                  | |       |_        nP| j                  r!t        j                  | | |       |_        n#t        j                  | d| | |       |_        |j                  d|j                  |||             |j                  dd| d|
        |j                  ddj                  | j                   |   d   |	|||             y)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    2AY
ABi++H5g>J++//'7;K*JD3++H5f=J++//&.AK)J..3jD)&1X=*PSBS++H5i@J++//)\BK/J..3jD<<**dVK=+ABBG\\**dVD6++GHBG**dV1TF4&+NOBG
vz((t[AB
w.xj*FG
%%""8,Z8		
 r1   c           
         |d| j                   |   d    }| j                  r|}n| j                  r| d| }n
| d| d| }|j                  d| j                   |   d   j	                  |||             t        j                  |      |_        |j                  dd| d	       |j                  d
dj	                  | j                   |   d   | j                  j                  dd      |||             y)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  s     !D..x8EF<<D\\V1XJ'DV1TF!H:.D
vt))(3H=DDT4QWXY&&t,
w.xj8IJK
$$""8,Z8(3		
r1   c                 0   |d| j                   |   d    }|d| j                   |   d    }| j                  r| d| }	n$| j                  r| d| d| }	n| d| d| d| }	|j                  d| j                   |   d   j	                  ||||             t        j                  |	      |_        |j                  dd	| d
       |j                  ddj	                  | j                   |   d   | j                  j                  dd      ||||             y)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  s6    Ad00:;GH	Ad00:;GH	<<[I;/D\\V1YKs9+6DV1TF!I;c)=D
vt))(3I>EEdDRY[bcd&&t,
w.xj8JKL
"))""8,Z8	95
	
r1   N)rm   rn   ro   rp   r   r   r   rq   r1   r/   rs   rs   [  s    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y)MagicShortenerTreeprocessorzHTree processor that finds repo issue and commit links and shortens them.r   r   r
               c                     || _         || _        || _        || _        || _        || _        || _        |
| _        || _        dddd| _	        |	| _
        t        j                  | |       y)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__  ss      	* 0 0&!" *$

 !tR(r1   c           
          |}t        j                  |      |_        d|vr|j                  d       |j	                  ddj                  || j                  j                  dd      |             y)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  si     ((.	!345t''++L,G	
r1   c           
          t        j                  d|       |_        d|vr|j                  d       |j	                  ddj                  || j                  j                  dd      |             y)zShorten user link.r   zmagiclink-mentionr   r   metionUserNr   )rj   r   r   r   r   s        r/   shorten_userz(MagicShortenerTreeprocessor.shorten_user  sj     ((1YK9	j012t''++Hf=y	
r1   c                    | j                   j                  dd      }| j                  r|d   d|  d|d   d|  }nT| j                  r2dj	                  |j                  d      d   |d   d| |d   d|       }n| d|d   d|  d|d   d|  }t        j                  |      |_        d	|vr|j                  d	       |j                  d
dj	                  |||j                  d      |d   d| |d   d|              y)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  s4    %%)))Y?
<<Ahq+,Ca90E/FGD\\&&ys';A'>a9@UW\]^W_`abkWlmD[%(1Y"7!8E!HQy<Q;RSD((.	j012&&z9#3#3C#8%(1Y:OQVWXQYZ[\eQf	
r1   c                    | j                   j                  dd      }| j                  r|d| }n>| j                  r(dj	                  |j                  d      d   |d|       }n
| d|d|  }t        j                  |      |_        d|vr|j                  d       |j                  d	d
j	                  |||j                  d      |d|              y)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=
<<9%D\\>>)//#"6q"959;MND[%)"4!56D((.	Z/01!!%Y5E5Ec5JERST]L^_	
r1   c           
         | j                   |   d   }|| j                  k(  r4| j                  j                  dd      }	d}
d|vr|j	                  d       n|| j
                  k(  rB| j                  j                  dd      }	| j                  rdnd}
d	|vrg|j	                  d	       nU|d
k(  rP|| j                  k(  rA| j                  j                  dd      }	| j                  rdnd}
d|vr|j	                  d       | j                  rt        j                  
 |       |_        nj| j                  r>t        j                  dj                  |j                  d      d   
|            |_        n t        j                  | 
 |       |_        |j                  ddj                  |	|j!                  d      |             y)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!!"34$))#))--fnEJ..3cDz1!!"23h&9+D))--iFJ..3cD%Z7!!"89<<,,veW-=>DI\\,,X__Y__S=QRS=TVZ\a-bcDI,,	{4&-HIDI/00
IDTDTUXDY[`abr1   c           	         | j                   |   d   }d| }|j                  dd      }	|	r|	j                  d      ng }
d|
vr|
j                  d       ||
vr|
j                  |       || j                  u r| j                  ||
||||       n<|| j                  u r| j                  ||
||||       n| j                  |||
||||       |j                  ddj                  |
             y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  s     ""8,Z8!(,
XXgr*
.8Z%%c*b
j(k*Z'j) 		!dJy%S$++%j%E9Uxz5)UT]^#((:./r1   c                    | j                   |   d   }d| }|j                  dd      }|r|j                  d      ng }d|vr|j                  d       ||vr|j                  |       || j                  u r| j                  ||||       n| j                  ||||       |j                  ddj                  |             y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*
.8Z%%c*b
j(k*Z'j) 		!dJyAdJyA#((:./r1   c                     |j                  d      rd}|S |j                  d      rd}|S |j                  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  sN     ;;x H
 	 [[%"H  [["Hr1   c                 V    |j                  d      rd}|S |j                  d      rd}S )zGet social provider.r&   r'   r   r   s      r/   get_social_providerz/MagicShortenerTreeprocessor.get_social_provider  s4     ;;y! H  [[Hr1   c                    	 |j                  |dz         5|j                  |dz         |j                  |dz         f}| j                  }n|j                  |dz         !|j                  |dz         }| j                  }n|j                  |dz         !|j                  |dz         }| j                  }nZ|dk(  r5|j                  |dz         !|j                  |dz         }| j                  }n |j                  |dz         }| j
                  }||fS # t        $ rM d	}	 |j                  |d
z         d}| j                  }d}n# t        $ r Y nw xY w|sd}| j                  }Y fS w xY w)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95;;xRZGZ;[\ II	X	12>Hy$89 KK	X/0<Hw$67 II	X%%++h6K*L*XHz$9: LL	Hx$78 JJ	 i  	&E;;x'12> E $		I E  II	i	&s6   DD E)$EE)	EE)EE)(E)c                 f    | j                   xr$ |j                  |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/     yyTU[[)@ACG499TTr1   c                 x    | j                   xr- |j                  |dz         j                  | j                         S )z)Check if link is from our specified user.r   )r   r=   
startswithr   s      r/   
is_my_userz&MagicShortenerTreeprocessor.is_my_user  s3     ~~a%++mg.E"F"Q"QRVR`R`"aar1   c                     |j                  |dz         }|j                         | j                  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  s;     {{=723zz|t}}0035AAAr1   c                 	   | j                   d   | _        |j                  d      }|D ]  }t        t	        |            }|j
                  j                  d      }|j
                  j                  dd      }|j                  }d}|r|j
                  d= |s|sq||k(  s|sy| j                  sd|z   |k(  s| j                  rt        j                  |      }	|	r| j                  |	      }
|
}| j                  |
|	      | _        | j                  xs | j                  |
|	      | _        | j!                  |
|	      \  }}d}| j#                  |
||	      s8| j%                  ||||	j'                  |
d	z         || j(                  |   d
          |s| j                  rt*        j                  |      }	|	r| j                  |	      }
|
}| j                  |
|	      | _        | j                  xs | j                  |
|	      | _        | j!                  |
|	      \  }}d}| j#                  |
||	      s&| j-                  ||||	j'                  |
d	z                |s| j.                  r| j.                  j1                         D ]  \  }}|d   j                  |      }	|	r|}| j(                  |   d   }
| j                  |
|	      | _        | j                  xs | j                  |
|	      | _        | j!                  |
|	      \  }}d}| j#                  |
||	      s8| j%                  ||||	j'                  |
d	z         || j(                  |   d
          |r|d   j                  |      }	|	s|}| j(                  |   d   }
| j                  |
|	      | _        | j                  xs | j                  |
|	      | _        | j!                  |
|	      \  }}d}| j#                  |
||	      re| j-                  ||||	j'                  |
d	z                 |r| j2                  st4        j                  |      }	|	s
| j7                  |	      }|dk(  rt9        d       | j                  ||	      | _        | j                  xs | j                  ||	      | _        | j!                  ||	      \  }}| j#                  |||	      r| j-                  ||||	j'                  |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	DDJI{{{3H;;??62.D99DEKK,  d.@.@jSWFW\`E`%%$**40A(,(>(>q(A#0'+}a'H'+||'Xt}VW7X+/==+J(y $  $}}]HaH 55 $ ( ) !(D E % $ 2 28 <[ I !3!3)//5A(,(>(>q(A#0'+}a'H'+||'Xt}VW7X+/==+J(y $#}}]HaH 22 $ ( ) !(D E	 !6!6)-)>)>)D)D)F %&!&M//5'-H,0,>,>v,Fv,NM+/??=!+LDL+/<<+\4??=Z[;\DL/3}}]A/N,E9$(E $(==!#L $ 9 9$($,$-$%GGML,H$I$)$($6$6x$@$M!"  % %f 3 3D 9A +1040B0B60J60R/3}a/P/3||/`t}^_?`37==PQ3R 0y(,'+}}]Ha'P$($:$:(,(0(1()0L(M	%&A%&N !6!6&,,T2A#'#;#;A#>#y0+,wx'+x'C'+||'StxQR7S+/==1+E(y#}}XxC 22 $ ( ) !7(: ;	Wp	b 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     s|    R EDFGDDD)B
 

,
*c<0.0*
! FUb
Bwr1   r   c                       e Zd ZdZdZd Zy)MagiclinkPatternz+Convert html, ftp links to clickable links.r   c                    t        j                  d      }t        j                  |j	                  d            |_        |j	                  d      r!dj                  |j	                  d            }nc|j	                  d      }| j                  d   rCt        j                  |j
                  |j
                  j                  d      dz   d       |_        |j                  d	| j                  |j                                      | j                  j                  d
d      r|j                  dt        t                     ||j                  d      |j!                  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775>%%aggfo6D776?D{{?+!..rwwrww||E7JQ7N7O/PQ
vt}}TZZ\23;;??/7FF;J01771:quuQx''r1   Nrm   rn   ro   rp   ANCESTOR_EXCLUDESr  rq   r1   r/   r  r  E  s    5(r1   r  c                       e Zd ZdZd Zy)MagiclinkAutoPatternz@Return a link Element given an auto link `<http://example/com>`.c                 >   t        j                  d      }|j                  d| j                  |j	                  d                   t        j                  |j	                  d            |_        | j                  d   rCt        j                  |j                  |j                  j                  d      dz   d       |_        | j                  j                  dd	      r|j                  d
t        t                     ||j                  d      |j                  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}}QWWQZ01&&qwwqz2;;'**277277<<3F3J3K+LMBG;;??/7FF;O 451771:quuQx''r1   N)rm   rn   ro   rp   r  rq   r1   r/   r  r  ]  s
    J(r1   r  c                        e Zd ZdZdZd Zd Zy)MagiclinkMailPatternz(Convert emails to clickable email links.r  c                 0    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(!44r1   c                 
   t        j                  d      }| j                  |j                  d            }d| }t	        j
                  dj                  |D cg c]  }| j                  t        |             c}            |_	        |j                  ddj                  |D cg c]#  }t        j                  dt        |      ddz   % c}             ||j                  d	      |j                  d	      fS c c}w c c}w )
Handle email link patterns.r   mailzmailto:rd   r~   ru   r$  r%  r   )r  r  r  r=   r   r   r   r(  ordr   r   r&  r  r  )rj   rE   r  r   emailr~   cs          r/   r  z MagiclinkMailPattern.handleMatchx  s     ]]3aggfo. &&rwwSX/Ya0A0A#a&0I/Y'Z[
vrwwRVWQ 6 61SVAJa HWXY1771:quuQx'' 0ZWs   !C;%(D N)rm   rn   ro   rp   r  r(  r  rq   r1   r/   r"  r"  o  s    25(r1   r"  c                       e Zd ZdZdZd Zy)MagiclinkMentionPatternzConvert @mention to links.r  c           
         |j                  d      dd }|j                  d      }t        |      dkD  r|d   }|d   }n| j                  }|d   }|dk(  rt	        d       t        j                  d      }|j                  d	d
j                  | j                  |   d   |             |j                  ddj                  | j                  |   d   | j                  j                  dd      |             |j                  dd| d       t        j                  d|       |_        ||j                  d      |j!                  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@   r2  r   s           r/   r  z#MagiclinkMentionPattern.handleMatch  s0    wwy!!"%

3u:>QxHAhG}}HAhGy gh]]3
vw~~d&8&8&B5&I7ST
t11(;JGYbdjIkmtu	
 	w.xj8JKL&&7)}51771:quuQx''r1   Nr  rq   r1   r/   r0  r0    s    $(r1   r0  c                       e Zd ZdZdZd Zy)MagiclinkRepositoryPatternzConvert @user/repo to links.r  c           
         |j                  d      dd }|j                  d      }t        |      dkD  r|d   }|d   }n| j                  }|d   }|j                  d      }t	        j
                  d      }|j                  dd	j                  | j                  |   d
   ||             |j                  ddj                  | j                  |   d   | j                  j                  dd      ||             |j                  dd| d       t        j                  | d|       |_        ||j                  d      |j                  d      fS )r*  r2  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   r3  r@   re   r   r   s	            r/   r  z&MagiclinkRepositoryPattern.handleMatch  s<    wwy!!"%

3u:>QxH8D}}H8Dww~&]]3
vz((););H)Ee)LdTXYZ
!!""8,Z8$++//,Xd:egkmq	
 	w.xj8MNO&&$q'781771:quuQx''r1   Nr  rq   r1   r/   r5  r5    s    &(r1   r5  c                       e Zd ZdZdZd Zy)MagiclinkExternalRefsPatternzXConvert repo#1, user/repo#1, repo!1, user/repo!1, repo@hash, or user/repo@hash to links.r  c                 ~   |j                  d      }|j                  d      }|r|j                  d      dd n|j                  d      }|r|j                  d      nd}|j                  d      }|j                  d      }|s| j                  }|j                  d      }	t        |	      dkD  r|	d	   }
|	d   }n| j                  }
|sy
|| j                  k(  xr |
| j                  k(  | _        | j
                  xr || j                  k(  | _        t        j                  d      }|r| j                  ||
||||       n_|r| j                  ||
|||       nG| j                  ||
|||      s2|j                  d	      |j                  d	      |j                  d	      fS ||j                  d	      |j                  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   r3  r@   r   s               r/   r  z(MagiclinkExternalRefsPattern.handleMatch  s    GGH%	''&/)2!!"%8H$+wwvwwv99D

3u:>QxH8D}}H #tyy(FX-F||9		(9]]3  XtT5&IHdD%@&&r8T4Gwwqz1771:quuQx771771:quuQx''r1   Nr  rq   r1   r/   r9  r9    s    b#(r1   r9  c                       e Zd ZdZdZd Zy)MagiclinkInternalRefsPatternz Convert #1, !1, and commit_hash.r  c                    | j                   r| j                  sy|j                  d      }|j                  d      }|r|j                  d      n|j                  d      }|r|j                  d      nd}| j                  }| j                   }| j                  }	d| _        d| _        t        j                  d      }
|r| j                  |
|	||||       n_|r| j                  |
|	|||       nG| j                  |
|	|||      s2|j                  d      |j                  d      |j                  d      fS |
|j                  d      |j                  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  r=  r>  r   r?  r   re   r@   r   s              r/   r  z(MagiclinkInternalRefsPattern.handleMatch  s    yy		#GGH%	''&/%.!AGGG4D$+yyyy==]]3  XtT5&IHdD%@&&r8T4Gwwqz1771:quuQx771771:quuQx''r1   Nr  rq   r1   r/   rA  rA    s    *(r1   rA  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        |   |i | y)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sOE? G	' X#
 Z%
 ]
 ,
 ^(
 9
 4
 8i8
r 	$)&)r1   c                 *   t        t        |      }||_        |j                  j	                  |dd       t        t        |      }||_        |j                  j	                  |dd       |j                  j	                  t        t        |      dd       y)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#) 
""#4j#F'4$
""<rB
""#7#DlTXYr1   c           
         dg}t        j                  ||       | j                  r	t        | j                  || j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |dd       | j                  st        t        j                  | j                         || j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |dd       t"        j                  dj%                  | j&                              }t)        ||| j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |dd       t)        t*        j                  | j                         || j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |d	d
       | j                  rzt-        | j.                  || j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |dd       | j                  st-        t0        j                  | j                         || j
                  | j                  | j                  | j                  | j                  | j                        }|j                  j                  |dd       t3        t4        || j
                  | j                  | j                  | j                  | j                  | j                        }	|j                  j                  |	dd       yyy)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_shortr5  re_git_ext_repo_mentionsre   r   r@   rf   rg   rh   rT  rU  	is_socialRE_GIT_INT_REPO_MENTIONSr>   int_mentionsRE_ALL_EXT_MENTIONSr   ext_mentionsr0  RE_INT_MENTIONSr9  re_git_ext_refsRE_GIT_INT_EXT_REFSrA  RE_GIT_INT_MICRO_REFS)
rj   rl   r]  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+ >>5--				""	L &&|5MtT>>9,33D4E4EFIIIIMMKKNN&&	  !!**<9QSWX &,,SXXd6G6G-HI-IIIIMMKKNN	
 	"";0CTJ-""4#4#45IIIIMMKKNN	
 	"";0CTJ >>7$$				""	L &&|5EtL>>;'..t/@/@AIIIIMMKKNN&&	  !!**<9I4P%A)IIIIMMKKNN&&	&" !!**+=?UW[\- " r1   c                 >   t        || j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                        }||_        |j                  j                  |dd       y)zSetup shortener.zmagic-repo-shortenerg#@N)r   r   r   rf   rg   r   r   r   shortener_exclusionsr@   rh   r   treeprocessorsrU  )rj   rl   r   	shorteners       r/   setup_shortenerz"MagiclinkExtension.setup_shortener  s     0MMKKNN!!!!%%MM
	 "	
""9.DcJr1   c                 P   d}d}| j                   r||fS | j                  r| j                  rwdj                  | j                  | j
                     d   | j                  | j                        }dj                  | j                  | j
                     d   | j                        }||fS )zGet base URLs.rd   z	{}/{}/{}/rN   z{}/{}/)r`  re   r   r>   rh   r@   )rj   r   r   r   s       r/   get_base_urlsz MagiclinkExtension.get_base_urls  s     >>]**99"))$*<*<T]]*KE*RTXT]T]_c_h_hiH$OOD,>,>t}},Me,TVZV_V_`M&&r1   c                 	   | j                         }|j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  di       | _        |j                  dd      | _        | j                  t        v | _        |j                  d	d      | _	        |j                  d
d      | _
        |j                  dd      | _        |j                  dd      | _        t        j                         D ci c]  \  }}|t        |       c}}| _        t"        j%                         | _        |j                  di       }|j                  di       }i | _        t*        t,        t.        g}|j                         D ]?  \  }}	t0        j3                  |      st5        d| d      || j&                  vs:t7        |	d   |	d         | j&                  |<   |	d   | j&                  |   d<   t9        j:                  dj=                  t?        |	d   |	d   |	j                  dd                        t9        j:                  dj=                  tA        |	d   |	d   |	j                  dd                        d| j(                  |<   ||vr|j                  |	d   g       ||<   |jC                  tE        ||	d                B tF        j=                  djI                  |            | _%        tL        j=                  djI                  |            | _'        |j                  di       j                         D ][  \  }
}|
| j&                  v stQ        |tR        tT        t        f      s1|D ch c]  }|jW                          c}| j                   |
<   ] | j                  | j&                  vrd| _        | jY                  ||       | j                  s| j                  rg | _-        | j                  r| jZ                  j]                  |       | j                  r>| jZ                  jC                  t^               | jZ                  jC                  t`               | j&                  | j                     d   | _1        | je                  |       | j                  s| j                  r1| jg                  |      \  | _4        | _5        | jm                  ||       yyc c}}w c c}w )z;Add support for turning html links and emails to link tags.re   rd   r   r@   r$   rf   rJ  FrH  rI  r  rF  r  rG  zName 'zB' not allowed, provider name must contain only letters and numbersrM   rA   r   z(?xi)^{}/?$rB   T)r   re   r\  rO   N)7
getConfigsr   re   r   r@   rf   rg   SOCIAL_PROVIDERSr`  r^  social_shortr   r   DEFAULT_EXCLUDESr  r   rq  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   r_  RE_GIT_EXT_REFSrf  
isinstancer   tupler9   rZ  rd  extendRE_X_EXT_MENTIONSRE_TWITTER_EXT_MENTIONSrb  ro  rv  r   r   rt  )rj   rl   r   kvcustom_providerr   external_usersr  r]   r^   r   r'   s                r/   extendMarkdownz!MagiclinkExtension.extendMarkdown  sH    " JJvr*	JJvr*	

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

+A5 I;K;Q;Q;S$T41aQAY$T!*//1 **Xr2::6; "02HJcd,224 	RMFE!''/ VH$fg  T///-<U6]ERXM-Z""6*9>w""6*:6JJ&--4U6]E&MSXS\S\]bdhSij
 JJ&--4U6]E&MSXS\S\]bdhSij1%%f- )'/||E&M2'FHV$%%&9&%-&PQ-	R0 )A(G(GQ_H`(a%.55chh~6NO **%=rBHHJ 	LJCd(((ZeS?Q-REJ1K!'')1K))#.	L
 == 2 22$DMR(>>T.. "D~~!!((8  !!(():;!!(()@A $ 2 24== A. QD  $ !6!6040B0B60J-DM4-  V, "7q %UH 2Ls   S"<S()rm   rn   ro   rp   ri   rZ  ro  rt  rv  r  __classcell__)rN  s   @r/   rD  rD    s+    M<*|Z^]@K.'J-r1   rD  c                      t        | i |S )zReturn extension.)rD  )rL  rM  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{  rW  rV  rS  r  r  RE_TWITTER_USER	RE_X_USERr*   r+   r,   rc  re  r0   r  r  r}  r~  r  r  ra  r  rg  rh  r;   r6   rF   r>   r   rI   rJ   r  r  ry  r[   r_   r|  ra   rs   r   r  r  r"  r0  r5  r9  rA  rD  r  rq   r1   r/   <module>r     s  2  1 $ ! % %  	 H
 Fc?5	/ 	 /-. 	?F)   5. &o%67 )% ,Xx@ ,Xx@ /[I      
2	3	; 		S 	
	; 	
1# L	5 rzz '' +?@ .EF +?@	 		 		 	 2	5 BJJ '' +?@ .GH +?@  
 		* # 
 &-4.7
  )+14B
 &+(2,5 '	 !	
S/ d y*?@	o	.h(<= .EFh(<=4 4b
!; b
JB- BJ(* (0(? ($(? (((8 (@(!; (>((#= ((V(#= (D_- _-D	/r1   