Format a mimebundle being created by _make_info_unformatted into a real mimebundle
(self, bundle: UnformattedBundle)
| 611 | return dict(defaults, **formatted) |
| 612 | |
| 613 | def format_mime(self, bundle: UnformattedBundle) -> Bundle: |
| 614 | """Format a mimebundle being created by _make_info_unformatted into a real mimebundle""" |
| 615 | # Format text/plain mimetype |
| 616 | assert isinstance(bundle["text/plain"], list) |
| 617 | for item in bundle["text/plain"]: |
| 618 | assert isinstance(item, tuple) |
| 619 | |
| 620 | new_b: Bundle = {} |
| 621 | lines = [] |
| 622 | _len = max(len(h) for h, _ in bundle["text/plain"]) |
| 623 | |
| 624 | for head, body in bundle["text/plain"]: |
| 625 | body = body.strip("\n") |
| 626 | delim = "\n" if "\n" in body else " " |
| 627 | lines.append( |
| 628 | f"{self.__head(head+':')}{(_len - len(head))*' '}{delim}{body}" |
| 629 | ) |
| 630 | |
| 631 | new_b["text/plain"] = "\n".join(lines) |
| 632 | |
| 633 | if "text/html" in bundle: |
| 634 | assert isinstance(bundle["text/html"], list) |
| 635 | for item in bundle["text/html"]: |
| 636 | assert isinstance(item, tuple) |
| 637 | # Format the text/html mimetype |
| 638 | if isinstance(bundle["text/html"], (list, tuple)): |
| 639 | # bundle['text/html'] is a list of (head, formatted body) pairs |
| 640 | new_b["text/html"] = "\n".join( |
| 641 | f"<h1>{head}</h1>\n{body}" for (head, body) in bundle["text/html"] |
| 642 | ) |
| 643 | |
| 644 | for k in bundle.keys(): |
| 645 | if k in ("text/html", "text/plain"): |
| 646 | continue |
| 647 | else: |
| 648 | new_b[k] = bundle[k] # type:ignore |
| 649 | return new_b |
| 650 | |
| 651 | def _append_info_field( |
| 652 | self, |