cf585b949f78d146b1de87a45025caad0ec120de
[mudsync.git] / mudsync / scrubl.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2017 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19 ;;; SCRUBL: S-exps Can Really Undermine Basic Language
20 ;;; a micro-"skribe-like" system
21
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))
28
29 (define (order-symlist-args symlist-args)
30   "Orders the args in a symlist so keyword pairs are at the end"
31   (define new-args
32     (let lp ((remaining symlist-args)
33              (args '())
34              (kwargs '()))
35       (match remaining
36         ('() (cons (reverse args)
37                    kwargs))
38         (((? keyword? kw) val rest ...)
39          (lp rest
40              args
41              (cons* kw val kwargs)))
42         ((arg rest ...)
43          (lp rest
44              (cons arg args)
45              kwargs)))))
46   new-args)
47
48 (define-immutable-record-type <scrubl>
49   (make-scrubl field-writers meta-write)
50   scrubl?
51   (field-writers scrubl-field-writers)
52   (meta-write scrubl-meta-write))
53
54 (define (scrubl-extend-fields scrubl new-field-writers)
55   "Returns a new <scrubl> instance extending SCRUBL's field-writers with
56 NEW-FIELD-WRITERS."
57   (set-field scrubl (scrubl-field-writers)
58              (append new-field-writers (scrubl-field-writers scrubl))))
59
60 (define (scrubl-write scrubl obj . args)
61   "Write out OBJ via SCRUBL
62
63 Pass in optional extra ARGS to the main META-WRITE" 
64  (apply (scrubl-meta-write scrubl) scrubl obj args))
65
66 (define* (scrubl-write-obj scrubl obj)
67   (match 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
74                 #:field sym
75                 #:args args))
76        (apply field-writer scrubl ordered-args)))
77     ((items ...)
78      (map (lambda (item)
79             (scrubl-write-obj scrubl item))
80           items))
81     (any-obj any-obj)))
82
83
84 \f
85 ;;; SXML scrubl writer
86
87 (define (sxml-write scrubl obj)
88   (call-with-output-string
89     (lambda (p)
90       (sxml->xml (scrubl-write-obj scrubl obj) p))))
91
92
93 (define (sxml-simple-field sym)
94   (lambda (scrubl args)
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))
99                    args))))
100
101 (define sxml-scrubl
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)))
106                sxml-write))