From 8012b512079cd88b619da46205732fd86fae9726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 14 Aug 2020 00:25:47 +0200 Subject: Add module for sxml transformations. --- module/sxml/transformations.scm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 module/sxml/transformations.scm diff --git a/module/sxml/transformations.scm b/module/sxml/transformations.scm new file mode 100644 index 00000000..e57c0433 --- /dev/null +++ b/module/sxml/transformations.scm @@ -0,0 +1,36 @@ +;;; Commentary: +;;; Module for transforming an already generated sxml tree. +;;; Ideally we would just generate the correct tree directly. But in some +;; circumstances that would lead to an absurd ammount of options and the +;; like, so these come in handy. +;;; Code: + +(define-module (sxml transformations) + :use-module (util) + :use-module ((srfi srfi-1) :select (concatenate)) + :use-module ((sxml transform) :select (pre-post-order)) + ) + +;; sxml, bindings → sxml +(define-public (attribute-transformer + tree attribute-bindings) + + (define bindings + `((@ ,attribute-bindings + . ,(lambda (_ . b) `(@ ,@b))) + (*default* . ,(lambda (t . b) `(,t ,@b))) + (*text* . ,(lambda (_ . b) (concatenate b))))) + + (pre-post-order tree bindings)) + + +(define-public (href-transformer tree transformer) + (attribute-transformer + tree + `((href . ,(lambda (_ . content) + `(href ,@(transformer (string-concatenate content))) + ))))) + +(define-public (href-prefixer tree prefix) + (href-transformer + tree (lambda (str) (string-append prefix str)))) -- cgit v1.2.3