factor/basis/html/templates/chloe/chloe.factor

136 lines
3.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel sequences combinators kernel fry
namespaces make classes.tuple assocs splitting words arrays io
io.files io.files.info io.encodings.utf8 io.streams.string
unicode.case mirrors math urls present multiline quotations xml
logging call
2009-02-05 22:17:03 -05:00
xml.data xml.writer xml.syntax strings
html.forms
2009-01-30 20:28:16 -05:00
html
2008-05-23 20:16:21 -04:00
html.components
2008-05-24 02:28:48 -04:00
html.templates
html.templates.chloe.compiler
html.templates.chloe.components
html.templates.chloe.syntax ;
2008-05-24 02:28:48 -04:00
IN: html.templates.chloe
2008-04-15 07:10:08 -04:00
TUPLE: chloe path ;
C: <chloe> chloe
CHLOE: chloe compile-children ;
CHLOE: title compile-children>string [ set-title ] [code] ;
CHLOE: write-title
2008-04-15 07:10:08 -04:00
drop
2008-06-02 16:00:03 -04:00
"head" tag-stack get member?
"title" tag-stack get member? not and
[ get-title [XML <title><-></title> XML] ]
[ get-title ] ?
[xml-code] ;
2008-04-15 07:10:08 -04:00
CHLOE: style
dup "include" optional-attr [
utf8 file-contents [ add-style ] [code-with]
2008-04-15 07:10:08 -04:00
] [
compile-children>string [ add-style ] [code]
] ?if ;
2008-04-15 07:10:08 -04:00
CHLOE: write-style
2008-09-23 17:32:10 -04:00
drop [
get-style
[XML <style type="text/css"> <-> </style> XML]
] [xml-code] ;
2008-04-15 07:10:08 -04:00
CHLOE: even
[ "index" value even? swap when ] process-children ;
2008-04-15 07:10:08 -04:00
CHLOE: odd
[ "index" value odd? swap when ] process-children ;
2008-05-26 01:47:27 -04:00
2008-05-26 03:54:53 -04:00
: (bind-tag) ( tag quot -- )
[
[ "name" required-attr compile-attr ] keep
] dip process-children ; inline
2008-05-26 03:54:53 -04:00
CHLOE: each [ with-each-value ] (bind-tag) ;
2008-05-26 03:54:53 -04:00
CHLOE: bind-each [ with-each-object ] (bind-tag) ;
CHLOE: bind [ with-form ] (bind-tag) ;
2008-05-26 03:54:53 -04:00
CHLOE: comment drop ;
2008-05-23 20:16:21 -04:00
CHLOE: call-next-template
drop reset-buffer \ call-next-template , ;
2008-05-23 20:16:21 -04:00
CHLOE: validation-errors
drop [ render-validation-errors ] [code] ;
: attr>word ( value -- word/f )
2008-06-18 04:26:50 -04:00
":" split1 swap lookup ;
: if>quot ( tag -- quot )
[
[ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
[ "value" optional-attr [ , \ value , ] [ t , ] if* ]
bi
\ and ,
] [ ] make ;
CHLOE: if dup if>quot [ swap when ] append process-children ;
2008-09-29 05:10:00 -04:00
COMPONENT: label
COMPONENT: link
COMPONENT: inspector
COMPONENT: comparison
COMPONENT: html
COMPONENT: hidden
COMPONENT: farkup
COMPONENT: field
COMPONENT: textarea
COMPONENT: password
COMPONENT: choice
COMPONENT: checkbox
COMPONENT: code
2008-05-23 20:16:21 -04:00
SYMBOL: template-cache
2008-09-08 03:52:42 -04:00
H{ } template-cache set-global
2008-09-08 03:52:42 -04:00
TUPLE: cached-template path last-modified quot ;
: load-template ( chloe -- cached-template )
path>> ".xml" append
[ ]
[ file-info modified>> ]
[ utf8 <file-reader> read-xml compile-template ] tri
\ cached-template boa ;
\ load-template DEBUG add-input-logging
: cached-template ( chloe -- cached-template/f )
template-cache get at* [
[
[ path>> file-info modified>> ]
[ last-modified>> ]
bi =
] keep and
] when ;
: template-quot ( chloe -- quot )
dup cached-template [ ] [
[ load-template dup ] keep
template-cache get set-at
] ?if quot>> ;
: reset-cache ( -- )
template-cache get clear-assoc ;
2008-04-15 07:10:08 -04:00
2008-04-22 22:08:27 -04:00
M: chloe call-template*
template-quot call( -- ) ;
INSTANCE: chloe template