Skip to content

plugin ¤

Configuration options for the MkDocs Manpage plugin.

Classes:

PygmentsConfig ¤

Bases: Config

Configuration options for the plugin.

PygmentsPlugin ¤

PygmentsPlugin()

Bases: BasePlugin[PygmentsConfig]

The MkDocs plugin to inject Pygments style sheets.

Methods:

  • on_config

    Inject dark and light style sheets in extra_css.

  • on_post_build

    Write the CSS contents to the injected style sheets.

Source code in src/mkdocs_pygments/plugin.py
75
76
77
def __init__(self):
    """Initialize the plugin."""
    self.styles = None

on_config ¤

on_config(config: MkDocsConfig) -> MkDocsConfig | None

Inject dark and light style sheets in extra_css.

Parameters:

  • config (MkDocsConfig) –

    The MkDocs config object.

Returns:

  • MkDocsConfig | None

    The config.

Source code in src/mkdocs_pygments/plugin.py
79
80
81
82
83
84
85
86
87
88
89
90
def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
    """Inject dark and light style sheets in `extra_css`.

    Arguments:
        config: The MkDocs config object.

    Returns:
        The config.
    """
    self.styles = _get_styles()
    config.extra_css.insert(0, self.css_filename)
    return config

on_post_build ¤

on_post_build(config: MkDocsConfig, **kwargs: Any) -> None

Write the CSS contents to the injected style sheets.

Parameters:

  • config (MkDocsConfig) –

    MkDocs configuration.

Source code in src/mkdocs_pygments/plugin.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def on_post_build(self, config: MkDocsConfig, **kwargs: Any) -> None:  # noqa: ARG002
    """Write the CSS contents to the injected style sheets.

    Parameters:
        config: MkDocs configuration.
    """
    try:
        light_css = _theme_css(
            self.styles[self.config.light],
            '[data-md-color-scheme="default"]',
            respect_background=self.config.respect_light_background,
        )
        dark_css = _theme_css(
            self.styles[self.config.dark],
            '[data-md-color-scheme="slate"]',
            respect_background=self.config.respect_dark_background,
        )
    except KeyError as error:
        available = ", ".join(self.styles.keys())
        raise PluginError(f"pygments: Unknown theme: {error}. Available themes: {available}") from error
    css_contents = f"{light_css}\n\n{dark_css}\n\n{_pygments_css()}"
    write_file(
        css_contents.encode("utf-8"),
        os.path.join(config.site_dir, self.css_filename.format(theme="base")),
    )