917b3a935a31b57751547aefb5055fa9ecccc616
[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   #:use-module (ice-9 vlist)
34   #:use-module (ice-9 vlist)
35   #:use-module (ice-9 hash-table)
36   #:use-module (web uri)
37   #:export (make-scrubl
38             scrubl? scrubl-extend-fields
39             scrubl-write
40             scrubl-sxml scrubl-sxml-simple-field))
41
42 (define (order-symlist-args symlist-args)
43   "Orders the args in a symlist so keyword pairs are at the end"
44   (define new-args
45     (let lp ((remaining symlist-args)
46              (args '())
47              (kwargs '()))
48       (match remaining
49         ('() (cons (reverse args)
50                    kwargs))
51         (((? keyword? kw) val rest ...)
52          (lp rest
53              args
54              (cons* kw val kwargs)))
55         ((arg rest ...)
56          (lp rest
57              (cons arg args)
58              kwargs)))))
59   new-args)
60
61 (define-record-type <scrubl>
62   (%make-scrubl field-writers meta-write)
63   scrubl?
64   (field-writers scrubl-field-writers)
65   (meta-write scrubl-meta-write))
66
67 (define (make-scrubl field-writers meta-write)
68   (%make-scrubl (alist->hashq-table field-writers)
69                 meta-write))
70
71 (define (scrubl-extend-fields scrubl new-field-writers)
72   "Returns a new <scrubl> instance extending SCRUBL's field-writers with
73 NEW-FIELD-WRITERS."
74   (define new-writers
75     (let ((new-table (make-hash-table)))
76       ;; Add old fields from hashq
77       (hash-for-each
78        (lambda (key val)
79          (hashq-set! new-table key val))
80        (scrubl-field-writers scrubl))
81       ;; Now add the new fields
82       (for-each
83        (match-lambda
84          ((key . val)
85           (hashq-set! new-table key val)))
86        new-field-writers)
87       new-table))
88
89   (%make-scrubl new-writers (scrubl-meta-write scrubl)))
90
91 (define (scrubl-write scrubl obj . args)
92   "Write out OBJ via SCRUBL
93
94 Pass in optional extra ARGS to the main META-WRITE" 
95  (apply (scrubl-meta-write scrubl) scrubl obj args))
96
97 (define* (scrubl-write-obj scrubl obj)
98   (match obj
99     (((? symbol? sym) args ...)
100      (let* ((field-writers (scrubl-field-writers scrubl))
101             (field-writer (hashq-ref field-writers sym))
102             (ordered-args (order-symlist-args args)))
103        (when (not field-writer)
104          (throw 'scrubl-unknown-field
105                 #:field sym
106                 #:args args))
107        (apply field-writer scrubl ordered-args)))
108     ((items ...)
109      (map (lambda (item)
110             (scrubl-write-obj scrubl item))
111           items))
112     (any-obj any-obj)))
113
114
115 \f
116 ;;; SXML scrubl writer
117
118 (define (scrubl-sxml-write scrubl obj)
119   (call-with-output-string
120     (lambda (p)
121       (sxml->xml
122        (scrubl-write-obj scrubl obj)
123        p))))
124
125
126 (define (scrubl-sxml-simple-field sym)
127   (lambda (scrubl args)
128     ;; sxml handles inlining automatically in case we have nested
129     ;; lists of strings, so we don't have to worry about that...
130     (cons sym (map (lambda (arg)
131                      (scrubl-write-obj scrubl arg))
132                    args))))
133
134 (define (scrubl-sxml-pre scrubl args)
135   `(span (@ (class "pre-ish"))
136          ,args))
137
138 ;; @@: For a text-only interface, we could put links at end of rendered
139 ;;  text, similar to how orgmode does.
140 (define (scrubl-sxml-anchor scrubl args)
141   (define (maybe-uri->string obj)
142     (if (uri? obj)
143         (uri->string obj)
144         obj))
145   (match args
146     (((= maybe-uri->string href) body1 body ...)
147      `(a (@ (href ,href))
148          ,body1 ,@body))))
149
150 (define scrubl-sxml
151   (make-scrubl `((p . ,(scrubl-sxml-simple-field 'p))
152                  (strong . ,(scrubl-sxml-simple-field 'strong))
153                  (bold . ,(scrubl-sxml-simple-field 'strong))
154                  (b . ,(scrubl-sxml-simple-field 'strong))
155                  (em . ,(scrubl-sxml-simple-field 'em))
156                  (i . ,(scrubl-sxml-simple-field 'em))
157                  (br . ,(scrubl-sxml-simple-field 'br))
158                  (anchor . ,scrubl-sxml-anchor)
159                  (a . ,scrubl-sxml-anchor)
160                  (pre . ,scrubl-sxml-pre)  ;; "pre" style whitespace handling.
161                  (ul . ,(scrubl-sxml-simple-field 'ul))
162                  (li . ,(scrubl-sxml-simple-field 'li)))
163                scrubl-sxml-write))