Deduplicate body-class logic into head-data struct

This commit is contained in:
Cadence Ember 2022-10-31 19:39:19 +13:00
parent 645fe1beee
commit e812f2082c
Signed by: cadence
GPG key ID: BC1C2C61CF521B17
5 changed files with 44 additions and 21 deletions

View file

@ -75,9 +75,9 @@
(params->query `(("search" . ,title) (params->query `(("search" . ,title)
("go" . "Go"))))] ("go" . "Go"))))]
[go (if (string-suffix? (third ind) "/") [go (if (string-suffix? (third ind) "/")
(regexp-replace "/$" (third ind) (λ (_) search-page)) (regexp-replace #rx"/$" (third ind) (λ (_) search-page))
(let* ([_ (println (regexp-match "/(w[^./]*)/" (third ind)))] [joiner (second (regexp-match "/(w[^./]*)/" (third ind)))]) (let* ([joiner (second (regexp-match #rx"/(w[^./]*)/" (third ind)))])
(regexp-replace "/w[^./]*/.*$" (third ind) (λ (_) (format "/~a~a" joiner search-page)))))]) (regexp-replace #rx"/w[^./]*/.*$" (third ind) (λ (_) (format "/~a~a" joiner search-page)))))])
`(aside (@ (class "niwa__notice")) `(aside (@ (class "niwa__notice"))
(h1 (@ (class "niwa__header")) ,(second ind) " has its own website separate from Fandom.") (h1 (@ (class "niwa__header")) ,(second ind) " has its own website separate from Fandom.")
(a (@ (class "niwa__go") (href ,go)) "Read " ,title " on " ,(second ind) "") (a (@ (class "niwa__go") (href ,go)) "Read " ,title " on " ,(second ind) "")
@ -100,12 +100,10 @@
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:body-class [body-class-in #f] #:head-data [head-data-in #f]
#:siteinfo [siteinfo-in #f]) #:siteinfo [siteinfo-in #f])
(define siteinfo (or siteinfo-in siteinfo-default)) (define siteinfo (or siteinfo-in siteinfo-default))
(define body-class (if (not body-class-in) (define head-data (or head-data-in ((head-data-getter wikiname))))
"skin-fandomdesktop"
body-class-in))
(define (required-styles origin) (define (required-styles origin)
(map (λ (dest-path) (map (λ (dest-path)
(define url (format dest-path origin)) (define url (format dest-path origin))
@ -135,6 +133,10 @@
,(if (config-true? 'feature_search_suggestions) ,(if (config-true? 'feature_search_suggestions)
`(script (@ (type "module") (src ,(get-static-url "search-suggestions.js")))) `(script (@ (type "module") (src ,(get-static-url "search-suggestions.js"))))
"") "")
(link (@ (rel "icon") (href ,(u (λ (v) (config-true? 'strict_proxy))
(λ (v) (u-proxy-url v))
(head-data^-icon-url head-data))))))
(body (@ (class ,(head-data^-body-class head-data)))
(div (@ (class "main-container")) (div (@ (class "main-container"))
(div (@ (class "fandom-community-header__background tileHorizontally header"))) (div (@ (class "fandom-community-header__background tileHorizontally header")))
(div (@ (class "page")) (div (@ (class "page"))

View file

@ -1,22 +1,29 @@
#lang racket/base #lang racket/base
(require racket/list (require racket/list
racket/match
(prefix-in easy: net/http-easy) (prefix-in easy: net/http-easy)
memo memo
"static-data.rkt"
"url-utils.rkt" "url-utils.rkt"
"xexpr-utils.rkt") "xexpr-utils.rkt")
(provide (provide
(struct-out siteinfo^) (struct-out siteinfo^)
(struct-out license^) (struct-out license^)
(struct-out head-data^)
siteinfo-fetch siteinfo-fetch
siteinfo-default siteinfo-default
license-default) license-default
head-data-getter
head-data-default)
(struct siteinfo^ (sitename basepage license) #:transparent) (struct siteinfo^ (sitename basepage license) #:transparent)
(struct license^ (text url) #:transparent) (struct license^ (text url) #:transparent)
(struct head-data^ (body-class icon-url) #:transparent)
(define license-default (license^ "CC-BY-SA" "https://www.fandom.com/licensing")) (define license-default (license^ "CC-BY-SA" "https://www.fandom.com/licensing"))
(define siteinfo-default (siteinfo^ "Test Wiki" "Main_Page" license-default)) (define siteinfo-default (siteinfo^ "Test Wiki" "Main_Page" license-default))
(define head-data-default (head-data^ "skin-fandomdesktop" (get-static-url "breezewiki-icon.svg")))
(define/memoize (siteinfo-fetch wikiname) #:hash hash (define/memoize (siteinfo-fetch wikiname) #:hash hash
(define dest-url (define dest-url
@ -34,3 +41,23 @@
(second (regexp-match #rx"/wiki/(.*)" (jp "/query/general/base" data))) (second (regexp-match #rx"/wiki/(.*)" (jp "/query/general/base" data)))
(license^ (jp "/query/rightsinfo/text" data) (license^ (jp "/query/rightsinfo/text" data)
(jp "/query/rightsinfo/url" data)))) (jp "/query/rightsinfo/url" data))))
(define/memoize (head-data-getter wikiname) #:hash hash
;; data will be stored here, can be referenced by the memoized closure
(define this-data head-data-default)
;; returns the getter
(λ ([res-in #f])
(when res-in
;; when actual information is provided, parse it into the struct and store it for the future
(define head-html (jp "/parse/headhtml" res-in ""))
(define data
(head-data^
(match (regexp-match #rx"<body [^>]*class=\"([^\"]*)" head-html)
[(list _ classes) classes]
[_ (head-data^-body-class head-data-default)])
(match (regexp-match #rx"<link rel=\"(?:shortcut )?icon\" href=\"([^\"]*)" head-html)
[(list _ icon-url) icon-url]
[_ (head-data^-icon-url head-data-default)])))
(set! this-data data))
;; then no matter what, return the best information we have so far
this-data))

View file

@ -137,7 +137,7 @@
"Founded on April 23, 2005 as Zelda Wiki, today's Zeldapedia is your definitive source for encyclopedic information on The Legend of Zelda series, as well as all of the latest Zelda news."))) "Founded on April 23, 2005 as Zelda Wiki, today's Zeldapedia is your definitive source for encyclopedic information on The Legend of Zelda series, as well as all of the latest Zelda news.")))
;; get the current dataset so it can be stored above ;; get the current dataset so it can be stored above
(module+ test (module+ fetch
(require racket/generator (require racket/generator
racket/list racket/list
net/http-easy net/http-easy

View file

@ -34,14 +34,14 @@
#:title title #:title title
#:members-data members-data #:members-data members-data
#:page page #:page page
#:body-class [body-class #f] #:head-data [head-data #f]
#:siteinfo [siteinfo #f]) #:siteinfo [siteinfo #f])
(define members (jp "/query/categorymembers" members-data)) (define members (jp "/query/categorymembers" members-data))
(generate-wiki-page (generate-wiki-page
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:body-class body-class #:head-data head-data
#:siteinfo siteinfo #:siteinfo siteinfo
`(div `(div
,(update-tree-wiki page wikiname) ,(update-tree-wiki page wikiname)
@ -94,17 +94,14 @@
(define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category))) (define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category)))
(define page-html (preprocess-html-wiki (jp "/parse/text" page-data ""))) (define page-html (preprocess-html-wiki (jp "/parse/text" page-data "")))
(define page (html->xexp page-html)) (define page (html->xexp page-html))
(define head-html (jp "/parse/headhtml" page-data "")) (define head-data ((head-data-getter wikiname) page-data))
(define body-class (match (regexp-match #rx"<body [^>]*class=\"([^\"]*)" head-html)
[(list _ classes) classes]
[_ ""]))
(define body (generate-results-page (define body (generate-results-page
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:members-data members-data #:members-data members-data
#:page page #:page page
#:body-class body-class #:head-data head-data
#:siteinfo siteinfo)) #:siteinfo siteinfo))
(when (config-true? 'debug) (when (config-true? 'debug)

View file

@ -289,10 +289,7 @@
[page-html (jp "/parse/text" data "")] [page-html (jp "/parse/text" data "")]
[page-html (preprocess-html-wiki page-html)] [page-html (preprocess-html-wiki page-html)]
[page (html->xexp page-html)] [page (html->xexp page-html)]
[head-html (jp "/parse/headhtml" data "")] [head-data ((head-data-getter wikiname) data)])
[body-class (match (regexp-match #rx"<body [^>]*class=\"([^\"]*)" head-html)
[(list _ classes) classes]
[_ ""])])
(if (equal? "missingtitle" (jp "/error/code" data #f)) (if (equal? "missingtitle" (jp "/error/code" data #f))
(next-dispatcher) (next-dispatcher)
(response-handler (response-handler
@ -302,7 +299,7 @@
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:body-class body-class #:head-data head-data
#:siteinfo siteinfo)) #:siteinfo siteinfo))
(define redirect-msg ((query-selector (attribute-selector 'class "redirectMsg") body))) (define redirect-msg ((query-selector (attribute-selector 'class "redirectMsg") body)))
(define headers (define headers