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 -- )
55 : get-style ( -- string )
56 style get <reversed> "\n" join ;
63 : add-script ( string -- )
66 : get-script ( -- string )
67 script get <reversed> "\n" join ;
74 : add-meta ( name content -- )
75 2array meta get push ;
79 [XML <meta name=<-> content=<->/> XML]
87 : add-atom-feed ( title url -- )
88 2array atom-feeds get push ;
90 : get-atom-feeds ( -- xml )
95 type="application/atom+xml"
101 : write-atom-feeds ( -- )
102 get-atom-feeds write-xml ;
104 SYMBOL: nested-template?
106 SYMBOL: next-template
108 : call-next-template ( -- )
109 next-template get write ;
111 M: f call-template* drop call-next-template ;
113 : with-boilerplate ( child master -- )
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
130 ] with-scope ; inline
132 : template-convert ( template output -- )
133 utf8 [ call-template ] with-file-writer ;