]> gitweb.factorcode.org Git - factor.git/blob - basis/html/templates/templates.factor
html.templates: reverse order of style and scripts
[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 arrays assocs boxes continuations debugger io
4 io.encodings.utf8 io.files io.streams.string kernel namespaces
5 prettyprint quotations sequences strings xml.data xml.syntax
6 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 ERROR: no-boilerplate ;
33
34 M: no-boilerplate error.
35     drop
36     "get-title and set-title can only be used from within" print
37     "a with-boilerplate form" print ;
38
39 SYMBOL: title
40
41 : set-title ( string -- )
42     title get [ >box ] [ no-boilerplate ] if* ;
43
44 : get-title ( -- string )
45     title get [ value>> ] [ no-boilerplate ] if* ;
46
47 : write-title ( -- )
48     get-title write ;
49
50 SYMBOL: style
51
52 : add-style ( string -- )
53     style get push ;
54
55 : get-style ( -- string )
56     style get <reversed> "\n" join ;
57
58 : write-style ( -- )
59     get-style write ;
60
61 SYMBOL: script
62
63 : add-script ( string -- )
64     script get push ;
65
66 : get-script ( -- string )
67     script get <reversed> "\n" join ;
68
69 : write-script ( -- )
70     get-script write ;
71
72 SYMBOL: meta
73
74 : add-meta ( name content -- )
75     2array meta get push ;
76
77 : get-meta ( -- xml )
78     meta get [
79         [XML <meta name=<-> content=<->/> XML]
80     ] { } assoc>map ;
81
82 : write-meta ( -- )
83     get-meta write-xml ;
84
85 SYMBOL: atom-feeds
86
87 : add-atom-feed ( title url -- )
88     2array atom-feeds get push ;
89
90 : get-atom-feeds ( -- xml )
91     atom-feeds get [
92         [XML
93             <link
94                 rel="alternate"
95                 type="application/atom+xml"
96                 title=<->
97                 href=<->/>
98         XML]
99     ] { } assoc>map ;
100
101 : write-atom-feeds ( -- )
102     get-atom-feeds write-xml ;
103
104 SYMBOL: nested-template?
105
106 SYMBOL: next-template
107
108 : call-next-template ( -- )
109     next-template get write ;
110
111 M: f call-template* drop call-next-template ;
112
113 : with-boilerplate ( child master -- )
114     [
115         title [ [ <box> ] unless* ] change
116         style [ [ V{ } clone ] unless* ] change
117         script [ [ V{ } clone ] unless* ] change
118         meta [ [ V{ } clone ] unless* ] change
119         atom-feeds [ V{ } like ] change
120
121         [
122             [
123                 nested-template? on
124                 call-template
125             ] with-string-writer
126             next-template set
127         ]
128         [ call-template ]
129         bi*
130     ] with-scope ; inline
131
132 : template-convert ( template output -- )
133     utf8 [ call-template ] with-file-writer ;