Debian Codegraph @

gallery_dl/extractor/utils/deviantart_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 from .. deviantart import eclipse_media
11
12
13 def to_html(markup):
14 html = []
15
16 html.append('<div data-editor-viewer="1" '
17 'class="_83r8m _2CKTq _3NjDa mDnFl">')
18 data = util.json_loads(markup)
19 for block in data["document"]["content"]:
20 process_content(html, block)
21 html.append("</div>")
22
23 return "".join(html)
24
25
26 def process_content(html, content):
27 type = content["type"]
28
29 if type == "paragraph":
30 if children := content.get("content"):
31 html.append('<p style="')
32
33 if attrs := content.get("attrs"):
34 if align := attrs.get("textAlign"):
35 html.append("text-align:")
36 html.append(align)
37 html.append(";")
38 process_indentation(html, attrs)
39 html.append('">')
40 else:
41 html.append('margin-inline-start:0px">')
42
43 for block in children:
44 process_content(html, block)
45 html.append("</p>")
46 else:
47 html.append('<p class="empty-p"><br/></p>')
48
49 elif type == "text":
50 process_text(html, content)
51
52 elif type == "heading":
53 attrs = content["attrs"]
54 level = str(attrs.get("level") or "3")
55
56 html.append("<h")
57 html.append(level)
58 html.append(' style="text-align:')
59 html.append(attrs.get("textAlign") or "left")
60 html.append('">')
61 html.append('<span style="')
62 process_indentation(html, attrs)
63 html.append('">')
64 process_children(html, content)
65 html.append("</span></h")
66 html.append(level)
67 html.append(">")
68
69 elif type in {"listItem", "bulletList", "orderedList", "blockquote"}:
70 c = type[1]
71 tag = (
72 "li" if c == "i" else
73 "ul" if c == "u" else
74 "ol" if c == "r" else
75 "blockquote"
76 )
77 html.append("<" + tag + ">")
78 process_children(html, content)
79 html.append("</" + tag + ">")
80
81 elif type == "anchor":
82 attrs = content["attrs"]
83 html.append('<a id="')
84 html.append(attrs.get("id") or "")
85 html.append('" data-testid="anchor"></a>')
86
87 elif type == "hardBreak":
88 html.append("<br/><br/>")
89
90 elif type == "horizontalRule":
91 html.append("<hr/>")
92
93 elif type == "da-deviation":
94 process_deviation(html, content)
95
96 elif type == "da-mention":
97 user = content["attrs"]["user"]["username"]
98 html.append('<a href="https://www.deviantart.com/')
99 html.append(user.lower())
100 html.append('" data-da-type="da-mention" data-user="">@<!-- -->')
101 html.append(user)
102 html.append('</a>')
103
104 elif type == "da-gif":
105 attrs = content["attrs"]
106 width = str(attrs.get("width") or "")
107 height = str(attrs.get("height") or "")
108 url = text.escape(attrs.get("url") or "")
109
110 html.append('<div data-da-type="da-gif" data-width="')
111 html.append(width)
112 html.append('" data-height="')
113 html.append(height)
114 html.append('" data-alignment="')
115 html.append(attrs.get("alignment") or "")
116 html.append('" data-url="')
117 html.append(url)
118 html.append('" class="t61qu"><video role="img" autoPlay="" '
119 'muted="" loop="" style="pointer-events:none" '
120 'controlsList="nofullscreen" playsInline="" '
121 'aria-label="gif" data-da-type="da-gif" width="')
122 html.append(width)
123 html.append('" height="')
124 html.append(height)
125 html.append('" src="')
126 html.append(url)
127 html.append('" class="_1Fkk6"></video></div>')
128
129 elif type == "da-video":
130 src = text.escape(content["attrs"].get("src") or "")
131 html.append('<div data-testid="video" data-da-type="da-video" '
132 'data-src="')
133 html.append(src)
134 html.append('" class="_1Uxvs"><div data-canfs="yes" data-testid="v'
135 'ideo-inner" class="main-video" style="width:780px;hei'
136 'ght:438px"><div style="width:780px;height:438px">'
137 '<video src="')
138 html.append(src)
139 html.append('" style="width:100%;height:100%;" preload="auto" cont'
140 'rols=""></video></div></div></div>')
141
142 else:
143 import logging
144 logging.getLogger("tiptap").warning(
145 "Unsupported content type '%s'", type)
146
147
148 def process_text(html, content):
149 if marks := content.get("marks"):
150 close = []
151 for mark in marks:
152 type = mark["type"]
153 if type == "link":
154 attrs = mark.get("attrs") or {}
155 html.append('<a href="')
156 html.append(text.escape(attrs.get("href") or ""))
157 if "target" in attrs:
158 html.append('" target="')
159 html.append(attrs["target"])
160 html.append('" rel="')
161 html.append(attrs.get("rel") or
162 "noopener noreferrer nofollow ugc")
163 html.append('">')
164 close.append("</a>")
165 elif type == "bold":
166 html.append("<strong>")
167 close.append("</strong>")
168 elif type == "italic":
169 html.append("<em>")
170 close.append("</em>")
171 elif type == "underline":
172 html.append("<u>")
173 close.append("</u>")
174 elif type == "strike":
175 html.append("<s>")
176 close.append("</s>")
177 elif type == "textStyle" and len(mark) <= 1:
178 pass
179 else:
180 import logging
181 logging.getLogger("tiptap").warning(
182 "Unsupported text marker '%s'", type)
183 close.reverse()
184 html.append(text.escape(content["text"]))
185 html.extend(close)
186 else:
187 html.append(text.escape(content["text"]))
188
189
190 def process_children(html, content):
191 if children := content.get("content"):
192 for block in children:
193 process_content(html, block)
194
195
196 def process_indentation(html, attrs):
197 itype = ("text-indent" if attrs.get("indentType") == "line" else
198 "margin-inline-start")
199 isize = str((attrs.get("indentation") or 0) * 24)
200 html.append(itype + ":" + isize + "px")
201
202
203 def process_deviation(html, content):
204 dev = content["attrs"]["deviation"]
205 media = dev.get("media") or ()
206
207 html.append('<div class="jjNX2">')
208 html.append('<figure class="Qf-HY" data-da-type="da-deviation" '
209 'data-deviation="" '
210 'data-width="" data-link="" data-alignment="center">')
211
212 if "baseUri" in media:
213 url, formats = eclipse_media(media)
214 full = formats["fullview"]
215
216 html.append('<a href="')
217 html.append(text.escape(dev["url"]))
218 html.append('" class="_3ouD5" style="margin:0 auto;display:flex;'
219 'align-items:center;justify-content:center;'
220 'overflow:hidden;width:780px;height:')
221 html.append(str(780 * full["h"] / full["w"]))
222 html.append('px">')
223
224 html.append('<img src="')
225 html.append(text.escape(url))
226 html.append('" alt="')
227 html.append(text.escape(dev["title"]))
228 html.append('" style="width:100%;max-width:100%;display:block"/>')
229 html.append("</a>")
230
231 elif "textContent" in dev:
232 html.append('<div class="_32Hs4" style="width:350px">')
233
234 html.append('<a href="')
235 html.append(text.escape(dev["url"]))
236 html.append('" class="_3ouD5">')
237
238 html.append('''\
239 <section class="Q91qI aG7Yi" style="width:350px;height:313px">\
240 <div class="_16ECM _1xMkk" aria-hidden="true">\
241 <svg height="100%" viewBox="0 0 15 12" preserveAspectRatio="xMidYMin slice" \
242 fill-rule="evenodd">\
243 <linearGradient x1="87.8481761%" y1="16.3690766%" \
244 x2="45.4107524%" y2="71.4898596%" id="app-root-3">\
245 <stop stop-color="#00FF62" offset="0%"></stop>\
246 <stop stop-color="#3197EF" stop-opacity="0" offset="100%"></stop>\
247 </linearGradient>\
248 <text class="_2uqbc" fill="url(#app-root-3)" text-anchor="end" x="15" y="11">J\
249 </text></svg></div><div class="_1xz9u">Literature</div><h3 class="_2WvKD">\
250 ''')
251 html.append(text.escape(dev["title"]))
252 html.append('</h3><div class="_2CPLm">')
253 html.append(text.escape(dev["textContent"]["excerpt"]))
254 html.append('</div></section></a></div>')
255
256 html.append('</figure></div>')