From e812f2082cb94be178f7c232602b666425330a16 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 31 Oct 2022 19:39:19 +1300 Subject: [PATCH] Deduplicate body-class logic into head-data struct --- src/application-globals.rkt | 16 +++++++++------- src/data.rkt | 29 ++++++++++++++++++++++++++++- src/niwa-data.rkt | 2 +- src/page-category.rkt | 11 ++++------- src/page-wiki.rkt | 7 ++----- 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/application-globals.rkt b/src/application-globals.rkt index bbd5236..5f290cb 100644 --- a/src/application-globals.rkt +++ b/src/application-globals.rkt @@ -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")) diff --git a/src/data.rkt b/src/data.rkt index 6673e4c..5e31fc7 100644 --- a/src/data.rkt +++ b/src/data.rkt @@ -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"]*class=\"([^\"]*)" head-html) + [(list _ classes) classes] + [_ (head-data^-body-class head-data-default)]) + (match (regexp-match #rx"xexp page-html)) - (define head-html (jp "/parse/headhtml" page-data "")) - (define body-class (match (regexp-match #rx"]*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) diff --git a/src/page-wiki.rkt b/src/page-wiki.rkt index 488886f..4e0b73a 100644 --- a/src/page-wiki.rkt +++ b/src/page-wiki.rkt @@ -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"]*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