Refactor embedded.factor
parent
e8e4cb37fb
commit
7327255050
|
@ -1,57 +1,24 @@
|
||||||
! Copyright (C) 2005 Alex Chapman.
|
! Copyright (C) 2005 Alex Chapman
|
||||||
|
! Copyright (C) 2006 Slava Pestov
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
IN: embedded
|
IN: embedded
|
||||||
USING: sequences kernel parser math namespaces io html test errors ;
|
USING: sequences kernel parser namespaces io html errors ;
|
||||||
|
|
||||||
! if example.fhtml contains:
|
! See libs/httpd/test/ or libs/furnace/ for embedded usage
|
||||||
! <html>
|
! examples!
|
||||||
! <head><title>Simple Embedded Factor Example</title></head>
|
|
||||||
! <body>
|
|
||||||
! <% 5 [ %><p>I like repetition</p>
|
|
||||||
! <% drop ] each %>
|
|
||||||
! </body>
|
|
||||||
! </html>
|
|
||||||
!
|
|
||||||
! then "example.fhtml" run-embedded-file prints to stdout:
|
|
||||||
! <html>
|
|
||||||
! <head><title>Simple Embedded Factor Example</title></head>
|
|
||||||
! <body>
|
|
||||||
! <p>I like repetition</p>
|
|
||||||
! <p>I like repetition</p>
|
|
||||||
! <p>I like repetition</p>
|
|
||||||
! <p>I like repetition</p>
|
|
||||||
! <p>I like repetition</p>
|
|
||||||
!
|
|
||||||
! </body>
|
|
||||||
! </html>
|
|
||||||
|
|
||||||
: get-text ( string -- remainder chunk )
|
: process-html ( parse-tree string -- parse-tree )
|
||||||
"<%" over start dup -1 = [
|
dup empty? [ drop ] [ parsed \ write-html parsed ] if ;
|
||||||
drop "" swap
|
|
||||||
] [
|
|
||||||
2dup head >r tail r>
|
|
||||||
] if ;
|
|
||||||
|
|
||||||
: get-embedded ( string -- string code-string )
|
: process-embedded ( parse-tree string -- string parse-tree )
|
||||||
! regexps where art thou?
|
"<%" split1 >r process-html r> "%>" split1 >r (parse) r> ;
|
||||||
"%>" over 2 start* 2dup swap 2 -rot subseq >r 2 + tail r> ;
|
|
||||||
|
|
||||||
: get-first-chunk ( string -- string )
|
: (parse-embedded) ( parse-tree string -- parse-tree )
|
||||||
dup "<%" head? [
|
dup empty?
|
||||||
get-embedded parse %
|
[ drop ] [ process-embedded (parse-embedded) ] if ;
|
||||||
] [
|
|
||||||
get-text , \ write-html ,
|
|
||||||
] if ;
|
|
||||||
|
|
||||||
: embedded>factor ( string -- )
|
|
||||||
dup length 0 > [
|
|
||||||
get-first-chunk embedded>factor
|
|
||||||
] [ drop ] if ;
|
|
||||||
|
|
||||||
: parse-embedded ( string -- quot )
|
: parse-embedded ( string -- quot )
|
||||||
#! simple example: "numbers: <% 3 [ 1 + pprint ] each %>"
|
[ f swap (parse-embedded) >quotation ] with-parser ;
|
||||||
#! => "\"numbers: \" write 3 [ 1 + pprint ] each"
|
|
||||||
[ embedded>factor ] [ ] make ;
|
|
||||||
|
|
||||||
: eval-embedded ( string -- ) parse-embedded call ;
|
: eval-embedded ( string -- ) parse-embedded call ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue