markdown

Python Markdown

A Python implementation of John Gruber’s Markdown.

  • Documentation: https://python-markdown.github.io/
  • GitHub: https://github.com/Python-Markdown/markdown/
  • PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/). Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org). Currently maintained by Waylan Limberg (https://github.com/waylan), Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).

Modules:

Classes:

  • Markdown

    Convert Markdown to HTML.

Functions:

  • markdown

    Convert a markdown string to HTML and return HTML as a Unicode string.

  • markdownFromFile

    Read markdown code from a file and write it to a file or a stream.

markdown.Markdown(**kwargs)

Convert Markdown to HTML.

Other Parameters:

  • extensions (list[str | Extension]) –

    A list of extensions. If an item is an instance of a subclass of markdown.extension.Extension, the instance will be used as-is. If an item is of type string, first an entry point will be loaded. If that fails, the string is assumed to use Python dot notation (path.to.module:ClassName) to load a markdown.Extension subclass. If no class is specified, then a makeExtension function is called within the specified module.

  • extension_configs (dict[str, dict[str, Any]]) –

    Configuration settings for extensions.

  • output_format (str) –

    Format of output. Supported formats are: * xhtml: Outputs XHTML style tags. Default. * html: Outputs HTML style tags.

  • tab_length (int) –

    Length of tabs in the source. Default: 4

Methods:

  • build_extension

    Build extension from a string name, then return an instance.

  • build_parser

    Build the parser from the various parts.

  • convert

    Convert markdown to serialized XHTML or HTML.

  • convertFile

    Converts a markdown file and returns the HTML as a Unicode string.

  • is_block_level

    Check if the tag is a block level HTML tag.

  • registerExtension

    This gets called by the extension

  • registerExtensions

    Register extensions with this instance of Markdown.

  • reset

    Resets all state variables so that we can start with a new text.

  • set_output_format

    Set the output format for the class instance.

markdown.Markdown.build_extension(ext_name, configs)

Build extension from a string name, then return an instance.

First attempt to load an entry point. The string name must be registered as an entry point in the markdown.extensions group which points to a subclass of the markdown.extensions.Extension class. If multiple distributions have registered the same name, the first one found is returned.

If no entry point is found, assume dot notation (path.to.module:ClassName). Load the specified class and return an instance. If no class is specified, import the module and call a makeExtension function and return the Extension instance returned by that function.

markdown.Markdown.build_parser()

Build the parser from the various parts.

markdown.Markdown.convert(source: str)

Convert markdown to serialized XHTML or HTML.

Parameters:

  • source (str) –

    Source text as a Unicode string.

Markdown processing takes place in five steps:

  1. A bunch of preprocessors munge the input text.
  2. BlockParser() parses the high-level structural elements of the pre-processed text into an ElementTree.
  3. A bunch of treeprocessors are run against the ElementTree. One such treeprocessor runs InlinePatterns against the ElementTree, detecting inline markup.
  4. Some post-processors are run against the text after the ElementTree has been serialized into text.
  5. The output is written to a string.

markdown.Markdown.convertFile(input: str | TextIO | None = None, output: str | TextIO | None = None, encoding: str | None = None)

Converts a markdown file and returns the HTML as a Unicode string.

Decodes the file using the provided encoding (defaults to utf-8), passes the file content to markdown, and outputs the html to either the provided stream or the file with provided name, using the same encoding as the source file. The xmlcharrefreplace error handler is used when encoding the output.

Note: This is the only place that decoding and encoding of Unicode takes place in Python-Markdown. (All other code is Unicode-in / Unicode-out.)

Parameters:

  • input (str | TextIO | None, default: None ) –

    File object or path. Reads from stdin if None.

  • output (str | TextIO | None, default: None ) –

    File object or path. Writes to stdout if None.

  • encoding (str | None, default: None ) –

    Encoding of input and output files. Defaults to utf-8.

markdown.Markdown.is_block_level(tag)

Check if the tag is a block level HTML tag.

markdown.Markdown.registerExtension(extension)

This gets called by the extension

markdown.Markdown.registerExtensions(extensions: list[str | Extension], configs: dict[str, dict[str, Any]]) -> Markdown

Register extensions with this instance of Markdown.

Parameters:

  • extensions (list[str | Extension]) –

    A list of extensions, which can either be strings or objects.

  • configs (dict[str, dict[str, Any]]) –

    A dictionary mapping extension names to configs options.

markdown.Markdown.reset()

Resets all state variables so that we can start with a new text.

markdown.Markdown.set_output_format(format)

Set the output format for the class instance.

markdown.markdown(text: str, **kwargs: Any) -> str

Convert a markdown string to HTML and return HTML as a Unicode string.

This is a shortcut function for Markdown class to cover the most basic use case. It initializes an instance of Markdown, loads the necessary extensions and runs the parser on the given text.

Parameters:

  • text (str) –

    Markdown formatted text as Unicode or ASCII string.

Other Parameters:

  • **kwargs (Any) –

    Any arguments accepted by the Markdown class.

Returns:

  • str

    An HTML document as a string.

markdown.markdownFromFile(**kwargs: Any)

Read markdown code from a file and write it to a file or a stream.

This is a shortcut function which initializes an instance of Markdown, and calls the convertFile method rather than convert.

Other Parameters:

  • input (str | TextIO) –

    A file name or readable object.

  • output (str | TextIO) –

    A file name or writable object.

  • encoding (str) –

    Encoding of input and output.

Parameters:

  • **kwargs (Any, default: {} ) –

    Any arguments accepted by the Markdown class.