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 --- _libs/formbox/comment.html | 3 +- _libs/formbox/format | 76 ---------------------------------------------- _libs/minify | 42 ------------------------- _libs/postprocess | 42 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 119 deletions(-) delete mode 100755 _libs/formbox/format delete mode 100755 _libs/minify create mode 100755 _libs/postprocess (limited to '_libs') 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') -- cgit 1.4.1