From 51bf087b30b8626038e4799726255510fbecbb33 Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 9 Oct 2022 10:49:10 +0700 Subject: [PATCH] raco fmt --- src/page-file.rkt | 150 ++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 84 deletions(-) diff --git a/src/page-file.rkt b/src/page-file.rkt index ef1c2f4..d04c135 100644 --- a/src/page-file.rkt +++ b/src/page-file.rkt @@ -20,8 +20,7 @@ "url-utils.rkt" "xexpr-utils.rkt") -(provide - page-file) +(provide page-file) ;(module+ test ; (require rackunit) @@ -36,21 +35,18 @@ (define (get-media-html url content-type) (cond [(eq? content-type #f) `""] - [(regexp-match? #rx"(?i:^image/)" content-type) - `(img (@ (src ,url)))] + [(regexp-match? #rx"(?i:^image/)" content-type) `(img (@ (src ,url)))] [(regexp-match? #rx"(?i:^audio/|^application/ogg(;|$))" content-type) - `(audio (@ (src ,url) (controls)))] - [(regexp-match? #rx"(?i:^video/)" content-type) - `(video (@ (src ,url) (controls)))] + `(audio (@ (src ,url) (controls)))] + [(regexp-match? #rx"(?i:^video/)" content-type) `(video (@ (src ,url) (controls)))] [else `""])) -(define (generate-results-page - #:source-url source-url - #:wikiname wikiname - #:title title - #:media-detail media-detail - #:image-content-type image-content-type - #:license [license #f]) +(define (generate-results-page #:source-url source-url + #:wikiname wikiname + #:title title + #:media-detail media-detail + #:image-content-type image-content-type + #:license [license #f]) (define video-embed-code (jp "/videoEmbedCode" media-detail "")) (define raw-image-url (jp "/rawImageUrl" media-detail)) (define image-url (jp "/imageUrl" media-detail raw-image-url)) @@ -59,9 +55,8 @@ (define smaller-article-list (jp "/smallerArticleList" media-detail)) (define article-list-is-smaller (jp "/articleListIsSmaller" media-detail)) (define image-description (jp "/imageDescription" media-detail #f)) - (define maybe-proxied-raw-image-url (if (config-true? 'strict_proxy) - (u-proxy-url raw-image-url) - raw-image-url)) + (define maybe-proxied-raw-image-url + (if (config-true? 'strict_proxy) (u-proxy-url raw-image-url) raw-image-url)) (generate-wiki-page #:source-url source-url #:wikiname wikiname @@ -69,35 +64,27 @@ #:license license `(div ,(if (non-empty-string? video-embed-code) - (update-tree-wiki (html->xexp (preprocess-html-wiki video-embed-code)) wikiname) - (get-media-html image-url image-content-type)) - (p - ,(if (non-empty-string? video-embed-code) - `"" - `(span (a (@ (href ,maybe-proxied-raw-image-url)) "View original file") ". ")) - "Added by " - (a (@ (href ,(format "/~a/wiki/User:~a" wikiname username))) ,username) - "." - ,(if is-posted-in - `(span " Posted in " - ,@(map - (λ (article) - (define page-path (jp "/title" article)) - (define title (jp "/titleText" article page-path)) - `(span - ,(if (eq? (car smaller-article-list) article) - "" - ", ") - (a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) - ,title))) - smaller-article-list) - ,(if (eq? article-list-is-smaller 1) - "…" - ".")) - `"")) + (update-tree-wiki (html->xexp (preprocess-html-wiki video-embed-code)) wikiname) + (get-media-html image-url image-content-type)) + (p ,(if (non-empty-string? video-embed-code) + `"" + `(span (a (@ (href ,maybe-proxied-raw-image-url)) "View original file") ". ")) + "Added by " + (a (@ (href ,(format "/~a/wiki/User:~a" wikiname username))) ,username) + "." + ,(if is-posted-in + `(span " Posted in " + ,@(map (λ (article) + (define page-path (jp "/title" article)) + (define title (jp "/titleText" article page-path)) + `(span ,(if (eq? (car smaller-article-list) article) "" ", ") + (a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) ,title))) + smaller-article-list) + ,(if (eq? article-list-is-smaller 1) "…" ".")) + `"")) ,(if (string? image-description) - (update-tree-wiki (html->xexp (preprocess-html-wiki image-description)) wikiname) - "")))) + (update-tree-wiki (html->xexp (preprocess-html-wiki image-description)) wikiname) + "")))) (define (page-file req) (define wikiname (path/param-path (first (url-path (request-uri req))))) @@ -105,45 +92,40 @@ (define origin (format "https://~a.fandom.com" wikiname)) (define source-url (format "~a/wiki/~a" origin prefixed-title)) - (thread-let - ([media-detail (define dest-url - (format "~a/wikia.php?~a" - origin - (params->query `(("format" . "json") - ("controller" . "Lightbox") - ("method" . "getMediaDetail") - ("fileTitle" . ,prefixed-title))))) - (log-outgoing dest-url) - (define dest-res (easy:get dest-url #:timeouts timeouts)) - (easy:response-json dest-res)] - [license (license-auto wikiname)]) - - (if (not (jp "/exists" media-detail #f)) - (next-dispatcher) - (response-handler - (define file-title (jp "/fileTitle" media-detail "")) - (define title (if (non-empty-string? file-title) - (format "File:~a" file-title) - prefixed-title)) - (define image-content-type (if (non-empty-string? (jp "/videoEmbedCode" media-detail "")) - #f - (url-content-type (jp "/imageUrl" media-detail)))) - (define body (generate-results-page - #:source-url source-url - #:wikiname wikiname - #:title title - #:media-detail media-detail - #:image-content-type image-content-type - #:license license)) - - (when (config-true? 'debug) - ; used for its side effects - ; convert to string with error checking, error will be raised if xexp is invalid - (xexp->html body)) - (response/output - #:code 200 - (λ (out) - (write-html body out))))))) + (thread-let ([media-detail + (define dest-url + (format "~a/wikia.php?~a" + origin + (params->query `(("format" . "json") ("controller" . "Lightbox") + ("method" . "getMediaDetail") + ("fileTitle" . ,prefixed-title))))) + (log-outgoing dest-url) + (define dest-res (easy:get dest-url #:timeouts timeouts)) + (easy:response-json dest-res)] + [license (license-auto wikiname)]) + (if (not (jp "/exists" media-detail #f)) + (next-dispatcher) + (response-handler + (define file-title (jp "/fileTitle" media-detail "")) + (define title + (if (non-empty-string? file-title) (format "File:~a" file-title) prefixed-title)) + (define image-content-type + (if (non-empty-string? (jp "/videoEmbedCode" media-detail "")) + #f + (url-content-type (jp "/imageUrl" media-detail)))) + (define body + (generate-results-page #:source-url source-url + #:wikiname wikiname + #:title title + #:media-detail media-detail + #:image-content-type image-content-type + #:license license)) + (when (config-true? 'debug) + ; used for its side effects + ; convert to string with error checking, error will be raised if xexp is invalid + (xexp->html body)) + (response/output #:code 200 + (λ (out) (write-html body out))))))) ;(module+ test ; (check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Ankle_Monitor") ; (generate-results-page