Debian Codegraph @

gallery_dl/extractor/utils/patreon_tiptap.py

raw
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 == "cta":
91 if (attrs := content.get("attrs")) and \
92 (link := attrs.get("button_link")):
93 html.append(f'<p><a href="{text.escape(link)}">'
94 f'{text.escape(attrs.get("button_text"))}</a></p>')
95 if caption := attrs.get("caption"):
96 html.append(f'<p class="caption">{text.escape(caption)}</p>')
97
98 elif type == "hardBreak":
99 html.append("<br/>")
100
101 elif type == "horizontalRule":
102 html.append("<hr/>")
103
104 else:
105 import logging
106 logging.getLogger("tiptap").warning(
107 "Unsupported content type '%s'", type)
108
109
110 def process_text(html, content):
111 if marks := content.get("marks"):
112 close = []
113 for mark in marks:
114 type = mark["type"]
115 if type == "link":
116 attrs = mark.get("attrs") or {}
117 html.append('<a href="')
118 html.append(text.escape(attrs.get("href") or ""))
119 html.append('"')
120 if "target" in attrs:
121 html.append(f' target="{attrs["target"]}"')
122 if "rel" in attrs:
123 html.append(f' rel="{attrs["rel"]}"')
124 html.append(">")
125 close.append("</a>")
126 elif type == "bold":
127 html.append("<strong>")
128 close.append("</strong>")
129 elif type == "italic":
130 html.append("<em>")
131 close.append("</em>")
132 elif type == "underline":
133 html.append("<u>")
134 close.append("</u>")
135 elif type == "strike":
136 html.append("<s>")
137 close.append("</s>")
138 elif type == "textStyle" and len(mark) <= 1:
139 pass
140 else:
141 import logging
142 logging.getLogger("tiptap").warning(
143 "Unsupported text marker '%s'", type)
144 close.reverse()
145 html.append(text.escape(content["text"]))
146 html.extend(close)
147 else:
148 html.append(text.escape(content["text"]))
149
150
151 def process_children(html, content):
152 if children := content.get("content"):
153 for block in children:
154 process_content(html, block)
155
156
157 def process_indentation(html, attrs):
158 itype = ("text-indent" if attrs.get("indentType") == "line" else
159 "margin-inline-start")
160 isize = str((attrs.get("indentation") or 0) * 24)
161 html.append(f"{itype}:{isize}px")