From 7446262a2a17bc86e5c6f1d5d899665264d188b0 Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 2 Oct 2022 16:44:44 +0700 Subject: [PATCH] Deduplicate the wiki name regex --- src/dispatcher-tree.rkt | 11 ++++++----- src/page-wiki.rkt | 2 +- src/url-utils.rkt | 6 +++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/dispatcher-tree.rkt b/src/dispatcher-tree.rkt index 93532ef..1a43458 100644 --- a/src/dispatcher-tree.rkt +++ b/src/dispatcher-tree.rkt @@ -8,7 +8,8 @@ (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer) (prefix-in lift: web-server/dispatchers/dispatch-lift) (prefix-in filter: web-server/dispatchers/dispatch-filter) - "config.rkt") + "config.rkt" + "url-utils.rkt") (provide ; syntax to make the hashmap from names @@ -43,9 +44,9 @@ (pathprocedure:make "/" (hash-ref ds 'page-home)) (pathprocedure:make "/proxy" (hash-ref ds 'page-proxy)) (pathprocedure:make "/search" (hash-ref ds 'page-global-search)) - (filter:make #px"^/[a-zA-Z0-9-]{3,50}/wiki/Category:.+$" (lift:make (hash-ref ds 'page-category))) - (filter:make #px"^/[a-zA-Z0-9-]{3,50}/wiki/.+$" (lift:make (hash-ref ds 'page-wiki))) - (filter:make #px"^/[a-zA-Z0-9-]{3,50}/search$" (lift:make (hash-ref ds 'page-search))) - (filter:make #px"^/[a-zA-Z0-9-]{3,50}(/(wiki(/)?)?)?$" (lift:make (hash-ref ds 'redirect-wiki-home))) + (filter:make (pregexp (format "^/~a/wiki/Category:.+$" wikiname-regex)) (lift:make (hash-ref ds 'page-category))) + (filter:make (pregexp (format "^/~a/wiki/.+$" wikiname-regex)) (lift:make (hash-ref ds 'page-wiki))) + (filter:make (pregexp (format "^/~a/search$" wikiname-regex)) (lift:make (hash-ref ds 'page-search))) + (filter:make (pregexp (format "^/~a(/(wiki(/)?)?)?$" wikiname-regex)) (lift:make (hash-ref ds 'redirect-wiki-home))) (hash-ref ds 'static-dispatcher) (lift:make (hash-ref ds 'page-not-found))))))) diff --git a/src/page-wiki.rkt b/src/page-wiki.rkt index 96410d0..795f34c 100644 --- a/src/page-wiki.rkt +++ b/src/page-wiki.rkt @@ -143,7 +143,7 @@ (λ (href) ((compose1 (λ (href) (regexp-replace #rx"^(/wiki/.*)" href (format "/~a\\1" wikiname))) - (λ (href) (regexp-replace #px"^https://([a-zA-Z0-9-]{3,50}).fandom.com(/wiki/.*)" href "/\\1\\2"))) + (λ (href) (regexp-replace (pregexp (format "^https://(~a)\\.fandom\\.com(/wiki/.*)" wikiname-regex)) href "/\\1\\2"))) href))) ; add noreferrer to a.image (curry u diff --git a/src/url-utils.rkt b/src/url-utils.rkt index 934a228..cefee19 100644 --- a/src/url-utils.rkt +++ b/src/url-utils.rkt @@ -3,6 +3,8 @@ "pure-utils.rkt") (provide + ; regex to match wiki names + wikiname-regex ; make a query string from an association list of strings params->query ; make a proxied version of a fandom url @@ -13,6 +15,8 @@ (module+ test (require "typed-rackunit.rkt")) +(define wikiname-regex "[a-zA-Z0-9-]{3,50}") + ;; https://url.spec.whatwg.org/#urlencoded-serializing (define urlencoded-set '(#\! #\' #\( #\) #\~ ; urlencoded set @@ -57,7 +61,7 @@ (: is-fandom-url? (String -> Boolean)) (define (is-fandom-url? url) - (regexp-match? #px"^https://static.wikia.nocookie.net/|^https://[a-zA-Z0-9-]{3,50}.fandom.com/" url)) + (regexp-match? (pregexp (format "^https://static\\.wikia\\.nocookie\\.net/|^https://~a\\.fandom\\.com/" wikiname-regex)) url)) (module+ test (check-true (is-fandom-url? "https://static.wikia.nocookie.net/wikiname/images/2/2f/SomeImage.jpg/revision/latest?cb=20110210094136")) (check-true (is-fandom-url? "https://test.fandom.com/wiki/Some_Page"))