changeset 325:78a6a71afbfd

use SimplyHTML git-svn-id: https://luan-java.googlecode.com/svn/trunk@326 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 03 Mar 2015 06:00:59 +0000
parents b24a35612947
children db37d6aee4db
files core/src/luan/modules/Html.luan lucene/src/luan/modules/lucene/Ab_testing.luan lucene/src/luan/modules/lucene/Web_search.luan web/src/luan/modules/web/HttpServicer.java web/src/luan/modules/web/web_run.luan web/src/luan/modules/web/web_shell.luan
diffstat 6 files changed, 178 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/Html.luan	Mon Feb 09 23:15:42 2015 +0000
+++ b/core/src/luan/modules/Html.luan	Tue Mar 03 06:00:59 2015 +0000
@@ -12,6 +12,7 @@
 local Luan = require "luan:Luan"
 local ipairs = Luan.ipairs
 local type = Luan.type
+local Io = require "luan:Io"
 
 function process_url_tags(html)
 	for i, v in ipairs(html) do
@@ -35,3 +36,31 @@
 		end
 	end
 end
+
+
+function simple_html_page(parts)
+	local head = parts.head
+%>
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, initial-scale=1">
+
+		<% head and head() %>
+		
+		<link href="http://www.simplyhtml.org/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet">
+		<link rel="stylesheet" href="http://www.simplyhtml.org/assets/font-awesome/css/font-awesome.min.css">
+		<script src="http://www.simplyhtml.org/assets/jquery/jquery.min.js"></script>
+
+		<link href="http://www.simplyhtml.org/assets/simplyhtml/simplyhtml.css" rel="stylesheet"/>
+		<script src="http://www.simplyhtml.org/assets/simplyhtml/simplyhtml.js"></script>
+	</head>
+	<body>
+		<% parts.body() %>
+		
+		<script src="http://www.simplyhtml.org/assets/bootstrap/js/bootstrap.min.js"></script>
+	</body>
+</html>
+<%
+end
--- a/lucene/src/luan/modules/lucene/Ab_testing.luan	Mon Feb 09 23:15:42 2015 +0000
+++ b/lucene/src/luan/modules/lucene/Ab_testing.luan	Tue Mar 03 06:00:59 2015 +0000
@@ -188,6 +188,7 @@
 	end
 end
 
+-- I will change this to use SimplyHTML when this is used again.
 local function basic_style() %>
 	body {font-family:'Arial',sans-serif;font-size:16px;padding:1em 2em}
 	h1 {font-weight:bold;font-size:20px}
--- a/lucene/src/luan/modules/lucene/Web_search.luan	Mon Feb 09 23:15:42 2015 +0000
+++ b/lucene/src/luan/modules/lucene/Web_search.luan	Tue Mar 03 06:00:59 2015 +0000
@@ -7,78 +7,98 @@
 local Io = require "luan:Io"
 local Http = require "luan:web/Http"
 local String = require "luan:String"
+local Html = require "luan:Html"
 
 
-local function basic_style() %>
-	body {font-family:'Arial',sans-serif;font-size:16px;padding:1em 2em}
-	input {padding:.5em;border-radius:10px;border:1px solid #ccc;font-size:16px}
-	input.btn {background:#3B619D;color:#FFF;padding:.5em 2em;font-size:20px}
-	h1 {font-weight:bold;font-size: 20px}
-	p {margin:1em 0 .2em}
-	span.label {min-width:100px;display:inline-block;text-align:right}
-	div.tip{color:#888;font-size:80%}
-	table.results {margin-top:2em;border-collapse:collapse;font-size:90%}
-	table.results th {background:#eee}
-	table.results th,table.results td {border-left:1px solid #bbb;padding:.4em}
-	table.results tr:nth-child(odd) td {background:#f8f8f8}
-<% end
-
-local function form() %>
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<title>Lucene Query</title>
-		<style><% basic_style() %></style>
-	</head>
-	<body>
-		<h1>Lucene Query</h1>
-		<form name="form0" method="post">
-			<p>
-				<span class="label">Query:</span> <input name="query" size="60" value="Query.all_docs" />
-				<div class="tip"><span class="label"></span> Query examples: <i>Query.term{ type = 'user' }</i> or <i>"type:user AND name:Joe"</i></div>
-			</p>
-			<p><span class="label">Max Rows:</span> <input name="rows" value="100" maxlength="5" onkeypress="return event.charCode >= 48 && event.charCode <= 57" style="width:3em"/></p>
-			<p>
-				<span class="label">Sort:</span> <input name="sort" size="60" />
-				<div class="tip"><span class="label"></span> Sort examples: Query.sort{{ field = 'id', type='int' }}</div>
-			</p>
-			<p><input type="submit" class="btn"/></p>
-		</form>
-		<script>document.form0.query.focus();</script>
-	</body>
-</html>
-<% end
+local function form()
+	Html.simple_html_page{
+		head = function() %>
+			<title>Lucene Query</title>
+<%		end;
+		body = function() %>
+			<div container>
+				<h3 margin-top="1.5em">Lucene Query</h3>
+				<form horizontal name="form0" method="post" margin-top="2em">
+					<div row>
+						<div colspan=2 align="right">
+							<label>Query:</label>
+						</div>
+						<div colspan=10>
+							<input name="query" size="80" value="Query.all_docs" autofocus />
+							<div textcolor="#888">Query examples: <i>Query.term{ type = 'user' }</i> or <i>"type:user AND name:Joe"</i></div>
+						</div>
+					</div>
+					<div row margin-top="1em">
+						<div colspan=2 align="right">
+							<label>Max Rows:</label>
+						</div>
+						<div colspan=10>
+							<input name="rows" value="100" size="3" maxlength="5" /></p>
+						</div>
+					</div>
+					<div row margin-top="1em">
+						<div colspan=2 align="right">
+							<label>Sort:</label>
+						</div>
+						<div colspan=10>
+							<input name="sort" size="60" />
+							<div textcolor="#888">Sort examples: Query.sort{{ field = 'id', type='int' }}</div>
+						</div>
+					</div>
+					<div row margin-top="1em">
+						<div colspan=2></div>
+						<div colspan=10>
+							<input type="submit" textcolor="white" bgcolor="#337ab7" large/>
+						</div>
+					</div>
+				</form>
+			</div>
+<%		end;
+	}
+end
 
 
-local function result(query,sort,headers,table) %>
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<style><% basic_style() %></style>
-	</head>
-	<body>
-		<h1>Lucene Query Results</h1>
-			<p><span class="label">Query:</span> <b><%=query%></b></p>
-			<p><span class="label">Sort:</span> <b><%=sort%></b></p>
-			<table class="results">
-				<tr>
-					<th></th>
-					<% for _, header in ipairs(headers) do %>
-						<th><%=header%></th>
-					<% end %>
-				</tr>
-				<% for i, row in ipairs(table) do %>
+local function result(query,sort,headers,table)
+	Html.simple_html_page{
+		body = function() %>
+			<div container>
+				<h3 margin-top="1.5em">Lucene Query Results</h3>
+				<div row>
+					<div colspan=2 align="right">
+						<label>Query:</label>
+					</div>
+					<div colspan=10>
+						<b><%=query%></b></p>
+					</div>
+				</div>
+				<div row>
+					<div colspan=2 align="right">
+						<label>Sort:</label>
+					</div>
+					<div colspan=10>
+						<b><%=sort%></b></p>
+					</div>
+				</div>
+				<table border condensed margin-top="1.5em">
 					<tr>
-						<td><%=i%></td>
-						<% for col in range(1, #headers) do %>
-							<td><%= row[col] or "" %></td>
+						<th></th>
+						<% for _, header in ipairs(headers) do %>
+							<th><%=header%></th>
 						<% end %>
 					</tr>
-				<% end %>
-			</table>
-	</body>
-</html>
-<% end
+					<% for i, row in ipairs(table) do %>
+						<tr>
+							<td><%=i%></td>
+							<% for col in range(1, #headers) do %>
+								<td><%= row[col] or "" %></td>
+							<% end %>
+						</tr>
+					<% end %>
+				</table>
+			</div>
+<%		end;
+	}
+end
 
 
 local function index_of(tbl,val)
--- a/web/src/luan/modules/web/HttpServicer.java	Mon Feb 09 23:15:42 2015 +0000
+++ b/web/src/luan/modules/web/HttpServicer.java	Tue Mar 03 06:00:59 2015 +0000
@@ -53,11 +53,9 @@
 					session.putValue("luan",luan);
 				}
 				tbl = (LuanTable)PackageLuan.require(luan,modName);
-				fn = (LuanFunction)tbl.get("service");
+				fn = getService(luan,tbl);
 			} else {
-				fn = (LuanFunction)tbl.get("service");
-				if( fn == null )
-					throw luan.exception( "function 'service' is not defined" );
+				fn = getService(luan,tbl);
 				DeepCloner cloner = new DeepCloner();
 				luan = cloner.deepClone(luan);
 				fn = cloner.get(fn);
@@ -89,6 +87,16 @@
 		return true;
 	}
 
+	private static LuanFunction getService(LuanState luan,LuanTable tbl)
+		throws LuanException
+	{
+		Object service = tbl.get("service");
+		if( service == null )
+			throw luan.exception( "function 'service' is not defined" );
+		if( !(service instanceof LuanFunction) )
+			throw luan.exception( "'service' must be a function but is a " + Luan.type(service) );
+		return (LuanFunction)service;
+	}
 
 
 	private final HttpServletRequest request;
--- a/web/src/luan/modules/web/web_run.luan	Mon Feb 09 23:15:42 2015 +0000
+++ b/web/src/luan/modules/web/web_run.luan	Tue Mar 03 06:00:59 2015 +0000
@@ -4,6 +4,7 @@
 local print = Io.print
 local Http = require "luan:web/Http"
 local String = require "luan:String"
+local Html = require "luan:Html"
 
 local function lines(s)
 	local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
@@ -21,33 +22,27 @@
 	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
+local function form()
+	Html.simple_html_page{
+		head = function() %>
+			<title>Run Luan Code</title>
+<%		end;
+		body = function() %>
+			<center margin-top=10>
+				<h3>Run Luan Code</h3>
+			</center>
+			<form name="form0" method="post">
+				<input type="hidden" name="content_type" value="text/plain" />
+				<center>
+					<textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
+				</center>
+				<center margin-top=5>
+					<input type="submit" value="Execute Luan Code" textcolor="white" bgcolor="#337ab7"/>
+				</center>
+			</form>
+<%		end;
+	}
+end
 
 function service()
 	Io.stdout = Http.response.text_writer()
--- a/web/src/luan/modules/web/web_shell.luan	Mon Feb 09 23:15:42 2015 +0000
+++ b/web/src/luan/modules/web/web_shell.luan	Tue Mar 03 06:00:59 2015 +0000
@@ -5,25 +5,25 @@
 local print = Io.print
 local Debug = require "luan:Debug"
 local Http = require "luan:web/Http"
+local Html = require "luan:Html"
 
 per_session = true
 
 local history = {}
 local env = {}
 
-Io.stdout = {}
-function Io.stdout.write(...)
-	for _,v in Luan.values(...) do
-		history[#history+1] = v
-	end
-end
-
 function service()
 	if Http.request.parameters.clear ~= nil then
 		history = {}
 	else
 		local cmd = Http.request.parameters.cmd
 		if cmd ~= nil then
+			Io.stdout = {}
+			function Io.stdout.write(...)
+				for _,v in Luan.values(...) do
+					history[#history+1] = v
+				end
+			end
 			print( "% "..cmd )
 			try
 				local line = load(cmd,"<web_shell>",env,true)
@@ -35,37 +35,27 @@
 		end
 	end
 
-	local write = Http.response.text_writer().write
-	write(%>
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<title>Luan Shell</title>
-		<style>
-			body {font-family:'Arial',sans-serif;font-size:16px;padding:1em 2em}
-			input {padding:.5em;border-radius:10px;border:1px solid #ccc;font-size:16px}
-			input.btn {background:#3B619D;color:#FFF;padding:.3em 0;font-size:20px;min-width:4em;}
-			h1 {font-weight:bold;font-size: 20px}
-			p {margin:1em 0 .2em}
-		</style>
-	</head>
-	<body>
-		<h1>Luan Shell</h1>
-		<p>This is a command shell.  Enter commands below.</p>
-		<pre><%)
-		for _,v in ipairs(history) do
-			write(v)
-		end
-		write(%></pre>
-		<form name='form0' method='post'>
-			% <input name='cmd' size="60">
-			<input type="submit" class="btn" value="run">
-			<input type="submit" class="btn" name="clear" value="clear">
-		</form>
-
-		<script>document.form0.cmd.focus();</script>
-	</body>
-</html>
-<%)
+	Io.stdout = Http.response.text_writer()
+	Html.simple_html_page{
+		head = function() %>
+			<title>Luan Shell</title>
+<%		end;
+		body = function() %>
+			<div container>
+				<h3>Luan Shell</h3>
+				<p>This is a command shell.  Enter commands below.</p>
+				<pre><%
+				for _,v in ipairs(history) do
+					Io.stdout.write(v)
+				end
+				%></pre>
+				<form name='form0' method='post'>
+					% <input name='cmd' size="80" autofocus>
+					<input type="submit" value="run" textcolor="white" bgcolor="#337ab7">
+					<input type="submit" name="clear" value="clear" textcolor="white" bgcolor="#337ab7">
+				</form>
+			</div>
+<%		end;
+	}
 
 end