IN: embedded USING: sequences kernel parser math namespaces io lists ; ! 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 ] [ swap 2dup head >r tail r> ] if ; : get-embedded ( "<%code%>blah" -- "blah" "code" ) ! regexps where art thou? "%>" over 2 start* 2dup swap 2 -rot subseq >r 2 + swap tail r> ; : get-first-chunk ( string -- string ) dup "<%" head? [ get-embedded parse % ] [ get-text , \ write , ] 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 ] f make ; : eval-embedded ( string -- ) parse-embedded call ; : open-embedded-file ( filename -- str ) lines "\n" join ; : with-embedded-file ( filename quot -- ) [ over file set ! so that reload works properly >r lines "\n" join r> call ] with-scope ; : parse-embedded-file ( filename -- quot ) [ parse-embedded ] with-embedded-file ; : run-embedded-file ( filename -- ) [ eval-embedded ] with-embedded-file ;