changeset 1227:6015434f0e05

mod_register_web: Move HTML into separate template files
author Kim Alvefur <zash@zash.se>
date Sat, 30 Nov 2013 18:57:54 +0100
parents 0667624637da
children db85ff22ae97
files mod_register_web/mod_register_web.lua mod_register_web/templates/recaptcha.html mod_register_web/templates/register.html mod_register_web/templates/success.html
diffstat 4 files changed, 83 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/mod_register_web/mod_register_web.lua	Sat Nov 30 18:25:32 2013 +0100
+++ b/mod_register_web/mod_register_web.lua	Sat Nov 30 18:57:54 2013 +0100
@@ -3,28 +3,36 @@
 local usermanager = require "core.usermanager";
 local http = require "util.http";
 
+function template(data)
+	-- Like util.template, but deals with plain text
+	return { apply = function(values) return (data:gsub("{([^}]+)}", values)); end }
+end
+
+local function get_template(name)
+	local fh = assert(module:load_resource("templates/"..name..".html"));
+	local data = assert(fh:read("*a"));
+	fh:close();
+	return template(data);
+end
+
+local function render(template, data)
+	return tostring(template.apply(data));
+end
+
+local register_tpl = get_template "register";
+local success_tpl = get_template "success";
+local recaptcha_tpl = get_template "recaptcha";
+
 function generate_captcha(display_options)
-	return (([[
-		<script type="text/javascript"
-     		src="https://www.google.com/recaptcha/api/challenge?k=$$recaptcha_public_key$$">
-  		</script>
-  		<noscript>
-     		<iframe src="https://www.google.com/recaptcha/api/noscript?k=$$recaptcha_public_key$$$$recaptcha_display_error$$"
-         		height="300" width="500" frameborder="0"></iframe><br>
-     		<textarea name="recaptcha_challenge_field" rows="3" cols="40">
-     		</textarea>
-     		<input type="hidden" name="recaptcha_response_field"
-         		value="manual_challenge">
-  		</noscript>
-  	]]):gsub("$$([^$]+)$%$", setmetatable({
+	return recaptcha_tpl.apply(setmetatable({
   		recaptcha_display_error = display_options and display_options.recaptcha_error
   			and ("&error="..display_options.recaptcha_error) or "";
   	}, {
   		__index = function (t, k)
   			if captcha_options[k] then return captcha_options[k]; end
   			module:log("error", "Missing parameter from captcha_options: %s", k);
-  		end })
-  	));
+		end
+	}));
 end
 function verify_captcha(form, callback)
 	http.request("https://www.google.com/recaptcha/api/verify", {
@@ -46,27 +54,12 @@
 
 function generate_page(event, display_options)
 	local request = event.request;
-	return [[<!DOCTYPE html>
-	<html><body>
-	<h1>XMPP Account Registration</h1>
-	<form action="]]..request.path..[[" method="POST">]]
-	..("<p>%s</p>\n"):format((display_options or {}).register_error or "")..
-	[[	<table>
-		<tr>
-			<td>Username:</td>
-			<td><input type="text" name="username">@]]..module.host..[[</td>
-		</tr>
-		<tr>
-			<td>Password:</td>
-			<td><input type="password" name="password"></td>
-		</tr>
-		<tr>
-			<td colspan='2'>]]..generate_captcha(display_options)..[[</td>
-		</tr>
-		</table>
-		<input type="submit" value="Register!">
-	</form>
-	</body></html>]];
+
+	return render(register_tpl, {
+		path = request.path; hostname = module.host;
+		notice = display_options and display_options.register_error or "";
+		captcha = generate_captcha(display_options);
+	})
 end
 
 function register_user(form)
@@ -78,10 +71,7 @@
 end
 
 function generate_success(event, form)
-	return [[<!DOCTYPE html>
-	<html><body><p>Registration succeeded! Your account is <pre>]]
-		..form.username.."@"..module.host..
-	[[</pre> - happy chatting!</p></body></html>]];
+	return render(success_tpl, { jid = nodeprep(form.username).."@"..module.host });
 end
 
 function generate_register_response(event, form, ok, err)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_register_web/templates/recaptcha.html	Sat Nov 30 18:57:54 2013 +0100
@@ -0,0 +1,11 @@
+<tr>
+  <td colspan="2">
+    <script src="https://www.google.com/recaptcha/api/challenge?k={recaptcha_public_key}" type="text/javascript"></script>
+    <noscript>
+      <iframe frameborder="0" height="300" src="https://www.google.com/recaptcha/api/noscript?k={recaptcha_public_key}{recaptcha_display_error}" width="500"></iframe>
+      <br>
+      <textarea cols="40" name="recaptcha_challenge_field" rows="3"></textarea>
+      <input name="recaptcha_response_field" type="hidden" value="manual_challenge">
+    </noscript>
+  </td>
+</tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_register_web/templates/register.html	Sat Nov 30 18:57:54 2013 +0100
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>XMPP Account Registration</title>
+  </head>
+  <body>
+    <h1>XMPP Account Registration</h1>
+    <form action="{path}" method="POST">
+      <p>{notice}</p>
+      <table>
+        <tbody>
+          <tr>
+            <th>Username:</th>
+            <td><input name="username" required>@{hostname}</td>
+          </tr>
+          <tr>
+            <th>Password:</th>
+            <td><input name="password" required type="password"></td>
+          </tr>
+          {captcha}
+          <tr>
+            <td colspan="2"><input type="submit" value="Register!"></td>
+          </tr>
+        </tbody>
+      </table>
+    </form>
+  </body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_register_web/templates/success.html	Sat Nov 30 18:57:54 2013 +0100
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Registration succeeded!</title>
+  </head>
+  <body>
+    <h1>Registration succeeded!</h1>
+    <p>Your account is</p>
+    <pre>{jid}</pre>
+    <p>- happy chatting!</p>
+  </body>
+</html>