Compare commits

...

4 commits

Author SHA1 Message Date
d3187cc310
Tweak extwiki-generic migration notice 2023-04-02 00:11:55 +13:00
ba6c5be990
Optimise pre-processing regular expression 2023-04-02 00:11:55 +13:00
Artemis Everfree
f5529ed12a
precompile regexp patterns 2023-04-02 00:11:54 +13:00
Artemis Everfree
3f1946a3b8
use faster string split 2023-04-02 00:11:54 +13:00
3 changed files with 18 additions and 16 deletions

View file

@ -12,17 +12,12 @@
update-tree-wiki) update-tree-wiki)
(define (preprocess-html-wiki html) (define (preprocess-html-wiki html)
(define ((rr* find replace) contents) (regexp-replace* #rx"(<(?:td|figcaption)[^>]*?>\n?)(?:<li>|[ \t]*?<p class=\"caption\">(.*?)</p>)"
(regexp-replace* find contents replace)) html (λ (whole first-tag [contents #f])
((compose1 (if (eq? (string-ref whole 1) #\f) ;; figcaption
; fix navbox list nesting (string-append first-tag "<span class=\"caption\">" contents "</span>")
; navbox on right of page has incorrect html "<td ...><li>" and the xexpr parser puts the <li> much further up the tree (string-append first-tag "<ul><li>")))))
; add a <ul> to make the parser happy
; usage: /fallout/wiki/Fallout:_New_Vegas_achievements_and_trophies
(rr* #rx"(<td[^>]*>\n?)(<li>)" "\\1<ul>\\2")
; change <figcaption><p> to <figcaption><span> to make the parser happy
(rr* #rx"(<figcaption[^>]*>)[ \t]*<p class=\"caption\">([^<]*)</p>" "\\1<span class=\"caption\">\\2</span>"))
html))
(module+ test (module+ test
(check-equal? (preprocess-html-wiki "<td class=\"va-navbox-column\" style=\"width: 33%\">\n<li>Hey</li>") (check-equal? (preprocess-html-wiki "<td class=\"va-navbox-column\" style=\"width: 33%\">\n<li>Hey</li>")
"<td class=\"va-navbox-column\" style=\"width: 33%\">\n<ul><li>Hey</li>") "<td class=\"va-navbox-column\" style=\"width: 33%\">\n<ul><li>Hey</li>")
@ -66,6 +61,11 @@
(iframe (@ (src "https://example.com/iframe-src"))))))) (iframe (@ (src "https://example.com/iframe-src")))))))
(define (updater wikiname #:strict-proxy? [strict-proxy? #f]) (define (updater wikiname #:strict-proxy? [strict-proxy? #f])
;; precompute wikiurl regex for efficency
(define wikiurl-regex (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)$" px-wikiname)))
;; precompute link replacement string for efficiency
(define wiki-substitution (format "/~a\\1" wikiname))
(define classlist-updater (define classlist-updater
(compose1 (compose1
; uncollapse all navbox items (bottom of page mass navigation) ; uncollapse all navbox items (bottom of page mass navigation)
@ -110,8 +110,8 @@
(curry attribute-maybe-update 'href (curry attribute-maybe-update 'href
(λ (href) (λ (href)
((compose1 ((compose1
(λ (href) (regexp-replace #rx"^(/wiki/.*)" href (format "/~a\\1" wikiname))) (λ (href) (regexp-replace #rx"^(/wiki/.*)$" href wiki-substitution))
(λ (href) (regexp-replace (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)" px-wikiname)) href "/\\1\\2"))) (λ (href) (regexp-replace wikiurl-regex href "/\\1\\2")))
href))) href)))
; add noreferrer to a.image ; add noreferrer to a.image
(curry u (curry u

View file

@ -190,7 +190,9 @@
'(body "Hey" (& nbsp) (a (@ (href "/")))))) '(body "Hey" (& nbsp) (a (@ (href "/"))))))
(define (has-class? name attributes) (define (has-class? name attributes)
(and (member name (string-split (or (get-attribute 'class attributes) "") " ")) #t)) ;; splitting without specifying separator or splitting on #px"\\s+" makes
;; string-split use a faster whitespace-specialized implementation.
(and (member name (string-split (or (get-attribute 'class attributes) "") #px"\\s+")) #t))
(module+ test (module+ test
(check-true (has-class? "red" '((class "yellow red blue")))) (check-true (has-class? "red" '((class "yellow red blue"))))
(check-false (has-class? "red" '((class "yellow blue")))) (check-false (has-class? "red" '((class "yellow blue"))))

View file

@ -110,8 +110,8 @@
(div (@ (class "niwa__left")) (div (@ (class "niwa__left"))
(a (@ (class "niwa__go") (href ,go)) "Read " ,title " on " ,display-name "") (a (@ (class "niwa__go") (href ,go)) "Read " ,title " on " ,display-name "")
,@body ,@body
(p "This wiki's core community has wholly migrated away from Fandom. You should " (p "This external wiki is a helpful alternative to Fandom. You should "
(a (@ (href ,go)) "go to " ,display-name " now!"))) (a (@ (href ,go)) "check it out now!")))
,(if logo ,(if logo
`(div (@ (class "niwa__right")) `(div (@ (class "niwa__right"))
(img (@ (class "niwa__logo") (src ,logo)))) (img (@ (class "niwa__logo") (src ,logo))))