comparison src/luan/modules/http/tools/Run.luan @ 1246:299996e03876

clean up http/tools
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Jul 2018 15:43:11 -0600
parents src/luan/modules/http/tools/run.luan@2de84f128be3
children 007ceb8dcf89
comparison
equal deleted inserted replaced
1245:2de84f128be3 1246:299996e03876
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local load = Luan.load or error()
4 local try = Luan.try or error()
5 local Io = require "luan:Io.luan"
6 local print = Io.print or error()
7 local String = require "luan:String.luan"
8 local gmatch = String.gmatch or error()
9 local Http = require "luan:http/Http.luan"
10
11
12 local Run = {}
13
14 local function lines(s)
15 local matcher = gmatch(s,"([^\n]*)\n|([^\n])+$")
16 return function()
17 local m1, m2 = matcher()
18 return m1 or m2
19 end
20 end
21
22 local function print_with_line_numbers(s)
23 local i = 1
24 for line in lines(s) do
25 print(i,line)
26 i = i + 1
27 end
28 end
29
30 local function form() %>
31 <!doctype html>
32 <html>
33 <head>
34 <title>Run Luan Code</title>
35 <style>
36 body {
37 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
38 text-align: center;
39 margin-top: 1em;
40 }
41 h2 {
42 margin-bottom: .3em;
43 font-weight: normal;
44 }
45 textarea {
46 font: inherit;
47 border-radius: 4px;
48 padding: .5em .8em;
49 }
50 input[type="submit"] {
51 margin-top: .3em;
52 color: white;
53 background: #337ab7;
54 border-color: #337ab7;
55 font: inherit;
56 padding: .5em;
57 border-radius: 4px;
58 }
59 input[type="submit"]:hover {
60 background: #236aa7 !important;
61 }
62 </style>
63 </head>
64 <body>
65 <h2>Run Luan Code</h2>
66 <form method="post">
67 <input type="hidden" name="content_type" value="text/plain; charset=utf-8" />
68 <div>
69 <textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
70 </div>
71 <div>
72 <input type="submit" value="Execute Luan Code"/>
73 </div>
74 </form>
75 </body>
76 </html>
77 <% end
78
79 function Run.respond()
80 local content_type = Http.request.parameters.content_type
81 if content_type ~= nil then
82 Http.response.headers["content-type"] = content_type
83 end
84 Io.stdout = Http.response.text_writer()
85 local code = Http.request.parameters.code
86 if code == nil then
87 form()
88 return
89 end
90 try {
91 function()
92 local run = load(code,"<web_run>")
93 run()
94 end;
95 catch = function(e)
96 Http.response.reset()
97 Http.response.headers["content-type"] = "text/plain; charset=utf-8"
98 Io.stdout = Http.response.text_writer()
99 print(e)
100 print""
101 print""
102 print_with_line_numbers(code)
103 end;
104 }
105 end
106
107 return Run