Skip to content

mkdocs_plugin

A plugin for MkDocs that allows generating & inserting SVGs using Termage.

TermageOptions dataclass

Options passed into the Termage plugin.

Source code in termage/mkdocs_plugin.py
48
49
50
51
52
53
54
55
56
57
58
59
60
@dataclass
class TermageOptions:  # pylint: disable=too-many-instance-attributes
    """Options passed into the Termage plugin."""

    title: str
    width: int
    height: int
    include: str
    foreground: str
    background: str
    chrome: bool
    tabs: tuple[str, str]
    highlight: bool

TermagePlugin

Bases: BasePlugin

An mkdocs plugin for Termage.

Source code in termage/mkdocs_plugin.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
class TermagePlugin(BasePlugin):
    """An mkdocs plugin for Termage."""

    config_scheme = (
        # File configuration
        ("write_files", Type(bool, default=False)),
        ("inline_styles", Type(bool, default=True)),
        ("path", Type(str, default="assets")),
        ("name_template", Type(str, default="termage_{count}.svg")),
        # SVG content configuration
        ("background", Type(str, default="#212121")),
        ("foreground", Type(str, default="#dddddd")),
        ("tabs", Type(list, default=["Python", "Output"])),
        ("chrome", Type(bool, default=True)),
        ("width", Type(int, default=80)),
        ("height", Type(int, default=24)),
    )

    def __init__(self) -> None:
        """Sets the initial SVG count."""

        self._svg_count = 0

    def _get_next_path(self, title: str | None) -> str:
        """Gets the next SVG path."""

        base = self.config["path"]
        name_template = self.config["name_template"]
        name = name_template.format(
            count=self._svg_count,
            title=str(title),
        )

        return f"{base}/{name}"

    def parse_options(self, options: str) -> TermageOptions:
        """Parses the options given to a block."""

        opt_dict = {key: self.config.get(key, None) for key in OPTS}

        extra_opts = ""

        for option in re.split(r"(?<!\\) ", options):
            if len(option) == 0:
                continue

            try:
                key, value = option.split("=")
            except ValueError:
                extra_opts += " " + option
                continue

            value = value.replace("\\", "")

            if key not in opt_dict:
                extra_opts += " " + option
                continue

            original = opt_dict[key]
            if isinstance(original, bool):
                value = value.lower() in ("1", "true", "yes")

            elif isinstance(original, int):
                value = int(value)

            elif isinstance(opt_dict[key], list):
                value = value.split(",")

            opt_dict[key] = value

        opt_dict["title"] = opt_dict["title"] or ""

        return TermageOptions(**opt_dict), extra_opts  # type: ignore

    def replace(self, matchobj: Match) -> str:  # pylint: disable=too-many-locals
        """Replaces a code block match with a generated SVG."""

        full, indentation, svg_only, options, code = matchobj.groups()
        indent_len = len(indentation)

        if indentation.endswith("\\"):
            return full.replace(r"\`", "`", 1)

        opts, extra_opts = self.parse_options(options)
        set_colors(opts.foreground, opts.background)

        if opts.include is not None:
            with open(opts.include, "r", encoding="utf-8") as includefile:
                included = ""
                for line in includefile:
                    if line.startswith("&"):
                        included += "&" + indentation + line[1:]
                        continue

                    included += indentation + line

                code = included + code

            opts.title = opts.title or opts.include

        code_disp, code_exec = format_codeblock(code)

        with patched_stdout_recorder(opts.width, opts.height) as recording:
            execute(code=code_exec, highlight=opts.highlight)

        svg = (
            recording.export_svg(
                title=opts.title,
                chrome=opts.chrome,
                prefix=f"termage-{self._svg_count}",
                inline_styles=self.config["inline_styles"],
            )
            .replace("_", r"\_")
            .replace("`", r"\`")
            .replace("*", r"\*")
        )

        self._svg_count += 1
        style = "margin-top: -1em;" if not opts.chrome else ""

        if self.config["write_files"]:
            filepath = self._get_next_path(opts.title)

            with open(Path("docs") / filepath, "w", encoding="utf-8") as export:
                export.write(svg)

            img_tag = (
                f"<img alt='{opts.title}' src='/{filepath}' style='{style}'></img>"
            )

            if svg_only:
                return img_tag

            return indent(
                TAB_TEMPLATE.format(
                    code_tab=opts.tabs[0],
                    extra_opts=extra_opts,
                    svg_tab=opts.tabs[1],
                    code=indent(code_disp, amount=4),
                    content=img_tag,
                ),
                amount=indent_len,
            )

        if style != "":
            svg = svg[: len("<svg ")] + f"style='{style}' " + svg[len("<svg ") :]

        if svg_only:
            return indent(svg, indent_len)

        return indent(
            TAB_TEMPLATE.format(
                code_tab=opts.tabs[0],
                extra_opts=extra_opts,
                svg_tab=opts.tabs[1],
                code=indent(code_disp, amount=4),
                content=svg,
            ),
            amount=indent_len,
        )

    def on_page_markdown(  # pylint: disable=unused-argument
        self, markdown, page, files, config
    ) -> str:
        """Replaces the termage markdown syntax."""

        return RE_BLOCK.sub(self.replace, markdown)

__init__()

Source code in termage/mkdocs_plugin.py
81
82
83
84
def __init__(self) -> None:
    """Sets the initial SVG count."""

    self._svg_count = 0

on_page_markdown(markdown, page, files, config)

Replaces the termage markdown syntax.

Source code in termage/mkdocs_plugin.py
224
225
226
227
228
229
def on_page_markdown(  # pylint: disable=unused-argument
    self, markdown, page, files, config
) -> str:
    """Replaces the termage markdown syntax."""

    return RE_BLOCK.sub(self.replace, markdown)

parse_options(options)

Parses the options given to a block.

Source code in termage/mkdocs_plugin.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def parse_options(self, options: str) -> TermageOptions:
    """Parses the options given to a block."""

    opt_dict = {key: self.config.get(key, None) for key in OPTS}

    extra_opts = ""

    for option in re.split(r"(?<!\\) ", options):
        if len(option) == 0:
            continue

        try:
            key, value = option.split("=")
        except ValueError:
            extra_opts += " " + option
            continue

        value = value.replace("\\", "")

        if key not in opt_dict:
            extra_opts += " " + option
            continue

        original = opt_dict[key]
        if isinstance(original, bool):
            value = value.lower() in ("1", "true", "yes")

        elif isinstance(original, int):
            value = int(value)

        elif isinstance(opt_dict[key], list):
            value = value.split(",")

        opt_dict[key] = value

    opt_dict["title"] = opt_dict["title"] or ""

    return TermageOptions(**opt_dict), extra_opts  # type: ignore

replace(matchobj)

Replaces a code block match with a generated SVG.

Source code in termage/mkdocs_plugin.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def replace(self, matchobj: Match) -> str:  # pylint: disable=too-many-locals
    """Replaces a code block match with a generated SVG."""

    full, indentation, svg_only, options, code = matchobj.groups()
    indent_len = len(indentation)

    if indentation.endswith("\\"):
        return full.replace(r"\`", "`", 1)

    opts, extra_opts = self.parse_options(options)
    set_colors(opts.foreground, opts.background)

    if opts.include is not None:
        with open(opts.include, "r", encoding="utf-8") as includefile:
            included = ""
            for line in includefile:
                if line.startswith("&"):
                    included += "&" + indentation + line[1:]
                    continue

                included += indentation + line

            code = included + code

        opts.title = opts.title or opts.include

    code_disp, code_exec = format_codeblock(code)

    with patched_stdout_recorder(opts.width, opts.height) as recording:
        execute(code=code_exec, highlight=opts.highlight)

    svg = (
        recording.export_svg(
            title=opts.title,
            chrome=opts.chrome,
            prefix=f"termage-{self._svg_count}",
            inline_styles=self.config["inline_styles"],
        )
        .replace("_", r"\_")
        .replace("`", r"\`")
        .replace("*", r"\*")
    )

    self._svg_count += 1
    style = "margin-top: -1em;" if not opts.chrome else ""

    if self.config["write_files"]:
        filepath = self._get_next_path(opts.title)

        with open(Path("docs") / filepath, "w", encoding="utf-8") as export:
            export.write(svg)

        img_tag = (
            f"<img alt='{opts.title}' src='/{filepath}' style='{style}'></img>"
        )

        if svg_only:
            return img_tag

        return indent(
            TAB_TEMPLATE.format(
                code_tab=opts.tabs[0],
                extra_opts=extra_opts,
                svg_tab=opts.tabs[1],
                code=indent(code_disp, amount=4),
                content=img_tag,
            ),
            amount=indent_len,
        )

    if style != "":
        svg = svg[: len("<svg ")] + f"style='{style}' " + svg[len("<svg ") :]

    if svg_only:
        return indent(svg, indent_len)

    return indent(
        TAB_TEMPLATE.format(
            code_tab=opts.tabs[0],
            extra_opts=extra_opts,
            svg_tab=opts.tabs[1],
            code=indent(code_disp, amount=4),
            content=svg,
        ),
        amount=indent_len,
    )

indent(text, amount)

Indents the text by the given amount.

Works multiline too!

Source code in termage/mkdocs_plugin.py
39
40
41
42
43
44
45
def indent(text: str, amount: int) -> str:
    """Indents the text by the given amount.

    Works multiline too!"""

    pad = amount * " "
    return "\n".join(pad + line for line in text.splitlines())