From 57df3b9ee54f3515fcf3b95268d5931db34897dc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 19 Sep 2008 18:46:54 -0500 Subject: [PATCH] Check template modification time, recompile if changed --- basis/html/templates/chloe/chloe.factor | 47 +++++++++++++------ .../templates/chloe/compiler/compiler.factor | 30 ++++++++---- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/basis/html/templates/chloe/chloe.factor b/basis/html/templates/chloe/chloe.factor index 5fe53fc7a5..cc51bd05d3 100644 --- a/basis/html/templates/chloe/chloe.factor +++ b/basis/html/templates/chloe/chloe.factor @@ -1,9 +1,9 @@ ! 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 -memoize io io.files io.encodings.utf8 io.streams.string -unicode.case mirrors math urls present multiline quotations xml +namespaces make classes.tuple assocs splitting words arrays io +io.files io.encodings.utf8 io.streams.string unicode.case +mirrors math urls present multiline quotations xml logging xml.data html.forms html.elements @@ -89,21 +89,40 @@ CHLOE-TUPLE: choice CHLOE-TUPLE: checkbox CHLOE-TUPLE: code -: read-template ( chloe -- xml ) - path>> ".xml" append utf8 read-xml ; +SYMBOL: template-cache -MEMO: template-quot ( chloe -- quot ) - read-template compile-template ; +H{ } template-cache set-global -MEMO: nested-template-quot ( chloe -- quot ) - read-template compile-nested-template ; +TUPLE: cached-template path last-modified quot ; -: reset-templates ( -- ) - { template-quot nested-template-quot } [ reset-memoized ] each ; +: load-template ( chloe -- cached-template ) + path>> ".xml" append + [ ] + [ file-info modified>> ] + [ utf8 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* - nested-template? get - [ nested-template-quot ] [ template-quot ] if - assert-depth ; + template-quot assert-depth ; INSTANCE: chloe template diff --git a/basis/html/templates/chloe/compiler/compiler.factor b/basis/html/templates/chloe/compiler/compiler.factor index f32923f620..aa741ebf9f 100644 --- a/basis/html/templates/chloe/compiler/compiler.factor +++ b/basis/html/templates/chloe/compiler/compiler.factor @@ -3,7 +3,7 @@ USING: assocs namespaces make kernel sequences accessors combinators strings splitting io io.streams.string present xml.writer xml.data xml.entities html.forms -html.templates.chloe.syntax ; +html.templates html.templates.chloe.syntax ; IN: html.templates.chloe.compiler : chloe-attrs-only ( assoc -- assoc' ) @@ -98,9 +98,6 @@ DEFER: compile-element reset-buffer ] [ ] make ; inline -: compile-nested-template ( xml -- quot ) - [ compile-element ] with-compiler ; - : compile-chunk ( seq -- ) [ compile-element ] each ; @@ -121,12 +118,25 @@ DEFER: compile-element : compile-with-scope ( quot -- ) 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 ) [ - { - [ prolog>> [ write-prolog ] [code-with] ] - [ before>> compile-chunk ] - [ compile-element ] - [ after>> compile-chunk ] - } cleave + [ compile-prologue ] + [ compile-element ] + [ compile-epilogue ] + tri ] with-compiler ;