From b6811d23266cf804ea58919ab85e6aec4be7b7e7 Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sat, 28 Dec 2019 21:35:20 -0500 Subject: Refactor SXML generation with a reader macro. --- haunt/jakob/utils/pagination.scm | 17 ++++++++--------- haunt/jakob/utils/sxml.scm | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 16 deletions(-) (limited to 'haunt/jakob/utils') diff --git a/haunt/jakob/utils/pagination.scm b/haunt/jakob/utils/pagination.scm index 2cda3c7..bd44665 100644 --- a/haunt/jakob/utils/pagination.scm +++ b/haunt/jakob/utils/pagination.scm @@ -62,15 +62,14 @@ lists of the form (index, items)." (define (render-listing content title previous-page next-page) "Return an SHTML document showing CONTENT, with the header TITLE and links to PREVIOUS-PAGE and NEXT-PAGE." - `((h1 ,title) - ,@content - (nav - (@ (id "pagination")) - ,@(maybe-list - (when previous-page - (hyperlink previous-page "← Previous Page")) - (when next-page - (hyperlink next-page "Next Page →")))))) + #<((h1 ,title) + ,@content + (nav + (@ (id "pagination")) + ,(when previous-page + (hyperlink previous-page "← Previous Page")) + ,(when next-page + (hyperlink next-page "Next Page →"))))) (define* (items->pages render-item items base-title base-file-name #:key (items-per-page %items-per-page)) diff --git a/haunt/jakob/utils/sxml.scm b/haunt/jakob/utils/sxml.scm index 0bde0fe..5753f2d 100644 --- a/haunt/jakob/utils/sxml.scm +++ b/haunt/jakob/utils/sxml.scm @@ -14,17 +14,20 @@ ;;; along with this program. If not, see ;;; . -;;; Commentary: -;;; -;;; A number of utility procedure to aid in writing SXML by hand. -;;; -;;; Code: - (define-module (jakob utils sxml) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) #:export (hyperlink image stylesheet - script)) + script + + sanitize-subtree)) + + +;;; +;;; Utility procedures to aid in writing SXML by hand. +;;; (define (hyperlink target text) `(a (@ (href ,target)) ,text)) @@ -41,3 +44,26 @@ (define (script file-name) (let ((src (string-append "/static/js/" file-name))) `(script (@ (src ,src))))) + + +;;; +;;; A reader extension for implicitly-sanitized SXML trees. +;;; + +(define (sanitize-subtree subtree) + "Remove `nil', `#f', and any unspecified elements from `sbtree'" + (if (list? subtree) + (map sanitize-subtree (remove (lambda (elt) + (or (unspecified? elt) + (eq? 'nil elt) + (eq? #f elt))) + subtree)) + subtree)) + +(define (sxml-reader chr port) + "Read an SXML literal expression possibly containing unquote forms and +sanitize the resultant subtree." + `(sanitize-subtree ,(cons 'quasiquote (list (read port))))) + +;; Install the reader extension when imported. +(read-hash-extend #\< sxml-reader) -- cgit v1.3