3838URL_BASE = 'https://github.com'
3939RE_PARTS = dict (
4040 USER = r'[-_\w]+' ,
41- PROJECT = r'[-_.\w]+'
41+ PROJECT = r'[-_.\w]+\b '
4242)
4343
4444
45+ def _build_link (label , title , href , classes ):
46+ el = etree .Element ('a' )
47+ el .text = label
48+ el .set ('title' , title )
49+ el .set ('href' , href )
50+ el .set ('class' , classes )
51+ return el
52+
53+
54+ class MentionPattern (Pattern ):
55+ def __init__ (self , config , md ):
56+ MENTION_RE = r'(@({USER})(?:\/({PROJECT}))?)' .format (** RE_PARTS )
57+ super (MentionPattern , self ).__init__ (MENTION_RE , md )
58+ self .config = config
59+
60+ def handleMatch (self , m ):
61+ label = m .group (2 )
62+ user = m .group (3 )
63+ project = m .group (4 )
64+ if project :
65+ title = 'GitHub Project: @{0}/{1}' .format (user , project )
66+ href = '{0}/{1}/{2}' .format (URL_BASE , user , project )
67+ else :
68+ title = 'GitHub User: @{0}' .format (user )
69+ href = '{0}/{1}' .format (URL_BASE , user )
70+ return _build_link (label , title , href , 'gh-link gh-mention' )
71+
72+
4573class IssuePattern (Pattern ):
4674 def __init__ (self , config , md ):
4775 ISSUE_RE = r'((?:({USER})\/({PROJECT}))?#([0-9]+))' .format (** RE_PARTS )
@@ -53,15 +81,9 @@ def handleMatch(self, m):
5381 user = m .group (3 ) or self .config ['user' ]
5482 project = m .group (4 ) or self .config ['project' ]
5583 num = m .group (5 ).lstrip ('0' )
56-
57- el = etree .Element ('a' )
58- el .text = label
5984 title = 'GitHub Issue {0}/{1} #{2}' .format (user , project , num )
60- el .set ('title' , title )
6185 href = '{0}/{1}/{2}/issues/{3}' .format (URL_BASE , user , project , num )
62- el .set ('href' , href )
63- el .set ('class' , 'gh-link gh-issue' )
64- return el
86+ return _build_link (label , title , href , 'gh-link gh-issue' )
6587
6688
6789class GithubLinks (Extension ):
@@ -73,7 +95,9 @@ def __init__(self, *args, **kwargs):
7395 super (GithubLinks , self ).__init__ (* args , ** kwargs )
7496
7597 def extendMarkdown (self , md , md_globals ):
98+ md .ESCAPED_CHARS .append ('@' )
7699 md .inlinePatterns ['issue' ] = IssuePattern (self .getConfigs (), md )
100+ md .inlinePatterns ['mention' ] = MentionPattern (self .getConfigs (), md )
77101
78102
79103def makeExtension (* args , ** kwargs ):
0 commit comments