44from markdown_it .common .utils import escapeHtml
55from markdown_it .rules_inline import StateInline
66
7- PATTERN = re .compile (r"^\{([a-zA-Z0-9\_\-\+\:]{1,36})\}(`+)(?!`)(.+?)(?<!`)\2(?!`) " )
7+ VALID_NAME_PATTERN = re .compile (r"^\{([a-zA-Z0-9\_\-\+\:]+)\} " )
88
99
1010def myst_role_plugin (md : MarkdownIt ):
@@ -14,22 +14,45 @@ def myst_role_plugin(md: MarkdownIt):
1414
1515
1616def myst_role (state : StateInline , silent : bool ):
17+
18+ # check name
19+ match = VALID_NAME_PATTERN .match (state .src [state .pos :])
20+ if not match :
21+ return False
22+ name = match .group (1 )
23+
24+ # check for starting backslash escape
1725 try :
1826 if state .srcCharCode [state .pos - 1 ] == 0x5C : # /* \ */
1927 # escaped (this could be improved in the case of edge case '\\{')
2028 return False
2129 except IndexError :
2230 pass
2331
24- match = PATTERN .search (state .src [state .pos :])
32+ # scan opening tick length
33+ start = pos = state .pos + match .end ()
34+ try :
35+ while state .src [pos ] == "`" :
36+ pos += 1
37+ except IndexError :
38+ return False
39+
40+ tick_length = pos - start
41+ if not tick_length :
42+ return False
43+
44+ # search for closing ticks
45+ match = re .search ("`" * tick_length , state .src [pos + 1 :])
2546 if not match :
2647 return False
27- state .pos += match .end ( )
48+ content = state .src [ pos : pos + match .start () + 1 ]. replace ( " \n " , " " )
2849
2950 if not silent :
3051 token = state .push ("myst_role" , "" , 0 )
31- token .meta = {"name" : match .group (1 )}
32- token .content = match .group (3 )
52+ token .meta = {"name" : name }
53+ token .content = content
54+
55+ state .pos = pos + match .end () + 1
3356
3457 return True
3558
@@ -38,7 +61,5 @@ def render_myst_role(self, tokens, idx, options, env):
3861 token = tokens [idx ]
3962 name = token .meta .get ("name" , "unknown" )
4063 return (
41- '<code class="sphinx-role">'
42- f"{{{ name } }}[{ escapeHtml (token .content )} ]"
43- "</code>"
64+ '<code class="myst role">' f"{{{ name } }}[{ escapeHtml (token .content )} ]" "</code>"
4465 )
0 commit comments