]> git.madduck.net Git - etc/neomutt.git/blobdiff - .config/neomutt/buildmimetree.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

confvars update to latest version
[etc/neomutt.git] / .config / neomutt / buildmimetree.py
index f10158fbe01ef0dadf0a5bc61477e03d74b0d39f..a27f64c1488bbf1417add4edae91f09a7127e992 100755 (executable)
@@ -11,7 +11,7 @@
 #       <enter-command> source '$my_confdir/buildmimetree.py \
 #       --tempdir $tempdir --extensions $my_mdwn_extensions \
 #       --css-file $my_confdir/htmlmail.css |'<enter>\
 #       <enter-command> source '$my_confdir/buildmimetree.py \
 #       --tempdir $tempdir --extensions $my_mdwn_extensions \
 #       --css-file $my_confdir/htmlmail.css |'<enter>\
-#       <enter-command> sourc e \$my_mdwn_postprocess_cmd_file<enter>\
+#       <enter-command> source \$my_mdwn_postprocess_cmd_file<enter>\
 #     " "Convert message into a modern MIME tree with inline images"
 #
 #     (Yes, we need to call source twice, as mutt only starts to process output
 #     " "Convert message into a modern MIME tree with inline images"
 #
 #     (Yes, we need to call source twice, as mutt only starts to process output
@@ -34,8 +34,8 @@
 # Latest version:
 #   https://git.madduck.net/etc/neomutt.git/blob_plain/HEAD:/.config/neomutt/buildmimetree.py
 #
 # Latest version:
 #   https://git.madduck.net/etc/neomutt.git/blob_plain/HEAD:/.config/neomutt/buildmimetree.py
 #
-# Copyright © 2023 martin f. krafft <madduck@madduck.net>
-# Released under the GPL-2+ licence, just like Mutt itself.
+# Copyright © 2023–24 martin f. krafft <madduck@madduck.net>
+# Released under the GPL-2+ licence, just like NeoMutt itself.
 #
 
 import sys
 #
 
 import sys
@@ -50,6 +50,7 @@ import bs4
 import xml.etree.ElementTree as etree
 import io
 import enum
 import xml.etree.ElementTree as etree
 import io
 import enum
+import warnings
 from contextlib import contextmanager
 from collections import namedtuple, OrderedDict
 from markdown.extensions import Extension
 from contextlib import contextmanager
 from collections import namedtuple, OrderedDict
 from markdown.extensions import Extension
@@ -71,7 +72,7 @@ def parse_cli_args(*args, **kwargs):
         )
     )
     parser.epilog = (
         )
     )
     parser.epilog = (
-        "Copyright © 2023 martin f. krafft <madduck@madduck.net>.\n"
+        "Copyright © 2023-24 martin f. krafft <madduck@madduck.net>.\n"
         "Released under the MIT licence"
     )
 
         "Released under the MIT licence"
     )
 
@@ -125,6 +126,11 @@ def parse_cli_args(*args, **kwargs):
         help="Only build, don't send the message",
     )
 
         help="Only build, don't send the message",
     )
 
+    parser.add_argument(
+        "--domain",
+        help="Domain to use in content IDs",
+    )
+
     parser.add_argument(
         "--tempdir",
         metavar="DIR",
     parser.add_argument(
         "--tempdir",
         metavar="DIR",
@@ -349,12 +355,12 @@ class ImageRegistry:
     def __init__(self):
         self._images = OrderedDict()
 
     def __init__(self):
         self._images = OrderedDict()
 
-    def register(self, path, description=None):
+    def register(self, path, description=None, *, domain=None):
         # path = str(pathlib.Path(path).expanduser())
         path = os.path.expanduser(path)
         if path.startswith("/"):
             path = f"file://{path}"
         # path = str(pathlib.Path(path).expanduser())
         path = os.path.expanduser(path)
         if path.startswith("/"):
             path = f"file://{path}"
-        cid = make_msgid()[1:-1]
+        cid = make_msgid(domain=domain)[1:-1]
         self._images[path] = InlineImageInfo(cid, description)
         return cid
 
         self._images[path] = InlineImageInfo(cid, description)
         return cid
 
@@ -446,7 +452,10 @@ def markdown_with_inline_image_support(
 
 
 try:
 
 
 try:
-    import pynliner
+    with warnings.catch_warnings():
+        # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1081037
+        warnings.filterwarnings("ignore", category=SyntaxWarning)
+        import pynliner
 
     _PYNLINER = True
 
 
     _PYNLINER = True
 
@@ -700,6 +709,7 @@ def convert_markdown_to_html(
     tempdir=None,
     extensions=None,
     extension_configs=None,
     tempdir=None,
     extensions=None,
     extension_configs=None,
+    domain=None,
 ):
     # TODO extension_configs need to be handled differently
     extension_configs = extension_configs or {}
 ):
     # TODO extension_configs need to be handled differently
     extension_configs = extension_configs or {}
@@ -734,7 +744,7 @@ def convert_markdown_to_html(
         for img in soup.find_all("img"):
             uri = img.attrs["src"]
             desc = img.attrs.get("title", img.attrs.get("alt"))
         for img in soup.find_all("img"):
             uri = img.attrs["src"]
             desc = img.attrs.get("title", img.attrs.get("alt"))
-            cid = image_registry.register(uri, desc)
+            cid = image_registry.register(uri, desc, domain=domain)
             img.attrs["src"] = f"cid:{cid}"
 
         htmlsig = str(soup)
             img.attrs["src"] = f"cid:{cid}"
 
         htmlsig = str(soup)
@@ -964,6 +974,7 @@ def do_massage(
     only_build=False,
     max_other_attachments=20,
     tempdir=None,
     only_build=False,
     max_other_attachments=20,
     tempdir=None,
+    domain=None,
     debug_commands=False,
     debug_walk=False,
 ):
     debug_commands=False,
     debug_walk=False,
 ):
@@ -985,6 +996,7 @@ def do_massage(
         related_to_html_only=related_to_html_only,
         tempdir=tempdir,
         extensions=extensions,
         related_to_html_only=related_to_html_only,
         tempdir=tempdir,
         extensions=extensions,
+        domain=domain,
     )
 
     mimetree = MIMETreeDFWalker(debug=debug_walk)
     )
 
     mimetree = MIMETreeDFWalker(debug=debug_walk)
@@ -1157,6 +1169,7 @@ if __name__ == "__main__":
                 max_other_attachments=args.max_number_other_attachments,
                 only_build=args.only_build,
                 tempdir=args.tempdir,
                 max_other_attachments=args.max_number_other_attachments,
                 only_build=args.only_build,
                 tempdir=args.tempdir,
+                domain=args.domain,
                 debug_commands=args.debug_commands,
                 debug_walk=args.debug_walk,
             )
                 debug_commands=args.debug_commands,
                 debug_walk=args.debug_walk,
             )
@@ -1868,6 +1881,15 @@ try:
             assert not cid.endswith(">")
             assert const1 in reg
 
             assert not cid.endswith(">")
             assert const1 in reg
 
+        @pytest.mark.imgproc
+        def test_image_registry_domain(self, const1, const2):
+            reg = ImageRegistry()
+            cid = reg.register(const1, domain=const2)
+            assert f"@{const2}" in cid
+            assert not cid.startswith("<")
+            assert not cid.endswith(">")
+            assert const1 in reg
+
         @pytest.mark.imgproc
         def test_image_registry_file_uri(self, const1):
             reg = ImageRegistry()
         @pytest.mark.imgproc
         def test_image_registry_file_uri(self, const1):
             reg = ImageRegistry()