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

Class BacktickInlineProcessor

markdown/inlinepatterns.py:407–477  ·  view source on GitHub ↗

Return a ` ` element containing the escaped matching text.

Source from the content-addressed store, hash-verified

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