MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / BacktickInlineProcessor

Class BacktickInlineProcessor

markdown/inlinepatterns.py:408–478  ·  view source on GitHub ↗

Return a ` ` element containing the escaped matching text.

Source from the content-addressed store, hash-verified

406
407
408class BacktickInlineProcessor(InlineProcessor):
409 """ Return a `<code>` element containing the escaped matching text. """
410
411 def __init__(self, pattern: str):
412 InlineProcessor.__init__(self, pattern)
413 self.ESCAPED_BSLASH = '{}{}{}'.format(util.STX, ord('\\'), util.ETX)
414 self.tag = 'code'
415 """ The tag of the rendered element. """
416
417 def find_code_spans(self, start: int, text: str) -> tuple[int, int | None]:
418 """Find code spans."""
419
420 last = len(text)
421
422 # Get the maximum starting ticks
423 max_ticks = 0
424 while start < last and text[start] == '`':
425 max_ticks += 1
426 start += 1
427
428 longest_span = 0
429 end = 0
430
431 # Find an ending span of backticks that matches our opening
432 i = start
433 while i < last:
434 span_length = 0
435 while i < last and text[i] == '`':
436 span_length += 1
437 i += 1
438 if not span_length:
439 i += 1
440 continue
441
442 # Did we find the end?
443 if max_ticks == span_length:
444 return start, i - span_length
445
446 # Track the longest span of backticks we find as a fallback.
447 if span_length > longest_span:
448 longest_span = span_length
449 end = i
450
451 # Since we didn't find an exact matching start and end,
452 # adjust start to match the largest end we could calculate.
453 if 0 < longest_span < max_ticks:
454 return start - (max_ticks - longest_span), end - longest_span
455
456 # We could not find a suitable pairing
457 return start + max_ticks, None
458
459 def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | str | None, int | None, int | None]:
460 """
461 If the match contains `group(3)` of a pattern, then return a `code`
462 [`Element`][xml.etree.ElementTree.Element] which contains HTML escaped text (with
463 [`code_escape`][markdown.util.code_escape]) as an [`AtomicString`][markdown.util.AtomicString].
464
465 If the match contains `group(1)` then return the text of `group(1)` as backslash escaped.

Callers 1

build_inlinepatternsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected