diff --git a/temp_index.py b/temp_index.py index 52a1e5a..95c96ab 100644 --- a/temp_index.py +++ b/temp_index.py @@ -13,6 +13,33 @@ from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer CWD = Path().resolve() +from html.parser import HTMLParser +from io import StringIO + + +class MLStripper(HTMLParser): + """https://stackoverflow.com/a/925630""" + + def __init__(self): + super().__init__() + self.reset() + self.strict = False + self.convert_charrefs = True + self.text = StringIO() + + def handle_data(self, d): + self.text.write(d) + + def get_data(self): + return self.text.getvalue() + + +def strip_tags(html): + """HTML stripper.""" + s = MLStripper() + s.feed(html) + return s.get_data() + def make_cards(filepath, db_path, side_a, side_b): """The main entrypoint for card generation.""" @@ -26,13 +53,13 @@ def make_cards(filepath, db_path, side_a, side_b): def select_fields(fields, content, styles, book): if "title" in fields: tag = "{}".format(book.title) - ptitle = Paragraph(tag, styles["Italic"]) + ptitle = Paragraph(strip_tags(tag), styles["Italic"]) content.append(ptitle) content.append(Spacer(1, 12)) if "timestamp" in fields: tag = "Timestamp: {}".format(book.timestamp) - ptime = Paragraph(tag, styles["Normal"]) + ptime = Paragraph(strip_tags(tag), styles["Normal"]) content.append(ptime) content.append(Spacer(1, 12)) @@ -44,7 +71,7 @@ def select_fields(fields, content, styles, book): ] ) tag = "{}".format(comments) - pcomments = Paragraph(tag) + pcomments = Paragraph(strip_tags(tag)) content.append(pcomments) if "authors" in fields: @@ -52,7 +79,7 @@ def select_fields(fields, content, styles, book): all_authors = [author.name for author in book.authors] glued_together = format_string.format(", ".join(all_authors)) - p = Paragraph(glued_together, styles["Normal"]) + p = Paragraph(strip_tags(glued_together), styles["Normal"]) content.append(p) content.append(Spacer(6, 12)) @@ -61,7 +88,7 @@ def select_fields(fields, content, styles, book): all_tags = [tag.name for tag in book.tags] tags_glued_together = format_string.format(", ".join(all_tags)) - p = Paragraph(tags_glued_together, styles["Normal"]) + p = Paragraph(strip_tags(tags_glued_together), styles["Normal"]) content.append(p) content.append(Spacer(6, 12))