Commit c48badb79e
Verified · cmc
Layout: unified · split
.qlty/.gitignore added +7
| @@ -0,0 +1,7 @@ | |||
| 1 | * | ||
| 2 | !configs | ||
| 3 | !configs/** | ||
| 4 | !hooks | ||
| 5 | !hooks/** | ||
| 6 | !qlty.toml | ||
| 7 | !.gitignore | ||
.qlty/qlty.toml added +44
| @@ -0,0 +1,44 @@ | |||
| 1 | config_version = "0" | ||
| 2 | |||
| 3 | exclude_patterns = [ | ||
| 4 | "**/vendor/**", | ||
| 5 | "**/node_modules/**", | ||
| 6 | "**/testdata/**", | ||
| 7 | "**/dist/**", | ||
| 8 | "**/.build/**", | ||
| 9 | "**/build/**", | ||
| 10 | "**/*.min.*", | ||
| 11 | "**/generated/**", | ||
| 12 | ".claude/**", | ||
| 13 | "**/Pods/**", | ||
| 14 | ] | ||
| 15 | |||
| 16 | test_patterns = [ | ||
| 17 | "**/test/**", | ||
| 18 | "**/tests/**", | ||
| 19 | "**/Tests/**", | ||
| 20 | "**/*_test.*", | ||
| 21 | "**/test_*.*", | ||
| 22 | "**/*.test.*", | ||
| 23 | "**/*Tests.swift", | ||
| 24 | "e2e/**", | ||
| 25 | ] | ||
| 26 | |||
| 27 | [[source]] | ||
| 28 | name = "default" | ||
| 29 | default = true | ||
| 30 | |||
| 31 | [[plugin]] | ||
| 32 | name = "osv-scanner" | ||
| 33 | |||
| 34 | [[plugin]] | ||
| 35 | name = "trufflehog" | ||
| 36 | |||
| 37 | # Early returns are guard clauses, which keep the happy path unindented. | ||
| 38 | [smells.return_statements] | ||
| 39 | enabled = false | ||
| 40 | |||
| 41 | # Total file complexity is the sum of its functions, which function_complexity | ||
| 42 | # already checks. | ||
| 43 | [smells.file_complexity] | ||
| 44 | enabled = false | ||
app/cli.go +88 −85
| @@ -70,22 +70,85 @@ type settings struct { | |||
| 70 | Settings settingsParams `json:"settings"` | 70 | Settings settingsParams `json:"settings"` |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | func addInstance() { | 73 | // promptLine asks for a line on stdin, repeating the question until it gets |
| 74 | prompt := func(txt string, necessary bool) string { | 74 | // an answer when necessary is set. |
| 75 | input := bufio.NewScanner(os.Stdin) | 75 | func promptLine(txt string, necessary bool) string { |
| 76 | for { | 76 | input := bufio.NewScanner(os.Stdin) |
| 77 | print(txt) | 77 | for { |
| 78 | print(": ") | 78 | print(txt) |
| 79 | input.Scan() | 79 | print(": ") |
| 80 | 80 | input.Scan() | |
| 81 | if i := input.Text(); necessary && i == "" { | 81 | |
| 82 | println("Please specify the", txt) | 82 | if i := input.Text(); necessary && i == "" { |
| 83 | } else { | 83 | println("Please specify the", txt) |
| 84 | return i | 84 | } else { |
| 85 | } | 85 | return i |
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | // promptInstance asks for this instance's listing details; its settings come | ||
| 91 | // from the running configuration. | ||
| 92 | func promptInstance() settings { | ||
| 93 | return settings{ | ||
| 94 | Title: promptLine("Title", true), | ||
| 95 | Country: promptLine("Country", true), | ||
| 96 | ModifiedSrc: promptLine("Link to modified sources", false), | ||
| 97 | Settings: settingsParams{ | ||
| 98 | Nsfw: CFG.Nsfw, | ||
| 99 | Proxy: CFG.Proxy, | ||
| 100 | HideAI: CFG.HideAI, | ||
| 101 | Theme: CFG.Theme, | ||
| 102 | }, | ||
| 103 | Urls: settingsUrls{ | ||
| 104 | Clearnet: promptLine("Clearnet link", false), | ||
| 105 | Ygg: promptLine("Yggdrasil link", false), | ||
| 106 | Tor: promptLine("Onion link", false), | ||
| 107 | I2P: promptLine("I2P link", false), | ||
| 108 | }, | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | // instanceMarkdownRow is the INSTANCES.md table row for an instance. | ||
| 113 | func instanceMarkdownRow(inst *settings) []byte { | ||
| 114 | var mdstr bytes.Buffer | ||
| 115 | |||
| 116 | mdbuilder := func(yes bool, link string, title string) { | ||
| 117 | switch { | ||
| 118 | case yes && (title != "" && link != ""): | ||
| 119 | mdstr.WriteString("[") | ||
| 120 | mdstr.WriteString(title) | ||
| 121 | mdstr.WriteString("](") | ||
| 122 | mdstr.WriteString(link) | ||
| 123 | mdstr.WriteString(")") | ||
| 124 | case yes && link != "": | ||
| 125 | mdstr.WriteString("[Yes](") | ||
| 126 | mdstr.WriteString(link) | ||
| 127 | mdstr.WriteString(")") | ||
| 128 | case yes: | ||
| 129 | mdstr.WriteString("Yes") | ||
| 130 | default: | ||
| 131 | mdstr.WriteString("No") | ||
| 86 | } | 132 | } |
| 133 | mdstr.WriteString("|") | ||
| 134 | } | ||
| 135 | |||
| 136 | mdstr.WriteString("\n|") | ||
| 137 | mdbuilder(inst.Urls.Clearnet != "", inst.Urls.Clearnet, inst.Title) | ||
| 138 | for _, url := range []string{inst.Urls.Ygg, inst.Urls.I2P, inst.Urls.Tor} { | ||
| 139 | mdbuilder(url != "", url, "") | ||
| 140 | } | ||
| 141 | for _, on := range []bool{inst.Settings.Nsfw, inst.Settings.Proxy} { | ||
| 142 | mdbuilder(on, "", "") | ||
| 87 | } | 143 | } |
| 144 | mdbuilder(inst.ModifiedSrc != "", inst.ModifiedSrc, "") | ||
| 88 | 145 | ||
| 146 | mdstr.WriteString(inst.Country) | ||
| 147 | mdstr.WriteString("|") | ||
| 148 | return mdstr.Bytes() | ||
| 149 | } | ||
| 150 | |||
| 151 | func addInstance() { | ||
| 89 | var settingsVar struct { | 152 | var settingsVar struct { |
| 90 | Instances []settings `json:"instances"` | 153 | Instances []settings `json:"instances"` |
| 91 | } | 154 | } |
| @@ -103,82 +166,22 @@ func addInstance() { | |||
| 103 | } | 166 | } |
| 104 | defer func() { try(instancesFile.Close()) }() | 167 | defer func() { try(instancesFile.Close()) }() |
| 105 | 168 | ||
| 106 | for { | 169 | for string(instances) == "" { |
| 107 | if string(instances) == "" { | 170 | print("\rDownloading instance list...") |
| 108 | print("\rDownloading instance list...") | 171 | time.Sleep(500 * time.Millisecond) |
| 109 | } else { | 172 | } |
| 110 | println("\r\033[2KDownloaded!") | 173 | println("\r\033[2KDownloaded!") |
| 111 | try(json.Unmarshal(instances, &settingsVar)) | 174 | try(json.Unmarshal(instances, &settingsVar)) |
| 112 | |||
| 113 | settingsVar.Instances = append(settingsVar.Instances, settings{ | ||
| 114 | Title: prompt("Title", true), | ||
| 115 | Country: prompt("Country", true), | ||
| 116 | ModifiedSrc: prompt("Link to modified sources", false), | ||
| 117 | Settings: settingsParams{ | ||
| 118 | Nsfw: CFG.Nsfw, | ||
| 119 | Proxy: CFG.Proxy, | ||
| 120 | HideAI: CFG.HideAI, | ||
| 121 | Theme: CFG.Theme, | ||
| 122 | }, | ||
| 123 | Urls: settingsUrls{ | ||
| 124 | Clearnet: prompt("Clearnet link", false), | ||
| 125 | Ygg: prompt("Yggdrasil link", false), | ||
| 126 | Tor: prompt("Onion link", false), | ||
| 127 | I2P: prompt("I2P link", false), | ||
| 128 | }, | ||
| 129 | }) | ||
| 130 | |||
| 131 | j, err := json.MarshalIndent(&settingsVar, "", " ") | ||
| 132 | try(err) | ||
| 133 | |||
| 134 | try(func() error { _, err := instancesJSON.Write(j); return err }()) | ||
| 135 | |||
| 136 | settingsVar := &settingsVar.Instances[len(settingsVar.Instances)-1] | ||
| 137 | var mdstr bytes.Buffer | ||
| 138 | |||
| 139 | mdbuilder := func(yes bool, link string, title string) { | ||
| 140 | switch { | ||
| 141 | case yes && (title != "" && link != ""): | ||
| 142 | mdstr.WriteString("[") | ||
| 143 | mdstr.WriteString(title) | ||
| 144 | mdstr.WriteString("](") | ||
| 145 | mdstr.WriteString(link) | ||
| 146 | mdstr.WriteString(")") | ||
| 147 | case yes && link != "": | ||
| 148 | mdstr.WriteString("[Yes](") | ||
| 149 | mdstr.WriteString(link) | ||
| 150 | mdstr.WriteString(")") | ||
| 151 | case yes: | ||
| 152 | mdstr.WriteString("Yes") | ||
| 153 | default: | ||
| 154 | mdstr.WriteString("No") | ||
| 155 | } | ||
| 156 | mdstr.WriteString("|") | ||
| 157 | } | ||
| 158 | |||
| 159 | mdstr.WriteString("\n|") | ||
| 160 | mdbuilder(settingsVar.Urls.Clearnet != "", settingsVar.Urls.Clearnet, settingsVar.Title) | ||
| 161 | 175 | ||
| 162 | urls := []string{settingsVar.Urls.Ygg, settingsVar.Urls.I2P, settingsVar.Urls.Tor} | 176 | settingsVar.Instances = append(settingsVar.Instances, promptInstance()) |
| 163 | for i, l := 0, len(urls); i < l; i++ { | ||
| 164 | url := urls[i] | ||
| 165 | mdbuilder(url != "", url, "") | ||
| 166 | } | ||
| 167 | 177 | ||
| 168 | settings := []bool{settingsVar.Settings.Nsfw, settingsVar.Settings.Proxy} | 178 | j, err := json.MarshalIndent(&settingsVar, "", " ") |
| 169 | for i, l := 0, len(settings); i < l; i++ { | 179 | try(err) |
| 170 | mdbuilder(settings[i], "", "") | ||
| 171 | } | ||
| 172 | 180 | ||
| 173 | mdbuilder(settingsVar.ModifiedSrc != "", settingsVar.ModifiedSrc, "") | 181 | try(func() error { _, err := instancesJSON.Write(j); return err }()) |
| 174 | 182 | ||
| 175 | mdstr.WriteString(settingsVar.Country) | 183 | row := instanceMarkdownRow(&settingsVar.Instances[len(settingsVar.Instances)-1]) |
| 176 | mdstr.WriteString("|") | 184 | try(func() error { _, err := instancesFile.Write(row); return err }()) |
| 177 | 185 | ||
| 178 | try(func() error { _, err := instancesFile.Write(mdstr.Bytes()); return err }()) | ||
| 179 | break | ||
| 180 | } | ||
| 181 | time.Sleep(500 * time.Millisecond) | ||
| 182 | } | ||
| 183 | exit("Done! Now add the files 'instances.json' and 'INSTANCES.md' to the 'main' branch by merge request to https://gitbay.org/krz/skunky-art", 0) | 186 | exit("Done! Now add the files 'instances.json' and 'INSTANCES.md' to the 'main' branch by merge request to https://gitbay.org/krz/skunky-art", 0) |
| 184 | } | 187 | } |
app/config.go +58 −47
| @@ -167,46 +167,13 @@ var cfgExplicit bool | |||
| 167 | // a config that cannot be parsed, that asks for caching without proxying, or | 167 | // a config that cannot be parsed, that asks for caching without proxying, or |
| 168 | // that points caching at a directory this process cannot write. | 168 | // that points caching at a directory this process cannot write. |
| 169 | func ExecuteConfig() { | 169 | func ExecuteConfig() { |
| 170 | f, err := os.ReadFile(CFG.cfg) | 170 | readConfigFile() |
| 171 | switch { | ||
| 172 | case err == nil: | ||
| 173 | tryWithExitStatus(json.Unmarshal(f, &CFG), 1) | ||
| 174 | case os.IsNotExist(err) && !cfgExplicit: | ||
| 175 | // The default file is optional: the built-in defaults are a working | ||
| 176 | // instance. A path given with -c is not, since a typo there would | ||
| 177 | // otherwise start something the operator did not configure. | ||
| 178 | println("no", CFG.cfg, "found; running with the built-in defaults") | ||
| 179 | default: | ||
| 180 | tryWithExitStatus(err, 1) | ||
| 181 | } | ||
| 182 | 171 | ||
| 183 | if CFG.Cache.Enabled && !CFG.Proxy { | 172 | if CFG.Cache.Enabled && !CFG.Proxy { |
| 184 | exit("Incompatible settings detected: cannot use caching media content without proxy", 1) | 173 | exit("Incompatible settings detected: cannot use caching media content without proxy", 1) |
| 185 | } | 174 | } |
| 186 | |||
| 187 | if CFG.Cache.Enabled { | 175 | if CFG.Cache.Enabled { |
| 188 | if err := checkCacheWritable(CFG.Cache.Path); err != nil { | 176 | setupMediaCache() |
| 189 | exit("Cache directory is not writable by this process (uid "+ | ||
| 190 | strconv.Itoa(os.Getuid())+"): "+err.Error()+ | ||
| 191 | "\nGrant that uid write access to the directory, or set cache.enabled to false."+ | ||
| 192 | "\nThe official container image runs as uid 10000, so a bind-mounted cache needs:"+ | ||
| 193 | "\n chown -R 10000:10000 <cache dir on the host>", 1) | ||
| 194 | } | ||
| 195 | |||
| 196 | if CFG.Cache.Lifetime != "" { | ||
| 197 | d, err := parseLifetime(CFG.Cache.Lifetime) | ||
| 198 | if err != nil { | ||
| 199 | exit("config: cache.lifetime: "+err.Error(), 1) | ||
| 200 | } | ||
| 201 | lifetimeParsed = d.Milliseconds() | ||
| 202 | } | ||
| 203 | // max-size is documented in megabytes. This was 1024^2, which in Go is | ||
| 204 | // XOR (1026), not exponentiation — so the cap was ~1000x too small. | ||
| 205 | CFG.Cache.MaxSize *= 1024 * 1024 | ||
| 206 | go InitCacheSystem() | ||
| 207 | if CFG.Cache.MemCache { | ||
| 208 | go InitMemCacheJanitor() | ||
| 209 | } | ||
| 210 | } | 177 | } |
| 211 | 178 | ||
| 212 | About = instanceAbout{ | 179 | About = instanceAbout{ |
| @@ -225,18 +192,7 @@ func ExecuteConfig() { | |||
| 225 | } | 192 | } |
| 226 | 193 | ||
| 227 | if CFG.APICache.Enabled { | 194 | if CFG.APICache.Enabled { |
| 228 | d, err := parseLifetime(CFG.APICache.TTL) | 195 | setupAPICache() |
| 229 | if err != nil { | ||
| 230 | exit("config: api-cache.ttl: "+err.Error(), 1) | ||
| 231 | } | ||
| 232 | apiCacheTTL = d | ||
| 233 | if CFG.APICache.Stale != "" { | ||
| 234 | d, err := parseLifetime(CFG.APICache.Stale) | ||
| 235 | if err != nil { | ||
| 236 | exit("config: api-cache.stale: "+err.Error(), 1) | ||
| 237 | } | ||
| 238 | apiCacheStale = d | ||
| 239 | } | ||
| 240 | } | 196 | } |
| 241 | 197 | ||
| 242 | // per-minute 0 turns the limit off; a burst below one token would | 198 | // per-minute 0 turns the limit off; a burst below one token would |
| @@ -254,6 +210,61 @@ func ExecuteConfig() { | |||
| 254 | devianter.UserAgent = CFG.UserAgent | 210 | devianter.UserAgent = CFG.UserAgent |
| 255 | } | 211 | } |
| 256 | 212 | ||
| 213 | func readConfigFile() { | ||
| 214 | f, err := os.ReadFile(CFG.cfg) | ||
| 215 | switch { | ||
| 216 | case err == nil: | ||
| 217 | tryWithExitStatus(json.Unmarshal(f, &CFG), 1) | ||
| 218 | case os.IsNotExist(err) && !cfgExplicit: | ||
| 219 | // The default file is optional: the built-in defaults are a working | ||
| 220 | // instance. A path given with -c is not, since a typo there would | ||
| 221 | // otherwise start something the operator did not configure. | ||
| 222 | println("no", CFG.cfg, "found; running with the built-in defaults") | ||
| 223 | default: | ||
| 224 | tryWithExitStatus(err, 1) | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | func setupMediaCache() { | ||
| 229 | if err := checkCacheWritable(CFG.Cache.Path); err != nil { | ||
| 230 | exit("Cache directory is not writable by this process (uid "+ | ||
| 231 | strconv.Itoa(os.Getuid())+"): "+err.Error()+ | ||
| 232 | "\nGrant that uid write access to the directory, or set cache.enabled to false."+ | ||
| 233 | "\nThe official container image runs as uid 10000, so a bind-mounted cache needs:"+ | ||
| 234 | "\n chown -R 10000:10000 <cache dir on the host>", 1) | ||
| 235 | } | ||
| 236 | |||
| 237 | if CFG.Cache.Lifetime != "" { | ||
| 238 | d, err := parseLifetime(CFG.Cache.Lifetime) | ||
| 239 | if err != nil { | ||
| 240 | exit("config: cache.lifetime: "+err.Error(), 1) | ||
| 241 | } | ||
| 242 | lifetimeParsed = d.Milliseconds() | ||
| 243 | } | ||
| 244 | // max-size is documented in megabytes. This was 1024^2, which in Go is | ||
| 245 | // XOR (1026), not exponentiation — so the cap was ~1000x too small. | ||
| 246 | CFG.Cache.MaxSize *= 1024 * 1024 | ||
| 247 | go InitCacheSystem() | ||
| 248 | if CFG.Cache.MemCache { | ||
| 249 | go InitMemCacheJanitor() | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | func setupAPICache() { | ||
| 254 | d, err := parseLifetime(CFG.APICache.TTL) | ||
| 255 | if err != nil { | ||
| 256 | exit("config: api-cache.ttl: "+err.Error(), 1) | ||
| 257 | } | ||
| 258 | apiCacheTTL = d | ||
| 259 | if CFG.APICache.Stale != "" { | ||
| 260 | d, err := parseLifetime(CFG.APICache.Stale) | ||
| 261 | if err != nil { | ||
| 262 | exit("config: api-cache.stale: "+err.Error(), 1) | ||
| 263 | } | ||
| 264 | apiCacheStale = d | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 257 | // forcedThemeCSS returns a block that pins the palette when the instance has | 268 | // forcedThemeCSS returns a block that pins the palette when the instance has |
| 258 | // chosen a theme, or "" for "auto". Light repeats what the prefers-color-scheme | 269 | // chosen a theme, or "" for "auto". Light repeats what the prefers-color-scheme |
| 259 | // block already holds; dark repeats :root. Both are emitted after the | 270 | // block already holds; dark repeats :root. Both are emitted after the |
app/parsers.go +367 −304
| @@ -110,7 +110,8 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, allowAtom bool, con | |||
| 110 | return "" | 110 | return "" |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | var list, listContent strings.Builder | 113 | atom := allowAtom && s.Atom |
| 114 | var listContent strings.Builder | ||
| 114 | var newest time.Time // the latest entry, for the feed's own <updated> | 115 | var newest time.Time // the latest entry, for the feed's own <updated> |
| 115 | 116 | ||
| 116 | for i, l := 0, len(devs); i < l; i++ { | 117 | for i, l := 0, len(devs); i < l; i++ { |
| @@ -118,129 +119,156 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, allowAtom bool, con | |||
| 118 | if !VisibleDeviation(data) { | 119 | if !VisibleDeviation(data) { |
| 119 | continue | 120 | continue |
| 120 | } | 121 | } |
| 121 | // Escaped once here: the same values go into both the HTML grid and the | 122 | item := s.listItem(data) |
| 122 | // Atom feed, and html.EscapeString produces entities XML accepts too. | 123 | if atom { |
| 123 | author, title := esc(data.Author.Username), esc(data.Title) | 124 | s.Writer.Header().Add("Content-Type", "application/atom+xml") |
| 124 | postURL := esc(ConvertDeviantArtURLToSkunkyArt(s.Host, data.Url)) | 125 | if published := s.writeAtomEntry(&listContent, data, item); published.After(newest) { |
| 125 | if preview, fullview := esc(ParseMedia(s.Host, data.Media, 320)), esc(ParseMedia(s.Host, data.Media)); true { | 126 | newest = published |
| 126 | if allowAtom && s.Atom { | ||
| 127 | s.Writer.Header().Add("Content-Type", "application/atom+xml") | ||
| 128 | // The entry id is the post's URL here: Atom wants an IRI, and a | ||
| 129 | // bare number is not one. Timestamps are RFC 3339, the only form | ||
| 130 | // the format allows. | ||
| 131 | entryURL := esc(URLBuilder(s.Host, "post", data.Author.Username, "atom-"+strconv.Itoa(data.ID))) | ||
| 132 | published := data.PublishedTime.UTC() | ||
| 133 | if published.After(newest) { | ||
| 134 | newest = published | ||
| 135 | } | ||
| 136 | listContent.WriteString(`<entry><author><name>`) | ||
| 137 | listContent.WriteString(author) | ||
| 138 | listContent.WriteString(`</name></author><title>`) | ||
| 139 | listContent.WriteString(title) | ||
| 140 | listContent.WriteString(`</title><link rel="alternate" type="text/html" href="`) | ||
| 141 | listContent.WriteString(entryURL) | ||
| 142 | listContent.WriteString(`"/><id>`) | ||
| 143 | listContent.WriteString(entryURL) | ||
| 144 | listContent.WriteString(`</id><published>`) | ||
| 145 | listContent.WriteString(published.Format(time.RFC3339)) | ||
| 146 | listContent.WriteString(`</published><updated>`) | ||
| 147 | listContent.WriteString(published.Format(time.RFC3339)) | ||
| 148 | listContent.WriteString(`</updated>`) | ||
| 149 | listContent.WriteString(`<media:group><media:title>`) | ||
| 150 | listContent.WriteString(title) | ||
| 151 | listContent.WriteString(`</media:title><media:thumbnail url="`) | ||
| 152 | listContent.WriteString(preview) | ||
| 153 | listContent.WriteString(`"/></media:group><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><a href="`) | ||
| 154 | listContent.WriteString(postURL) | ||
| 155 | listContent.WriteString(`"><img src="`) | ||
| 156 | listContent.WriteString(fullview) | ||
| 157 | listContent.WriteString(`"/></a><p>`) | ||
| 158 | listContent.WriteString(ParseDescription(s.Host, data.TextContent)) | ||
| 159 | listContent.WriteString(`</p></div></content></entry>`) | ||
| 160 | } else { | ||
| 161 | listContent.WriteString(`<div class="block">`) | ||
| 162 | if fullview != "" && preview != "" { | ||
| 163 | listContent.WriteString(`<a title="` + esc(T(s.Lang, "deviation.open")) + `" href="`) | ||
| 164 | listContent.WriteString(fullview) | ||
| 165 | listContent.WriteString(`"><img loading="lazy" src="`) | ||
| 166 | listContent.WriteString(preview) | ||
| 167 | listContent.WriteString(`" width="15%" alt="`) | ||
| 168 | listContent.WriteString(author) | ||
| 169 | listContent.WriteString(" - ") | ||
| 170 | listContent.WriteString(title) | ||
| 171 | listContent.WriteString(`"></a>`) | ||
| 172 | } else { | ||
| 173 | listContent.WriteString("<h1>[ " + esc(T(s.Lang, "list.text")) + " ]</h1>") | ||
| 174 | } | ||
| 175 | listContent.WriteString(`<br><a href="`) | ||
| 176 | listContent.WriteString(postURL) | ||
| 177 | listContent.WriteString(`">`) | ||
| 178 | listContent.WriteString(author) | ||
| 179 | listContent.WriteString(" - ") | ||
| 180 | listContent.WriteString(title) | ||
| 181 | |||
| 182 | if data.NSFW { | ||
| 183 | listContent.WriteString(` [<span class="nsfw">NSFW</span>]`) | ||
| 184 | } | ||
| 185 | if data.AI { | ||
| 186 | listContent.WriteString(" [🤖]") | ||
| 187 | } | ||
| 188 | if data.DD { | ||
| 189 | listContent.WriteString(` [<span class="dd">DD</span>]`) | ||
| 190 | } | ||
| 191 | |||
| 192 | listContent.WriteString("</a></div>") | ||
| 193 | } | 127 | } |
| 128 | } else { | ||
| 129 | s.writeGridEntry(&listContent, data, item) | ||
| 194 | } | 130 | } |
| 195 | } | 131 | } |
| 196 | 132 | ||
| 197 | if allowAtom && s.Atom { | 133 | if atom { |
| 198 | list.WriteString(`<?xml version="1.0" encoding="UTF-8"?><feed xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">`) | 134 | feed := s.atomFeed(devs, listContent.String(), newest) |
| 199 | 135 | wr(s.Writer, feed) | |
| 200 | list.WriteString(`<title>`) | 136 | return feed |
| 201 | switch { | 137 | } |
| 202 | case s.Type == 0: | ||
| 203 | list.WriteString("Daily Deviations") | ||
| 204 | case s.Type == 'g' && len(devs) != 0: | ||
| 205 | list.WriteString(esc(devs[0].Author.Username)) | ||
| 206 | default: | ||
| 207 | list.WriteString("SkunkyArt") | ||
| 208 | } | ||
| 209 | list.WriteString(`</title>`) | ||
| 210 | 138 | ||
| 211 | // Atom requires a feed id and updated time; readers reject a feed | 139 | var list strings.Builder |
| 212 | // without them. The id is the feed's own URL on this instance. | 140 | list.WriteString(`<div class="content">`) |
| 213 | feedURL := esc(s.Host + s._pth + "?" + s.Args.Encode()) | 141 | list.WriteString(listContent.String()) |
| 214 | if newest.IsZero() { | 142 | list.WriteString("</div>") |
| 215 | newest = time.Now().UTC() | 143 | if content != nil { |
| 216 | } | 144 | list.WriteString(s.NavBase(content[0])) |
| 217 | list.WriteString(`<id>`) | 145 | } |
| 218 | list.WriteString(feedURL) | 146 | return list.String() |
| 219 | list.WriteString(`</id><updated>`) | 147 | } |
| 220 | list.WriteString(newest.Format(time.RFC3339)) | 148 | |
| 221 | list.WriteString(`</updated><link rel="self" type="application/atom+xml" href="`) | 149 | // listEntry holds one deviation's values for a list, escaped once: the same |
| 222 | list.WriteString(feedURL) | 150 | // values go into both the HTML grid and the Atom feed, and html.EscapeString |
| 223 | list.WriteString(`"/>`) | 151 | // produces entities XML accepts too. |
| 224 | 152 | type listEntry struct { | |
| 225 | list.WriteString(`<link rel="alternate" href="`) | 153 | author, title, postURL, preview, fullview string |
| 226 | list.WriteString(esc(s.Host)) | 154 | } |
| 227 | list.WriteString(`"/>`) | 155 | |
| 228 | 156 | func (s skunkyart) listItem(data *devianter.Deviation) listEntry { | |
| 229 | list.WriteString(listContent.String()) | 157 | return listEntry{ |
| 230 | 158 | author: esc(data.Author.Username), | |
| 231 | list.WriteString("</feed>") | 159 | title: esc(data.Title), |
| 232 | wr(s.Writer, list.String()) | 160 | postURL: esc(ConvertDeviantArtURLToSkunkyArt(s.Host, data.Url)), |
| 161 | preview: esc(ParseMedia(s.Host, data.Media, 320)), | ||
| 162 | fullview: esc(ParseMedia(s.Host, data.Media)), | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | // writeAtomEntry writes one feed entry and returns its published time. | ||
| 167 | func (s skunkyart) writeAtomEntry(b *strings.Builder, data *devianter.Deviation, item listEntry) time.Time { | ||
| 168 | // The entry id is the post's URL here: Atom wants an IRI, and a | ||
| 169 | // bare number is not one. Timestamps are RFC 3339, the only form | ||
| 170 | // the format allows. | ||
| 171 | entryURL := esc(URLBuilder(s.Host, "post", data.Author.Username, "atom-"+strconv.Itoa(data.ID))) | ||
| 172 | published := data.PublishedTime.UTC() | ||
| 173 | b.WriteString(`<entry><author><name>`) | ||
| 174 | b.WriteString(item.author) | ||
| 175 | b.WriteString(`</name></author><title>`) | ||
| 176 | b.WriteString(item.title) | ||
| 177 | b.WriteString(`</title><link rel="alternate" type="text/html" href="`) | ||
| 178 | b.WriteString(entryURL) | ||
| 179 | b.WriteString(`"/><id>`) | ||
| 180 | b.WriteString(entryURL) | ||
| 181 | b.WriteString(`</id><published>`) | ||
| 182 | b.WriteString(published.Format(time.RFC3339)) | ||
| 183 | b.WriteString(`</published><updated>`) | ||
| 184 | b.WriteString(published.Format(time.RFC3339)) | ||
| 185 | b.WriteString(`</updated>`) | ||
| 186 | b.WriteString(`<media:group><media:title>`) | ||
| 187 | b.WriteString(item.title) | ||
| 188 | b.WriteString(`</media:title><media:thumbnail url="`) | ||
| 189 | b.WriteString(item.preview) | ||
| 190 | b.WriteString(`"/></media:group><content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><a href="`) | ||
| 191 | b.WriteString(item.postURL) | ||
| 192 | b.WriteString(`"><img src="`) | ||
| 193 | b.WriteString(item.fullview) | ||
| 194 | b.WriteString(`"/></a><p>`) | ||
| 195 | b.WriteString(ParseDescription(s.Host, data.TextContent)) | ||
| 196 | b.WriteString(`</p></div></content></entry>`) | ||
| 197 | return published | ||
| 198 | } | ||
| 199 | |||
| 200 | func (s skunkyart) writeGridEntry(b *strings.Builder, data *devianter.Deviation, item listEntry) { | ||
| 201 | b.WriteString(`<div class="block">`) | ||
| 202 | if item.fullview != "" && item.preview != "" { | ||
| 203 | b.WriteString(`<a title="` + esc(T(s.Lang, "deviation.open")) + `" href="`) | ||
| 204 | b.WriteString(item.fullview) | ||
| 205 | b.WriteString(`"><img loading="lazy" src="`) | ||
| 206 | b.WriteString(item.preview) | ||
| 207 | b.WriteString(`" width="15%" alt="`) | ||
| 208 | b.WriteString(item.author) | ||
| 209 | b.WriteString(" - ") | ||
| 210 | b.WriteString(item.title) | ||
| 211 | b.WriteString(`"></a>`) | ||
| 233 | } else { | 212 | } else { |
| 234 | list.WriteString(`<div class="content">`) | 213 | b.WriteString("<h1>[ " + esc(T(s.Lang, "list.text")) + " ]</h1>") |
| 214 | } | ||
| 215 | b.WriteString(`<br><a href="`) | ||
| 216 | b.WriteString(item.postURL) | ||
| 217 | b.WriteString(`">`) | ||
| 218 | b.WriteString(item.author) | ||
| 219 | b.WriteString(" - ") | ||
| 220 | b.WriteString(item.title) | ||
| 221 | |||
| 222 | if data.NSFW { | ||
| 223 | b.WriteString(` [<span class="nsfw">NSFW</span>]`) | ||
| 224 | } | ||
| 225 | if data.AI { | ||
| 226 | b.WriteString(" [🤖]") | ||
| 227 | } | ||
| 228 | if data.DD { | ||
| 229 | b.WriteString(` [<span class="dd">DD</span>]`) | ||
| 230 | } | ||
| 235 | 231 | ||
| 236 | list.WriteString(listContent.String()) | 232 | b.WriteString("</a></div>") |
| 233 | } | ||
| 237 | 234 | ||
| 238 | list.WriteString("</div>") | 235 | // atomFeed wraps the entries in an Atom feed for this request. |
| 239 | if content != nil { | 236 | func (s skunkyart) atomFeed(devs []devianter.Deviation, entries string, newest time.Time) string { |
| 240 | list.WriteString(s.NavBase(content[0])) | 237 | var list strings.Builder |
| 241 | } | 238 | list.WriteString(`<?xml version="1.0" encoding="UTF-8"?><feed xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">`) |
| 239 | |||
| 240 | list.WriteString(`<title>`) | ||
| 241 | switch { | ||
| 242 | case s.Type == 0: | ||
| 243 | list.WriteString("Daily Deviations") | ||
| 244 | case s.Type == 'g' && len(devs) != 0: | ||
| 245 | list.WriteString(esc(devs[0].Author.Username)) | ||
| 246 | default: | ||
| 247 | list.WriteString("SkunkyArt") | ||
| 242 | } | 248 | } |
| 249 | list.WriteString(`</title>`) | ||
| 243 | 250 | ||
| 251 | // Atom requires a feed id and updated time; readers reject a feed | ||
| 252 | // without them. The id is the feed's own URL on this instance. | ||
| 253 | feedURL := esc(s.Host + s._pth + "?" + s.Args.Encode()) | ||
| 254 | if newest.IsZero() { | ||
| 255 | newest = time.Now().UTC() | ||
| 256 | } | ||
| 257 | list.WriteString(`<id>`) | ||
| 258 | list.WriteString(feedURL) | ||
| 259 | list.WriteString(`</id><updated>`) | ||
| 260 | list.WriteString(newest.Format(time.RFC3339)) | ||
| 261 | list.WriteString(`</updated><link rel="self" type="application/atom+xml" href="`) | ||
| 262 | list.WriteString(feedURL) | ||
| 263 | list.WriteString(`"/>`) | ||
| 264 | |||
| 265 | list.WriteString(`<link rel="alternate" href="`) | ||
| 266 | list.WriteString(esc(s.Host)) | ||
| 267 | list.WriteString(`"/>`) | ||
| 268 | |||
| 269 | list.WriteString(entries) | ||
| 270 | |||
| 271 | list.WriteString("</feed>") | ||
| 244 | return list.String() | 272 | return list.String() |
| 245 | } | 273 | } |
| 246 | 274 | ||
| @@ -285,206 +313,241 @@ func ParseDescription(host string, dscr devianter.Text) string { | |||
| 285 | return renderDocument(host, doc) | 313 | return renderDocument(host, doc) |
| 286 | } | 314 | } |
| 287 | 315 | ||
| 316 | description, dl := dscr.Html.Markup, len(dscr.Html.Markup) | ||
| 317 | if dl != 0 && description[0] == '{' && description[dl-1] == '}' { | ||
| 318 | return renderDraftJS(host, description) | ||
| 319 | } | ||
| 320 | if dl != 0 { | ||
| 321 | return renderMarkupHTML(host, description) | ||
| 322 | } | ||
| 323 | return "" | ||
| 324 | } | ||
| 325 | |||
| 326 | // tagBuilder wraps content in each tag in turn, innermost first. | ||
| 327 | func tagBuilder(content string, tags ...string) string { | ||
| 328 | for _, tag := range tags { | ||
| 329 | var htm strings.Builder | ||
| 330 | htm.WriteString("<") | ||
| 331 | htm.WriteString(tag) | ||
| 332 | htm.WriteString(">") | ||
| 333 | htm.WriteString(content) | ||
| 334 | htm.WriteString("</") | ||
| 335 | htm.WriteString(tag) | ||
| 336 | htm.WriteString(">") | ||
| 337 | content = htm.String() | ||
| 338 | } | ||
| 339 | return content | ||
| 340 | } | ||
| 341 | |||
| 342 | type draftBlock struct { | ||
| 343 | Text string `json:"text"` | ||
| 344 | Type string `json:"type"` | ||
| 345 | InlineStyleRanges []struct { | ||
| 346 | Offset int `json:"offset"` | ||
| 347 | Length int `json:"length"` | ||
| 348 | Style string `json:"style"` | ||
| 349 | } `json:"inlineStyleRanges"` | ||
| 350 | EntityRanges []struct { | ||
| 351 | Offset int `json:"offset"` | ||
| 352 | Length int `json:"length"` | ||
| 353 | Key int `json:"key"` | ||
| 354 | } `json:"entityRanges"` | ||
| 355 | Data struct { | ||
| 356 | TextAlignment string `json:"textAlignment"` | ||
| 357 | } `json:"data"` | ||
| 358 | } | ||
| 359 | |||
| 360 | type draftDescription struct { | ||
| 361 | Blocks []draftBlock `json:"blocks"` | ||
| 362 | EntityMap map[string]struct { | ||
| 363 | Type string `json:"type"` | ||
| 364 | Data struct { | ||
| 365 | URL string `json:"url"` | ||
| 366 | Config struct { | ||
| 367 | // "aligment" is DeviantArt's own spelling; do not correct it. | ||
| 368 | Aligment string `json:"aligment"` | ||
| 369 | Width int `json:"width"` | ||
| 370 | } `json:"config"` | ||
| 371 | Data devianter.Deviation `json:"data"` | ||
| 372 | } `json:"data"` | ||
| 373 | } `json:"entityMap"` | ||
| 374 | } | ||
| 375 | |||
| 376 | // renderDraftJS renders the older Draft.js description JSON. | ||
| 377 | func renderDraftJS(host, description string) string { | ||
| 378 | var descr draftDescription | ||
| 379 | e := json.Unmarshal([]byte(description), &descr) | ||
| 380 | try(e) | ||
| 381 | |||
| 382 | entities := make(map[int]devianter.Deviation) | ||
| 383 | urls := make(map[int]string) | ||
| 384 | for n, x := range descr.EntityMap { | ||
| 385 | num, _ := strconv.Atoi(n) | ||
| 386 | if x.Data.URL != "" { | ||
| 387 | urls[num] = deleteTrackingFromURL(x.Data.URL) | ||
| 388 | } | ||
| 389 | entities[num] = x.Data.Data | ||
| 390 | } | ||
| 391 | |||
| 288 | var parsedDescription strings.Builder | 392 | var parsedDescription strings.Builder |
| 289 | TagBuilder := func(content string, tags ...string) string { | 393 | for _, x := range descr.Blocks { |
| 290 | l := len(tags) | 394 | styles := draftStyles(x) |
| 291 | for x := range l { | 395 | switch x.Type { |
| 292 | var htm strings.Builder | 396 | case "atomic": |
| 293 | htm.WriteString("<") | 397 | if len(x.EntityRanges) != 0 { |
| 294 | htm.WriteString(tags[x]) | 398 | writeDraftArtwork(&parsedDescription, host, entities[x.EntityRanges[0].Key]) |
| 295 | htm.WriteString(">") | 399 | } |
| 296 | 400 | case "unstyled": | |
| 297 | htm.WriteString(content) | 401 | writeDraftText(&parsedDescription, x, styles, urls) |
| 298 | |||
| 299 | htm.WriteString("</") | ||
| 300 | htm.WriteString(tags[x]) | ||
| 301 | htm.WriteString(">") | ||
| 302 | content = htm.String() | ||
| 303 | } | 402 | } |
| 304 | return content | 403 | parsedDescription.WriteString("<br>") |
| 305 | } | 404 | } |
| 306 | DeleteTrackingFromURL := deleteTrackingFromURL | 405 | return parsedDescription.String() |
| 307 | 406 | } | |
| 308 | if description, dl := dscr.Html.Markup, len(dscr.Html.Markup); dl != 0 && | 407 | |
| 309 | description[0] == '{' && | 408 | // draftStyles renders each inline style range of a block as tagged text. |
| 310 | description[dl-1] == '}' { | 409 | func draftStyles(x draftBlock) []text { |
| 311 | var descr struct { | 410 | styles := make([]text, len(x.InlineStyleRanges)) |
| 312 | Blocks []struct { | 411 | if len(x.InlineStyleRanges) == 0 { |
| 313 | Text string `json:"text"` | 412 | return styles |
| 314 | Type string `json:"type"` | 413 | } |
| 315 | InlineStyleRanges []struct { | 414 | var tags = make(map[int][]string) |
| 316 | Offset int `json:"offset"` | 415 | for n, rngs := range x.InlineStyleRanges { |
| 317 | Length int `json:"length"` | 416 | style := &styles[n] |
| 318 | Style string `json:"style"` | 417 | switch rngs.Style { |
| 319 | } `json:"inlineStyleRanges"` | 418 | case "BOLD": |
| 320 | EntityRanges []struct { | 419 | rngs.Style = "b" |
| 321 | Offset int `json:"offset"` | 420 | case "UNDERLINE": |
| 322 | Length int `json:"length"` | 421 | rngs.Style = "u" |
| 323 | Key int `json:"key"` | 422 | case "ITALIC": |
| 324 | } `json:"entityRanges"` | 423 | rngs.Style = "i" |
| 325 | Data struct { | ||
| 326 | TextAlignment string `json:"textAlignment"` | ||
| 327 | } `json:"data"` | ||
| 328 | } `json:"blocks"` | ||
| 329 | EntityMap map[string]struct { | ||
| 330 | Type string `json:"type"` | ||
| 331 | Data struct { | ||
| 332 | URL string `json:"url"` | ||
| 333 | Config struct { | ||
| 334 | // "aligment" is DeviantArt's own spelling; do not correct it. | ||
| 335 | Aligment string `json:"aligment"` | ||
| 336 | Width int `json:"width"` | ||
| 337 | } `json:"config"` | ||
| 338 | Data devianter.Deviation `json:"data"` | ||
| 339 | } `json:"data"` | ||
| 340 | } `json:"entityMap"` | ||
| 341 | } | 424 | } |
| 342 | e := json.Unmarshal([]byte(description), &descr) | 425 | style.From = rngs.Offset |
| 343 | try(e) | 426 | style.To = rngs.Offset + rngs.Length |
| 344 | 427 | ft := style.From * style.To | |
| 345 | entities := make(map[int]devianter.Deviation) | 428 | tags[ft] = append(tags[ft], rngs.Style) |
| 346 | urls := make(map[int]string) | 429 | } |
| 347 | for n, x := range descr.EntityMap { | 430 | for n := range styles { |
| 348 | num, _ := strconv.Atoi(n) | 431 | style := &styles[n] |
| 349 | if x.Data.URL != "" { | 432 | style.TxtRaw = x.Text[style.From:style.To] |
| 350 | urls[num] = DeleteTrackingFromURL(x.Data.URL) | 433 | style.Txt = tagBuilder(esc(style.TxtRaw), tags[style.From*style.To]...) |
| 351 | } | 434 | } |
| 352 | entities[num] = x.Data.Data | 435 | return styles |
| 436 | } | ||
| 437 | |||
| 438 | // writeDraftArtwork writes an embedded deviation as a linked thumbnail. | ||
| 439 | func writeDraftArtwork(b *strings.Builder, host string, d devianter.Deviation) { | ||
| 440 | b.WriteString(`<a href="`) | ||
| 441 | b.WriteString(esc(ConvertDeviantArtURLToSkunkyArt(host, d.Url))) | ||
| 442 | b.WriteString(`"><img width="50%" src="`) | ||
| 443 | b.WriteString(esc(ParseMedia(host, d.Media))) | ||
| 444 | b.WriteString(`" alt="`) | ||
| 445 | b.WriteString(esc(d.Author.Username)) | ||
| 446 | b.WriteString(" - ") | ||
| 447 | b.WriteString(esc(d.Title)) | ||
| 448 | b.WriteString(`" title="`) | ||
| 449 | b.WriteString(esc(d.Author.Username)) | ||
| 450 | b.WriteString(" - ") | ||
| 451 | b.WriteString(esc(d.Title)) | ||
| 452 | b.WriteString(`"></a>`) | ||
| 453 | } | ||
| 454 | |||
| 455 | // writeDraftText writes an unstyled block with its styled ranges and link. | ||
| 456 | func writeDraftText(b *strings.Builder, x draftBlock, styles []text, urls map[int]string) { | ||
| 457 | l := len(styles) | ||
| 458 | if l == 0 { | ||
| 459 | b.WriteString(esc(x.Text)) | ||
| 460 | return | ||
| 461 | } | ||
| 462 | for n, r := range styles { | ||
| 463 | var tag string | ||
| 464 | if x.Type == "header-two" { | ||
| 465 | tag = "h2" | ||
| 353 | } | 466 | } |
| 354 | 467 | ||
| 355 | for _, x := range descr.Blocks { | 468 | b.WriteString(esc(x.Text[:r.From])) |
| 356 | Styles := make([]text, len(x.InlineStyleRanges)) | 469 | if len(urls) != 0 && len(x.EntityRanges) != 0 { |
| 357 | 470 | ra := &x.EntityRanges[0] | |
| 358 | if len(x.InlineStyleRanges) != 0 { | 471 | |
| 359 | var tags = make(map[int][]string) | 472 | b.WriteString(`<a target="_blank" href="`) |
| 360 | for n, rngs := range x.InlineStyleRanges { | 473 | b.WriteString(esc(urls[ra.Key])) |
| 361 | Styles := &Styles[n] | 474 | b.WriteString(`">`) |
| 362 | switch rngs.Style { | 475 | b.WriteString(r.Txt) |
| 363 | case "BOLD": | 476 | b.WriteString(`</a>`) |
| 364 | rngs.Style = "b" | 477 | } else if l > n+1 { |
| 365 | case "UNDERLINE": | 478 | b.WriteString(r.Txt) |
| 366 | rngs.Style = "u" | 479 | } |
| 367 | case "ITALIC": | 480 | b.WriteString(tagBuilder(tag, esc(x.Text[r.To:]))) |
| 368 | rngs.Style = "i" | 481 | } |
| 369 | } | 482 | } |
| 370 | Styles.From = rngs.Offset | ||
| 371 | Styles.To = rngs.Offset + rngs.Length | ||
| 372 | FT := Styles.From * Styles.To | ||
| 373 | tags[FT] = append(tags[FT], rngs.Style) | ||
| 374 | } | ||
| 375 | for n := range Styles { | ||
| 376 | Styles := &Styles[n] | ||
| 377 | Styles.TxtRaw = x.Text[Styles.From:Styles.To] | ||
| 378 | Styles.Txt = TagBuilder(esc(Styles.TxtRaw), tags[Styles.From*Styles.To]...) | ||
| 379 | } | ||
| 380 | } | ||
| 381 | 483 | ||
| 382 | switch x.Type { | 484 | // renderMarkupHTML renders plain HTML markup, keeping links, DeviantArt |
| 383 | case "atomic": | 485 | // emoticons, and a few bare formatting tags. |
| 384 | if len(x.EntityRanges) != 0 { | 486 | func renderMarkupHTML(host, markup string) string { |
| 385 | d := entities[x.EntityRanges[0].Key] | 487 | var parsedDescription strings.Builder |
| 386 | parsedDescription.WriteString(`<a href="`) | 488 | for tt := html.NewTokenizer(strings.NewReader(markup)); ; { |
| 387 | parsedDescription.WriteString(esc(ConvertDeviantArtURLToSkunkyArt(host, d.Url))) | 489 | switch tt.Next() { |
| 388 | parsedDescription.WriteString(`"><img width="50%" src="`) | 490 | case html.ErrorToken: |
| 389 | parsedDescription.WriteString(esc(ParseMedia(host, d.Media))) | 491 | return parsedDescription.String() |
| 390 | parsedDescription.WriteString(`" alt="`) | 492 | case html.CommentToken, html.DoctypeToken: |
| 391 | parsedDescription.WriteString(esc(d.Author.Username)) | 493 | // No renderable content; skip. |
| 392 | parsedDescription.WriteString(" - ") | 494 | case html.StartTagToken, html.EndTagToken, html.SelfClosingTagToken: |
| 393 | parsedDescription.WriteString(esc(d.Title)) | 495 | writeMarkupTag(&parsedDescription, host, tt, tt.Token()) |
| 394 | parsedDescription.WriteString(`" title="`) | 496 | case html.TextToken: |
| 395 | parsedDescription.WriteString(esc(d.Author.Username)) | 497 | parsedDescription.WriteString(esc(string(tt.Text()))) |
| 396 | parsedDescription.WriteString(" - ") | ||
| 397 | parsedDescription.WriteString(esc(d.Title)) | ||
| 398 | parsedDescription.WriteString(`"></a>`) | ||
| 399 | } | ||
| 400 | case "unstyled": | ||
| 401 | if l := len(Styles); l != 0 { | ||
| 402 | for n, r := range Styles { | ||
| 403 | var tag string | ||
| 404 | if x.Type == "header-two" { | ||
| 405 | tag = "h2" | ||
| 406 | } | ||
| 407 | |||
| 408 | parsedDescription.WriteString(esc(x.Text[:r.From])) | ||
| 409 | if len(urls) != 0 && len(x.EntityRanges) != 0 { | ||
| 410 | ra := &x.EntityRanges[0] | ||
| 411 | |||
| 412 | parsedDescription.WriteString(`<a target="_blank" href="`) | ||
| 413 | parsedDescription.WriteString(esc(urls[ra.Key])) | ||
| 414 | parsedDescription.WriteString(`">`) | ||
| 415 | parsedDescription.WriteString(r.Txt) | ||
| 416 | parsedDescription.WriteString(`</a>`) | ||
| 417 | } else if l > n+1 { | ||
| 418 | parsedDescription.WriteString(r.Txt) | ||
| 419 | } | ||
| 420 | parsedDescription.WriteString(TagBuilder(tag, esc(x.Text[r.To:]))) | ||
| 421 | } | ||
| 422 | } else { | ||
| 423 | parsedDescription.WriteString(esc(x.Text)) | ||
| 424 | } | ||
| 425 | } | ||
| 426 | parsedDescription.WriteString("<br>") | ||
| 427 | } | 498 | } |
| 428 | } else if dl != 0 { | 499 | } |
| 429 | for tt := html.NewTokenizer(strings.NewReader(dscr.Html.Markup)); ; { | 500 | } |
| 430 | switch tt.Next() { | 501 | |
| 431 | case html.ErrorToken: | 502 | func writeMarkupTag(b *strings.Builder, host string, tt *html.Tokenizer, token html.Token) { |
| 432 | return parsedDescription.String() | 503 | switch token.Data { |
| 433 | case html.CommentToken, html.DoctypeToken: | 504 | case "a": |
| 434 | // No renderable content; skip. | 505 | for _, a := range token.Attr { |
| 435 | case html.StartTagToken, html.EndTagToken, html.SelfClosingTagToken: | 506 | if a.Key == "href" { |
| 436 | token := tt.Token() | 507 | url := deleteTrackingFromURL(a.Val) |
| 437 | switch token.Data { | 508 | b.WriteString(`<a target="_blank" href="`) |
| 438 | case "a": | 509 | b.WriteString(esc(url)) |
| 439 | for _, a := range token.Attr { | 510 | b.WriteString(`">`) |
| 440 | if a.Key == "href" { | 511 | b.WriteString(esc(GetValueOfTag(tt))) |
| 441 | url := DeleteTrackingFromURL(a.Val) | 512 | b.WriteString("</a> ") |
| 442 | parsedDescription.WriteString(`<a target="_blank" href="`) | ||
| 443 | parsedDescription.WriteString(esc(url)) | ||
| 444 | parsedDescription.WriteString(`">`) | ||
| 445 | parsedDescription.WriteString(esc(GetValueOfTag(tt))) | ||
| 446 | parsedDescription.WriteString("</a> ") | ||
| 447 | } | ||
| 448 | } | ||
| 449 | case "img": | ||
| 450 | // Only DeviantArt's emoticons are carried over, served | ||
| 451 | // through this instance; any other image is dropped. | ||
| 452 | var uri, title string | ||
| 453 | for _, a := range token.Attr { | ||
| 454 | switch a.Key { | ||
| 455 | case "src": | ||
| 456 | uri = emoticonURL(host, a.Val) | ||
| 457 | case "title": | ||
| 458 | title = a.Val | ||
| 459 | } | ||
| 460 | } | ||
| 461 | if uri != "" { | ||
| 462 | parsedDescription.WriteString(`<img src="`) | ||
| 463 | parsedDescription.WriteString(esc(uri)) | ||
| 464 | parsedDescription.WriteString(`" alt="`) | ||
| 465 | parsedDescription.WriteString(esc(title)) | ||
| 466 | parsedDescription.WriteString(`" title="`) | ||
| 467 | parsedDescription.WriteString(esc(title)) | ||
| 468 | parsedDescription.WriteString(`">`) | ||
| 469 | } | ||
| 470 | case "br", "li", "ul", "p", "b": | ||
| 471 | // The bare tag, not token.String(): that would carry over | ||
| 472 | // whatever attributes DeviantArt's markup put on it. | ||
| 473 | if token.Type == html.EndTagToken { | ||
| 474 | parsedDescription.WriteString("</") | ||
| 475 | } else { | ||
| 476 | parsedDescription.WriteString("<") | ||
| 477 | } | ||
| 478 | parsedDescription.WriteString(token.Data) | ||
| 479 | parsedDescription.WriteString(">") | ||
| 480 | case "div": | ||
| 481 | parsedDescription.WriteString("<p> ") | ||
| 482 | } | ||
| 483 | case html.TextToken: | ||
| 484 | parsedDescription.WriteString(esc(string(tt.Text()))) | ||
| 485 | } | 513 | } |
| 486 | } | 514 | } |
| 515 | case "img": | ||
| 516 | writeEmoticon(b, host, token) | ||
| 517 | case "br", "li", "ul", "p", "b": | ||
| 518 | // The bare tag, not token.String(): that would carry over | ||
| 519 | // whatever attributes DeviantArt's markup put on it. | ||
| 520 | if token.Type == html.EndTagToken { | ||
| 521 | b.WriteString("</") | ||
| 522 | } else { | ||
| 523 | b.WriteString("<") | ||
| 524 | } | ||
| 525 | b.WriteString(token.Data) | ||
| 526 | b.WriteString(">") | ||
| 527 | case "div": | ||
| 528 | b.WriteString("<p> ") | ||
| 487 | } | 529 | } |
| 530 | } | ||
| 488 | 531 | ||
| 489 | return parsedDescription.String() | 532 | // writeEmoticon carries over DeviantArt's emoticons, served through this |
| 533 | // instance; any other image is dropped. | ||
| 534 | func writeEmoticon(b *strings.Builder, host string, token html.Token) { | ||
| 535 | var uri, title string | ||
| 536 | for _, a := range token.Attr { | ||
| 537 | switch a.Key { | ||
| 538 | case "src": | ||
| 539 | uri = emoticonURL(host, a.Val) | ||
| 540 | case "title": | ||
| 541 | title = a.Val | ||
| 542 | } | ||
| 543 | } | ||
| 544 | if uri != "" { | ||
| 545 | b.WriteString(`<img src="`) | ||
| 546 | b.WriteString(esc(uri)) | ||
| 547 | b.WriteString(`" alt="`) | ||
| 548 | b.WriteString(esc(title)) | ||
| 549 | b.WriteString(`" title="`) | ||
| 550 | b.WriteString(esc(title)) | ||
| 551 | b.WriteString(`">`) | ||
| 552 | } | ||
| 490 | } | 553 | } |
app/router.go +174 −150
| @@ -40,178 +40,202 @@ func Router() { | |||
| 40 | tryWithExitStatus(srv.ListenAndServe(), 1) | 40 | tryWithExitStatus(srv.ListenAndServe(), 1) |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | // Handler returns the single catch-all handler that dispatches every path. | 43 | // parsePath splits a request path below CFG.URI into its segments. |
| 44 | func Handler() http.HandlerFunc { | 44 | func parsePath(path string) map[int]string { |
| 45 | parsepath := func(path string) map[int]string { | 45 | if l := len(CFG.URI); len(path) > l { |
| 46 | if l := len(CFG.URI); len(path) > l { | 46 | path = path[l-1:] |
| 47 | path = path[l-1:] | 47 | } else { |
| 48 | } else { | 48 | path = "/" |
| 49 | path = "/" | 49 | } |
| 50 | } | ||
| 51 | 50 | ||
| 52 | parsedpath := make(map[int]string) | 51 | parsedpath := make(map[int]string) |
| 53 | for x := 0; true; x++ { | 52 | for x := 0; true; x++ { |
| 54 | slash := strings.Index(path, "/") + 1 | 53 | slash := strings.Index(path, "/") + 1 |
| 55 | content := path[:slash] | 54 | content := path[:slash] |
| 56 | path = path[slash:] | 55 | path = path[slash:] |
| 57 | if slash == 0 { | 56 | if slash == 0 { |
| 58 | parsedpath[x] = path | 57 | parsedpath[x] = path |
| 59 | break | 58 | break |
| 60 | } | ||
| 61 | parsedpath[x] = content[:slash-1] | ||
| 62 | } | 59 | } |
| 63 | return parsedpath | 60 | parsedpath[x] = content[:slash-1] |
| 64 | } | 61 | } |
| 62 | return parsedpath | ||
| 63 | } | ||
| 65 | 64 | ||
| 66 | next := func(path map[int]string, from int) string { | 65 | // joinPath rejoins the path segments from index from onward. |
| 67 | var out strings.Builder | 66 | func joinPath(path map[int]string, from int) string { |
| 68 | for x, l := from, len(path)-1; x <= l; x++ { | 67 | var out strings.Builder |
| 69 | out.WriteString(path[x]) | 68 | for x, l := from, len(path)-1; x <= l; x++ { |
| 70 | if x != l { | 69 | out.WriteString(path[x]) |
| 71 | out.WriteString("/") | 70 | if x != l { |
| 72 | } | 71 | out.WriteString("/") |
| 73 | } | 72 | } |
| 74 | return out.String() | ||
| 75 | } | 73 | } |
| 74 | return out.String() | ||
| 75 | } | ||
| 76 | 76 | ||
| 77 | open := func(name string) []byte { | 77 | // openStatic reads an embedded static file. |
| 78 | file, err := static.Templates.Open(name) | 78 | func openStatic(name string) []byte { |
| 79 | if err != nil { | 79 | file, err := static.Templates.Open(name) |
| 80 | try(err) | 80 | if err != nil { |
| 81 | return nil | 81 | try(err) |
| 82 | } | 82 | return nil |
| 83 | defer func() { try(file.Close()) }() | 83 | } |
| 84 | defer func() { try(file.Close()) }() | ||
| 84 | 85 | ||
| 85 | fileReaded, err := io.ReadAll(file) | 86 | fileReaded, err := io.ReadAll(file) |
| 86 | if err != nil { | 87 | if err != nil { |
| 87 | try(err) | 88 | try(err) |
| 88 | return nil | 89 | return nil |
| 89 | } | ||
| 90 | return fileReaded | ||
| 91 | } | 90 | } |
| 91 | return fileReaded | ||
| 92 | } | ||
| 92 | 93 | ||
| 94 | // Handler returns the single catch-all handler that dispatches every path. | ||
| 95 | func Handler() http.HandlerFunc { | ||
| 93 | // the function that drives everything | 96 | // the function that drives everything |
| 94 | return func(w http.ResponseWriter, r *http.Request) { | 97 | return func(w http.ResponseWriter, r *http.Request) { |
| 95 | path := parsepath(r.URL.Path) | 98 | path := parsePath(r.URL.Path) |
| 96 | 99 | skunky := newSkunkyart(w, r, path[1]) | |
| 97 | // Per-request, not a package global: requests arrive concurrently on | 100 | skunky.API.main = &skunky |
| 98 | // different hosts and ports (bots hitting a proxy's alternate ports, for | 101 | setPageHeaders(w) |
| 99 | // one), and a shared global lets one request's host leak into another's | 102 | |
| 100 | // rendered URLs. Those URLs then point at a different origin, which this | 103 | if daLimiter != nil && limited(skunky.Endpoint) && !daLimiter.allow(clientAddr(r)) { |
| 101 | // handler's own default-src 'self' CSP blocks. | 104 | w.Header().Set("Retry-After", "60") |
| 102 | host := "http://" + r.Host | 105 | skunky.ReturnHTTPError(http.StatusTooManyRequests) |
| 103 | if h := r.Header["X-Forwarded-Proto"]; len(h) != 0 && h[0] == "https" { | 106 | return |
| 104 | host = "https://" + r.Host | ||
| 105 | } | 107 | } |
| 108 | dispatch(&skunky, w, path) | ||
| 109 | } | ||
| 110 | } | ||
| 106 | 111 | ||
| 107 | var skunky = skunkyart{Version: Release.Version, Host: host} | 112 | // newSkunkyart builds the per-request state. |
| 108 | skunky._pth = r.URL.Path | 113 | func newSkunkyart(w http.ResponseWriter, r *http.Request, endpoint string) skunkyart { |
| 114 | // Per-request, not a package global: requests arrive concurrently on | ||
| 115 | // different hosts and ports (bots hitting a proxy's alternate ports, for | ||
| 116 | // one), and a shared global lets one request's host leak into another's | ||
| 117 | // rendered URLs. Those URLs then point at a different origin, which this | ||
| 118 | // handler's own default-src 'self' CSP blocks. | ||
| 119 | host := "http://" + r.Host | ||
| 120 | if h := r.Header["X-Forwarded-Proto"]; len(h) != 0 && h[0] == "https" { | ||
| 121 | host = "https://" + r.Host | ||
| 122 | } | ||
| 109 | 123 | ||
| 110 | skunky.Args = r.URL.Query() | 124 | var skunky = skunkyart{Version: Release.Version, Host: host} |
| 111 | arg := skunky.Args.Get | 125 | skunky._pth = r.URL.Path |
| 112 | p, _ := strconv.Atoi(arg("p")) | ||
| 113 | 126 | ||
| 114 | skunky.Endpoint = path[1] | 127 | skunky.Args = r.URL.Query() |
| 115 | skunky.API.main = &skunky | 128 | arg := skunky.Args.Get |
| 116 | skunky.Writer = w | 129 | p, _ := strconv.Atoi(arg("p")) |
| 117 | skunky.BasePath = CFG.URI | ||
| 118 | skunky.Lang = ResolveLang(r.Header.Get("Accept-Language")) | ||
| 119 | skunky.QueryRaw = arg("q") | ||
| 120 | skunky.Query = url.QueryEscape(skunky.QueryRaw) | ||
| 121 | skunky.Page = p | ||
| 122 | |||
| 123 | if t := arg("type"); len(t) > 0 { | ||
| 124 | skunky.Type = rune(t[0]) | ||
| 125 | } | ||
| 126 | 130 | ||
| 127 | if arg("atom") == "true" { | 131 | skunky.Endpoint = endpoint |
| 128 | skunky.Atom = true | 132 | skunky.Writer = w |
| 129 | } | 133 | skunky.BasePath = CFG.URI |
| 134 | skunky.Lang = ResolveLang(r.Header.Get("Accept-Language")) | ||
| 135 | skunky.QueryRaw = arg("q") | ||
| 136 | skunky.Query = url.QueryEscape(skunky.QueryRaw) | ||
| 137 | skunky.Page = p | ||
| 130 | 138 | ||
| 131 | if CFG.Proxy { | 139 | if t := arg("type"); len(t) > 0 { |
| 132 | w.Header().Add("Content-Security-Policy", "default-src 'self'; script-src 'none'; style-src 'self' 'unsafe-inline'") | 140 | skunky.Type = rune(t[0]) |
| 133 | } else { | 141 | } |
| 134 | w.Header().Add("Content-Security-Policy", "default-src 'self'; img-src 'self' *.wixmp.com; script-src 'none'; style-src 'self' 'unsafe-inline'") | ||
| 135 | } | ||
| 136 | 142 | ||
| 137 | w.Header().Add("X-Frame-Options", "DENY") | 143 | if arg("atom") == "true" { |
| 138 | w.Header().Set("Cache-Control", cacheControlPage) | 144 | skunky.Atom = true |
| 145 | } | ||
| 146 | return skunky | ||
| 147 | } | ||
| 139 | 148 | ||
| 140 | if daLimiter != nil && limited(skunky.Endpoint) && !daLimiter.allow(clientAddr(r)) { | 149 | func setPageHeaders(w http.ResponseWriter) { |
| 141 | w.Header().Set("Retry-After", "60") | 150 | if CFG.Proxy { |
| 142 | skunky.ReturnHTTPError(http.StatusTooManyRequests) | 151 | w.Header().Add("Content-Security-Policy", "default-src 'self'; script-src 'none'; style-src 'self' 'unsafe-inline'") |
| 143 | return | 152 | } else { |
| 153 | w.Header().Add("Content-Security-Policy", "default-src 'self'; img-src 'self' *.wixmp.com; script-src 'none'; style-src 'self' 'unsafe-inline'") | ||
| 154 | } | ||
| 155 | |||
| 156 | w.Header().Add("X-Frame-Options", "DENY") | ||
| 157 | w.Header().Set("Cache-Control", cacheControlPage) | ||
| 158 | } | ||
| 159 | |||
| 160 | // dispatch routes a request to the page, media, asset, or API handler for its endpoint. | ||
| 161 | func dispatch(skunky *skunkyart, w http.ResponseWriter, path map[int]string) { | ||
| 162 | switch skunky.Endpoint { | ||
| 163 | // main | ||
| 164 | case "": | ||
| 165 | skunky.ExecuteTemplate("index.htm", "html", skunky) | ||
| 166 | case "about": | ||
| 167 | skunky.Templates.About = About | ||
| 168 | skunky.ExecuteTemplate("about.htm", "html", skunky) | ||
| 169 | case "post": | ||
| 170 | skunky.Deviation(path[2], path[3]) | ||
| 171 | case "search": | ||
| 172 | skunky.Search() | ||
| 173 | case "dd": | ||
| 174 | skunky.DD() | ||
| 175 | case "group_user": | ||
| 176 | skunky.GRUser() | ||
| 177 | case "media": | ||
| 178 | serveMedia(skunky, w, path) | ||
| 179 | case "stylesheet", "favicon.ico", "robots.txt": | ||
| 180 | serveAsset(skunky.Endpoint, w) | ||
| 181 | case "api": | ||
| 182 | w.Header().Add("Content-Type", "application/json") | ||
| 183 | serveAPI(skunky, path) | ||
| 184 | |||
| 185 | // 404 | ||
| 186 | default: | ||
| 187 | skunky.ReturnHTTPError(404) | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | func serveMedia(skunky *skunkyart, w http.ResponseWriter, path map[int]string) { | ||
| 192 | switch path[2] { | ||
| 193 | case "file": | ||
| 194 | if a := skunky.Args.Get("filename"); a != "" { | ||
| 195 | skunky.SetFilename(a) | ||
| 144 | } | 196 | } |
| 197 | w.Header().Set("Cache-Control", cacheControlMedia) | ||
| 198 | skunky.DownloadAndSendMedia(path[3], joinPath(path, 4)) | ||
| 199 | case "emojitar": | ||
| 200 | w.Header().Set("Cache-Control", cacheControlAssets) | ||
| 201 | skunky.Emojitar(path[3]) | ||
| 202 | default: | ||
| 203 | skunky.ReturnHTTPError(404) | ||
| 204 | } | ||
| 205 | } | ||
| 145 | 206 | ||
| 146 | switch skunky.Endpoint { | 207 | func serveAsset(endpoint string, w http.ResponseWriter) { |
| 147 | // main | 208 | w.Header().Set("Cache-Control", cacheControlAssets) |
| 148 | case "": | 209 | switch endpoint { |
| 149 | skunky.ExecuteTemplate("index.htm", "html", &skunky) | 210 | case "stylesheet": |
| 150 | case "about": | 211 | w.Header().Add("Content-Type", "text/css") |
| 151 | skunky.Templates.About = About | 212 | _, _ = w.Write(openStatic("css/skunky.css")) |
| 152 | skunky.ExecuteTemplate("about.htm", "html", &skunky) | 213 | // "auto" is the stylesheet as written: dark, with a light palette |
| 153 | case "post": | 214 | // behind prefers-color-scheme. Forcing a theme means re-declaring |
| 154 | skunky.Deviation(path[2], path[3]) | 215 | // that palette unconditionally, which outranks the media query |
| 155 | case "search": | 216 | // because it comes later with equal specificity. |
| 156 | skunky.Search() | 217 | if css := forcedThemeCSS(); css != "" { |
| 157 | case "dd": | 218 | _, _ = w.Write([]byte(css)) |
| 158 | skunky.DD() | ||
| 159 | case "group_user": | ||
| 160 | skunky.GRUser() | ||
| 161 | |||
| 162 | // media | ||
| 163 | case "media": | ||
| 164 | switch path[2] { | ||
| 165 | case "file": | ||
| 166 | if a := arg("filename"); a != "" { | ||
| 167 | skunky.SetFilename(a) | ||
| 168 | } | ||
| 169 | w.Header().Set("Cache-Control", cacheControlMedia) | ||
| 170 | skunky.DownloadAndSendMedia(path[3], next(path, 4)) | ||
| 171 | case "emojitar": | ||
| 172 | w.Header().Set("Cache-Control", cacheControlAssets) | ||
| 173 | skunky.Emojitar(path[3]) | ||
| 174 | default: | ||
| 175 | skunky.ReturnHTTPError(404) | ||
| 176 | } | ||
| 177 | case "stylesheet": | ||
| 178 | w.Header().Set("Cache-Control", cacheControlAssets) | ||
| 179 | w.Header().Add("Content-Type", "text/css") | ||
| 180 | _, _ = w.Write(open("css/skunky.css")) | ||
| 181 | // "auto" is the stylesheet as written: dark, with a light palette | ||
| 182 | // behind prefers-color-scheme. Forcing a theme means re-declaring | ||
| 183 | // that palette unconditionally, which outranks the media query | ||
| 184 | // because it comes later with equal specificity. | ||
| 185 | if css := forcedThemeCSS(); css != "" { | ||
| 186 | _, _ = w.Write([]byte(css)) | ||
| 187 | } | ||
| 188 | case "favicon.ico": | ||
| 189 | w.Header().Set("Cache-Control", cacheControlAssets) | ||
| 190 | _, _ = w.Write(open("images/logo.png")) | ||
| 191 | case "robots.txt": | ||
| 192 | w.Header().Set("Cache-Control", cacheControlAssets) | ||
| 193 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 194 | wr(w, robotsTXT(CFG.URI)) | ||
| 195 | |||
| 196 | // API | ||
| 197 | case "api": | ||
| 198 | w.Header().Add("Content-Type", "application/json") | ||
| 199 | switch path[2] { | ||
| 200 | case "instance": | ||
| 201 | skunky.API.Info() | ||
| 202 | case "random": | ||
| 203 | skunky.API.Random() | ||
| 204 | case "search": | ||
| 205 | skunky.API.Search() | ||
| 206 | case "post": | ||
| 207 | skunky.API.Post(path[3], path[4]) | ||
| 208 | default: | ||
| 209 | skunky.API.Error("Not Found", 404) | ||
| 210 | } | ||
| 211 | |||
| 212 | // 404 | ||
| 213 | default: | ||
| 214 | skunky.ReturnHTTPError(404) | ||
| 215 | } | 219 | } |
| 220 | case "favicon.ico": | ||
| 221 | _, _ = w.Write(openStatic("images/logo.png")) | ||
| 222 | case "robots.txt": | ||
| 223 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 224 | wr(w, robotsTXT(CFG.URI)) | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | func serveAPI(skunky *skunkyart, path map[int]string) { | ||
| 229 | switch path[2] { | ||
| 230 | case "instance": | ||
| 231 | skunky.API.Info() | ||
| 232 | case "random": | ||
| 233 | skunky.API.Random() | ||
| 234 | case "search": | ||
| 235 | skunky.API.Search() | ||
| 236 | case "post": | ||
| 237 | skunky.API.Post(path[3], path[4]) | ||
| 238 | default: | ||
| 239 | skunky.API.Error("Not Found", 404) | ||
| 216 | } | 240 | } |
| 217 | } | 241 | } |
app/util.go +69 −61
| @@ -130,35 +130,38 @@ type skunkyart struct { | |||
| 130 | Comments template.HTML | 130 | Comments template.HTML |
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | GroupUser struct { | 133 | GroupUser groupUserPage |
| 134 | GR devianter.GRuser | 134 | Search struct { |
| 135 | Admins template.HTML | ||
| 136 | Group bool | ||
| 137 | CreationDate string | ||
| 138 | |||
| 139 | About struct { | ||
| 140 | A devianter.About | ||
| 141 | |||
| 142 | DescriptionFormatted template.HTML | ||
| 143 | Interests, Social template.HTML | ||
| 144 | Comments template.HTML | ||
| 145 | BG string | ||
| 146 | BGMeta devianter.Deviation | ||
| 147 | } | ||
| 148 | |||
| 149 | Gallery struct { | ||
| 150 | Folders template.HTML | ||
| 151 | Pages int | ||
| 152 | List template.HTML | ||
| 153 | } | ||
| 154 | } | ||
| 155 | Search struct { | ||
| 156 | Content devianter.Search | 135 | Content devianter.Search |
| 157 | List template.HTML | 136 | List template.HTML |
| 158 | } | 137 | } |
| 159 | } | 138 | } |
| 160 | } | 139 | } |
| 161 | 140 | ||
| 141 | // groupUserPage is the data the group/user page template renders. | ||
| 142 | type groupUserPage struct { | ||
| 143 | GR devianter.GRuser | ||
| 144 | Admins template.HTML | ||
| 145 | Group bool | ||
| 146 | CreationDate string | ||
| 147 | |||
| 148 | About struct { | ||
| 149 | A devianter.About | ||
| 150 | |||
| 151 | DescriptionFormatted template.HTML | ||
| 152 | Interests, Social template.HTML | ||
| 153 | Comments template.HTML | ||
| 154 | BG string | ||
| 155 | BGMeta devianter.Deviation | ||
| 156 | } | ||
| 157 | |||
| 158 | Gallery struct { | ||
| 159 | Folders template.HTML | ||
| 160 | Pages int | ||
| 161 | List template.HTML | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 162 | // pageTemplates is every page template parsed once per language, by | 165 | // pageTemplates is every page template parsed once per language, by |
| 163 | // ParseTemplates. One set per language because T is bound at parse time, so | 166 | // ParseTemplates. One set per language because T is bound at parse time, so |
| 164 | // templates ask for a key and never have to know which catalogue answered. | 167 | // templates ask for a key and never have to know which catalogue answered. |
| @@ -434,40 +437,10 @@ func (s skunkyart) NavBase(c DeviationList) string { | |||
| 434 | var list strings.Builder | 437 | var list strings.Builder |
| 435 | 438 | ||
| 436 | list.WriteString("<br>") | 439 | list.WriteString("<br>") |
| 437 | prevrev := func(msg string, page int, onpage bool) { | ||
| 438 | if !onpage { | ||
| 439 | list.WriteString(`<a href="`) | ||
| 440 | list.WriteString(esc(s._pth)) | ||
| 441 | list.WriteString(`?p=`) | ||
| 442 | list.WriteString(strconv.Itoa(page)) | ||
| 443 | if s.Type != 0 { | ||
| 444 | list.WriteString("&type=") | ||
| 445 | list.WriteRune(s.Type) | ||
| 446 | } | ||
| 447 | if s.Query != "" { | ||
| 448 | list.WriteString("&q=") | ||
| 449 | list.WriteString(esc(s.Query)) | ||
| 450 | } | ||
| 451 | if f := s.Args.Get("folder"); f != "" { | ||
| 452 | list.WriteString("&folder=") | ||
| 453 | list.WriteString(esc(f)) | ||
| 454 | } | ||
| 455 | if s.Args.Get("comments") != "" { | ||
| 456 | list.WriteString("&comments=1") | ||
| 457 | } | ||
| 458 | list.WriteString(`">`) | ||
| 459 | list.WriteString(msg) | ||
| 460 | list.WriteString("</a> ") | ||
| 461 | } else { | ||
| 462 | list.WriteString(strconv.Itoa(page)) | ||
| 463 | list.WriteString(" ") | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | p := s.Page | 440 | p := s.Page |
| 468 | 441 | ||
| 469 | if p > 1 { | 442 | if p > 1 { |
| 470 | prevrev("<= "+esc(T(s.Lang, "nav.prev"))+" |", p-1, false) | 443 | s.writeNavLink(&list, "<= "+esc(T(s.Lang, "nav.prev"))+" |", p-1, false) |
| 471 | } else { | 444 | } else { |
| 472 | p = 1 | 445 | p = 1 |
| 473 | } | 446 | } |
| @@ -482,19 +455,54 @@ func (s skunkyart) NavBase(c DeviationList) string { | |||
| 482 | 455 | ||
| 483 | for i, x := p-6, 0; (i <= last && i <= p+6) && x < 12; i++ { | 456 | for i, x := p-6, 0; (i <= last && i <= p+6) && x < 12; i++ { |
| 484 | if i > 0 { | 457 | if i > 0 { |
| 485 | var onPage bool | 458 | s.writeNavLink(&list, strconv.Itoa(i), i, i == p) |
| 486 | if i == p { | ||
| 487 | onPage = true | ||
| 488 | } | ||
| 489 | |||
| 490 | prevrev(strconv.Itoa(i), i, onPage) | ||
| 491 | x++ | 459 | x++ |
| 492 | } | 460 | } |
| 493 | } | 461 | } |
| 494 | 462 | ||
| 495 | if c.More { | 463 | if c.More { |
| 496 | prevrev("| "+esc(T(s.Lang, "nav.next"))+" =>", p+1, false) | 464 | s.writeNavLink(&list, "| "+esc(T(s.Lang, "nav.next"))+" =>", p+1, false) |
| 497 | } | 465 | } |
| 498 | 466 | ||
| 499 | return list.String() | 467 | return list.String() |
| 500 | } | 468 | } |
| 469 | |||
| 470 | // writeNavLink writes a link to page labelled msg, or just the page number | ||
| 471 | // when it is the current page. | ||
| 472 | func (s skunkyart) writeNavLink(list *strings.Builder, msg string, page int, onpage bool) { | ||
| 473 | if onpage { | ||
| 474 | list.WriteString(strconv.Itoa(page)) | ||
| 475 | list.WriteString(" ") | ||
| 476 | return | ||
| 477 | } | ||
| 478 | list.WriteString(`<a href="`) | ||
| 479 | list.WriteString(esc(s._pth)) | ||
| 480 | list.WriteString(s.pageQuery(page)) | ||
| 481 | list.WriteString(`">`) | ||
| 482 | list.WriteString(msg) | ||
| 483 | list.WriteString("</a> ") | ||
| 484 | } | ||
| 485 | |||
| 486 | // pageQuery is the query string for page, carrying over the request's type, | ||
| 487 | // search, folder, and comments arguments. | ||
| 488 | func (s skunkyart) pageQuery(page int) string { | ||
| 489 | var q strings.Builder | ||
| 490 | q.WriteString(`?p=`) | ||
| 491 | q.WriteString(strconv.Itoa(page)) | ||
| 492 | if s.Type != 0 { | ||
| 493 | q.WriteString("&type=") | ||
| 494 | q.WriteRune(s.Type) | ||
| 495 | } | ||
| 496 | if s.Query != "" { | ||
| 497 | q.WriteString("&q=") | ||
| 498 | q.WriteString(esc(s.Query)) | ||
| 499 | } | ||
| 500 | if f := s.Args.Get("folder"); f != "" { | ||
| 501 | q.WriteString("&folder=") | ||
| 502 | q.WriteString(esc(f)) | ||
| 503 | } | ||
| 504 | if s.Args.Get("comments") != "" { | ||
| 505 | q.WriteString("&comments=1") | ||
| 506 | } | ||
| 507 | return q.String() | ||
| 508 | } | ||
app/wrapper.go +175 −138
| @@ -22,6 +22,12 @@ var ( | |||
| 22 | g := devianter.Group{Name: name} | 22 | g := devianter.Group{Name: name} |
| 23 | return g.Get() | 23 | return g.Get() |
| 24 | } | 24 | } |
| 25 | fetchGallery = func(g devianter.Group, page, folderid int) (devianter.Group, devianter.Error, error) { | ||
| 26 | return g.Gallery(page, folderid) | ||
| 27 | } | ||
| 28 | fetchFavourites = func(g devianter.Group, page int, all bool, folderid int) (devianter.Group, devianter.Error) { | ||
| 29 | return g.Favourites(page, all, folderid) | ||
| 30 | } | ||
| 25 | ) | 31 | ) |
| 26 | 32 | ||
| 27 | // groupSearchURL is the DeviantArt group search page for query, paged ten | 33 | // groupSearchURL is the DeviantArt group search page for query, paged ten |
| @@ -88,43 +94,11 @@ func (s skunkyart) GRUser() { | |||
| 88 | 94 | ||
| 89 | switch s.Type { | 95 | switch s.Type { |
| 90 | case 'a': | 96 | case 'a': |
| 91 | g := group.GR | ||
| 92 | s.Atom = false | 97 | s.Atom = false |
| 93 | for _, x := range g.Gruser.Page.Modules { | 98 | for _, x := range group.GR.Gruser.Page.Modules { |
| 94 | switch x.Name { | 99 | switch x.Name { |
| 95 | case "about", "group_about": | 100 | case "about", "group_about": |
| 96 | if g.Owner.Group { | 101 | s.fillAbout(group, x.ModuleData.GroupAbout, x.ModuleData.About) |
| 97 | var about = &x.ModuleData.GroupAbout | ||
| 98 | group.Group = true | ||
| 99 | group.CreationDate = x.ModuleData.GroupAbout.FoundatedAt.UTC().String() | ||
| 100 | group.About.DescriptionFormatted = template.HTML(ParseDescription(s.Host, about.Description)) //nolint:gosec // G203: ParseDescription escapes its input | ||
| 101 | } else { | ||
| 102 | group.About.A = x.ModuleData.About | ||
| 103 | var about = &group.About.A | ||
| 104 | group.CreationDate = time.Unix(time.Now().Unix()-x.ModuleData.About.RegDate, 0).UTC().String() | ||
| 105 | group.About.DescriptionFormatted = template.HTML(ParseDescription(s.Host, about.Description)) //nolint:gosec // G203: ParseDescription escapes its input | ||
| 106 | |||
| 107 | for _, val := range x.ModuleData.About.SocialLinks { | ||
| 108 | var social strings.Builder | ||
| 109 | social.WriteString(`<a target="_blank" href="`) | ||
| 110 | social.WriteString(esc(val.Value)) | ||
| 111 | social.WriteString(`">`) | ||
| 112 | social.WriteString(esc(val.Value)) | ||
| 113 | social.WriteString("</a><br>") | ||
| 114 | group.About.Social += template.HTML(social.String()) //nolint:gosec // G203: escaped above | ||
| 115 | } | ||
| 116 | |||
| 117 | for _, val := range x.ModuleData.About.Interests { | ||
| 118 | var interest strings.Builder | ||
| 119 | interest.WriteString(esc(val.Label)) | ||
| 120 | interest.WriteString(": <b>") | ||
| 121 | interest.WriteString(esc(val.Value)) | ||
| 122 | interest.WriteString("</b><br>") | ||
| 123 | group.About.Interests += template.HTML(interest.String()) //nolint:gosec // G203: escaped above | ||
| 124 | } | ||
| 125 | } | ||
| 126 | group.About.Comments = s.commentsOrLink(strconv.Itoa(group.GR.Gruser.ID), "", 4, -1) | ||
| 127 | |||
| 128 | case "cover_deviation": | 102 | case "cover_deviation": |
| 129 | group.About.BGMeta = x.ModuleData.CoverDeviation.Deviation | 103 | group.About.BGMeta = x.ModuleData.CoverDeviation.Deviation |
| 130 | group.About.BGMeta.Url = ConvertDeviantArtURLToSkunkyArt(s.Host, group.About.BGMeta.Url) | 104 | group.About.BGMeta.Url = ConvertDeviantArtURLToSkunkyArt(s.Host, group.About.BGMeta.Url) |
| @@ -136,94 +110,187 @@ func (s skunkyart) GRUser() { | |||
| 136 | } | 110 | } |
| 137 | group.Admins += template.HTML(htm.String()) //nolint:gosec // G203: BuildUserPlate escapes its input | 111 | group.Admins += template.HTML(htm.String()) //nolint:gosec // G203: BuildUserPlate escapes its input |
| 138 | } | 112 | } |
| 139 | |||
| 140 | } | 113 | } |
| 141 | case 'g', 'f': | 114 | case 'g', 'f': |
| 142 | var all bool | ||
| 143 | var content devianter.Group | ||
| 144 | |||
| 145 | folderid, _ := strconv.Atoi(s.Args.Get("folder")) | ||
| 146 | |||
| 147 | if s.Args.Get("all") == "true" { | ||
| 148 | all = true | ||
| 149 | } | ||
| 150 | |||
| 151 | if s.Page == 0 { | 115 | if s.Page == 0 { |
| 152 | s.Page++ | 116 | s.Page++ |
| 153 | } | 117 | } |
| 154 | 118 | if !s.fillGallery(group, g) { | |
| 155 | if s.Type == 'f' { | ||
| 156 | content, daError = g.Favourites(s.Page, all, folderid) | ||
| 157 | } else { | ||
| 158 | content, daError, err = g.Gallery(s.Page, folderid) | ||
| 159 | try(err) | ||
| 160 | } | ||
| 161 | |||
| 162 | if daError.RAW != nil { | ||
| 163 | s.Error(daError) | ||
| 164 | return | 119 | return |
| 165 | } | 120 | } |
| 121 | default: | ||
| 122 | s.ReturnHTTPError(400) | ||
| 123 | } | ||
| 124 | |||
| 125 | if !s.Atom { | ||
| 126 | s.ExecuteTemplate("gruser.htm", "html", &s) | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | // fillAbout fills the about tab from a group's or a user's about module. | ||
| 131 | func (s skunkyart) fillAbout(group *groupUserPage, groupAbout devianter.GroupAbout, about devianter.About) { | ||
| 132 | if group.GR.Owner.Group { | ||
| 133 | group.Group = true | ||
| 134 | group.CreationDate = groupAbout.FoundatedAt.UTC().String() | ||
| 135 | group.About.DescriptionFormatted = template.HTML(ParseDescription(s.Host, groupAbout.Description)) //nolint:gosec // G203: ParseDescription escapes its input | ||
| 136 | } else { | ||
| 137 | group.About.A = about | ||
| 138 | group.CreationDate = time.Unix(time.Now().Unix()-about.RegDate, 0).UTC().String() | ||
| 139 | group.About.DescriptionFormatted = template.HTML(ParseDescription(s.Host, about.Description)) //nolint:gosec // G203: ParseDescription escapes its input | ||
| 140 | group.About.Social += profileLinks(about) | ||
| 141 | group.About.Interests += profileInterests(about) | ||
| 142 | } | ||
| 143 | group.About.Comments = s.commentsOrLink(strconv.Itoa(group.GR.Gruser.ID), "", 4, -1) | ||
| 144 | } | ||
| 145 | |||
| 146 | func profileLinks(about devianter.About) template.HTML { | ||
| 147 | var out template.HTML | ||
| 148 | for _, val := range about.SocialLinks { | ||
| 149 | var social strings.Builder | ||
| 150 | social.WriteString(`<a target="_blank" href="`) | ||
| 151 | social.WriteString(esc(val.Value)) | ||
| 152 | social.WriteString(`">`) | ||
| 153 | social.WriteString(esc(val.Value)) | ||
| 154 | social.WriteString("</a><br>") | ||
| 155 | out += template.HTML(social.String()) //nolint:gosec // G203: escaped above | ||
| 156 | } | ||
| 157 | return out | ||
| 158 | } | ||
| 159 | |||
| 160 | func profileInterests(about devianter.About) template.HTML { | ||
| 161 | var out template.HTML | ||
| 162 | for _, val := range about.Interests { | ||
| 163 | var interest strings.Builder | ||
| 164 | interest.WriteString(esc(val.Label)) | ||
| 165 | interest.WriteString(": <b>") | ||
| 166 | interest.WriteString(esc(val.Value)) | ||
| 167 | interest.WriteString("</b><br>") | ||
| 168 | out += template.HTML(interest.String()) //nolint:gosec // G203: escaped above | ||
| 169 | } | ||
| 170 | return out | ||
| 171 | } | ||
| 172 | |||
| 173 | // fillGallery fills the gallery or favourites tab. It returns false when | ||
| 174 | // DeviantArt answered with an error, which has already been rendered. | ||
| 175 | func (s skunkyart) fillGallery(group *groupUserPage, g devianter.Group) bool { | ||
| 176 | var err error | ||
| 177 | var daError devianter.Error | ||
| 178 | var content devianter.Group | ||
| 179 | folderid, _ := strconv.Atoi(s.Args.Get("folder")) | ||
| 180 | all := s.Args.Get("all") == "true" | ||
| 181 | |||
| 182 | if s.Type == 'f' { | ||
| 183 | content, daError = fetchFavourites(g, s.Page, all, folderid) | ||
| 184 | } else { | ||
| 185 | content, daError, err = fetchGallery(g, s.Page, folderid) | ||
| 186 | try(err) | ||
| 187 | } | ||
| 188 | |||
| 189 | if daError.RAW != nil { | ||
| 190 | s.Error(daError) | ||
| 191 | return false | ||
| 192 | } | ||
| 166 | 193 | ||
| 167 | if folderid > 0 || (s.Type == 'f' && all) { | 194 | if folderid > 0 || (s.Type == 'f' && all) { |
| 168 | group.Gallery.List = template.HTML(s.DeviationList(content.Content.Results, true, DeviationList{ //nolint:gosec // G203: DeviationList escapes its input | 195 | group.Gallery.List = template.HTML(s.DeviationList(content.Content.Results, true, DeviationList{ //nolint:gosec // G203: DeviationList escapes its input |
| 169 | More: content.Content.HasMore, | 196 | More: content.Content.HasMore, |
| 197 | })) | ||
| 198 | return true | ||
| 199 | } | ||
| 200 | for _, x := range content.Content.Gruser.Page.Modules { | ||
| 201 | if len(x.ModuleData.Folders.Results) != 0 { | ||
| 202 | group.Gallery.Folders = s.folderGrid(x.ModuleData.Folders.Results) | ||
| 203 | } | ||
| 204 | if x.Name == "folder_deviations" { | ||
| 205 | group.Gallery.List = template.HTML(s.DeviationList(x.ModuleData.Folder.Deviations, true, DeviationList{ //nolint:gosec // G203: DeviationList escapes its input | ||
| 206 | Pages: x.ModuleData.Folder.Pages, | ||
| 207 | More: x.ModuleData.Folder.HasMore, | ||
| 170 | })) | 208 | })) |
| 209 | } | ||
| 210 | } | ||
| 211 | return true | ||
| 212 | } | ||
| 213 | |||
| 214 | // galleryFolder is one entry of a gallery's folder list, as devianter decodes it. | ||
| 215 | type galleryFolder = struct { | ||
| 216 | Deviations int `json:"totalItemCount"` | ||
| 217 | FolderId int //nolint:staticcheck // ST1003: must match devianter's field for the types to be identical | ||
| 218 | Size int | ||
| 219 | Name string | ||
| 220 | Thumb devianter.Deviation | ||
| 221 | } | ||
| 222 | |||
| 223 | // folderGrid renders the non-empty folders of a gallery as thumbnail links. | ||
| 224 | func (s skunkyart) folderGrid(results []galleryFolder) template.HTML { | ||
| 225 | var folders strings.Builder | ||
| 226 | folders.WriteString(`<h1 id="folders"><a href="#folders">#</a> ` + esc(T(s.Lang, "gallery.folders")) + `</h1><div class="folders"><br>`) | ||
| 227 | for _, x := range results { | ||
| 228 | if x.FolderId == -1 || x.Size == 0 { | ||
| 229 | continue | ||
| 230 | } | ||
| 231 | folders.WriteString(`<div class="block folder-item">`) | ||
| 232 | if !x.Thumb.NSFW || CFG.Nsfw { | ||
| 233 | folders.WriteString(`<a href="`) | ||
| 234 | folders.WriteString(esc(ConvertDeviantArtURLToSkunkyArt(s.Host, x.Thumb.Url))) | ||
| 235 | folders.WriteString(`"><img loading="lazy" src="`) | ||
| 236 | folders.WriteString(esc(ParseMedia(s.Host, x.Thumb.Media))) | ||
| 237 | folders.WriteString(`" title="`) | ||
| 238 | folders.WriteString(esc(x.Thumb.Title)) | ||
| 239 | folders.WriteString(`" alt="`) | ||
| 240 | folders.WriteString(esc(x.Thumb.Title)) | ||
| 241 | folders.WriteString(`"></a>`) | ||
| 171 | } else { | 242 | } else { |
| 172 | for _, x := range content.Content.Gruser.Page.Modules { | 243 | folders.WriteString(`<h1>[ <span class="nsfw">NSFW</span> ]</h1>`) |
| 173 | if len(x.ModuleData.Folders.Results) != 0 { | 244 | } |
| 174 | var folders strings.Builder | 245 | folders.WriteString("<br>") |
| 175 | folders.WriteString(`<h1 id="folders"><a href="#folders">#</a> ` + esc(T(s.Lang, "gallery.folders")) + `</h1><div class="folders"><br>`) | 246 | |
| 176 | for _, x := range x.ModuleData.Folders.Results { | 247 | folders.WriteString(`<a href="group_user?folder=`) |
| 177 | if x.FolderId != -1 && x.Size != 0 { | 248 | folders.WriteString(strconv.Itoa(x.FolderId)) |
| 178 | folders.WriteString(`<div class="block folder-item">`) | 249 | folders.WriteString("&q=") |
| 179 | 250 | folders.WriteString(esc(s.Query)) | |
| 180 | if !x.Thumb.NSFW || CFG.Nsfw { | 251 | folders.WriteString("&type=") |
| 181 | folders.WriteString(`<a href="`) | 252 | folders.WriteRune(s.Type) |
| 182 | folders.WriteString(esc(ConvertDeviantArtURLToSkunkyArt(s.Host, x.Thumb.Url))) | 253 | folders.WriteString(`">`) |
| 183 | folders.WriteString(`"><img loading="lazy" src="`) | 254 | folders.WriteString(esc(x.Name)) |
| 184 | folders.WriteString(esc(ParseMedia(s.Host, x.Thumb.Media))) | 255 | folders.WriteString(`</a>`) |
| 185 | folders.WriteString(`" title="`) | 256 | |
| 186 | folders.WriteString(esc(x.Thumb.Title)) | 257 | folders.WriteString("</div>") |
| 187 | folders.WriteString(`" alt="`) | 258 | } |
| 188 | folders.WriteString(esc(x.Thumb.Title)) | 259 | folders.WriteString(`</div><h1 id="content"><a href="#content">#</a> ` + esc(T(s.Lang, "gallery.content")) + `</h1>`) |
| 189 | folders.WriteString(`"></a>`) | 260 | return template.HTML(folders.String()) //nolint:gosec // G203: escaped above |
| 190 | } else { | 261 | } |
| 191 | folders.WriteString(`<h1>[ <span class="nsfw">NSFW</span> ]</h1>`) | ||
| 192 | } | ||
| 193 | folders.WriteString("<br>") | ||
| 194 | |||
| 195 | folders.WriteString(`<a href="group_user?folder=`) | ||
| 196 | folders.WriteString(strconv.Itoa(x.FolderId)) | ||
| 197 | folders.WriteString("&q=") | ||
| 198 | folders.WriteString(esc(s.Query)) | ||
| 199 | folders.WriteString("&type=") | ||
| 200 | folders.WriteRune(s.Type) | ||
| 201 | folders.WriteString(`">`) | ||
| 202 | folders.WriteString(esc(x.Name)) | ||
| 203 | folders.WriteString(`</a>`) | ||
| 204 | |||
| 205 | folders.WriteString("</div>") | ||
| 206 | } | ||
| 207 | } | ||
| 208 | folders.WriteString(`</div><h1 id="content"><a href="#content">#</a> ` + esc(T(s.Lang, "gallery.content")) + `</h1>`) | ||
| 209 | group.Gallery.Folders = template.HTML(folders.String()) //nolint:gosec // G203: escaped above | ||
| 210 | } | ||
| 211 | 262 | ||
| 212 | if x.Name == "folder_deviations" { | 263 | // groupSearchUsernames scrapes the group names from DeviantArt's group search page. |
| 213 | group.Gallery.List = template.HTML(s.DeviationList(x.ModuleData.Folder.Deviations, true, DeviationList{ //nolint:gosec // G203: DeviationList escapes its input | 264 | func groupSearchUsernames(body []byte) []string { |
| 214 | Pages: x.ModuleData.Folder.Pages, | 265 | var usernames []string |
| 215 | More: x.ModuleData.Folder.HasMore, | 266 | for z := html.NewTokenizer(strings.NewReader(string(body))); ; { |
| 216 | })) | 267 | n, token := z.Next(), z.Token() |
| 217 | } | 268 | if n == html.ErrorToken { |
| 269 | return usernames | ||
| 270 | } | ||
| 271 | if n != html.StartTagToken || token.Data != "a" { | ||
| 272 | continue | ||
| 273 | } | ||
| 274 | for _, x := range token.Attr { | ||
| 275 | if x.Key == "class" && x.Val == "u regular username" { | ||
| 276 | usernames = append(usernames, GetValueOfTag(z)) | ||
| 218 | } | 277 | } |
| 219 | } | 278 | } |
| 220 | default: | ||
| 221 | s.ReturnHTTPError(400) | ||
| 222 | } | 279 | } |
| 280 | } | ||
| 223 | 281 | ||
| 224 | if !s.Atom { | 282 | // groupPlates renders scraped group names as user plates with page navigation. |
| 225 | s.ExecuteTemplate("gruser.htm", "html", &s) | 283 | func (s skunkyart) groupPlates(usernames []string) template.HTML { |
| 284 | var plates strings.Builder | ||
| 285 | plates.WriteString(`<div class="content plates">`) | ||
| 286 | for _, name := range usernames { | ||
| 287 | plates.WriteString(BuildUserPlate(s.Host, name)) | ||
| 226 | } | 288 | } |
| 289 | plates.WriteString(`</div>`) | ||
| 290 | plates.WriteString(s.NavBase(DeviationList{ | ||
| 291 | More: true, | ||
| 292 | })) | ||
| 293 | return template.HTML(plates.String()) //nolint:gosec // G203: BuildUserPlate escapes its input | ||
| 227 | } | 294 | } |
| 228 | 295 | ||
| 229 | // Deviation renders a single artwork page, with its description, tags, comments | 296 | // Deviation renders a single artwork page, with its description, tags, comments |
| @@ -335,39 +402,9 @@ func (s skunkyart) Search() { | |||
| 335 | case 'g', 'f': | 402 | case 'g', 'f': |
| 336 | ss.Content, daError, err = devianter.PerformSearch(s.Query, s.Page, s.Type, s.Args.Get("usr")) | 403 | ss.Content, daError, err = devianter.PerformSearch(s.Query, s.Page, s.Type, s.Args.Get("usr")) |
| 337 | case 'r': // scraper, since DeviantArt withholds the guest API for group search | 404 | case 'r': // scraper, since DeviantArt withholds the guest API for group search |
| 338 | var ( | ||
| 339 | usernames = make(map[int]string) | ||
| 340 | num int | ||
| 341 | ) | ||
| 342 | |||
| 343 | dwnld := Download(groupSearchURL(s.Query, s.Page)) | 405 | dwnld := Download(groupSearchURL(s.Query, s.Page)) |
| 344 | 406 | if usernames := groupSearchUsernames(dwnld.Body); len(usernames) != 0 { | |
| 345 | for z := html.NewTokenizer(strings.NewReader(string(dwnld.Body))); ; { | 407 | ss.List = s.groupPlates(usernames) |
| 346 | if n, token := z.Next(), z.Token(); n == html.StartTagToken && token.Data == "a" { | ||
| 347 | for _, x := range token.Attr { | ||
| 348 | if x.Key == "class" && x.Val == "u regular username" { | ||
| 349 | usernames[num] = GetValueOfTag(z) | ||
| 350 | num++ | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } else if n == 0 { | ||
| 354 | break | ||
| 355 | } else { | ||
| 356 | continue | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | if len(usernames) != 0 { | ||
| 361 | var plates strings.Builder | ||
| 362 | plates.WriteString(`<div class="content plates">`) | ||
| 363 | for x := range len(usernames) { | ||
| 364 | plates.WriteString(BuildUserPlate(s.Host, usernames[x])) | ||
| 365 | } | ||
| 366 | plates.WriteString(`</div>`) | ||
| 367 | plates.WriteString(s.NavBase(DeviationList{ | ||
| 368 | More: true, | ||
| 369 | })) | ||
| 370 | ss.List = template.HTML(plates.String()) //nolint:gosec // G203: BuildUserPlate escapes its input | ||
| 371 | } | 408 | } |
| 372 | default: | 409 | default: |
| 373 | s.ReturnHTTPError(400) | 410 | s.ReturnHTTPError(400) |