util.dataforms: Fixed to actually work, mostly

Sat, 04 Apr 2009 15:32:32 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 04 Apr 2009 15:32:32 +0100
changeset 951
4b9207949735
parent 950
a00b4269de70
child 952
ef648f49e734

util.dataforms: Fixed to actually work, mostly

util/dataforms.lua file | annotate | diff | comparison | revisions
--- a/util/dataforms.lua	Fri Apr 03 01:29:59 2009 +0100
+++ b/util/dataforms.lua	Sat Apr 04 15:32:32 2009 +0100
@@ -1,3 +1,6 @@
+local setmetatable = setmetatable;
+local pairs, ipairs = pairs, ipairs;
+local st = require "util.stanza";
 
 module "dataforms"
 
@@ -13,11 +16,17 @@
 local form_x_attr = { xmlns = xmlns_forms };
 
 function form_t.form(layout, data)
-	local form = st.tag("x", form_x_attr);
+	local form = st.stanza("x", form_x_attr);
+	if layout.title then
+		form:tag("title"):text(layout.title):up();
+	end
+	if layout.instructions then
+		form:tag("instructions"):text(layout.instructions):up();
+	end
 	for n, field in ipairs(layout) do
-		local field_type = field.type;
+		local field_type = field.type or "text-single";
 		-- Add field tag
-		form:tag("field", { type = field_type, var = field.name });
+		form:tag("field", { type = field_type, var = field.name, label = field.label });
 
 		local value = data[field.name];
 		
@@ -30,7 +39,7 @@
 				form:text(tostring(value));
 			end
 		elseif field_type == "boolean" then
-			form:text((value and "1") or "0");
+			form:tag("value"):text((value and "1") or "0");
 		elseif field_type == "fixed" then
 			
 		elseif field_type == "jid-multi" then
@@ -39,7 +48,13 @@
 			end
 		elseif field_type == "jid-single" then
 			form:tag("value"):text(value):up();
-			
+		elseif field_type == "text-single" or field_type == "text-private" then
+			form:tag("value"):text(value):up();
+		elseif field_type == "text-multi" then
+			-- Split into multiple <value> tags, one for each line
+			for line in value:gmatch("([^\r\n]+)\r?\n*") do
+				form:tag("value"):text(line):up();
+			end
 		end
 		
 		-- Jump back up to list of fields
@@ -52,9 +67,10 @@
 	
 end
 
+return _M;
 
 
---[[
+--[=[
 
 Layout:
 {
@@ -67,4 +83,4 @@
 }
 
 
---]]
+--]=]

mercurial