From 3f1946a3b8b061b7f8a03e1681471212ff79ac71 Mon Sep 17 00:00:00 2001 From: Artemis Everfree Date: Wed, 22 Mar 2023 21:44:40 -0700 Subject: [PATCH 1/4] use faster string split --- lib/xexpr-utils.rkt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/xexpr-utils.rkt b/lib/xexpr-utils.rkt index 018d8c3..cb40510 100644 --- a/lib/xexpr-utils.rkt +++ b/lib/xexpr-utils.rkt @@ -190,7 +190,9 @@ '(body "Hey" (& nbsp) (a (@ (href "/")))))) (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 (check-true (has-class? "red" '((class "yellow red blue")))) (check-false (has-class? "red" '((class "yellow blue")))) From f5529ed12a3b6de478e30fdfbfe74ed787f65234 Mon Sep 17 00:00:00 2001 From: Artemis Everfree Date: Wed, 22 Mar 2023 21:45:09 -0700 Subject: [PATCH 2/4] precompile regexp patterns --- lib/tree-updater.rkt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/tree-updater.rkt b/lib/tree-updater.rkt index 074c5a1..6c369b5 100644 --- a/lib/tree-updater.rkt +++ b/lib/tree-updater.rkt @@ -66,6 +66,11 @@ (iframe (@ (src "https://example.com/iframe-src"))))))) (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 (compose1 ; uncollapse all navbox items (bottom of page mass navigation) @@ -110,8 +115,8 @@ (curry attribute-maybe-update 'href (λ (href) ((compose1 - (λ (href) (regexp-replace #rx"^(/wiki/.*)" href (format "/~a\\1" wikiname))) - (λ (href) (regexp-replace (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)" px-wikiname)) href "/\\1\\2"))) + (λ (href) (regexp-replace #rx"^(/wiki/.*)$" href wiki-substitution)) + (λ (href) (regexp-replace wikiurl-regex href "/\\1\\2"))) href))) ; add noreferrer to a.image (curry u From ba6c5be9905529019c011b0d2de45eb451fb45ba Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 2 Apr 2023 00:04:35 +1300 Subject: [PATCH 3/4] Optimise pre-processing regular expression --- lib/tree-updater.rkt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/tree-updater.rkt b/lib/tree-updater.rkt index 6c369b5..0dbf695 100644 --- a/lib/tree-updater.rkt +++ b/lib/tree-updater.rkt @@ -12,17 +12,12 @@ update-tree-wiki) (define (preprocess-html-wiki html) - (define ((rr* find replace) contents) - (regexp-replace* find contents replace)) - ((compose1 - ; fix navbox list nesting - ; navbox on right of page has incorrect html "
  • " and the xexpr parser puts the
  • much further up the tree - ; add a
      to make the parser happy - ; usage: /fallout/wiki/Fallout:_New_Vegas_achievements_and_trophies - (rr* #rx"(]*>\n?)(
    • )" "\\1
        \\2") - ; change

        to

        to make the parser happy - (rr* #rx"(]*>)[ \t]*

        ([^<]*)

        " "\\1\\2")) - html)) + (regexp-replace* #rx"(<(?:td|figcaption)[^>]*?>\n?)(?:
      • |[ \t]*?

        (.*?)

        )" + html (λ (whole first-tag [contents #f]) + (if (eq? (string-ref whole 1) #\f) ;; figcaption + (string-append first-tag "" contents "") + (string-append first-tag "
        • "))))) + (module+ test (check-equal? (preprocess-html-wiki "\n
        • Hey
        • ") "\n
          • Hey
          • ") From d3187cc310a4f622c88dcb9df7f2859bd101d87a Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 2 Apr 2023 00:02:07 +1300 Subject: [PATCH 4/4] Tweak extwiki-generic migration notice --- src/extwiki-generic.rkt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extwiki-generic.rkt b/src/extwiki-generic.rkt index fe30b14..113b139 100644 --- a/src/extwiki-generic.rkt +++ b/src/extwiki-generic.rkt @@ -110,8 +110,8 @@ (div (@ (class "niwa__left")) (a (@ (class "niwa__go") (href ,go)) "Read " ,title " on " ,display-name " →") ,@body - (p "This wiki's core community has wholly migrated away from Fandom. You should " - (a (@ (href ,go)) "go to " ,display-name " now!"))) + (p "This external wiki is a helpful alternative to Fandom. You should " + (a (@ (href ,go)) "check it out now!"))) ,(if logo `(div (@ (class "niwa__right")) (img (@ (class "niwa__logo") (src ,logo))))