Fix HTML export of markdown (with newlines, tables and other styling preserved)

This commit is contained in:
Miguel Jacq 2025-11-27 09:57:14 +11:00
parent 917e51aa0b
commit 5a54d809ed
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
4 changed files with 41 additions and 4 deletions

View file

@ -3,6 +3,7 @@
* Update icon again to remove background * Update icon again to remove background
* Adjust History icon and reorder toolbar items * Adjust History icon and reorder toolbar items
* Try to address checkbox/bullet size issues (again) * Try to address checkbox/bullet size issues (again)
* Fix HTML export of markdown (with newlines, tables and other styling preserved)
# 0.5.1 # 0.5.1

View file

@ -5,6 +5,7 @@ import datetime as _dt
import hashlib import hashlib
import html import html
import json import json
import markdown
import re import re
from dataclasses import dataclass from dataclasses import dataclass
@ -440,14 +441,33 @@ class DBManager:
'<html lang="en">', '<html lang="en">',
'<meta charset="utf-8">', '<meta charset="utf-8">',
f"<title>{html.escape(title)}</title>", f"<title>{html.escape(title)}</title>",
"<style>body{font:16px/1.5 system-ui,Segoe UI,Roboto,Helvetica,Arial,sans-serif;padding:24px;max-width:900px;margin:auto;}", "<style>"
"article{padding:16px 0;border-bottom:1px solid #ddd;} time{font-weight:600;color:#333;} section{margin-top:8px;}</style>", "body{font:16px/1.5 system-ui,Segoe UI,Roboto,Helvetica,Arial,sans-serif;"
"padding:24px;max-width:900px;margin:auto;}"
"article{padding:16px 0;border-bottom:1px solid #ddd;}"
"article header time{font-weight:600;color:#333;}"
"section{margin-top:8px;}"
"table{border-collapse:collapse;margin-top:8px;}"
"th,td{border:1px solid #ddd;padding:4px 8px;text-align:left;}"
"</style>",
"<body>", "<body>",
f"<h1>{html.escape(title)}</h1>", f"<h1>{html.escape(title)}</h1>",
] ]
for d, c in entries: for d, c in entries:
body_html = markdown.markdown(
c,
extensions=[
"extra",
"nl2br",
],
output_format="html5",
)
parts.append( parts.append(
f"<article><header><time>{html.escape(d)}</time></header><section>{c}</section></article>" f"<article>"
f"<header><time>{html.escape(d)}</time></header>"
f"<section>{body_html}</section>"
f"</article>"
) )
parts.append("</body></html>") parts.append("</body></html>")

17
poetry.lock generated
View file

@ -307,6 +307,21 @@ files = [
{file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"}, {file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"},
] ]
[[package]]
name = "markdown"
version = "3.10"
description = "Python implementation of John Gruber's Markdown."
optional = false
python-versions = ">=3.10"
files = [
{file = "markdown-3.10-py3-none-any.whl", hash = "sha256:b5b99d6951e2e4948d939255596523444c0e677c669700b1d17aa4a8a464cb7c"},
{file = "markdown-3.10.tar.gz", hash = "sha256:37062d4f2aa4b2b6b32aefb80faa300f82cc790cb949a35b8caede34f2b68c0e"},
]
[package.extras]
docs = ["mdx_gh_links (>=0.2)", "mkdocs (>=1.6)", "mkdocs-gen-files", "mkdocs-literate-nav", "mkdocs-nature (>=0.6)", "mkdocs-section-index", "mkdocstrings[python]"]
testing = ["coverage", "pyyaml"]
[[package]] [[package]]
name = "packaging" name = "packaging"
version = "25.0" version = "25.0"
@ -744,4 +759,4 @@ zstd = ["zstandard (>=0.18.0)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">=3.10,<3.14" python-versions = ">=3.10,<3.14"
content-hash = "d5fd8ea759b6bd3f23336930bdce9241659256ed918ec31746787cc86e817235" content-hash = "86db2b4d6498ce9eba7fa9aedf9ce58fec6a63542b5f3bdb3cf6c4067e5b87df"

View file

@ -14,6 +14,7 @@ python = ">=3.10,<3.14"
pyside6 = ">=6.8.1,<7.0.0" pyside6 = ">=6.8.1,<7.0.0"
sqlcipher3-wheels = "^0.5.5.post0" sqlcipher3-wheels = "^0.5.5.post0"
requests = "^2.32.5" requests = "^2.32.5"
markdown = "^3.10"
[tool.poetry.scripts] [tool.poetry.scripts]
bouquin = "bouquin.__main__:main" bouquin = "bouquin.__main__:main"