From cb35d1b5811aac349fd4d09bc3c0d666bd7ebeae Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 31 Dec 2021 23:14:08 +0700 Subject: Improve dependency injection --- .builds/auto.yml | 2 +- .builds/shell.nix | 11 ++++--- README.md | 2 +- _css/style.css | 12 +++++--- _layout/page_foot.html | 2 +- _libs/formbox/comment.html | 3 +- _libs/formbox/format | 76 ---------------------------------------------- _libs/minify | 42 ------------------------- _libs/postprocess | 42 +++++++++++++++++++++++++ utils.jl | 3 +- 10 files changed, 62 insertions(+), 133 deletions(-) delete mode 100755 _libs/formbox/format delete mode 100755 _libs/minify create mode 100755 _libs/postprocess diff --git a/.builds/auto.yml b/.builds/auto.yml index c5b9a7b..34756e1 100644 --- a/.builds/auto.yml +++ b/.builds/auto.yml @@ -19,7 +19,7 @@ tasks: - build: | cd site nix-shell .builds/shell.nix --run 'julia --project=@. .builds/franklin.jl' - nix-shell -p python3Packages.css-html-js-minify --run _libs/minify + nix-shell -p python3Packages.css-html-js-minify --run _libs/postprocess - deploy: | tar -cvzf site.tar.gz -C site/__site . acurl -f https://pages.sr.ht/publish/cnx.srht.site -F content=@site.tar.gz diff --git a/.builds/shell.nix b/.builds/shell.nix index 3f21d40..b73cc6c 100644 --- a/.builds/shell.nix +++ b/.builds/shell.nix @@ -1,12 +1,15 @@ with import {}; mkShell { nativeBuildInputs = [ - julia-bin nodejs nodePackages.katex - ] ++ (with python3Packages; [ bleach markdown ]); + julia-bin nodejs nodePackages.katex python3Packages.formbox + ]; shellHook = '' npm install highlight.js # https://github.com/NixOS/nixpkgs/issues/152201 - ln -fns ${nodePackages.katex}/lib/node_modules/katex/dist _libs/katex - ln -fs _libs/katex/{fonts,katex.min.css} _css + katex=${nodePackages.katex}/lib/node_modules/katex/dist + install -m 755 -d _css/fonts _libs/katex + install -m 644 $katex/katex.min.css _css + install -m 644 $katex/fonts/* _css/fonts + install -m 644 $katex/{katex.min.js,contrib/auto-render.min.js} _libs/katex ''; } diff --git a/README.md b/README.md index 7030b3d..e639285 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is my Franklin-generated static site, *without* client-side scripts. ## Building julia --project=@. .builds/franklin.jl - _libs/minify + _libs/postprocess ### Explicit dependencies diff --git a/_css/style.css b/_css/style.css index af502d7..fa15219 100644 --- a/_css/style.css +++ b/_css/style.css @@ -20,6 +20,7 @@ --block-bg: var(--black); --fade-fg: var(--bright-black); --link-fg: var(--green); + --overlay-bg: #8881; --text-bg: #121212; --text-fg: var(--bright-white); } @@ -42,7 +43,7 @@ html { font-size: min(max(100%, 2vw), 150%); margin: auto; max-width: 36rem; - scrollbar-color: var(--fade-fg) var(--block-bg); + scrollbar-color: var(--fade-fg) var(--overlay-bg); } body { margin: 0 1rem } @@ -50,7 +51,7 @@ body { margin: 0 1rem } .franklin-content .row { display: block } /* Text geometry */ -.franklin-content p { +p, details { hyphens: auto; line-height: 1.4rem; text-align: justify; @@ -129,7 +130,7 @@ body { margin: 0 1rem } .note p:first-child { font-weight: bold } .franklin-content blockquote { - background: var(--block-bg); + background: var(--overlay-bg); border-left: 0.25rem solid var(--fade-fg); } @@ -228,13 +229,14 @@ code, .hljs { .hljs-addition { color: var(--red) } .comment { - background-color: #8881; + background-color: var(--overlay-bg); clear: both; margin: 1ex 0; overflow: hidden; padding: 1ex; } .comment p { margin: 1ex } +.comment blockquote, .comment .note { margin: -1ex -1ex 0 } .openring { display: flex; @@ -244,7 +246,7 @@ code, .hljs { } .openring h3 { margin: 0 0 1ex } .openring article { - background: var(--block-bg); + background: var(--overlay-bg); display: flex; flex-direction: column; flex: 1 1 0; diff --git a/_layout/page_foot.html b/_layout/page_foot.html index f6745c4..bb10e5a 100644 --- a/_layout/page_foot.html +++ b/_layout/page_foot.html @@ -8,5 +8,5 @@ {{comments_rendered}}

Follow the anchor in an author's name to reply via plaintext email. Markdown - inline markups, block quotes and code blocks are supported.

{{end}} + inline markups, block quotes, lists and code blocks are supported.

{{end}} {{insert footer.html}} diff --git a/_libs/formbox/comment.html b/_libs/formbox/comment.html index 60459d9..fe7aa6a 100644 --- a/_libs/formbox/comment.html +++ b/_libs/formbox/comment.html @@ -1,6 +1,7 @@
{body} -

—{author}, +

{author}, {date}

{children} diff --git a/_libs/formbox/format b/_libs/formbox/format deleted file mode 100755 index bad40db..0000000 --- a/_libs/formbox/format +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python -# Format mbox as HTML/XML -# Copyright (C) 2021 Nguyễn Gia Phong -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -from argparse import ArgumentParser -from email.header import decode_header -from email.utils import parsedate_to_datetime -from functools import partial -from itertools import starmap -from mailbox import mbox -from pathlib import Path -from urllib.parse import quote, unquote - -from bleach import clean, linkify -from markdown import markdown - -sanitise = partial(clean, tags=('a', 'code', 'em', 'strong', 'sub', 'sup', - 'blockquote', 'p', 'pre', 'ul', 'ol', 'li'), - protocols=('ftp', 'gemini', 'gopher', 'http', 'https', - 'irc', 'ircs', 'mailto', 'matrix', 'xmpp')) - - -def extract(archive, parent): - """Recursively extract emails in reply to given message ID.""" - for message_id, message in archive.copy().items(): - # TODO: handle multipart - if message['In-Reply-To'] != parent: continue - archive.pop(message_id) - yield message, extract(archive, message_id) - - -def decode(header): - """Return the decoded email header.""" - for string, charset in decode_header(header): - encoding = 'utf-8' if charset is None else charset - yield string.decode(encoding) - - -def render(template, forest, parent): - """Render the thread recursively based on given template.""" - for self, children in forest: - message_id = self['Message-Id'] - date = parsedate_to_datetime(self['Date']).date().isoformat() - author, address = decode(self['From']) - body = sanitise(linkify(markdown(self.get_payload(), - output_format='html5'))) - rendered_children = render(template, children, message_id) - yield template.format(message_id=quote(message_id), - date=date, author=author, body=body, - children='\n'.join(rendered_children)) - - -if __name__ == '__main__': - parser = ArgumentParser() - parser.add_argument('mbox') - parser.add_argument('id', type=unquote) - parser.add_argument('template', type=Path) - args = parser.parse_args() - - archive = {m['Message-Id']: m for m in mbox(args.mbox)} - template = args.template.read_text() - print(*render(template, extract(archive, args.id), args.id), - sep='', end='') diff --git a/_libs/minify b/_libs/minify deleted file mode 100755 index 8416d3b..0000000 --- a/_libs/minify +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python -# Minify HTML and CSS, patch HTML and RSS and remove JS -# Copyright (C) 2021 Nguyễn Gia Phong -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -import re -from contextlib import suppress -from pathlib import Path -from shutil import rmtree - -from css_html_js_minify import html_minify, process_single_css_file - -FOOTNOTES_START = re.compile('(

)?\s*\s*(

)?') - - -def fix_footnotes(html): - """Work around https://github.com/tlienart/Franklin.jl/issues/936""" - return FOOTNOTES_START.sub('', html)) - - -site = Path(__file__).parent.parent / '__site' -process_single_css_file(site/'css'/'style.css', overwrite=True) -for html in site.rglob('*.html'): - print('Minifying and fixing up', html) - html.write_text(fix_footnotes(html_minify(html.read_text()))) -for rss in site.rglob('feed.xml'): - print('Fixing up', rss) - rss.write_text(fix_footnotes(rss.read_text())) -with suppress(FileNotFoundError): rmtree(site/'libs') diff --git a/_libs/postprocess b/_libs/postprocess new file mode 100755 index 0000000..8416d3b --- /dev/null +++ b/_libs/postprocess @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# Minify HTML and CSS, patch HTML and RSS and remove JS +# Copyright (C) 2021 Nguyễn Gia Phong +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import re +from contextlib import suppress +from pathlib import Path +from shutil import rmtree + +from css_html_js_minify import html_minify, process_single_css_file + +FOOTNOTES_START = re.compile('(

)?\s*\s*(

)?') + + +def fix_footnotes(html): + """Work around https://github.com/tlienart/Franklin.jl/issues/936""" + return FOOTNOTES_START.sub('', html)) + + +site = Path(__file__).parent.parent / '__site' +process_single_css_file(site/'css'/'style.css', overwrite=True) +for html in site.rglob('*.html'): + print('Minifying and fixing up', html) + html.write_text(fix_footnotes(html_minify(html.read_text()))) +for rss in site.rglob('feed.xml'): + print('Fixing up', rss) + rss.write_text(fix_footnotes(rss.read_text())) +with suppress(FileNotFoundError): rmtree(site/'libs') diff --git a/utils.jl b/utils.jl index a056c30..9c12c25 100644 --- a/utils.jl +++ b/utils.jl @@ -17,10 +17,9 @@ hfun_comments() = @sprintf("https://lists.sr.ht/~cnx/site?search=%s:%s", function render_comments(template) prefix = joinpath(path(:libs), "formbox") - format = joinpath(prefix, "format") mbox = joinpath(prefix, "mbox") template_path = joinpath(prefix, template) - readchomp(`$format $mbox $(message_id()) $template_path`) + readchomp(`formbox $mbox $(message_id()) $template_path`) end hfun_comments_rendered() = render_comments("comment.html") -- cgit 1.4.1