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
6 quotations xml.data xml.writer 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-atom-feed ( title url -- )
65 2array atom-feeds get push ;
67 : get-atom-feeds ( -- xml )
72 type="application/atom+xml"
78 : write-atom-feeds ( -- )
79 get-atom-feeds write-xml ;
81 SYMBOL: nested-template?
85 : call-next-template ( -- )
86 next-template get write ;
88 M: f call-template* drop call-next-template ;
90 : with-boilerplate ( child master -- )
92 title [ <box> or ] change
93 style [ SBUF" " clone or ] change
94 atom-feeds [ V{ } like ] change
105 ] with-scope ; inline
107 : template-convert ( template output -- )
108 utf8 [ call-template ] with-file-writer ;