1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2017 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
6 ;;; Mudsync is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
11 ;;; Mudsync is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;; General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
19 ;;; SCRUBL: S-exps Can Really Undermine Basic Language
20 ;;; a micro-"skribe-like" system
22 (define-module (mudsync scrubl)
23 #:use-module (ice-9 match)
24 #:use-module (srfi srfi-9)
25 #:use-module (srfi srfi-9 gnu)
26 #:use-module (sxml simple)
27 #:use-module (oop goops))
29 (define (order-symlist-args symlist-args)
30 "Orders the args in a symlist so keyword pairs are at the end"
32 (let lp ((remaining symlist-args)
36 ('() (cons (reverse args)
38 (((? keyword? kw) val rest ...)
41 (cons* kw val kwargs)))
48 (define-immutable-record-type <scrubl>
49 (make-scrubl field-writers meta-write)
51 (field-writers scrubl-field-writers)
52 (meta-write scrubl-meta-write))
54 (define (scrubl-extend-fields scrubl new-field-writers)
55 "Returns a new <scrubl> instance extending SCRUBL's field-writers with
57 (set-field scrubl (scrubl-field-writers)
58 (append new-field-writers (scrubl-field-writers scrubl))))
60 (define (scrubl-write scrubl obj . args)
61 "Write out OBJ via SCRUBL
63 Pass in optional extra ARGS to the main META-WRITE"
64 (apply (scrubl-meta-write scrubl) scrubl obj args))
66 (define* (scrubl-write-obj scrubl obj)
68 (((? symbol? sym) args ...)
69 (let* ((field-writers (scrubl-field-writers scrubl))
70 (field-writer (assoc-ref field-writers sym))
71 (ordered-args (order-symlist-args args)))
72 (when (not field-writer)
73 (throw 'scrubl-unknown-field
76 (apply field-writer scrubl ordered-args)))
79 (scrubl-write-obj scrubl item))
85 ;;; SXML scrubl writer
87 (define (sxml-write scrubl obj)
88 (call-with-output-string
90 (sxml->xml (scrubl-write-obj scrubl obj) p))))
93 (define (sxml-simple-field sym)
95 ;; sxml handles inlining automatically in case we have nested
96 ;; lists of strings, so we don't have to worry about that...
97 (cons sym (map (lambda (arg)
98 (scrubl-write-obj scrubl arg))
102 (make-scrubl `((p . ,(sxml-simple-field 'p))
103 (bold . ,(sxml-simple-field 'b))
104 (it . ,(sxml-simple-field 'it))
105 (emph . ,(sxml-simple-field 'it)))