Code formatting
This commit is contained in:
parent
1a87464861
commit
2d845ed5cd
3 changed files with 39 additions and 21 deletions
|
@ -72,7 +72,7 @@ class CLI:
|
|||
|
||||
thing_overrides = {"rendered": "page", "ran": "command"}
|
||||
res_parts = []
|
||||
for (key, count) in get_counts().items():
|
||||
for key, count in get_counts().items():
|
||||
s = "" if count == 1 else "s"
|
||||
res_parts.append(f"{key} {count} {thing_overrides.get(key, 'file')}{s}")
|
||||
|
||||
|
|
|
@ -50,17 +50,21 @@ class LazyLoadingImageHTMLRenderer(mistune.HTMLRenderer):
|
|||
s = '<img loading="lazy" src="' + src + '" alt="' + alt + '"'
|
||||
if title:
|
||||
s += ' title="' + mistune.util.safe_entity(title) + '"'
|
||||
return s + ' />'
|
||||
return s + " />"
|
||||
|
||||
|
||||
class CustomHTMLRenderer(LevelAdjustingHTMLRenderer, SyntaxHighlightingHTMLRenderer, LazyLoadingImageHTMLRenderer):
|
||||
class CustomHTMLRenderer(
|
||||
LevelAdjustingHTMLRenderer,
|
||||
SyntaxHighlightingHTMLRenderer,
|
||||
LazyLoadingImageHTMLRenderer,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class CustomMarkdown(mistune.Markdown):
|
||||
def parse(
|
||||
self, s: str, state: None | mistune.BlockState = None
|
||||
) -> tuple[str | list[dict[str, Any]], mistune.BlockState]:
|
||||
self, s: str, state: None | mistune.BlockState = None
|
||||
) -> tuple[str | list[dict[str, Any]], mistune.BlockState]:
|
||||
r, state = super().parse(s, state=state)
|
||||
r = '<div class="rendered-markdown">' + r + "</div>"
|
||||
return r, state
|
||||
|
@ -80,7 +84,9 @@ def create(
|
|||
[mistune.directives.Figure(), mistune.directives.TableOfContents()]
|
||||
),
|
||||
],
|
||||
renderer=CustomHTMLRenderer(header_level_delta=header_level_delta, escape=escape),
|
||||
renderer=CustomHTMLRenderer(
|
||||
header_level_delta=header_level_delta, escape=escape
|
||||
),
|
||||
)
|
||||
|
||||
mistune.toc.add_toc_hook(r)
|
||||
|
@ -91,4 +97,6 @@ def create(
|
|||
|
||||
def render_toc_from_state(render_state: dict[str, Any], min_level: int = 1) -> str:
|
||||
# hilarious note: if you supply a raw filter object, this dies. hence the cast to list. see https://github.com/lepture/mistune/pull/407
|
||||
return mistune.toc.render_toc_ul(list(filter(lambda x: x[0] >= min_level, render_state.env["toc_items"])))
|
||||
return mistune.toc.render_toc_ul(
|
||||
list(filter(lambda x: x[0] >= min_level, render_state.env["toc_items"]))
|
||||
)
|
||||
|
|
|
@ -73,7 +73,7 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
|
|||
markdown_to_html = markdown.create(escape=False)
|
||||
|
||||
walk_dir = base_dir / "content"
|
||||
for (fpath, filetype) in _walk_content(walk_dir):
|
||||
for fpath, filetype in _walk_content(walk_dir):
|
||||
site_inner_path = fpath.relative_to(
|
||||
walk_dir
|
||||
) # the path of the file *inside* a site directory structure (eg. inside of `_dist` or inside of `content`)
|
||||
|
@ -123,9 +123,11 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
|
|||
internal_site_path = target_path.relative_to(output_dir)
|
||||
tpl_frontmatter["canonicalURL"] = _make_canonical_url(
|
||||
site_config,
|
||||
internal_site_path.parent
|
||||
if internal_site_path.name.lower() == "index.html"
|
||||
else internal_site_path,
|
||||
(
|
||||
internal_site_path.parent
|
||||
if internal_site_path.name.lower() == "index.html"
|
||||
else internal_site_path
|
||||
),
|
||||
)
|
||||
|
||||
ctx["page"] = tpl_frontmatter
|
||||
|
@ -147,13 +149,15 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
|
|||
ctx["rendered"], render_state = markdown_to_html.parse(raw_tpl)
|
||||
|
||||
tpl_str = '{% extends "_layouts/base.html" %}{% block main %}{{ rendered | safe }}{% endblock %}'
|
||||
|
||||
|
||||
if tpl_frontmatter.get("showToc", False):
|
||||
ctx["toc"] = markdown.render_toc_from_state(render_state, min_level = 2)
|
||||
ctx["toc"] = markdown.render_toc_from_state(
|
||||
render_state, min_level=2
|
||||
)
|
||||
tpl_str += '{% import "_imports/toc.html" as tc %}{% block aside %}{{ tc.render(toc) }}{% endblock %}'
|
||||
|
||||
tpl = jinja_env.from_string(tpl_str)
|
||||
|
||||
|
||||
case _:
|
||||
assert False, "impossible state"
|
||||
|
||||
|
@ -200,7 +204,7 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
|
|||
|
||||
walk_dir = base_dir / "blog"
|
||||
posts = {}
|
||||
for (fpath, filetype) in _walk_content(walk_dir):
|
||||
for fpath, filetype in _walk_content(walk_dir):
|
||||
inner_path = fpath.relative_to(walk_dir)
|
||||
|
||||
match filetype:
|
||||
|
@ -316,9 +320,11 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
|
|||
post["title"],
|
||||
post.get("description", ""),
|
||||
post["publishedDate"],
|
||||
None
|
||||
if "updatedDate" not in post or len(post["updatedDate"]) == 0
|
||||
else post["updatedDate"][0],
|
||||
(
|
||||
None
|
||||
if "updatedDate" not in post or len(post["updatedDate"]) == 0
|
||||
else post["updatedDate"][0]
|
||||
),
|
||||
"favourite" in (post_tags := (post["tags"] if "tags" in post else [])),
|
||||
post_tags,
|
||||
)
|
||||
|
@ -553,12 +559,14 @@ def compress_png(output_dir: Path):
|
|||
proc.check_returncode()
|
||||
update_counts("compressed", 1)
|
||||
|
||||
|
||||
def strip_exif(output_dir: Path):
|
||||
mogrify_exe: str | None = shutil.which("mogrify")
|
||||
|
||||
if mogrify_exe is None:
|
||||
rprint(
|
||||
WARN_LEADER + "cannot find mogrify (of imagemagick) executable, skipping EXIF data removal"
|
||||
WARN_LEADER
|
||||
+ "cannot find mogrify (of imagemagick) executable, skipping EXIF data removal"
|
||||
)
|
||||
return
|
||||
|
||||
|
@ -570,6 +578,8 @@ def strip_exif(output_dir: Path):
|
|||
output_dir.rglob("*.png"),
|
||||
output_dir.rglob("*.PNG"),
|
||||
):
|
||||
proc: subprocess.CompletedProcess = subprocess.run([mogrify_exe, "-strip", image])
|
||||
proc: subprocess.CompletedProcess = subprocess.run(
|
||||
[mogrify_exe, "-strip", image]
|
||||
)
|
||||
proc.check_returncode()
|
||||
update_counts("sanitised", 1)
|
||||
update_counts("sanitised", 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue