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:
-
Inline Processors no longer need to match the entire block, so regular expressions no longer need to start with
r'^(.*?)'and end withr'(.*?)%'. This runs faster. The returnedMatchobject will only contain what is explicitly matched in the pattern, and extension pattern groups now start withm.group(1). -
The
handleMatchmethod now takes an additional input calleddata, 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 todatathat the return element is replacing (usuallym.start(0)andm.end(0)). If the boundaries are returned asNone, it is assumed that the match did not take place, and nothing will be altered indata.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:
-
EmStrongItem–Emphasis/strong pattern item.
-
Pattern–Base class that inline patterns subclass.
-
InlineProcessor–Base class that inline processors subclass.
-
SimpleTextPattern–Return a simple text of
group(2)of a Pattern. -
SimpleTextInlineProcessor–Return a simple text of
group(1)of a Pattern. -
EscapeInlineProcessor–Return an escaped character.
-
SimpleTagPattern–Return element of type
tagwith a text attribute ofgroup(3) -
SimpleTagInlineProcessor–Return element of type
tagwith a text attribute ofgroup(2) -
SubstituteTagPattern–Return an element of type
tagwith no children. -
SubstituteTagInlineProcessor–Return an element of type
tagwith no children. -
BacktickInlineProcessor–Return a
<code>element containing the escaped matching text. -
DoubleTagPattern–Return a ElementTree element nested in tag2 nested in tag1.
-
DoubleTagInlineProcessor–Return a ElementTree element nested in tag2 nested in tag1.
-
HtmlInlineProcessor–Store raw inline html and return a placeholder.
-
Delimiter–Delimiter.
-
DelimiterProcessor–Processor for handling complex nested patterns such as strong and em matches.
-
LinkInlineProcessor–Return a link element from the given match.
-
ImageInlineProcessor–Return a
imgelement from the given match. -
ReferenceInlineProcessor–Match to a stored reference and return link element.
-
ShortReferenceInlineProcessor–Short form of reference:
[google]. -
ImageReferenceInlineProcessor–Match to a stored reference and return
imgelement. -
ShortImageReferenceInlineProcessor–Short form of image reference:
![ref]. -
AutolinkInlineProcessor–Return a link Element given an auto-link (
<http://example/com>). -
AutomailInlineProcessor–Return a
mailtolink Element given an auto-mail link (<foo@example.com>).
Functions:
-
build_inlinepatterns–Build the default set of inline patterns for Markdown.
-
dequote–Remove quotes from around a string.
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 (
or). -
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 (
&(decimal) or&(hex) or&(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
§
markdown.inlinepatterns.BACKTICK_RE
module-attribute
§
markdown.inlinepatterns.ESCAPE_RE
module-attribute
§
markdown.inlinepatterns.LINK_RE
module-attribute
§
markdown.inlinepatterns.IMAGE_LINK_RE
module-attribute
§
markdown.inlinepatterns.REFERENCE_RE
module-attribute
§
markdown.inlinepatterns.IMAGE_REFERENCE_RE
module-attribute
§
markdown.inlinepatterns.AUTOLINK_RE
module-attribute
§
markdown.inlinepatterns.AUTOMAIL_RE
module-attribute
§
markdown.inlinepatterns.HTML_RE
module-attribute
§
Match an HTML tag (<...>).
Defined Value:
markdown.inlinepatterns.ENTITY_RE
module-attribute
§
markdown.inlinepatterns.LINE_BREAK_RE
module-attribute
§
markdown.inlinepatterns.dequote(string: str) -> str
§
Remove quotes from around a string.
markdown.inlinepatterns.EmStrongItem
§
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.Markdownand is available asself.mdon 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:
-
ANCESTOR_EXCLUDES(Collection[str]) –A collection of elements which are undesirable ancestors. The processor will be skipped if it
markdown.inlinepatterns.Pattern.ANCESTOR_EXCLUDES: Collection[str]
class-attribute
instance-attribute
§
markdown.inlinepatterns.Pattern.getCompiledRegExp() -> re.Pattern
§
Return a compiled regular expression.
markdown.inlinepatterns.Pattern.handleMatch(m: re.Match[str]) -> etree.Element | str
§
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.Markdownand is available asself.mdon 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:
-
ANCESTOR_EXCLUDES(Collection[str]) –A collection of elements which are undesirable ancestors. The processor will be skipped if it
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:
markdown.inlinepatterns.InlineProcessor.ANCESTOR_EXCLUDES: Collection[str]
class-attribute
instance-attribute
§
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.Markdownand is available asself.mdon 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:
-
ANCESTOR_EXCLUDES(Collection[str]) –A collection of elements which are undesirable ancestors. The processor will be skipped if it
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
§
markdown.inlinepatterns.SimpleTextPattern.getCompiledRegExp() -> re.Pattern
§
Return a compiled regular expression.
markdown.inlinepatterns.SimpleTextPattern.type() -> str
§
Return class name, to define pattern type