optimizations #1

Open
artemis wants to merge 2 commits from optimizations into main
2 changed files with 15 additions and 7 deletions

View file

@ -100,6 +100,9 @@
(define (cardimage-class-updater c) (define (cardimage-class-updater c)
(string-append c " bw-updated-cardtable-cardimage")) (string-append c " bw-updated-cardtable-cardimage"))
; precompute wikiurl regex for efficency
(define wikiurl-regex (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)$" px-wikiname)))
(define attributes-updater (define attributes-updater
(compose1 (compose1
; uncollapsing ; uncollapsing
@ -107,12 +110,15 @@
(λ (class) (string-join (classlist-updater (string-split class " ")) " "))) (λ (class) (string-join (classlist-updater (string-split class " ")) " ")))
(curry attribute-maybe-update 'class class-updater) (curry attribute-maybe-update 'class class-updater)
; change links to stay on the same wiki ; change links to stay on the same wiki
(curry attribute-maybe-update 'href (let
(λ (href) ; precompute wikiname replacement pattern
((compose1 ([wiki-substitution (format "/~a\\1" wikiname)])
(λ (href) (regexp-replace #rx"^(/wiki/.*)" href (format "/~a\\1" wikiname))) (curry attribute-maybe-update 'href
(λ (href) (regexp-replace (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)" px-wikiname)) href "/\\1\\2"))) (λ (href)
href))) ((compose1
(λ (href) (regexp-replace #rx"^(/wiki/.*)$" href wiki-substitution))
(λ (href) (regexp-replace wikiurl-regex href "/\\1\\2")))
href))))
; add noreferrer to a.image ; add noreferrer to a.image
(curry u (curry u
(λ (v) (and #;(eq? element-type 'a) (λ (v) (and #;(eq? element-type 'a)

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"))))