2 USING: sequences kernel parser math namespaces io ;
4 ! if example.fhtml contains:
6 ! <head><title>Simple Embedded Factor Example</title></head>
8 ! <% 5 [ %><p>I like repetition</p>
13 ! then "example.fhtml" run-embedded-file prints to stdout:
15 ! <head><title>Simple Embedded Factor Example</title></head>
17 ! <p>I like repetition</p>
18 ! <p>I like repetition</p>
19 ! <p>I like repetition</p>
20 ! <p>I like repetition</p>
21 ! <p>I like repetition</p>
26 : get-text ( string -- remainder chunk )
27 "<%" over start dup -1 = [
33 : get-embedded ( string -- string code-string )
34 ! regexps where art thou?
35 "%>" over 2 start* 2dup swap 2 -rot subseq >r 2 + tail r> ;
37 : get-first-chunk ( string -- string )
44 : embedded>factor ( string -- )
46 get-first-chunk embedded>factor
49 : parse-embedded ( string -- quot )
50 #! simple example: "numbers: <% 3 [ 1 + pprint ] each %>"
51 #! => "\"numbers: \" write 3 [ 1 + pprint ] each"
52 [ embedded>factor ] [ ] make ;
54 : eval-embedded ( string -- ) parse-embedded call ;
56 : with-embedded-file ( filename quot -- )
59 over file set ! so that reload works properly
60 >r <file-reader> contents r> call
63 : parse-embedded-file ( filename -- quot )
64 [ parse-embedded ] with-embedded-file ;
66 : run-embedded-file ( filename -- )
67 [ eval-embedded ] with-embedded-file ;
69 : embedded-convert ( infile outfile -- )
70 <file-writer> [ run-embedded-file ] with-stream ;
72 PROVIDE: contrib/embedded ;