From a5079b5a5777fd0226540dcf70a149937f1acd34 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 30 Aug 2022 21:58:59 +1200 Subject: [PATCH] Refactor `let` to `define` where applicable Racke style guide: https://docs.racket-lang.org/style/Choosing_the_Right_Construct.html#%28part._.Definitions%29 --- src/config.rkt | 42 +++++++++++++------------- src/page-category.rkt | 10 +++---- src/page-wiki.rkt | 19 ++++++++---- src/reloadable.rkt | 20 ++++++------- src/xexpr-utils.rkt | 68 ++++++++++++++++++++----------------------- 5 files changed, 82 insertions(+), 77 deletions(-) diff --git a/src/config.rkt b/src/config.rkt index d2bcf7a..b4d93ec 100644 --- a/src/config.rkt +++ b/src/config.rkt @@ -24,26 +24,28 @@ (make-hasheq (append default-config - (with-handlers ([exn:fail:filesystem:errno? - (λ (exn) - (begin0 - '() - (displayln "note: config file not detected, using defaults")))] - [exn:fail:contract? - (λ (exn) - (begin0 - '() - (displayln "note: config file empty or missing [] section, using defaults")))]) - (let ([l (hash->list - (hash-ref - (ini->hash - (call-with-input-file path-config - (λ (in) - (read-ini in)))) - '||))]) - (begin0 - l - (printf "note: ~a items loaded from config file~n" (length l)))))))) + (with-handlers + ([exn:fail:filesystem:errno? + (λ (exn) + (begin0 + '() + (displayln "note: config file not detected, using defaults")))] + [exn:fail:contract? + (λ (exn) + (begin0 + '() + (displayln "note: config file empty or missing [] section, using defaults")))]) + (define l + (hash->list + (hash-ref + (ini->hash + (call-with-input-file path-config + (λ (in) + (read-ini in)))) + '||))) + (begin0 + l + (printf "note: ~a items loaded from config file~n" (length l))))))) (when (config-true? 'debug) (printf "config: ~v~n" config)) diff --git a/src/page-category.rkt b/src/page-category.rkt index 7760c97..83f0f03 100644 --- a/src/page-category.rkt +++ b/src/page-category.rkt @@ -33,11 +33,11 @@ (ul (@ (class "my-category-list")) ,@(map (λ (result) - (let* ([title (jp "/title" result)] - [page-path (regexp-replace* #rx" " title "_")]) - `(li - (a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) - ,title)))) + (define title (jp "/title" result)) + (define page-path (regexp-replace* #rx" " title "_")) + `(li + (a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) + ,title))) members))))) (define (page-category req) diff --git a/src/page-wiki.rkt b/src/page-wiki.rkt index f070521..ff86b20 100644 --- a/src/page-wiki.rkt +++ b/src/page-wiki.rkt @@ -42,7 +42,8 @@ (div (@ (class "collapsible-content")) (p "Another page link: " (a (@ (data-test-wikilink) (href "https://test.fandom.com/wiki/Another_Page") (title "Another Page")) - "Another Page")))))))) + "Another Page")))) + (iframe (@ (src "https://example.com/iframe-src"))))))) (define (preprocess-html-wiki html) (define (rr* find replace contents) @@ -95,10 +96,10 @@ return-no-element] ; display a link instead of an iframe [(eq? element-type 'iframe) - (let ([src (car (dict-ref attributes 'src null))]) - `(a - ((class "iframe-alternative") (href ,src)) - (,(format "Embedded media: ~a" src))))] + (define src (car (dict-ref attributes 'src null))) + `(a + ((class "iframe-alternative") (href ,src)) + (,(format "Embedded media: ~a" src)))] [#t (list element-type ;; attributes @@ -193,7 +194,13 @@ ((query-selector (λ (t a c) (dict-has-key? a 'data-test-collapsesection)) transformed)))) - "collapsible collapsetoggle-inline")) + "collapsible collapsetoggle-inline") + ; check that iframes are gone + (check-false ((query-selector (λ (t a c) (eq? t 'iframe)) transformed))) + (check-equal? (let* ([alternative ((query-selector (λ (t a c) (has-class? "iframe-alternative" a)) transformed))] + [link ((query-selector (λ (t a c) (eq? t 'a)) alternative))]) + (get-attribute 'href (bits->attributes link))) + "https://example.com/iframe-src")) (define (page-wiki req) (define wikiname (path/param-path (first (url-path (request-uri req))))) diff --git a/src/reloadable.rkt b/src/reloadable.rkt index 61c93c5..1939a8b 100644 --- a/src/reloadable.rkt +++ b/src/reloadable.rkt @@ -70,16 +70,16 @@ (define real-error-port (thread-receive)) (say-loading-once! real-error-port) (let loop () - (let ([line (read-line i)]) - (cond - [(eof-object? line) - (void)] - [(string-contains? line "[load") - (display "." real-error-port) - (loop)] - [#t - (displayln line real-error-port) - (loop)])))) + (define line (read-line i)) + (cond + [(eof-object? line) + (void)] + [(string-contains? line "[load") + (display "." real-error-port) + (loop)] + [#t + (displayln line real-error-port) + (loop)]))) ;; Only to be called from reloader-main (define (do-reload!) diff --git a/src/xexpr-utils.rkt b/src/xexpr-utils.rkt index b2b4422..1ba4c16 100644 --- a/src/xexpr-utils.rkt +++ b/src/xexpr-utils.rkt @@ -100,20 +100,22 @@ (check-false (element-is-content? '(@ (alt "Cute cat.")))) (check-true (element-is-content? "hi"))) +; get the actual attributes, leaving out the @ signs (define (xattributes->attributes xattrs) (filter pair? xattrs)) (define (bits->attributes bits) + ; (append) is a clean and general approach to finding and combining any attributes (xattributes->attributes (apply append (filter element-is-xattributes? bits)))) (module+ test (check-equal? (bits->attributes demo-attributes) '((title "Inside joke.") (style "color: blue")))) (define (get-attribute name attributes) - (let ([a (assq name attributes)]) - (if (pair? a) - (cadr a) - #f))) + (define a (assq name attributes)) + (if (pair? a) + (cadr a) + #f)) (module+ test (check-equal? (get-attribute 'title (bits->attributes demo-attributes)) "Inside joke.")) @@ -131,20 +133,17 @@ (generator () (let loop ([element element]) - (let* ([element-type (car element)] - ; (append) is a clean and general approach to finding and combining any attributes - ; (filter pair?) is to get the actual attributes and leaves out the @ signs - [attributes (bits->attributes (cdr element))] - ; only recurse through real children - [children (filter element-is-element? (cdr element))]) - (cond - [(equal? element-type '*DECL*) #f] - [(equal? element-type '@) #f] - [#t - (when (selector element-type attributes children) - (yield element)) - (for ([child children]) (loop child))]))) - #f)) + (define element-type (car element)) + (define attributes (bits->attributes (cdr element))) + (define children (filter element-is-element? (cdr element))) ; only recurse through real children + (cond + [(equal? element-type '*DECL*) #f] + [(equal? element-type '@) #f] + [#t + (when (selector element-type attributes children) + (yield element)) + (for ([child children]) (loop child))])) + #f)) (module+ test (let ([result (query-selector (attribute-selector 'title "Really.") demo-document)]) @@ -155,24 +154,21 @@ (define (update-tree transformer element) (let loop ([element element]) - (let* ([element-type (car element)] - ; (append) is a clean and general approach to finding and combining any attributes - ; (filter pair?) is to get the actual attributes and leaves out the @ signs - [attributes (bits->attributes (cdr element))] - ; provide elements and strings - [contents (filter element-is-content? (cdr element))]) - (if (or (equal? element-type '*DECL) - (equal? element-type '@)) - ; special element, do nothing - element - ; regular element, transform it - (match (transformer element element-type attributes contents) - [(list element-type attributes contents) - (append (list element-type) - (if (pair? attributes) (list (append '(@) attributes)) (list)) - (map (λ (content) - (if (element-is-element? content) (loop content) content)) - contents))]))))) + (define element-type (car element)) + (define attributes (bits->attributes (cdr element))) + (define contents (filter element-is-content? (cdr element))) ; provide elements and strings + (if (or (equal? element-type '*DECL) + (equal? element-type '@)) + ; special element, do nothing + element + ; regular element, transform it + (match (transformer element element-type attributes contents) + [(list element-type attributes contents) + (append (list element-type) + (if (pair? attributes) (list (append '(@) attributes)) (list)) + (map (λ (content) + (if (element-is-element? content) (loop content) content)) + contents))])))) (define (has-class? name attributes) (and (member name (string-split (or (get-attribute 'class attributes) "") " ")) #t))