krz/skunky-art
Alternative privacy frontend for DeviantArt.
clone: git clone https://gitbay.org/krz/skunky-art.git
sonarcloud-cleanup: app/parsers.go · raw
1package app
2
3import (
4 "encoding/json"
5 "strconv"
6 "strings"
7
8 "github.com/krazywarez/devianter"
9 "golang.org/x/net/html"
10)
11
12// ParseComments renders a comment thread, nesting replies under the comment they
13// answer. It returns a placeholder message rather than failing if the upstream
14// fetch errored.
15func (s skunkyart) ParseComments(c devianter.Comments, daError devianter.Error) string {
16 if daError.RAW != nil {
17 return "Failed to fetch comments :("
18 }
19
20 var cmmts strings.Builder
21 replied := make(map[int]string)
22
23 cmmts.WriteString("<details><summary>Comments: <b>")
24 cmmts.WriteString(strconv.Itoa(c.Total))
25 cmmts.WriteString("</b></summary>")
26 for _, x := range c.Thread {
27 replied[x.ID] = x.User.Username
28 cmmts.WriteString(`<div class="msg`)
29 if x.Parent > 0 {
30 cmmts.WriteString(` reply`)
31 }
32 cmmts.WriteString(`"><p id="`)
33 cmmts.WriteString(strconv.Itoa(x.ID))
34 cmmts.WriteString(`"><img src="`)
35 cmmts.WriteString(URLBuilder(s.Host, "media", "emojitar", x.User.Username, "?type=a"))
36 cmmts.WriteString(`" width="30px" height="30px"><a href="`)
37 cmmts.WriteString(URLBuilder(s.Host, "group_user", "?q=", x.User.Username, "&type=a"))
38 cmmts.WriteString(`"><b`)
39 cmmts.WriteString(` class="`)
40 if x.User.Banned {
41 cmmts.WriteString(`banned`)
42 }
43 if x.Author {
44 cmmts.WriteString(`author`)
45 }
46 cmmts.WriteString(`">`)
47 cmmts.WriteString(x.User.Username)
48 cmmts.WriteString("</b></a> ")
49
50 if x.Parent > 0 {
51 cmmts.WriteString(` In reply to <a href="`)
52 cmmts.WriteString(s._pth)
53 cmmts.WriteString("#")
54 cmmts.WriteString(strconv.Itoa(x.Parent))
55 cmmts.WriteString(`">`)
56 if replied[x.Parent] == "" {
57 cmmts.WriteString("???")
58 } else {
59 cmmts.WriteString(replied[x.Parent])
60 }
61 cmmts.WriteString("</a>")
62 }
63 cmmts.WriteString(" [")
64 cmmts.WriteString(x.Posted.UTC().String())
65 cmmts.WriteString("]<p>")
66
67 cmmts.WriteString(ParseDescription(s.Host, x.TextContent))
68 cmmts.WriteString("<p>👍: ")
69 cmmts.WriteString(strconv.Itoa(x.Likes))
70 cmmts.WriteString(" ⏩: ")
71 cmmts.WriteString(strconv.Itoa(x.Replies))
72 cmmts.WriteString("</p></div>\n")
73 }
74 cmmts.WriteString(s.NavBase(DeviationList{
75 Pages: 0,
76 More: c.HasMore,
77 }))
78 cmmts.WriteString("</details>")
79 return cmmts.String()
80}
81
82// DeviationList renders devs as an HTML grid, or as an Atom feed when the
83// request asked for one and allowAtom permits it. NSFW entries are dropped
84// unless the instance allows them. Passing content adds a navigation bar.
85func (s skunkyart) DeviationList(devs []devianter.Deviation, allowAtom bool, content ...DeviationList) string {
86 if s.Atom && s.Page > 1 {
87 s.ReturnHTTPError(400)
88 return ""
89 }
90
91 var list, listContent strings.Builder
92
93 for i, l := 0, len(devs); i < l; i++ {
94 data := &devs[i]
95 if data.AI && CFG.HideAI {
96 continue
97 }
98 if preview, fullview := ParseMedia(s.Host, data.Media, 320), ParseMedia(s.Host, data.Media); !data.NSFW || CFG.Nsfw {
99 if allowAtom && s.Atom {
100 s.Writer.Header().Add("Content-Type", "application/atom+xml")
101 id := strconv.Itoa(data.ID)
102 listContent.WriteString(`<entry><author><name>`)
103 listContent.WriteString(data.Author.Username)
104 listContent.WriteString(`</name></author><title>`)
105 listContent.WriteString(data.Title)
106 listContent.WriteString(`</title><link rel="alternate" type="text/html" href="`)
107 listContent.WriteString(URLBuilder(s.Host, "post", data.Author.Username, "atom-"+id))
108 listContent.WriteString(`"/><id>`)
109 listContent.WriteString(id)
110 listContent.WriteString(`</id><published>`)
111 listContent.WriteString(data.PublishedTime.UTC().Format("Mon, 02 Jan 2006 15:04:05 -0700"))
112 listContent.WriteString(`</published>`)
113 listContent.WriteString(`<media:group><media:title>`)
114 listContent.WriteString(data.Title)
115 listContent.WriteString(`</media:title><media:thumbinal url="`)
116 listContent.WriteString(preview)
117 listContent.WriteString(`"/></media:group><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><a href="`)
118 listContent.WriteString(ConvertDeviantArtURLToSkunkyArt(s.Host, data.Url))
119 listContent.WriteString(`"><img src="`)
120 listContent.WriteString(fullview)
121 listContent.WriteString(`"/></a><p>`)
122 listContent.WriteString(ParseDescription(s.Host, data.TextContent))
123 listContent.WriteString(`</p></div></content></entry>`)
124 } else {
125 listContent.WriteString(`<div class="block">`)
126 if fullview != "" && preview != "" {
127 listContent.WriteString(`<a title="open/download" href="`)
128 listContent.WriteString(fullview)
129 listContent.WriteString(`"><img loading="lazy" src="`)
130 listContent.WriteString(preview)
131 listContent.WriteString(`" width="15%"></a>`)
132 } else {
133 listContent.WriteString(`<h1>[ TEXT ]</h1>`)
134 }
135 listContent.WriteString(`<br><a href="`)
136 listContent.WriteString(ConvertDeviantArtURLToSkunkyArt(s.Host, data.Url))
137 listContent.WriteString(`">`)
138 listContent.WriteString(data.Author.Username)
139 listContent.WriteString(" - ")
140 listContent.WriteString(data.Title)
141
142 if data.NSFW {
143 listContent.WriteString(` [<span class="nsfw">NSFW</span>]`)
144 }
145 if data.AI {
146 listContent.WriteString(" [🤖]")
147 }
148 if data.DD {
149 listContent.WriteString(` [<span class="dd">DD</span>]`)
150 }
151
152 listContent.WriteString("</a></div>")
153 }
154 }
155 }
156
157 if allowAtom && s.Atom {
158 list.WriteString(`<?xml version="1.0" encoding="UTF-8"?><feed xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">`)
159
160 list.WriteString(`<title>`)
161 switch {
162 case s.Type == 0:
163 list.WriteString("Daily Deviations")
164 case s.Type == 'g' && len(devs) != 0:
165 list.WriteString(devs[0].Author.Username)
166 default:
167 list.WriteString("SkunkyArt")
168 }
169 list.WriteString(`</title>`)
170
171 list.WriteString(`<link rel="alternate" href="`)
172 list.WriteString(s.Host)
173 list.WriteString(`"/>`)
174
175 list.WriteString(listContent.String())
176
177 list.WriteString("</feed>")
178 wr(s.Writer, list.String())
179 } else {
180 list.WriteString(`<div class="content">`)
181
182 list.WriteString(listContent.String())
183
184 list.WriteString("</div>")
185 if content != nil {
186 list.WriteString(s.NavBase(content[0]))
187 }
188 }
189
190 return list.String()
191}
192
193/* DESCRIPTION/COMMENT PARSER */
194
195// text is one styled run within a description: the rendered HTML, the raw source
196// it came from, and the offsets it spans in the original block.
197type text struct {
198 Txt string
199 TxtRaw string
200 From int
201 To int
202}
203
204// ParseDescription renders a DeviantArt description into HTML, handling both the
205// Draft.js-style JSON payload and the plain HTML markup DeviantArt returns, and
206// rewriting embedded links and artwork references to point at this instance.
207// host is the request's scheme and host, as taken by URLBuilder.
208//
209// TODO: rewrite this whole mess.
210func ParseDescription(host string, dscr devianter.Text) string {
211 var parsedDescription strings.Builder
212 TagBuilder := func(content string, tags ...string) string {
213 l := len(tags)
214 for x := range l {
215 var htm strings.Builder
216 htm.WriteString("<")
217 htm.WriteString(tags[x])
218 htm.WriteString(">")
219
220 htm.WriteString(content)
221
222 htm.WriteString("</")
223 htm.WriteString(tags[x])
224 htm.WriteString(">")
225 content = htm.String()
226 }
227 return content
228 }
229 DeleteTrackingFromURL := func(url string) string {
230 if len(url) > 42 && url[:42] == "https://www.deviantart.com/users/outgoing?" {
231 url = url[42:]
232 }
233 return url
234 }
235
236 if description, dl := dscr.Html.Markup, len(dscr.Html.Markup); dl != 0 &&
237 description[0] == '{' &&
238 description[dl-1] == '}' {
239 var descr struct {
240 Blocks []struct {
241 Text string `json:"text"`
242 Type string `json:"type"`
243 InlineStyleRanges []struct {
244 Offset int `json:"offset"`
245 Length int `json:"length"`
246 Style string `json:"style"`
247 } `json:"inlineStyleRanges"`
248 EntityRanges []struct {
249 Offset int `json:"offset"`
250 Length int `json:"length"`
251 Key int `json:"key"`
252 } `json:"entityRanges"`
253 Data struct {
254 TextAlignment string `json:"textAlignment"`
255 } `json:"data"`
256 } `json:"blocks"`
257 EntityMap map[string]struct {
258 Type string `json:"type"`
259 Data struct {
260 URL string `json:"url"`
261 Config struct {
262 // "aligment" is DeviantArt's own spelling; do not correct it.
263 Aligment string `json:"aligment"`
264 Width int `json:"width"`
265 } `json:"config"`
266 Data devianter.Deviation `json:"data"`
267 } `json:"data"`
268 } `json:"entityMap"`
269 }
270 e := json.Unmarshal([]byte(description), &descr)
271 try(e)
272
273 entities := make(map[int]devianter.Deviation)
274 urls := make(map[int]string)
275 for n, x := range descr.EntityMap {
276 num, _ := strconv.Atoi(n)
277 if x.Data.URL != "" {
278 urls[num] = DeleteTrackingFromURL(x.Data.URL)
279 }
280 entities[num] = x.Data.Data
281 }
282
283 for _, x := range descr.Blocks {
284 Styles := make([]text, len(x.InlineStyleRanges))
285
286 if len(x.InlineStyleRanges) != 0 {
287 var tags = make(map[int][]string)
288 for n, rngs := range x.InlineStyleRanges {
289 Styles := &Styles[n]
290 switch rngs.Style {
291 case "BOLD":
292 rngs.Style = "b"
293 case "UNDERLINE":
294 rngs.Style = "u"
295 case "ITALIC":
296 rngs.Style = "i"
297 }
298 Styles.From = rngs.Offset
299 Styles.To = rngs.Offset + rngs.Length
300 FT := Styles.From * Styles.To
301 tags[FT] = append(tags[FT], rngs.Style)
302 }
303 for n := range Styles {
304 Styles := &Styles[n]
305 Styles.TxtRaw = x.Text[Styles.From:Styles.To]
306 Styles.Txt = TagBuilder(Styles.TxtRaw, tags[Styles.From*Styles.To]...)
307 }
308 }
309
310 switch x.Type {
311 case "atomic":
312 if len(x.EntityRanges) != 0 {
313 d := entities[x.EntityRanges[0].Key]
314 parsedDescription.WriteString(`<a href="`)
315 parsedDescription.WriteString(ConvertDeviantArtURLToSkunkyArt(host, d.Url))
316 parsedDescription.WriteString(`"><img width="50%" src="`)
317 parsedDescription.WriteString(ParseMedia(host, d.Media))
318 parsedDescription.WriteString(`" title="`)
319 parsedDescription.WriteString(d.Author.Username)
320 parsedDescription.WriteString(" - ")
321 parsedDescription.WriteString(d.Title)
322 parsedDescription.WriteString(`"></a>`)
323 }
324 case "unstyled":
325 if l := len(Styles); l != 0 {
326 for n, r := range Styles {
327 var tag string
328 if x.Type == "header-two" {
329 tag = "h2"
330 }
331
332 parsedDescription.WriteString(x.Text[:r.From])
333 if len(urls) != 0 && len(x.EntityRanges) != 0 {
334 ra := &x.EntityRanges[0]
335
336 parsedDescription.WriteString(`<a target="_blank" href="`)
337 parsedDescription.WriteString(urls[ra.Key])
338 parsedDescription.WriteString(`">`)
339 parsedDescription.WriteString(r.Txt)
340 parsedDescription.WriteString(`</a>`)
341 } else if l > n+1 {
342 parsedDescription.WriteString(r.Txt)
343 }
344 parsedDescription.WriteString(TagBuilder(tag, x.Text[r.To:]))
345 }
346 } else {
347 parsedDescription.WriteString(x.Text)
348 }
349 }
350 parsedDescription.WriteString("<br>")
351 }
352 } else if dl != 0 {
353 for tt := html.NewTokenizer(strings.NewReader(dscr.Html.Markup)); ; {
354 switch tt.Next() {
355 case html.ErrorToken:
356 return parsedDescription.String()
357 case html.CommentToken, html.DoctypeToken:
358 // No renderable content; skip.
359 case html.StartTagToken, html.EndTagToken, html.SelfClosingTagToken:
360 token := tt.Token()
361 switch token.Data {
362 case "a":
363 for _, a := range token.Attr {
364 if a.Key == "href" {
365 url := DeleteTrackingFromURL(a.Val)
366 parsedDescription.WriteString(`<a target="_blank" href="`)
367 parsedDescription.WriteString(url)
368 parsedDescription.WriteString(`">`)
369 parsedDescription.WriteString(GetValueOfTag(tt))
370 parsedDescription.WriteString("</a> ")
371 }
372 }
373 case "img":
374 var uri, title string
375 for b, a := range token.Attr {
376 switch a.Key {
377 case "src":
378 if len(a.Val) > 9 && a.Val[8:9] == "e" {
379 uri = URLBuilder(host, "media", "emojitar", a.Val[37:len(a.Val)-4], "?type=e")
380 }
381 case "title":
382 title = a.Val
383 }
384 if title != "" {
385 for x := -1; x < b; x++ {
386 parsedDescription.WriteString(`<img src="`)
387 parsedDescription.WriteString(uri)
388 parsedDescription.WriteString(`" title="`)
389 parsedDescription.WriteString(title)
390 parsedDescription.WriteString(`">`)
391 }
392 }
393 }
394 case "br", "li", "ul", "p", "b":
395 parsedDescription.WriteString(token.String())
396 case "div":
397 parsedDescription.WriteString("<p> ")
398 }
399 case html.TextToken:
400 parsedDescription.Write(tt.Text())
401 }
402 }
403 }
404
405 return parsedDescription.String()
406}