boxnotes2html

Convert Box's proprietary Box Notes to HTML, Markdown, or plain text
Log | Files | Refs | README | LICENSE

commit 7317a1746874dd8ef66200c60b2f3386c8c9b4f1
parent 55a1df9606283c6fba8eb65808211fc146e18e6b
Author: Alex Hayes <alex.hayes@rea-group.com>
Date:   Tue, 14 Jul 2020 15:22:00 +1000

Ensure markdown headers always appear at the start of a line - this avoids a header being wrapped in bold or an underline (which is invalid markdown).

Diffstat:
Mboxnotes2html/boxnote.py | 14+++++++++++---
Mboxnotes2html/markdown.py | 15+++++++++++++--
2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/boxnotes2html/boxnote.py b/boxnotes2html/boxnote.py @@ -117,13 +117,21 @@ class FormattedText: for character in characters_to_escape: tmp = tmp.replace(character, "\\{}".format(character)) for line in tmp.split("\n"): + _prefix = "" + for box_attribute in self.attributes: if line or box_attribute[0] in ["list", "image", "link"]: - start, end = markdown.convert_simple_element_to_markdown( + start, end, prefix = markdown.convert_simple_element_to_markdown( box_attribute ) - line = start + line + end - out_text += line + if prefix: + # Hacky solution to ensure headers always appear at the start of the line + _prefix += start + line = line + end + else: + line = start + line + end + + out_text += _prefix + line out_text += "\n" * (self.num_linebreaks) return out_text diff --git a/boxnotes2html/markdown.py b/boxnotes2html/markdown.py @@ -4,11 +4,21 @@ Tools for converting Box Notes to Markdown from . import html -def convert_simple_element_to_markdown(box_attribute): +def convert_simple_element_to_markdown(box_attribute: (str, str)) -> (str, str, bool): + """ + Return the starting and ending markdown that should appear around some text. + + :param box_attribute: Tuple containing the attribute_type and attribute_value. + :return: A tuple containing the start, end and a boolean value that defines whether this particular start should + take precedence and be prefixed at the start of the line. + """ attribute_type = box_attribute[0] attribute_value = box_attribute[1] start = "" end = "" + # If True then it signifies that this value should appear at the start of the line. + prefix = None + if not attribute_type: start = end = "" elif attribute_type == "bold": @@ -21,6 +31,7 @@ def convert_simple_element_to_markdown(box_attribute): sizemap = {"small": "", "medium": "", "large": "## ", "verylarge": "# "} size = attribute_type.split("-")[-1] start = sizemap[size] + prefix = True elif attribute_type.startswith("link-"): start = "[" end = "]({})".format(html._decode_link(attribute_type)) @@ -40,4 +51,4 @@ def convert_simple_element_to_markdown(box_attribute): else: formatter = '* ' start = " " * (level - 1) + formatter - return start, end + return start, end, prefix