1 ! Copyright (C) 2005 Alex Chapman.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: sequences kernel parser math namespaces io html test ;
6 ! if example.fhtml contains:
8 ! <head><title>Simple Embedded Factor Example</title></head>
10 ! <% 5 [ %><p>I like repetition</p>
15 ! then "example.fhtml" run-embedded-file prints to stdout:
17 ! <head><title>Simple Embedded Factor Example</title></head>
19 ! <p>I like repetition</p>
20 ! <p>I like repetition</p>
21 ! <p>I like repetition</p>
22 ! <p>I like repetition</p>
23 ! <p>I like repetition</p>
28 : get-text ( string -- remainder chunk )
29 "<%" over start dup -1 = [
35 : get-embedded ( string -- string code-string )
36 ! regexps where art thou?
37 "%>" over 2 start* 2dup swap 2 -rot subseq >r 2 + tail r> ;
39 : get-first-chunk ( string -- string )
43 get-text , \ write-html ,
46 : embedded>factor ( string -- )
48 get-first-chunk embedded>factor
51 : parse-embedded ( string -- quot )
52 #! simple example: "numbers: <% 3 [ 1 + pprint ] each %>"
53 #! => "\"numbers: \" write 3 [ 1 + pprint ] each"
54 [ embedded>factor ] [ ] make ;
56 : eval-embedded ( string -- ) parse-embedded call ;
58 : run-embedded-file ( filename -- )
62 dup file set ! so that reload works properly
63 dup <file-reader> contents eval-embedded
67 : embedded-convert ( infile outfile -- )
68 <file-writer> [ run-embedded-file ] with-stream ;