diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/run.luan	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,106 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local load = Luan.load or error()
+local try = Luan.try or error()
+local Io = require "luan:Io.luan"
+local print = Io.print or error()
+local String = require "luan:String.luan"
+local gmatch = String.gmatch or error()
+local Http = require "luan:http/Http.luan"
+
+
+local function lines(s)
+	local matcher = gmatch(s,"([^\n]*)\n|([^\n])+$")
+	return function()
+		local m1, m2 = matcher()
+		return m1 or m2
+	end
+end
+
+local function print_with_line_numbers(s)
+	local i = 1
+	for line in lines(s) do
+		print(i,line)
+		i = i + 1
+	end
+end
+
+local function form() %>
+<html>
+	<head>
+		<title>Run Luan Code</title>
+		<style>
+			body {
+				font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+				text-align: center;
+				margin-top: 1em;
+			}
+			h2 {
+				margin-bottom: .3em;
+				font-weight: normal;
+			}
+			textarea {
+				font: inherit;
+				border-radius: 4px;
+				padding: .5em .8em;
+			}
+			input[type="submit"] {
+				margin-top: .3em;
+				color: white;
+				background: #337ab7;
+				border-color: #337ab7;
+				font: inherit;
+				padding: .5em;
+				border-radius: 4px;
+			}
+			input[type="submit"]:hover {
+				background: #236aa7 !important;
+			}
+		</style>
+	</head>
+	<body>
+		<h2>Run Luan Code</h2>
+		<form name="form0" method="post">
+			<input type="hidden" name="content_type" value="text/plain" />
+			<div>
+				<textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
+			</div>
+			<div>
+				<input type="submit" value="Execute Luan Code"/>
+			</div>
+		</form>
+	</body>
+</html>
+<% end
+
+return function()
+	local content_type = Http.request.parameter.content_type
+	if content_type ~= nil then
+		Http.response.header.content_type = content_type
+	end
+	Io.stdout = Http.response.text_writer()
+	local code = Http.request.parameter.code
+	if code == nil then
+		form()
+		return
+	end
+	local env = {
+		request = Http.request;
+		response = Http.response;
+	}
+	try {
+		function()
+			local run = load(code,"<web_run>",env)
+			run()
+		end;
+		catch = function(e)
+			Http.response.reset()
+			Http.response.header.content_type = "text/plain"
+			Io.stdout = Http.response.text_writer()
+			print(e)
+			print""
+			print""
+			print_with_line_numbers(code)
+		end;
+	}
+end