docs: syntax highlighting in reference

This commit is contained in:
phaer 2024-06-18 09:10:48 +02:00
parent e62c7b759a
commit 4175449e93
2 changed files with 10 additions and 4 deletions

View File

@ -9,6 +9,11 @@ from mkdocs.structure.files import Files
from mkdocs.config.defaults import MkDocsConfig
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
log = logging.getLogger("mkdocs")
@ -16,8 +21,8 @@ def is_reference_page(page: Page) -> bool:
return page.file.src_path.startswith("reference/")
def slugify(name: str) -> str:
return name.lower().replace(".", "-")
def pygments(code: str, lang: str) -> str:
return highlight(code, get_lexer_by_name(lang), HtmlFormatter())
def sort_options(item: Tuple[str, Dict]):
@ -60,6 +65,7 @@ def on_page_markdown(
return markdown
src_path = Path(config.docs_dir) / page.file.src_path
env = config.theme.get_env()
env.filters["pygments"] = pygments
header = env.get_template("reference_header.html").render(meta=page.meta)

View File

@ -18,14 +18,14 @@
{%- if option.default -%}
<tr>
<td>default</td>
<td><pre>{{(option.default | default({})).text}}</pre></td>
<td><pre>{{(option.default | default({})).text | pygments("nix")}}</pre></td>
</tr>
{%- endif -%}
{%- if option.example -%}
<tr>
<td>example</td>
<td>
<pre>{{(option.example | default({})).text}}</pre>
<pre>{{(option.example | default({})).text | pygments("nix")}}</pre>
</td>
</tr>
{%- endif -%}