comparison web/src/luan/modules/web/run.luan @ 347:612a283b3d14

improve luan/web file names and add serve.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 Apr 2015 07:17:40 -0600
parents web/src/luan/modules/web/web_run.luan@fb18724521d2
children 2f5cc9c2cbf0
comparison
equal deleted inserted replaced
346:dc21bd260690 347:612a283b3d14
1 local Luan = require "luan:Luan"
2 local load = Luan.load
3 local try = Luan.try
4 local Io = require "luan:Io"
5 local print = Io.print
6 local Http = require "luan:web/Http"
7 local String = require "luan:String"
8 local Html = require "luan:Html"
9
10 local function lines(s)
11 local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
12 return function()
13 local m1, m2 = matcher()
14 return m1 or m2
15 end
16 end
17
18 local function print_with_line_numbers(s)
19 i = 1
20 for line in lines(s) do
21 print(i,line)
22 i = i + 1
23 end
24 end
25
26 local function form()
27 Html.simply_html_page{
28 head = function() %>
29 <title>Run Luan Code</title>
30 <% end;
31 body = function() %>
32 <center margin-top=10>
33 <h3>Run Luan Code</h3>
34 </center>
35 <form name="form0" method="post">
36 <input type="hidden" name="content_type" value="text/plain" />
37 <center>
38 <textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
39 </center>
40 <center margin-top=5>
41 <input type="submit" value="Execute Luan Code" textcolor="white" bgcolor="#337ab7"/>
42 </center>
43 </form>
44 <% end;
45 }
46 end
47
48 function service()
49 Io.stdout = Http.response.text_writer()
50 local code = Http.request.parameters.code
51 if code == nil then
52 form()
53 return
54 end
55 local content_type = Http.request.parameters.content_type
56 if content_type ~= nil then
57 Http.response.content_type = content_type
58 end
59 local env = {
60 request = Http.request;
61 response = Http.response;
62 }
63 try {
64 function()
65 local run = load(code,"<web_run>",env)
66 run()
67 end;
68 catch = function(e)
69 Http.response.content_type = "text/plain"
70 print(e)
71 print()
72 print()
73 print_with_line_numbers(code)
74 end;
75 }
76 end