@@ -70,7 +70,7 @@ def build_inlinepatterns(md: Markdown, **kwargs: Any) -> util.Registry[InlinePro
7070
7171 """
7272 inlinePatterns = util .Registry ()
73- inlinePatterns .register (BacktickInlineProcessor (BACKTICK_RE ), 'backtick' , 190 )
73+ inlinePatterns .register (BacktickInlineProcessor (BACKTICK_RE , md ), 'backtick' , 190 )
7474 inlinePatterns .register (EscapeInlineProcessor (ESCAPE_RE , md ), 'escape' , 180 )
7575 inlinePatterns .register (ReferenceInlineProcessor (REFERENCE_RE , md ), 'reference' , 170 )
7676 inlinePatterns .register (LinkInlineProcessor (LINK_RE , md ), 'link' , 160 )
@@ -435,12 +435,14 @@ def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int,
435435
436436class BacktickInlineProcessor (InlineProcessor ):
437437 """ Return a `<code>` element containing the escaped matching text. """
438- def __init__ (self , pattern : str ):
438+ def __init__ (self , pattern : str , md : Markdown ):
439439 InlineProcessor .__init__ (self , pattern )
440440 self .ESCAPED_BSLASH = '{}{}{}' .format (util .STX , ord ('\\ ' ), util .ETX )
441441 self .tag = 'code'
442442 """ The tag of the rendered element. """
443443
444+ self .md = md
445+
444446 def handleMatch (self , m : re .Match [str ], data : str ) -> tuple [etree .Element | str , int , int ]:
445447 """
446448 If the match contains `group(3)` of a pattern, then return a `code`
@@ -451,6 +453,8 @@ def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | str,
451453
452454 """
453455 if m .group (3 ):
456+ if data .lstrip ('[' ).rstrip (']' ).lower () in self .md .references : # ignore known references
457+ return None , None , None
454458 el = etree .Element (self .tag )
455459 el .text = util .AtomicString (util .code_escape (m .group (3 ).strip ()))
456460 return el , m .start (0 ), m .end (0 )
@@ -879,6 +883,8 @@ class ReferenceInlineProcessor(LinkInlineProcessor):
879883
880884 RE_LINK = re .compile (r'\s?\[([^\]]*)\]' , re .DOTALL | re .UNICODE )
881885
886+ RE_BACKTICK = re .compile (BACKTICK_RE , re .DOTALL | re .UNICODE )
887+
882888 def handleMatch (self , m : re .Match [str ], data : str ) -> tuple [etree .Element | None , int | None , int | None ]:
883889 """
884890 Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`.
@@ -925,7 +931,19 @@ def makeTag(self, href: str, title: str, text: str) -> etree.Element:
925931 if title :
926932 el .set ('title' , title )
927933
928- el .text = text
934+ if '`' in text : # Process possible backtick within text
935+ m = self .RE_BACKTICK .search (text )
936+ if m and m .group (3 ):
937+ el2 = etree .Element ('code' )
938+ el2 .text = util .AtomicString (util .code_escape (m .group (3 ).strip ()))
939+ el .append (el2 )
940+ el .text = text [0 :m .start (0 )]
941+ el2 .tail = text [m .end (0 ):]
942+ else :
943+ el .text = text
944+ else :
945+ el .text = text
946+
929947 return el
930948
931949
0 commit comments