comparison src/luan/modules/http/run.luan @ 775:1a68fc55a80c

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