]> gitweb.factorcode.org Git - factor.git/blob - apps/furnace-fjsc/furnace-fjsc.factor
11056820320ca24c4f8c2644225a9625e0f43bf3
[factor.git] / apps / furnace-fjsc / furnace-fjsc.factor
1 ! Copyright (C) 2006 Chris Double. All Rights Reserved.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 IN: furnace:fjsc
5 USING: kernel html furnace xml io httpd sequences 
6        namespaces file-responder parser-combinators lazy-lists
7        fjsc  ;
8
9 : script ( path -- )
10   #! given a path to a javascript file, output the 
11   #! script tag that references it.
12   <script "text/javascript" =type =src script> </script> ;
13
14 : fjsc-page ( scripts title quot -- )
15   #! Display a web page importing the given script
16   #! tags and using the title. The body of the page
17   #! is generated by calling the quotation.
18   -rot xhtml-preamble
19   chars>entities
20   <html " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"" write-html html>
21     <head>
22       <title> write </title>
23       [ script ] each
24     </head>
25     <body>
26      call
27     </body>
28   </html> ;
29   
30 : fjsc-render ( template title -- )
31   #! Render the fjsc page importing the required
32   #! scripts.
33   serving-html {
34     "/responder/fjsc-resources/jquery.js"
35     "/responder/fjsc-resources/bootstrap.js"
36   } swap [
37     [
38       f swap render-template
39     ] fjsc-page
40   ] with-html-stream ;
41
42 : compile ( code -- )
43   #! Compile the facor code as a string, outputting the http
44   #! response containing the javascript.
45   serving-text
46   'expression' parse car parse-result-parsed fjsc-compile 
47    write flush ;
48
49 ! The 'compile' action results in an URL that looks like
50 ! 'responder/fjsc/compile'. It takes one query or post 
51 ! parameter called 'code'. It calls the 'compile' word
52 ! passing the parameter to it on the stack.
53 \ compile { 
54   { "code" v-required } 
55 } define-action
56
57 : repl ( -- )
58   #! The main 'repl' page.
59   f "repl" "Factor to Javascript REPL" fjsc-render ;
60
61 ! An action called 'repl' 
62 \ repl { } define-action
63
64 ! Create the web app, providing access 
65 ! under '/responder/fjsc' which calls the
66 ! 'repl' action.
67 "fjsc" "repl" "apps/furnace-fjsc" web-app
68
69 ! An URL to the javascript resource files used by
70 ! the 'fjsc' responder.
71 "fjsc-resources" [
72  [
73    "libs/fjsc/resources/" resource-path "doc-root" set
74    file-responder
75  ] with-scope
76 ] add-simple-responder