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
11 GENERIC: call-template* ( template -- )
13 M: string call-template* write ;
15 M: callable call-template* call( -- ) ;
17 M: xml call-template* write-xml ;
19 M: object call-template* output-stream get stream-copy ;
21 ERROR: template-error template error ;
23 M: template-error error.
24 "Error while processing template " write
25 [ template>> short. ":" print nl ]
29 : call-template ( template -- )
30 [ call-template* ] [ \ template-error boa rethrow ] recover ;
32 ERROR: no-boilerplate ;
34 M: no-boilerplate error.
36 "get-title and set-title can only be used from within" print
37 "a with-boilerplate form" print ;
41 : set-title ( string -- )
42 title get [ >box ] [ no-boilerplate ] if* ;
44 : get-title ( -- string )
45 title get [ value>> ] [ no-boilerplate ] if* ;
52 : add-style ( string -- )
53 "\n" style get push-all
56 : get-style ( -- string )
64 : add-script ( string -- )
65 "\n" script get push-all
68 : get-script ( -- string )
76 : add-meta ( name content -- )
77 2array meta get push ;
81 [XML <meta name=<-> content=<->/> XML]
89 : add-atom-feed ( title url -- )
90 2array atom-feeds get push ;
92 : get-atom-feeds ( -- xml )
97 type="application/atom+xml"
103 : write-atom-feeds ( -- )
104 get-atom-feeds write-xml ;
106 SYMBOL: nested-template?
108 SYMBOL: next-template
110 : call-next-template ( -- )
111 next-template get write ;
113 M: f call-template* drop call-next-template ;
115 : with-boilerplate ( child master -- )
117 title [ [ <box> ] unless* ] change
118 style [ [ SBUF" " clone ] unless* ] change
119 script [ [ SBUF" " clone ] unless* ] change
120 meta [ [ V{ } clone ] unless* ] change
121 atom-feeds [ V{ } like ] change
132 ] with-scope ; inline
134 : template-convert ( template output -- )
135 utf8 [ call-template ] with-file-writer ;