diff options
author | Case Duckworth | 2023-08-28 23:12:18 -0500 |
---|---|---|
committer | Case Duckworth | 2023-08-28 23:12:18 -0500 |
commit | d6468b246c4319e215062c5b8627286bc27d556e (patch) | |
tree | 9997141c0d1ebbfaabdfee166e0a5510af6ca35a /README.md | |
parent | Clean up import declaration in render-specials (diff) | |
download | schmaltz-d6468b246c4319e215062c5b8627286bc27d556e.tar.gz schmaltz-d6468b246c4319e215062c5b8627286bc27d556e.zip |
Add README and COPYING
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..7307850 --- /dev/null +++ b/README.md | |||
@@ -0,0 +1,145 @@ | |||
1 | # Schmaltz | ||
2 | ## Render embedded scheme in texts | ||
3 | |||
4 | Schmaltz is a way to embed scheme code in text files using escape characters. This makes it a decent templating language à la PHP or shell here-documents. | ||
5 | |||
6 | ## Rationale | ||
7 | |||
8 | Schmaltz was inspired by shell here-documents as well as CHICKEN's extension to scheme string literals, which looks like this: | ||
9 | |||
10 | ```scheme | ||
11 | (define some-string #<#end | ||
12 | Here is a multiline string literal with #(display "embedded") scheme code. | ||
13 | 1 + 2 = #(+ 1 2), etc. | ||
14 | end | ||
15 | ) | ||
16 | ``` | ||
17 | |||
18 | While this works fine enough, there are a number of limitations preventing it from more thorough use: | ||
19 | |||
20 | - The above notation only works with string literals. | ||
21 | - The `end` tag must be alone on a line and at the beginning of the line. That, plus the fact that this is a non-standard extension, mean that editors (Emacs) indent that end tag, leading to a mis-parse. Fixing it is possible but fiddly. | ||
22 | - The escape notation isn't extensible. | ||
23 | |||
24 | Here-documents, in shell and other scripts, are similar in form (I assume that CHICKEN's extension was inspired by this syntax): | ||
25 | |||
26 | ```sh | ||
27 | cat <<EOF | ||
28 | Here is a here-document in bourne shell. It performs expansion, so you can include things like $(echo "this") in. | ||
29 | 1 + 2 = $((1 + 2)), etc. | ||
30 | EOF | ||
31 | ``` | ||
32 | |||
33 | In shell here-documents, however, every expanded form is executed in a sub-shell, meaning that variable assignments and function definitions are not persistent across the document. | ||
34 | |||
35 | Schmaltz attempts to fix all of these problems. In fact, within a schmaltz-powered document you should have all of the power of scheme within rendered forms, as well as persistence of environment and access to the rest of your scheme system. Here is a schmaltz document in the same format as the above for illustration: | ||
36 | |||
37 | ``` | ||
38 | #,(define word "expansion") | ||
39 | Here is a schmaltz-powered text thing. It can do #,(display word), as well as arithmetic and all sorts of other things. | ||
40 | #,(import (scheme inexact)) | ||
41 | The square root of 13 is #,(sqrt 13), etc. | ||
42 | ``` | ||
43 | |||
44 | ## Installation | ||
45 | |||
46 | Schmaltz is written in CHICKEN Scheme v.5.3.0 and Works on My Machine™. Your mileage may vary, though I welcome bug reports and merge requests! | ||
47 | |||
48 | To install schmaltz, you'll need the following eggs: | ||
49 | |||
50 | - [r7rs](http://wiki.call-cc.org/eggref/5/r7rs) | ||
51 | - [utf8](http://wiki.call-cc.org/eggref/5/utf8) | ||
52 | - [srfi-1](http://wiki.call-cc.org/eggref/5/srfi-1) | ||
53 | - [html-parser](http://wiki.call-cc.org/eggref/5/html-parser) | ||
54 | |||
55 | This repository comes with an egg file, so you should just be able to run `chicken-install -s` inside of it to install all the dependencies as well as schmaltz. | ||
56 | |||
57 | ## Usage | ||
58 | |||
59 | Schmaltz is packaged both as a portable R7RS library (untested) and a CHICKEN executable. | ||
60 | |||
61 | ### R7RS library: `(schmaltz)` | ||
62 | |||
63 | The schmaltz library exports the following definitions in its public API: | ||
64 | |||
65 | #### `(render [port] [environment])` *procedure* | ||
66 | Read the input PORT, evaluating every expansion form in ENVIRONMENT. PORT defaults to `current-input-port` and ENVIRONMENT defaults to `render-environment`. Outputs to `current-output-port`. | ||
67 | |||
68 | #### `(render-string string [environment])` *procedure* | ||
69 | Perform `render` with STRING as the input port. | ||
70 | |||
71 | #### `(render->string . args)` *procedure* | ||
72 | Perform `render`, returning a string with the output. Takes the same arguments as `render`. | ||
73 | |||
74 | #### `(render-string->string . args)` *procedure* | ||
75 | Perform `render-string`, returning a string with the output. Takes the same arguments as `render-string`. | ||
76 | |||
77 | #### `render-specials` *parameter* | ||
78 | Special characters to trigger expansion. Every expansion begins with `#`, then another character, then a scheme form. `render-specials` is an alist where CARs is a character literal and CDRs are procedures of one parameter, an input port. | ||
79 | |||
80 | `render-specials` defaults to | ||
81 | |||
82 | ```scheme | ||
83 | `((#\, . ,(lambda (in) | ||
84 | (eval (read in) (render-environment))))) | ||
85 | ``` | ||
86 | |||
87 | This renders `#,(+ 1 2)` to `3`. | ||
88 | |||
89 | #### `render-unprintables` *parameter* | ||
90 | String results not to include in the rendered output. For example, `define` might return an unspecified or void value, which when displayed could be `#<unspecified>`. `render-unprintables` is an alist where CARs are strings and CDRs are procedures of one parameter, the current character being processed. The default value of `render-unprintable` is an empty list, but see the section on the `schmaltz` executable for a more useful example. | ||
91 | |||
92 | #### `(unprintable/skip . _)`, `(unprintable/backtrack char)` *procedures* | ||
93 | Helper procedures for definitions in `render-unprintables`. See the example below for usage ideas. | ||
94 | |||
95 | `unprintable/skip` skips whatever output is given from the rendered scheme code and prints nothing. `unprintable/backtrack` skips the output, but backtracks to output the character triggering the expansion as well as the preceding `#`. | ||
96 | |||
97 | #### `render-environment` *parameter* | ||
98 | The environment to evaluate rendered forms in. Defaults to `interaction-environment`. | ||
99 | |||
100 | #### `environment` *procedure* | ||
101 | Re-exported from `(scheme eval)` for ease of building `render-environment`s. | ||
102 | |||
103 | ### Executable: `schmaltz` | ||
104 | |||
105 | The `schmaltz` executable reads standard input or files from the command line, rendering and concatenating all of them to standard output, like `cat`. Also like `cat`, if `-` is passed as a parameter to `schmaltz`, it will insert the contents of standard input at that position in the output. | ||
106 | |||
107 | You can call it as a compiled file or as a script as long as you have `csi` installed. | ||
108 | |||
109 | By default, `schmaltz` extends the definitions of the following library parameters: | ||
110 | |||
111 | #### `render-specials` | ||
112 | |||
113 | Adds `@` for expanding to html via `sxml->html`, e.g. | ||
114 | ``` | ||
115 | #@(a (@ (href "https://example.com")) "link") | ||
116 | ``` | ||
117 | renders to | ||
118 | ``` | ||
119 | <a href="https://example.com">link</a> | ||
120 | ``` | ||
121 | |||
122 | It wraps the form in an implicit `quasiquote` so you can include (string) variables, e.g. | ||
123 | ``` | ||
124 | #,(define title "Schmaltz!") | ||
125 | #@(h1 ,title) | ||
126 | ``` | ||
127 | |||
128 | #### `render-unprintables` | ||
129 | |||
130 | ```scheme | ||
131 | (list (cons "#<unspecified>" unprintable/skip) | ||
132 | (cons "#!eof" unprintable/backtrack)) | ||
133 | ``` | ||
134 | |||
135 | The above skips printing unspecified values altogether, and the end-of-file marker backtracks to print the (non-)escaping `#` at the input's end. | ||
136 | |||
137 | ### License | ||
138 | |||
139 | Schmaltz, the library and the program, are licensed under the GOD WILLING LICENSE, version 1.0. See [COPYING][] for details. | ||
140 | |||
141 | [COPYING]: /schmaltz/tree/COPYING | ||
142 | |||
143 | ### Contributing | ||
144 | |||
145 | Contributions are welcome! Email me at `git at acdw dot net` or get in touch via whatever other channels you know how with questions or comments or whatever. | ||