]> gitweb.factorcode.org Git - factor.git/blob - basis/html/templates/templates.factor
Merge branch 'master' of git://factorcode.org/git/factor into clean-linux-x86-32
[factor.git] / basis / html / templates / templates.factor
1 ! Copyright (C) 2008, 2009 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 io.streams.string assocs call
6 quotations xml.data xml.writer xml.syntax ;
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 : get-title ( -- string )
38     title get value>> ;
39
40 : write-title ( -- )
41     get-title write ;
42
43 SYMBOL: style
44
45 : add-style ( string -- )
46     "\n" style get push-all
47          style get push-all ;
48
49 : get-style ( -- string )
50     style get >string ;
51
52 : write-style ( -- )
53     get-style write ;
54
55 SYMBOL: atom-feeds
56
57 : add-atom-feed ( title url -- )
58     2array atom-feeds get push ;
59
60 : get-atom-feeds ( -- xml )
61     atom-feeds get [
62         [XML
63             <link
64                 rel="alternate"
65                 type="application/atom+xml"
66                 title=<->
67                 href=<->/>
68         XML]
69     ] { } assoc>map ;
70
71 : write-atom-feeds ( -- )
72     get-atom-feeds write-xml ;
73
74 SYMBOL: nested-template?
75
76 SYMBOL: next-template
77
78 : call-next-template ( -- )
79     next-template get write ;
80
81 M: f call-template* drop call-next-template ;
82
83 : with-boilerplate ( child master -- )
84     [
85         title [ <box> or ] change
86         style [ SBUF" " clone or ] change
87         atom-feeds [ V{ } like ] change
88
89         [
90             [
91                 nested-template? on
92                 call-template
93             ] with-string-writer
94             next-template set
95         ]
96         [ call-template ]
97         bi*
98     ] with-scope ; inline
99
100 : template-convert ( template output -- )
101     utf8 [ call-template ] with-file-writer ;