view web/src/luan/modules/web/web_run.luan @ 321:7f7708e8fdd4

remove import statement git-svn-id: https://luan-java.googlecode.com/svn/trunk@322 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 08 Feb 2015 07:26:20 +0000
parents 5652cdea25f5
children 78a6a71afbfd
line wrap: on
line source

local Luan = require "luan:Luan"
local load = Luan.load
local Io = require "luan:Io"
local print = Io.print
local Http = require "luan:web/Http"
local String = require "luan:String"

local function lines(s)
	local matcher = s.gmatch "([^\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

local function basic_style() %>
	body {font-family:'Arial',sans-serif;font-size:16px;text-align:center;padding:1em 2em}
	input,textarea {padding:.5em;border-radius:10px;border:1px solid #ccc;font-size:16px;display:block}
	textarea {width:50%;margin:0 auto}
	input.btn {background:#3B619D;color:#FFF;padding:.5em 2em;font-size:20px;margin:.5em auto}
	h1 {font-weight:bold;font-size: 20px}
	p {margin:1em 0 .2em}
<% end

local function form() %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Run Luan Code</title>
		<style><% basic_style() %></style>
	</head>
	<body>
		<h1>Run Luan Code</h1>
		<form name="form0" method="post">
			<input type="hidden" name="content_type" value="text/plain" />
			<textarea name="code" rows="20" cols="90" wrap="off"></textarea>
			<input type="submit" class="btn" value="Execute Luan Code"/>
		</form>
		<script>document.form0.code.focus();</script>
	</body>
</html>
<% end

function service()
	Io.stdout = Http.response.text_writer()
	local code = Http.request.parameters.code
	if code == nil then
		form()
		return
	end
	local content_type = Http.request.parameters.content_type
	if content_type ~= nil then
		Http.response.content_type = content_type
	end
	local env = {
		request = Http.request;
		response = Http.response;
	}
	try
		local run = load(code,"<web_run>",env)
		run()
	catch e do
		Http.response.content_type = "text/plain"
		print(e)
		print()
		print()
		print_with_line_numbers(code)
	end
end