Skip to content

markdown.inlinepatterns §

In version 3.0, a new, more flexible inline processor was added, markdown.inlinepatterns.InlineProcessor. The original inline patterns, which inherit from markdown.inlinepatterns.Pattern or one of its children are still supported, though users are encouraged to migrate.

The new InlineProcessor provides two major enhancements to Patterns:

  1. Inline Processors no longer need to match the entire block, so regular expressions no longer need to start with r'^(.*?)' and end with r'(.*?)%'. This runs faster. The returned Match object will only contain what is explicitly matched in the pattern, and extension pattern groups now start with m.group(1).

  2. The handleMatch method now takes an additional input called data, which is the entire block under analysis, not just what is matched with the specified pattern. The method now returns the element and the indexes relative to data that the return element is replacing (usually m.start(0) and m.end(0)). If the boundaries are returned as None, it is assumed that the match did not take place, and nothing will be altered in data.

    This allows handling of more complex constructs than regular expressions can handle, e.g., matching nested brackets, and explicit control of the span “consumed” by the processor.

Classes:

Functions:

Attributes:

  • NOIMG –

    Match not an image. Partial regular expression which matches if not preceded by !.

  • BACKTICK_RE –

    Match backtick quoted string (`e=f()` or ``e=f("`")``).

  • ESCAPE_RE –

    Match a backslash escaped character (\< or \*).

  • LINK_RE –

    Match start of in-line link ([text](url) or [text](<url>) or [text](url "title")).

  • IMAGE_LINK_RE –

    Match start of in-line image link (![alttxt](url) or ![alttxt](<url>)).

  • REFERENCE_RE –

    Match start of reference link ([Label][3]).

  • IMAGE_REFERENCE_RE –

    Match start of image reference (![alt text][2]).

  • AUTOLINK_RE –

    Match an automatic link (<http://www.example.com>).

  • AUTOMAIL_RE –

    Match an automatic email link (<me@example.com>).

  • HTML_RE –

    Match an HTML tag (<...>).

  • ENTITY_RE –

    Match an HTML entity (&#38; (decimal) or &#x26; (hex) or &amp; (named)).

  • LINE_BREAK_RE –

    Match two spaces at end of line.

markdown.inlinepatterns.build_inlinepatterns(md: Markdown, **kwargs: Any) -> util.Registry[InlineProcessor] §

Build the default set of inline patterns for Markdown.

The order in which processors and/or patterns are applied is very important - e.g. if we first replace http://.../ links with <a> tags and then try to replace inline HTML, we would end up with a mess. So, we apply the expressions in the following order:

  • backticks and escaped characters have to be handled before everything else so that we can preempt any markdown patterns by escaping them;

  • then we handle the various types of links (auto-links must be handled before inline HTML);

  • then we handle inline HTML. At this point we will simply replace all inline HTML strings with a placeholder and add the actual HTML to a stash;

  • finally we apply strong, emphasis, etc.

Return a Registry instance which contains the following collection of classes with their assigned names and priorities.

Class Instance Name Priority
BacktickInlineProcessor(BACKTICK_RE) backtick 190
EscapeInlineProcessor(ESCAPE_RE) escape 180
ReferenceInlineProcessor(REFERENCE_RE) reference 170
LinkInlineProcessor(LINK_RE) link 160
ImageInlineProcessor(IMAGE_LINK_RE) image_link 150
ImageReferenceInlineProcessor(IMAGE_REFERENCE_RE) image_reference 140
ShortReferenceInlineProcessor(REFERENCE_RE) short_reference 130
ShortImageReferenceInlineProcessor(IMAGE_REFERENCE_RE) short_image_ref 125
AutolinkInlineProcessor(AUTOLINK_RE) autolink 120
AutomailInlineProcessor(AUTOMAIL_RE) automail 110
SubstituteTagInlineProcessor(LINE_BREAK_RE) linebreak 100
HtmlInlineProcessor(HTML_RE) html 90
HtmlInlineProcessor(ENTITY_RE) entity 80
DelimiterProcessor("*") em_strong 60

markdown.inlinepatterns.NOIMG module-attribute §

Match not an image. Partial regular expression which matches if not preceded by !.

Defined Value:

NOIMG = r'(?<!\!)'

markdown.inlinepatterns.BACKTICK_RE module-attribute §

Match backtick quoted string (`e=f()` or ``e=f("`")``).

Defined Value:

BACKTICK_RE = r'(?:(?<!\\)((?:\\{2})+)(?=`+)|(?<!\\)`)'

markdown.inlinepatterns.ESCAPE_RE module-attribute §

Match a backslash escaped character (\< or \*).

Defined Value:

ESCAPE_RE = r'\\(.)'

Match start of in-line link ([text](url) or [text](<url>) or [text](url "title")).

Defined Value:

LINK_RE = NOIMG + r'\['

Match start of in-line image link (![alttxt](url) or ![alttxt](<url>)).

Defined Value:

IMAGE_LINK_RE = r'\!\['

markdown.inlinepatterns.REFERENCE_RE module-attribute §

Match start of reference link ([Label][3]).

Defined Value:

REFERENCE_RE = LINK_RE

markdown.inlinepatterns.IMAGE_REFERENCE_RE module-attribute §

Match start of image reference (![alt text][2]).

Defined Value:

IMAGE_REFERENCE_RE = IMAGE_LINK_RE

Match an automatic link (<http://www.example.com>).

Defined Value:

AUTOLINK_RE = r'<([A-Za-z][A-Za-z0-9+.-]*://[^<>]*)>'

markdown.inlinepatterns.AUTOMAIL_RE module-attribute §

Match an automatic email link (<me@example.com>).

Defined Value:

AUTOMAIL_RE = r'<([^<> !]+@[^@<> ]+)>'

markdown.inlinepatterns.HTML_RE module-attribute §

Match an HTML tag (<...>).

Defined Value:

HTML_RE = (
    # Tag
    r'''(<(\/?+[a-zA-Z][^\s"'<>@]*+(?:\s+[^\s"'=<>]++(?:\s*+=\s*+(?:"[^"]*+"|'[^']*+'|[^\s"'=<>]++))?+)*+\s*+/?|'''
    # Comment
    r'!--(?:(?!<!--|-->).)*--|'
    # Processing instruction
    r'[?](?:(?!<[?]|[?]>).)*[?]|'
    # `CDATA`
    r'!\[CDATA\[(?:(?!<!\[CDATA\[|\]\]>).)*\]\]'
    ')>)'
)

markdown.inlinepatterns.ENTITY_RE module-attribute §

Match an HTML entity (&#38; (decimal) or &#x26; (hex) or &amp; (named)).

Defined Value:

ENTITY_RE = r'(&(?:\#[0-9]+|\#x[0-9a-fA-F]+|[a-zA-Z0-9]+);)'

markdown.inlinepatterns.LINE_BREAK_RE module-attribute §

Match two spaces at end of line.

Defined Value:

LINE_BREAK_RE = r'  \n'

markdown.inlinepatterns.dequote(string: str) -> str §

Remove quotes from around a string.

markdown.inlinepatterns.EmStrongItem §

Bases: NamedTuple

Emphasis/strong pattern item.

markdown.inlinepatterns.Pattern(pattern: str, md: Markdown | None = None) §

Base class that inline patterns subclass.

Inline patterns are handled by means of Pattern subclasses, one per regular expression. Each pattern object uses a single regular expression and must support the following methods: getCompiledRegExp and handleMatch.

All the regular expressions used by Pattern subclasses must capture the whole block. For this reason, they all start with ^(.*) and end with (.*)!. When passing a regular expression on class initialization, the ^(.*) and (.*)! are added automatically and the regular expression is pre-compiled.

It is strongly suggested that the newer style markdown.inlinepatterns.InlineProcessor that use a more efficient and flexible search approach be used instead. However, the older style Pattern remains for backward compatibility with many existing third-party extensions.

Parameters:

  • pattern (str) –

    A regular expression that matches a pattern.

  • md (Markdown | None, default: None ) –

    An optional pointer to the instance of markdown.Markdown and is available as self.md on the class instance.

Methods:

  • getCompiledRegExp –

    Return a compiled regular expression.

  • handleMatch –

    Return a ElementTree element from the given match.

  • type –

    Return class name, to define pattern type

  • unescape –

    Return unescaped text given text with an inline placeholder.

Attributes:

markdown.inlinepatterns.Pattern.ANCESTOR_EXCLUDES: Collection[str] class-attribute instance-attribute §

A collection of elements which are undesirable ancestors. The processor will be skipped if it would cause the content to be a descendant of one of the listed tag names.

Defined Value:

ANCESTOR_EXCLUDES: Collection[str] = tuple()

markdown.inlinepatterns.Pattern.getCompiledRegExp() -> re.Pattern §

Return a compiled regular expression.

markdown.inlinepatterns.Pattern.handleMatch(m: re.Match[str]) -> etree.Element | str §

Return a ElementTree element from the given match.

Subclasses should override this method.

Parameters:

  • m (Match[str]) –

    A match object containing a match of the pattern.

Returns: An ElementTree Element object.

markdown.inlinepatterns.Pattern.type() -> str §

Return class name, to define pattern type

markdown.inlinepatterns.Pattern.unescape(text: str) -> str §

Return unescaped text given text with an inline placeholder.

markdown.inlinepatterns.InlineProcessor(pattern: str, md: Markdown | None = None) §

Bases: Pattern

Base class that inline processors subclass.

This is the newer style inline processor that uses a more efficient and flexible search approach.

Parameters:

  • pattern (str) –

    A regular expression that matches a pattern.

  • md (Markdown | None, default: None ) –

    An optional pointer to the instance of markdown.Markdown and is available as self.md on the class instance.

Methods:

  • handleMatch –

    Return a ElementTree element from the given match and the

  • getCompiledRegExp –

    Return a compiled regular expression.

  • type –

    Return class name, to define pattern type

  • unescape –

    Return unescaped text given text with an inline placeholder.

Attributes:

markdown.inlinepatterns.InlineProcessor.handleMatch(m: re.Match[str], data: str) -> tuple[etree.Element | str | None, int | None, int | None] §

Return a ElementTree element from the given match and the start and end index of the matched text.

If start and/or end are returned as None, it will be assumed that the processor did not find a valid region of text.

Subclasses should override this method.

Parameters:

  • m (Match[str]) –

    A re match object containing a match of the pattern.

  • data (str) –

    The buffer currently under analysis.

Returns:

  • el ( Element | str | None ) –

    The ElementTree element, text or None.

  • start ( int | None ) –

    The start of the region that has been matched or None.

  • end ( int | None ) –

    The end of the region that has been matched or None.

markdown.inlinepatterns.InlineProcessor.ANCESTOR_EXCLUDES: Collection[str] class-attribute instance-attribute §

A collection of elements which are undesirable ancestors. The processor will be skipped if it would cause the content to be a descendant of one of the listed tag names.

Defined Value:

ANCESTOR_EXCLUDES: Collection[str] = tuple()

markdown.inlinepatterns.InlineProcessor.getCompiledRegExp() -> re.Pattern §

Return a compiled regular expression.

markdown.inlinepatterns.InlineProcessor.type() -> str §

Return class name, to define pattern type

markdown.inlinepatterns.InlineProcessor.unescape(text: str) -> str §

Return unescaped text given text with an inline placeholder.

markdown.inlinepatterns.SimpleTextPattern(pattern: str, md: Markdown | None = None) §

Bases: Pattern

Return a simple text of group(2) of a Pattern.

Parameters:

  • pattern (str) –

    A regular expression that matches a pattern.

  • md (Markdown | None, default: None ) –

    An optional pointer to the instance of markdown.Markdown and is available as self.md on the class instance.

Methods:

  • handleMatch –

    Return string content of group(2) of a matching pattern.

  • getCompiledRegExp –

    Return a compiled regular expression.

  • type –

    Return class name, to define pattern type

  • unescape –

    Return unescaped text given text with an inline placeholder.

Attributes:

markdown.inlinepatterns.SimpleTextPattern.handleMatch(m: re.Match[str]) -> str §

Return string content of group(2) of a matching pattern.

markdown.inlinepatterns.SimpleTextPattern.ANCESTOR_EXCLUDES: Collection[str] class-attribute instance-attribute §

A collection of elements which are undesirable ancestors. The processor will be skipped if it would cause the content to be a descendant of one of the listed tag names.

Defined Value:

ANCESTOR_EXCLUDES: Collection[str] = tuple()

markdown.inlinepatterns.SimpleTextPattern.getCompiledRegExp() -> re.Pattern §

Return a compiled regular expression.

markdown.inlinepatterns.SimpleTextPattern.type() -> str §

Return class name, to define pattern type