factor/basis/html/templates/templates.factor

102 lines
2.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 Slava Pestov.
2008-05-23 20:16:21 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel fry io io.encodings.utf8 io.files
debugger prettyprint continuations namespaces boxes sequences
2009-03-16 21:11:36 -04:00
arrays strings html io.streams.string assocs
2009-02-05 22:17:03 -05:00
quotations xml.data xml.writer xml.syntax ;
2008-05-23 20:16:21 -04:00
IN: html.templates
MIXIN: template
GENERIC: call-template* ( template -- )
M: string call-template* write ;
M: callable call-template* call( -- ) ;
2008-05-23 20:16:21 -04:00
M: xml call-template* write-xml ;
2008-05-23 20:16:21 -04:00
M: object call-template* output-stream get stream-copy ;
ERROR: template-error template error ;
M: template-error error.
"Error while processing template " write
2008-05-26 01:47:27 -04:00
[ template>> short. ":" print nl ]
2008-05-23 20:16:21 -04:00
[ error>> error. ]
bi ;
: call-template ( template -- )
2008-05-26 01:47:27 -04:00
[ call-template* ] [ \ template-error boa rethrow ] recover ;
2008-05-23 20:16:21 -04:00
SYMBOL: title
: set-title ( string -- )
title get >box ;
: get-title ( -- string )
title get value>> ;
2008-05-23 20:16:21 -04:00
: write-title ( -- )
get-title write ;
2008-05-23 20:16:21 -04:00
SYMBOL: style
: add-style ( string -- )
"\n" style get push-all
style get push-all ;
: get-style ( -- string )
style get >string ;
2008-05-23 20:16:21 -04:00
: write-style ( -- )
get-style write ;
2008-05-23 20:16:21 -04:00
SYMBOL: atom-feeds
2008-05-23 20:16:21 -04:00
: add-atom-feed ( title url -- )
2array atom-feeds get push ;
2008-05-23 20:16:21 -04:00
: get-atom-feeds ( -- xml )
atom-feeds get [
[XML
2009-01-30 20:28:16 -05:00
<link
rel="alternate"
type="application/atom+xml"
title=<->
href=<->/>
XML]
] { } assoc>map ;
: write-atom-feeds ( -- )
get-atom-feeds write-xml ;
2008-05-23 20:16:21 -04:00
SYMBOL: nested-template?
SYMBOL: next-template
: call-next-template ( -- )
next-template get write ;
2008-05-23 20:16:21 -04:00
M: f call-template* drop call-next-template ;
2008-09-23 23:01:26 -04:00
: with-boilerplate ( child master -- )
2008-05-23 20:16:21 -04:00
[
title [ <box> or ] change
style [ SBUF" " clone or ] change
atom-feeds [ V{ } like ] change
2008-05-23 20:16:21 -04:00
[
[
nested-template? on
call-template
] with-string-writer
next-template set
]
[ call-template ]
bi*
] with-scope ; inline
: template-convert ( template output -- )
utf8 [ call-template ] with-file-writer ;