Check template modification time, recompile if changed

db4
Slava Pestov 2008-09-19 18:46:54 -05:00
parent c6cbd502bc
commit 57df3b9ee5
2 changed files with 53 additions and 24 deletions

View File

@ -1,9 +1,9 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel sequences combinators kernel fry USING: accessors kernel sequences combinators kernel fry
namespaces make classes.tuple assocs splitting words arrays namespaces make classes.tuple assocs splitting words arrays io
memoize io io.files io.encodings.utf8 io.streams.string io.files io.encodings.utf8 io.streams.string unicode.case
unicode.case mirrors math urls present multiline quotations xml mirrors math urls present multiline quotations xml logging
xml.data xml.data
html.forms html.forms
html.elements html.elements
@ -89,21 +89,40 @@ CHLOE-TUPLE: choice
CHLOE-TUPLE: checkbox CHLOE-TUPLE: checkbox
CHLOE-TUPLE: code CHLOE-TUPLE: code
: read-template ( chloe -- xml ) SYMBOL: template-cache
path>> ".xml" append utf8 <file-reader> read-xml ;
MEMO: template-quot ( chloe -- quot ) H{ } template-cache set-global
read-template compile-template ;
MEMO: nested-template-quot ( chloe -- quot ) TUPLE: cached-template path last-modified quot ;
read-template compile-nested-template ;
: reset-templates ( -- ) : load-template ( chloe -- cached-template )
{ template-quot nested-template-quot } [ reset-memoized ] each ; 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 ;
M: chloe call-template* M: chloe call-template*
nested-template? get template-quot assert-depth ;
[ nested-template-quot ] [ template-quot ] if
assert-depth ;
INSTANCE: chloe template INSTANCE: chloe template

View File

@ -3,7 +3,7 @@
USING: assocs namespaces make kernel sequences accessors USING: assocs namespaces make kernel sequences accessors
combinators strings splitting io io.streams.string present combinators strings splitting io io.streams.string present
xml.writer xml.data xml.entities html.forms xml.writer xml.data xml.entities html.forms
html.templates.chloe.syntax ; html.templates html.templates.chloe.syntax ;
IN: html.templates.chloe.compiler IN: html.templates.chloe.compiler
: chloe-attrs-only ( assoc -- assoc' ) : chloe-attrs-only ( assoc -- assoc' )
@ -98,9 +98,6 @@ DEFER: compile-element
reset-buffer reset-buffer
] [ ] make ; inline ] [ ] make ; inline
: compile-nested-template ( xml -- quot )
[ compile-element ] with-compiler ;
: compile-chunk ( seq -- ) : compile-chunk ( seq -- )
[ compile-element ] each ; [ compile-element ] each ;
@ -121,12 +118,25 @@ DEFER: compile-element
: compile-with-scope ( quot -- ) : compile-with-scope ( quot -- )
compile-quot [ with-scope ] [code] ; inline compile-quot [ with-scope ] [code] ; inline
: if-not-nested ( quot -- )
nested-template? get swap unless ; inline
: compile-prologue ( xml -- )
[
[ before>> compile-chunk ]
[ prolog>> [ write-prolog ] [code-with] ]
bi
] compile-quot
[ if-not-nested ] [code] ;
: compile-epilogue ( xml -- )
[ after>> compile-chunk ] compile-quot
[ if-not-nested ] [code] ;
: compile-template ( xml -- quot ) : compile-template ( xml -- quot )
[ [
{ [ compile-prologue ]
[ prolog>> [ write-prolog ] [code-with] ] [ compile-element ]
[ before>> compile-chunk ] [ compile-epilogue ]
[ compile-element ] tri
[ after>> compile-chunk ]
} cleave
] with-compiler ; ] with-compiler ;