def __str__(self):
return f"<multipart/{self.subtype}> children={len(self.children)}"
+ def __hash__(self):
+ return hash(str(self.subtype) + "".join(str(self.children)))
+
def filewriter_fn(path, content, mode="w", **kwargs):
with open(path, mode, **kwargs) as out_f:
):
relparts = []
for path, info in images.items():
+ if path.startswith("cid:"):
+ continue
+
data = request.urlopen(path)
mimetype = data.headers["Content-Type"]
class MIMETreeDFWalker:
def __init__(self, *, visitor_fn=None, debug=False):
- self._visitor_fn = visitor_fn
+ self._visitor_fn = visitor_fn or self._echovisit
self._debug = debug
+ def _echovisit(self, node, ancestry, debugprint):
+ debugprint(f"node={node} ancestry={ancestry}")
+
def walk(self, root, *, visitor_fn=None):
"""
Recursive function to implement a depth-dirst walk of the MIME-tree
rooted at `root`.
"""
-
if isinstance(root, list):
- root = Multipart("mixed", children=root)
+ if len(root) > 1:
+ root = Multipart("mixed", children=root)
+ else:
+ root = root[0]
self._walk(
root,
assert items[4][0].subtype == "relative"
assert items[4][1] == 0
- def test_MIMETreeDFWalker_list_to_mixed(self, basic_mime_tree):
+ def test_MIMETreeDFWalker_list_to_mixed(self, const1):
mimetree = MIMETreeDFWalker()
items = []
def visitor_fn(item, stack, debugprint):
items.append(item)
- mimetree.walk([basic_mime_tree], visitor_fn=visitor_fn)
+ p = Part("text", "plain", const1)
+ mimetree.walk([p], visitor_fn=visitor_fn)
+ assert items[-1].subtype == "plain"
+ mimetree.walk([p, p], visitor_fn=visitor_fn)
assert items[-1].subtype == "mixed"
def test_MIMETreeDFWalker_visitor_in_constructor(
assert tree.children[1].path == written[0]
assert written[1] == request.urlopen(test_png).read()
+ def test_converter_tree_inline_image_cid(
+ self, const1, fake_filewriter
+ ):
+ text = f"![inline base64 image](cid:{const1})"
+ path = pathlib.Path(const1)
+ tree = convert_markdown_to_html(
+ text,
+ path,
+ filewriter_fn=fake_filewriter,
+ related_to_html_only=False,
+ )
+ assert len(tree.children) == 2
+ assert tree.children[0].cid != const1
+ assert tree.children[0].type != "image"
+ assert tree.children[1].cid != const1
+ assert tree.children[1].type != "image"
+
def test_inline_image_collection(
self, test_png, const1, const2, fake_filewriter
):