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-clickfor 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_cssbyAnsiColorPlugin.on_config()so every page links it, and written into the build output byAnsiColorPlugin.on_post_build().
- class click_extra.mkdocs.AnsiColorPlugin[SomeConfig: Config][source]¶
Bases:
BasePluginMkDocs plugin that adds ANSI color support to Pygments code blocks.
Monkey-patches
pymdownx.highlight’s block and inline formatter classes to inherit fromAnsiHtmlFormatter. This gives every code block in the MkDocs site full ANSI color rendering: compound tokens likeToken.Ansi.Bold.Cyanare decomposed into individual CSS classes, and a generated stylesheet (written byon_post_build()and registered inextra_cssbyon_config()) supplies the color rules for the 256-color indexed palette and all SGR text attributes.When
mkdocs-clickis installed, its code-block generators are also patched to use theansi-outputlexer 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_STYLESHEETtoextra_cssmakes MkDocs link it from every page (resolving the relative path per page, including underuse_directory_urls). The file itself is written later, inon_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 inon_config()emit carry no color. Runs after the theme copied its own assets, so the file survives the site-directory cleanup.- Return type: