write cached pygments stylesheet to static

This commit is contained in:
Daniel Fichtinger 2025-07-03 01:55:53 -04:00
parent c6cd90001a
commit 5ea472e014
3 changed files with 27 additions and 6 deletions

View file

@ -96,7 +96,11 @@ class ZonaBuilder:
self.item_map = { self.item_map = {
item.source.resolve(): item for item in self.items item.source.resolve(): item for item in self.items
} }
# print(item_map) # write code highlighting stylesheet
pygments_style = zmd.get_style_defs(self.config)
(self.layout.content / "static" / "pygments.css").write_text(
pygments_style
)
for item in self.item_map.values(): for item in self.item_map.values():
dst = item.destination dst = item.destination
# print(item) # print(item)

View file

@ -10,6 +10,12 @@
type="text/css" type="text/css"
media="all" media="all"
/> />
<link
href="/static/pygments.css"
rel="stylesheet"
type="text/css"
media="all"
/>
</head> </head>
<body> <body>
<div id="container"> <div id="container">

View file

@ -97,11 +97,7 @@ class ZonaRenderer(HTMLRenderer):
except Exception: except Exception:
lexer = TextLexer(stripall=False) # type: ignore lexer = TextLexer(stripall=False) # type: ignore
formatter = HtmlFormatter( formatter = get_formatter(self.config)
style=config.theme,
nowrap=not config.wrap,
noclasses=True,
)
highlighted = highlight(code, lexer, formatter) # type: ignore highlighted = highlight(code, lexer, formatter) # type: ignore
return ( return (
@ -110,6 +106,14 @@ class ZonaRenderer(HTMLRenderer):
) )
def get_formatter(config: ZonaConfig):
c = config.markdown.syntax_highlighting
formatter = HtmlFormatter(
style=c.theme, nowrap=not c.wrap, nobackground=True
)
return formatter
def md_to_html( def md_to_html(
content: str, content: str,
config: ZonaConfig | None, config: ZonaConfig | None,
@ -134,3 +138,10 @@ def md_to_html(
item_map=item_map, item_map=item_map,
) )
return renderer.render(ast) return renderer.render(ast)
def get_style_defs(config: ZonaConfig) -> str:
formatter = get_formatter(config)
defs = formatter.get_style_defs("pre.code-block code")
assert isinstance(defs, str)
return defs