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 untrusted user: cadence
GPG key ID: BC1C2C61CF521B17
5 changed files with 44 additions and 21 deletions

View file

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

View file

@ -1,22 +1,29 @@
#lang racket/base
(require racket/list
racket/match
(prefix-in easy: net/http-easy)
memo
"static-data.rkt"
"url-utils.rkt"
"xexpr-utils.rkt")
(provide
(struct-out siteinfo^)
(struct-out license^)
(struct-out head-data^)
siteinfo-fetch
siteinfo-default
license-default)
license-default
head-data-getter
head-data-default)
(struct siteinfo^ (sitename basepage license) #: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 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 dest-url
@ -34,3 +41,23 @@
(second (regexp-match #rx"/wiki/(.*)" (jp "/query/general/base" data)))
(license^ (jp "/query/rightsinfo/text" 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.")))
;; get the current dataset so it can be stored above
(module+ test
(module+ fetch
(require racket/generator
racket/list
net/http-easy

View file

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

View file

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