IN: embedded USING: sequences kernel parser math namespaces io ; ! if example.fhtml contains: ! !
I like repetition
! <% drop ] each %> ! ! ! ! then "example.fhtml" run-embedded-file prints to stdout: ! !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 ] [ ] make ; : eval-embedded ( string -- ) parse-embedded call ; : with-embedded-file ( filename quot -- ) [ over file set ! so that reload works properly >r