refactor highlighting to use module level singletons
remove the repeated instantiation of TextLexer and HtmlFormatter in favor of a singleton
This commit is contained in:
+16
-6
@@ -15,6 +15,7 @@ from pygments import highlight as pygments_highlight
|
|||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
from pygments.lexers import get_lexer_by_name
|
from pygments.lexers import get_lexer_by_name
|
||||||
from pygments.lexers.special import TextLexer
|
from pygments.lexers.special import TextLexer
|
||||||
|
from pygments.util import ClassNotFound
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -26,12 +27,21 @@ class ContentError(RuntimeError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _highlight_code(code: str, lang: str, attrs: str) -> str:
|
text_lexer = TextLexer()
|
||||||
|
html_formatter = HtmlFormatter()
|
||||||
|
|
||||||
|
|
||||||
|
def highlight_style() -> str:
|
||||||
|
style = html_formatter.get_style_defs(".highlight") # type: ignore[no-untyped-call]
|
||||||
|
return cast(str, style)
|
||||||
|
|
||||||
|
|
||||||
|
def highlight(code: str, lang: str, attrs: str) -> str:
|
||||||
try:
|
try:
|
||||||
lexer = get_lexer_by_name(lang) if lang else TextLexer()
|
lexer = get_lexer_by_name(lang) if lang else text_lexer
|
||||||
except Exception:
|
except ClassNotFound:
|
||||||
lexer = TextLexer()
|
lexer = text_lexer
|
||||||
return pygments_highlight(code, lexer, HtmlFormatter())
|
return pygments_highlight(code, lexer, html_formatter)
|
||||||
|
|
||||||
|
|
||||||
def read_frontmatter(path: Path) -> dict[str, Any]:
|
def read_frontmatter(path: Path) -> dict[str, Any]:
|
||||||
@@ -127,7 +137,7 @@ class Article(Content):
|
|||||||
|
|
||||||
def render(self, site: Site) -> None:
|
def render(self, site: Site) -> None:
|
||||||
md = MarkdownIt()
|
md = MarkdownIt()
|
||||||
md.options["highlight"] = _highlight_code
|
md.options["highlight"] = highlight
|
||||||
self.content = Markup(md.render(self.read_body()))
|
self.content = Markup(md.render(self.read_body()))
|
||||||
|
|
||||||
template = site.jinja_env.get_template("article.html")
|
template = site.jinja_env.get_template("article.html")
|
||||||
|
|||||||
+2
-1
@@ -6,6 +6,7 @@ from omicron.ssg.content import (
|
|||||||
create_content,
|
create_content,
|
||||||
CONTENT_EXTENSIONS,
|
CONTENT_EXTENSIONS,
|
||||||
ContentError,
|
ContentError,
|
||||||
|
highlight_style,
|
||||||
)
|
)
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import yaml
|
import yaml
|
||||||
@@ -90,7 +91,7 @@ class Site:
|
|||||||
self.discover()
|
self.discover()
|
||||||
self.output_path.mkdir(parents=True, exist_ok=True)
|
self.output_path.mkdir(parents=True, exist_ok=True)
|
||||||
(self.output_path / "pygments.css").write_text(
|
(self.output_path / "pygments.css").write_text(
|
||||||
HtmlFormatter().get_style_defs(".highlight"), encoding="utf-8"
|
highlight_style(), encoding="utf-8"
|
||||||
)
|
)
|
||||||
for content in self.content:
|
for content in self.content:
|
||||||
content.render(self)
|
content.render(self)
|
||||||
|
|||||||
Reference in New Issue
Block a user