markup.utils module

markup.utils.match_inline_math(text)[source]

Return first match object of regex search for inline math $...$ or \(...\).

markup.utils.match_displayed_math(text)[source]

Return first match object of regex search for displayed math $$...$$ or \[...\].

markup.utils.match_md_header(text, level=None)[source]

Return first match object of regex search for Markdown headers in form #{level,}.

If not level is given, all levels 1 to 6 are checked, returning the first match or None.

markup.utils.match_rst_header(text, symbol=None)[source]

Return first match object of regex search for reStructuredText header.

Python conventions are followed, namely that # and * headers have both over and underline (of equal length, so faulty ones are not matched), while the others (=, -, " and ^) only have the underline.

markup.utils.match_md_blockquote(text)[source]

Return first match of regex search for Markdown blockquote.

Return first match of regex search for Markdown inline hyperlink.

Return first match of regex search for Markdown reference-style hyperlink.

Return first match of regex search for reStructuredText inline hyperlink.

Return first match of regex search for reStructuredText reference-style hyperlink.

markup.utils.match_rst_role(text, role=None)[source]

Return first match object of regex search for given ReST role :role:… .

If no role is given, all roles in ReST_ROLES are tested one by one.

markup.utils.match_rst_directive(text, directive=None)[source]

Return first match object of regex search for given ReST directive.

If no directive is given, all directives in ReST_DIRECTIVES are tested one by one.

The first one to three lines after the directive statement are also captured.

markup.utils.match_md_unordered_list(text)[source]

Return first match of Markdown list (excluding ReST-shared * pattern).

markup.utils.match_md_or_rst_unordered_list(text)[source]

Return first match of Markdown/ReST unordered list using shared * marker.

markup.utils.match_md_or_rst_ordered_list(text)[source]

Return the first match of Markdown/ReST ordered list (using numbers).

markup.utils.match_rst_ordered_list(text)[source]
markup.utils.check_markers(markers)[source]

Checks the consistency of a markers dictionary. Returns a detector.

markup.utils.detect_markup_language(text)[source]

Detect whether text is plain text, Markdown or reStructuredText.

This method returns a dictionary containing:

  • language

  • errors

Inline and displayed maths are assumed enabled through MathJax. For plain text and Markdown, this assumes the conventions

  • inline: $ … $ and ( … )

  • displayed: $$ … $$ and [ … ]

while for reStructuredText, the math role and directive are used.

We define markers, and indicator. A marker is a regex which occurs in only one of the languages. An indicator occurs in more than one, but not all languages.

Language markers:

Markdown:

  • headers: [one or more #] [non-empty text]

  • blockquotes: one or more lines starting with > [non-empty text]

reStructuredText:

  • use of the :math: role or .. math: directive

  • [two or more #][blank space][carriage return] [text on a single line, as long as or shorter than # sequence] [same length of #]

  • same thing but for * headlines

  • other header markers (=, -, ” and ^)

  • use of any other role

  • use of any other directive

Language indicators:

Plain text or Markdown:

  • inline or displayed maths

Markdown or reStructuredText:

  • [=]+ alone on a line <- users discouraged to use this in Markdown

  • [-]+ alone on a line <- users discouraged to use this in Markdown

Exclusions (sources of errors): * inline or displayed maths cannot be used in ReST

Any simultaneously present markers to two different languages return an error.

Checking order:

  • maths

  • headers/blockquotes

  • hyperlinks

  • rst roles

  • rst directives

markup.utils.apply_markdown_preserving_displayed_maths_bracket(text)[source]

Subsidiary function called by apply_markdown_preserving_displayed_maths. See explanations in docstring of that method.

markup.utils.apply_markdown_preserving_displayed_maths(text)[source]

Processes the string text by first splitting out displayed maths, then applying Markdown on the non-displayed math parts.

Both $$ ... $$ and \[ ... \] are recognized, so a double recursive logic is used, first dealing with the $$ ... $$ and then with the \[ .. \]. See the complementary method apply_markdown_preserving_displayed_maths_bracket.

markup.utils.process_markup(text, language_forced=None, include_errors=False)[source]

Process a text in a markup language into HTML.

The markup language used is auto-detected. Supported: plain text, reStructuredTest, Markdown.

Parameters: * language_forced=None: override language auto-detection