click_extra.mkdocs module

        classDiagram
  BasePlugin <|-- AnsiColorPlugin
    

MkDocs plugin for ANSI color rendering in code blocks.

Patches the formatter classes of pymdownx.highlight so that Token.Ansi.* tokens produced by Click Extra’s Pygments lexers are decomposed into individual CSS classes and styled with the correct colors.

Decomposing tokens into classes only strips the raw escape codes: the classes carry no color until a matching stylesheet is present. Unlike Sphinx’s PygmentsBridge, which regenerates pygments.css from the active formatter on every build, MkDocs never emits one for these classes. The plugin therefore writes the ANSI rules to a dedicated stylesheet, ANSI_STYLESHEET, and registers it in extra_css so every page links it.

When mkdocs-click is installed, the plugin also patches its code-block generators to use the ansi-output lexer instead of plain text, so that CLI help text with ANSI escape codes renders with colors.

Note

This is the MkDocs counterpart of the Sphinx integration in click_extra.sphinx, which achieves the same result by replacing sphinx.highlighting.PygmentsBridge.html_formatter.

click_extra.mkdocs.ANSI_OUTPUT_FENCE = '```ansi-output'

Fenced code-block opening that triggers the ANSI-aware Pygments lexer.

click_extra.mkdocs.TEXT_FENCE = '```text'

Fenced code-block opening used by mkdocs-click for CLI help output.

click_extra.mkdocs.ANSI_STYLESHEET = 'assets/click-extra/ansi.css'

Site-relative path of the generated ANSI color stylesheet.

Registered in the MkDocs config’s extra_css by AnsiColorPlugin.on_config() so every page links it, and written into the build output by AnsiColorPlugin.on_post_build().

class click_extra.mkdocs.AnsiColorPlugin[SomeConfig: Config][source]

Bases: BasePlugin

MkDocs plugin that adds ANSI color support to Pygments code blocks.

Monkey-patches pymdownx.highlight’s block and inline formatter classes to inherit from AnsiHtmlFormatter. This gives every code block in the MkDocs site full ANSI color rendering: compound tokens like Token.Ansi.Bold.Cyan are decomposed into individual CSS classes, and a generated stylesheet (written by on_post_build() and registered in extra_css by on_config()) supplies the color rules for the 256-color indexed palette and all SGR text attributes.

When mkdocs-click is installed, its code-block generators are also patched to use the ansi-output lexer so that CLI help text renders with colors.

on_config(config)[source]

Patch pymdownx.highlight formatters and register the ANSI stylesheet.

Runs before page processing begins. Appending ANSI_STYLESHEET to extra_css makes MkDocs link it from every page (resolving the relative path per page, including under use_directory_urls). The file itself is written later, in on_post_build().

Return type:

MkDocsConfig

on_post_build(config)[source]

Write the ANSI color stylesheet into the built site.

Without it, the -Ansi-* classes the formatters patched in on_config() emit carry no color. Runs after the theme copied its own assets, so the file survives the site-directory cleanup.

Return type:

None