Restrict our scrubl structure to p, strong, emph for now
[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 Craftily/Crappily Rendering Underlying Basic Language
20 ;;; a micro-"skribe-like" system (kinda ugly tho)
21 ;;; Turns quasiquoted structures into something rendered.
22 ;;;
23 ;;; This is an immutable interface but it does use mutation under the
24 ;;; hood for expediency.
25 ;;; To make a new scrubl that extends an existing scrubl, use the exported
26 ;;; scrubl-extend-fields.
27
28 (define-module (mudsync scrubl)
29   #:use-module (ice-9 match)
30   #:use-module (srfi srfi-9)
31   #:use-module (sxml simple)
32   #:use-module (oop goops)
33   #:export (make-scrubl
34             scrubl? scrubl-extend-fields
35             scrubl-sxml scrubl-sxml-simple-field))
36
37 (define (order-symlist-args symlist-args)
38   "Orders the args in a symlist so keyword pairs are at the end"
39   (define new-args
40     (let lp ((remaining symlist-args)
41              (args '())
42              (kwargs '()))
43       (match remaining
44         ('() (cons (reverse args)
45                    kwargs))
46         (((? keyword? kw) val rest ...)
47          (lp rest
48              args
49              (cons* kw val kwargs)))
50         ((arg rest ...)
51          (lp rest
52              (cons arg args)
53              kwargs)))))
54   new-args)
55
56 (define-record-type <scrubl>
57   (%make-scrubl field-writers meta-write)
58   scrubl?
59   (field-writers scrubl-field-writers)
60   (meta-write scrubl-meta-write))
61
62 (define (make-scrubl field-writers meta-write)
63   (%make-scrubl (alist->hashq-table field-writers)
64                 meta-write))
65
66 (define (scrubl-extend-fields scrubl new-field-writers)
67   "Returns a new <scrubl> instance extending SCRUBL's field-writers with
68 NEW-FIELD-WRITERS."
69   (define new-writers
70     (let ((new-table (make-hash-table)))
71       ;; Add old fields from hashq
72       (hash-for-each
73        (lambda (key val)
74          (hashq-set! new-table key val))
75        (scrubl-field-writers scrubl))
76       ;; Now add the new fields
77       (for-each
78        (match-lambda
79          ((key . val)
80           (hashq-set! new-table key val)))
81        new-field-writers)
82       new-table))
83
84   (%make-scrubl new-writers (scrubl-meta-write scrubl)))
85
86 (define (scrubl-write scrubl obj . args)
87   "Write out OBJ via SCRUBL
88
89 Pass in optional extra ARGS to the main META-WRITE" 
90  (apply (scrubl-meta-write scrubl) scrubl obj args))
91
92 (define* (scrubl-write-obj scrubl obj)
93   (match obj
94     (((? symbol? sym) args ...)
95      (let* ((field-writers (scrubl-field-writers scrubl))
96             (field-writer (hashq-ref field-writers sym))
97             (ordered-args (order-symlist-args args)))
98        (when (not field-writer)
99          (throw 'scrubl-unknown-field
100                 #:field sym
101                 #:args args))
102        (apply field-writer scrubl ordered-args)))
103     ((items ...)
104      (map (lambda (item)
105             (scrubl-write-obj scrubl item))
106           items))
107     (any-obj any-obj)))
108
109
110 \f
111 ;;; SXML scrubl writer
112
113 (define (scrubl-sxml-write scrubl obj)
114   (call-with-output-string
115     (lambda (p)
116       (sxml->xml (scrubl-write-obj scrubl obj) p))))
117
118
119 (define (scrubl-sxml-simple-field sym)
120   (lambda (scrubl args)
121     ;; sxml handles inlining automatically in case we have nested
122     ;; lists of strings, so we don't have to worry about that...
123     (cons sym (map (lambda (arg)
124                      (scrubl-write-obj scrubl arg))
125                    args))))
126
127 (define scrubl-sxml
128   (make-scrubl `((p . ,(scrubl-sxml-simple-field 'p))
129                  (strong . ,(scrubl-sxml-simple-field 'strong)) ; usually bold
130                  (emph . ,(scrubl-sxml-simple-field 'em))) ; usually italicized
131                scrubl-sxml-write))