! Copyright (C) 2005 Alex Chapman. ! See http://factorcode.org/license.txt for BSD license. IN: embedded USING: sequences kernel parser math namespaces io html test errors ; ! if example.fhtml contains: ! ! Simple Embedded Factor Example ! ! <% 5 [ %>

I like repetition

! <% drop ] each %> ! ! ! ! then "example.fhtml" run-embedded-file prints to stdout: ! ! Simple Embedded Factor Example ! !

I like repetition

!

I like repetition

!

I like repetition

!

I like repetition

!

I like repetition

! ! ! : get-text ( string -- remainder chunk ) "<%" over start dup -1 = [ drop "" swap ] [ 2dup head >r tail r> ] if ; : get-embedded ( string -- string code-string ) ! regexps where art thou? "%>" over 2 start* 2dup swap 2 -rot subseq >r 2 + tail r> ; : get-first-chunk ( string -- string ) dup "<%" head? [ get-embedded parse % ] [ get-text , \ write-html , ] if ; : embedded>factor ( string -- ) dup length 0 > [ get-first-chunk embedded>factor ] [ drop ] if ; : parse-embedded ( string -- quot ) #! simple example: "numbers: <% 3 [ 1 + pprint ] each %>" #! => "\"numbers: \" write 3 [ 1 + pprint ] each" [ embedded>factor ] [ ] make ; : eval-embedded ( string -- ) parse-embedded call ; : run-embedded-file ( filename -- ) [ [ file-vocabs dup file set ! so that reload works properly dup contents eval-embedded ] with-scope ] assert-depth drop ; : embedded-convert ( infile outfile -- ) [ run-embedded-file ] with-stream ;