| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
from ... import text, util |
| 10 |
|
| 11 |
|
| 12 |
def to_html(markup): |
| 13 |
html = [] |
| 14 |
|
| 15 |
data = util.json_loads(markup) |
| 16 |
for block in data["content"]: |
| 17 |
process_content(html, block) |
| 18 |
|
| 19 |
return "".join(html) |
| 20 |
|
| 21 |
|
| 22 |
def process_content(html, content): |
| 23 |
type = content["type"] |
| 24 |
|
| 25 |
if type == "paragraph": |
| 26 |
if children := content.get("content"): |
| 27 |
html.append('<p>') |
| 28 |
for block in children: |
| 29 |
process_content(html, block) |
| 30 |
html.append("</p>") |
| 31 |
else: |
| 32 |
html.append("<p></p>") |
| 33 |
|
| 34 |
elif type == "text": |
| 35 |
process_text(html, content) |
| 36 |
|
| 37 |
elif type == "heading": |
| 38 |
if attrs := content.get("attrs"): |
| 39 |
level = str(attrs.get("level") or "3") |
| 40 |
else: |
| 41 |
level = "3" |
| 42 |
html.append(f"<h{level}>") |
| 43 |
process_children(html, content) |
| 44 |
html.append(f"</h{level}>") |
| 45 |
|
| 46 |
elif type in {"listItem", "bulletList", "orderedList", "blockquote"}: |
| 47 |
c = type[1] |
| 48 |
tag = ( |
| 49 |
"li" if c == "i" else |
| 50 |
"ul" if c == "u" else |
| 51 |
"ol" if c == "r" else |
| 52 |
"blockquote" |
| 53 |
) |
| 54 |
html.append(f"<{tag}>") |
| 55 |
process_children(html, content) |
| 56 |
html.append(f"</{tag}>") |
| 57 |
|
| 58 |
elif type == "image": |
| 59 |
if (attrs := content.get("attrs")) and (src := attrs.get("src")): |
| 60 |
html.append('<div data-image-container="true"') |
| 61 |
if align := attrs.get("alignment"): |
| 62 |
html.append(f' data-alignment="{text.escape(align)}"') |
| 63 |
html.append(f'><figure><img src="{text.escape(src)}"') |
| 64 |
if mid := attrs.get("media_id"): |
| 65 |
html.append(f' data-media-id="{text.escape(mid)}"') |
| 66 |
html.append('/></figure></div>') |
| 67 |
|
| 68 |
elif type == "video": |
| 69 |
if (attrs := content.get("attrs")) and (mid := attrs.get("media_id")): |
| 70 |
mid = text.escape(mid) |
| 71 |
html.append('<div data-video-container="true"') |
| 72 |
if align := attrs.get("alignment"): |
| 73 |
html.append(f' data-alignment="{text.escape(align)}"') |
| 74 |
html.append('><video src="') |
| 75 |
html.append(f'https://www.patreon.com/api/video/{mid}/video.m3u8') |
| 76 |
html.append(f'" data-media-id="{mid} controls playsinline') |
| 77 |
html.append('></video></div>') |
| 78 |
|
| 79 |
elif type == "caption": |
| 80 |
html.append('<p class="caption" style="margin: 20px 0;">') |
| 81 |
process_children(html, content) |
| 82 |
html.append("</p>") |
| 83 |
|
| 84 |
elif type == "link": |
| 85 |
if (attrs := content.get("attrs")) and (href := attrs.get("href")): |
| 86 |
html.append(f'<a href="{text.escape(href)}">') |
| 87 |
process_children(html, content) |
| 88 |
html.append("</a>") |
| 89 |
|
| 90 |
elif type == "hardBreak": |
| 91 |
html.append("<br/>") |
| 92 |
|
| 93 |
elif type == "horizontalRule": |
| 94 |
html.append("<hr/>") |
| 95 |
|
| 96 |
else: |
| 97 |
import logging |
| 98 |
logging.getLogger("tiptap").warning( |
| 99 |
"Unsupported content type '%s'", type) |
| 100 |
|
| 101 |
|
| 102 |
def process_text(html, content): |
| 103 |
if marks := content.get("marks"): |
| 104 |
close = [] |
| 105 |
for mark in marks: |
| 106 |
type = mark["type"] |
| 107 |
if type == "link": |
| 108 |
attrs = mark.get("attrs") or {} |
| 109 |
html.append('<a href="') |
| 110 |
html.append(text.escape(attrs.get("href") or "")) |
| 111 |
html.append('"') |
| 112 |
if "target" in attrs: |
| 113 |
html.append(f' target="{attrs["target"]}"') |
| 114 |
if "rel" in attrs: |
| 115 |
html.append(f' rel="{attrs["rel"]}"') |
| 116 |
html.append(">") |
| 117 |
close.append("</a>") |
| 118 |
elif type == "bold": |
| 119 |
html.append("<strong>") |
| 120 |
close.append("</strong>") |
| 121 |
elif type == "italic": |
| 122 |
html.append("<em>") |
| 123 |
close.append("</em>") |
| 124 |
elif type == "underline": |
| 125 |
html.append("<u>") |
| 126 |
close.append("</u>") |
| 127 |
elif type == "strike": |
| 128 |
html.append("<s>") |
| 129 |
close.append("</s>") |
| 130 |
elif type == "textStyle" and len(mark) <= 1: |
| 131 |
pass |
| 132 |
else: |
| 133 |
import logging |
| 134 |
logging.getLogger("tiptap").warning( |
| 135 |
"Unsupported text marker '%s'", type) |
| 136 |
close.reverse() |
| 137 |
html.append(text.escape(content["text"])) |
| 138 |
html.extend(close) |
| 139 |
else: |
| 140 |
html.append(text.escape(content["text"])) |
| 141 |
|
| 142 |
|
| 143 |
def process_children(html, content): |
| 144 |
if children := content.get("content"): |
| 145 |
for block in children: |
| 146 |
process_content(html, block) |
| 147 |
|
| 148 |
|
| 149 |
def process_indentation(html, attrs): |
| 150 |
itype = ("text-indent" if attrs.get("indentType") == "line" else |
| 151 |
"margin-inline-start") |
| 152 |
isize = str((attrs.get("indentation") or 0) * 24) |
| 153 |
html.append(f"{itype}:{isize}px") |