From 0265868c4d27b6c232a2bbba0408eb5f02ec2272 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 28 Jun 2024 15:40:22 +0900 Subject: Support maildir format --- src/formbox.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/formbox.py b/src/formbox.py index b9da5a1..a1e3a58 100644 --- a/src/formbox.py +++ b/src/formbox.py @@ -1,5 +1,5 @@ -# Format mbox as HTML/XML -# Copyright (C) 2021-2023 Nguyễn Gia Phong +# Format mailbox as HTML/XML +# Copyright (C) 2021-2024 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 @@ -19,7 +19,7 @@ from collections import defaultdict from email.header import decode_header from email.utils import parsedate_to_datetime from functools import partial -from mailbox import mbox +from mailbox import Maildir, mbox from pathlib import Path from urllib.parse import quote, unquote, urlencode @@ -33,6 +33,17 @@ sanitise = partial(clean, tags={'a', 'code', 'em', 'strong', 'sub', 'sup', 'irc', 'ircs', 'mailto', 'matrix', 'xmpp'}) +def mailbox(path): + """Parse and return the mailbox at given path. + + Supported formats are Maildir and mbox. + """ + try: + return mbox(path, create=False) + except IsADirectoryError: + return Maildir(path, create=False) + + def get_body(message): """Return the Markdown message body converted to HTML.""" if message.is_multipart(): @@ -86,14 +97,16 @@ def render(template, archive, parent): def main(): """Parse command-line arguments and pass them to routines.""" - parser = ArgumentParser(description='format mbox as HTML/XML') - parser.add_argument('mbox', type=mbox, help='path to mbox file') + parser = ArgumentParser(description='format mailbox as HTML/XML') + parser.add_argument('mailbox', type=mailbox, + help='path to mbox file or maildir') parser.add_argument('id', type=unquote, help='root message ID') parser.add_argument('template', type=Path, help='path to template') args = parser.parse_args() archive = defaultdict(list) - for message in args.mbox: archive[message['In-Reply-To']].append(message) + for message in args.mailbox: + archive[message['In-Reply-To']].append(message) template = args.template.read_text() print(*render(template, archive, args.id), sep='', end='') -- cgit 1.4.1