source: project/wiki/hart @ 8415

Last change on this file since 8415 was 8415, checked in by graham, 12 years ago

updated hart documentation

File size: 3.5 KB
Line 
1[[tags:eggs]]
2[[toc:]]
3
4== Introduction
5
6The {{hart}} egg provides an SXML-inspired syntax for HTML generation.
7
8== Authors
9
10[[Graham Fawcett]]
11
12== Requirements
13
14* [[http://www.call-with-current-continuation.org/eggs/vector-lib.html|vector-lib]]
15
16== Examples
17
18=== Canonical Usage
19
20<enscript highlight=scheme>
21(use hart)
22(define people '((Fred fred@example.net 25) (Mary mary@example.net 33)))
23(hart (html (head (title "People"))
24            (body (h1 (fmt: "A list of ~a people" (length people)))
25                  (if: (null? people)
26                       (h2 "No people!")
27                       (ol (for: ((name email age) people)
28                             (li (a (@ (href (conc "mailto:" email)))
29                                    (text: name))
30                                 (fmt: ", who is ~a years old." age))))))))
31</enscript>
32
33which produces
34
35<enscript highlight=html>
36<html><head><title>People</title></head><body>
37<h1>A list of 2 people</h1>
38<ol><li><a href="mailto:fred@example.net">Fred</a>, who is 25 years old.</li>
39<li><a href="mailto:mary@example.net">Mary</a>, who is 33 years old.</li>
40</ol></body></html>
41</enscript>
42
43
44== Syntax
45
46 [syntax] (hart hart-expr ...)
47 [syntax] (hart->string hart-expr ...)
48
49Compiles hart-expr, an expression in the Hart syntax. In short, Hart is SXML syntax plus a set of "keyword expressions" which handle flow-control, iteration, escaping, etc.
50
51(hart) outputs to (current-output-port) and returns an undefined value. (hart->string) returns a string value.
52
53Multiple expressions can be passed to (hart) and (hart->string): there is an implicit (let: () ...) enclosing the expressions (see let: below).
54
55=== Keyword expressions
56
57 [syntax] (if: expr hart-true hart-false)
58 [syntax] (when: expr hart-true*)
59 [syntax] (unless: expr hart-false*)
60
61These are like their Scheme counterparts, except hart-true and hart-false are expressions in Hart syntax.
62
63 [syntax] (for: (iter-expr list-or-vec) . hart-body)
64
65A looping construct. The iter-expr is a match-expression which is matched to each successive value in list-or-vec. Hart-body is a list of Hart expressions, evaluated for each value of iter-expr.
66
67 [syntax] (let: let-forms . hart-body)
68 [syntax] (let*: let-forms . hart-body)
69 [syntax] (letrec: let-forms . hart-body)
70
71Like a (let ...) expression, but hart-body is a list of Hart expressions.
72
73=== Formatting keywords
74
75 [syntax] (raw: . expr)
76
77The Scheme expressions (not Hart!) are evaluated to strings and outputted, without HTML/XML escaping.
78
79 [syntax] (text: . exprs) or (t: . exprs)
80
81The Scheme expressions are evaluated to strings, HTML-escaped, and outputted.
82
83 [syntax] (fmt: format-string . args)
84
85The result of (apply format format-string args) is HTML-escaped and outputted.
86
87 [syntax] (scheme: . exprs)
88
89The Scheme expressions are evaluated. If they produce output, that output will be inserted into the HTML.
90
91=== On Attributes
92
93Note that attributes (such as the "href" in an A tag) are of the form (name value), where "name" is a string or symbol, and "value" is a Scheme expression (literal or otherwise). No escaping is done on these expressions at the current time.
94
95An attribute whose value evaluates to #f will be omitted from the HTML output. This provides a mechanism for specifying optional attributes.
96
97== Utility functions
98
99 [procedure] (hart-html-escape string)
100
101A fast, basic HTML-escaping routine.
102
103== History
104
105Version 1.1.2 (February 2008): bugfix releae
106
107== License
108
109{{hart}} is licensed under the BSD License. Copyright (C) 2008 Graham Fawcett <graham.fawcett@gmail.com>
Note: See TracBrowser for help on using the repository browser.