diff web/src/luan/modules/web/web_run.luan @ 170:7c792a328a83

add web component git-svn-id: https://luan-java.googlecode.com/svn/trunk@171 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:20:30 +0000
parents src/luan/modules/web/web_run.luan@9169581dc8fc
children 58c6ca5d4524
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/web_run.luan	Sun Jun 22 05:20:30 2014 +0000
@@ -0,0 +1,40 @@
+import "Http"
+
+local function lines(s)
+	local matcher = String.gmatch(s,"([^\n]*)\n|([^\n])+$")
+	return function()
+		local m1, m2 = matcher()
+		return m1 or m2
+	end
+end
+
+local function print_with_line_numbers(s)
+	i = 1
+	for line in lines(s) do
+		print(i,line)
+		i = i + 1
+	end
+end
+
+function page()
+	local content_type = Http.request.get_parameter("content_type")
+	if content_type ~= nil then
+		Http.response.set_content_type(content_type)
+	end
+	Io.stdout = Http.response.text_writer()
+	local code = Http.request.get_parameter "code"
+	local env = {
+		request = Http.request;
+		response = Http.response;
+	}
+	try
+		local run = load(code,"<web_run>",env)
+		run()
+	catch e do
+		Http.response.set_content_type "text/plain"
+		print(e)
+		print()
+		print()
+		print_with_line_numbers(code)
+	end
+end