From 6514e5f7cb0e34c691f4f10f9926a4a82841aa4a Mon Sep 17 00:00:00 2001 From: "chapman.alex" Date: Tue, 28 Mar 2006 01:20:03 +0000 Subject: [PATCH] added embedded factor within <% %> --- contrib/httpd/embedded.factor | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 contrib/httpd/embedded.factor diff --git a/contrib/httpd/embedded.factor b/contrib/httpd/embedded.factor new file mode 100644 index 0000000000..1746da72e0 --- /dev/null +++ b/contrib/httpd/embedded.factor @@ -0,0 +1,70 @@ +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 ; +