]> gitweb.factorcode.org Git - factor.git/blob - basis/html/templates/templates.factor
Docs for html.*
[factor.git] / basis / html / templates / templates.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel fry io io.encodings.utf8 io.files
4 debugger prettyprint continuations namespaces boxes sequences
5 arrays strings html.elements io.streams.string
6 quotations xml.data xml.writer ;
7 IN: html.templates
8
9 MIXIN: template
10
11 GENERIC: call-template* ( template -- )
12
13 M: string call-template* write ;
14
15 M: callable call-template* call ;
16
17 M: xml call-template* write-xml ;
18
19 M: object call-template* output-stream get stream-copy ;
20
21 ERROR: template-error template error ;
22
23 M: template-error error.
24     "Error while processing template " write
25     [ template>> short. ":" print nl ]
26     [ error>> error. ]
27     bi ;
28
29 : call-template ( template -- )
30     [ call-template* ] [ \ template-error boa rethrow ] recover ;
31
32 SYMBOL: title
33
34 : set-title ( string -- )
35     title get >box ;
36
37 : write-title ( -- )
38     title get value>> write ;
39
40 SYMBOL: style
41
42 : add-style ( string -- )
43     "\n" style get push-all
44          style get push-all ;
45
46 : write-style ( -- )
47     style get >string write ;
48
49 SYMBOL: atom-feeds
50
51 : add-atom-feed ( title url -- )
52     2array atom-feeds get push ;
53
54 : write-atom-feeds ( -- )
55     atom-feeds get [
56         <link "alternate" =rel "application/atom+xml" =type
57         first2 [ =title ] [ =href ] bi*
58         link/>
59     ] each ;
60
61 SYMBOL: nested-template?
62
63 SYMBOL: next-template
64
65 : call-next-template ( -- )
66     next-template get write-html ;
67
68 M: f call-template* drop call-next-template ;
69
70 : with-boilerplate ( child master -- )
71     [
72         title [ <box> or ] change
73         style [ SBUF" " clone or ] change
74         atom-feeds [ V{ } like ] change
75
76         [
77             [
78                 nested-template? on
79                 call-template
80             ] with-string-writer
81             next-template set
82         ]
83         [ call-template ]
84         bi*
85     ] with-scope ; inline
86
87 : template-convert ( template output -- )
88     utf8 [ call-template ] with-file-writer ;