Comparison

tools/form2table.lua @ 11194:9d1ce6f28401

tools/form2table: Convert XEP-0004 dataform from XML to util.dataforms Lua format Used this to generate code for a number of PubSub forms IIRC
author Kim Alvefur <zash@zash.se>
date Wed, 28 Oct 2020 23:15:52 +0100
child 13142:879a6a33c21b
comparison
equal deleted inserted replaced
11193:5850d24a4ad3 11194:9d1ce6f28401
1 -- Read an XML dataform and spit out a serialized Lua table of it
2
3 local function from_stanza(stanza)
4 local layout = {
5 title = stanza:get_child_text("title");
6 instructions = stanza:get_child_text("instructions");
7 };
8 for tag in stanza:childtags("field") do
9 local field = {
10 name = tag.attr.var;
11 type = tag.attr.type;
12 label = tag.attr.label;
13 desc = tag:get_child_text("desc");
14 required = tag:get_child("required") and true or nil;
15 value = tag:get_child_text("value");
16 options = nil;
17 };
18
19 if field.type == "list-single" or field.type == "list-multi" then
20 local options = {};
21 for option in tag:childtags("option") do
22 options[#options+1] = { label = option.attr.label, value = option:get_child_text("value") };
23 end
24 field.options = options;
25 end
26
27 if field.type == "jid-multi" or field.type == "list-multi" or field.type == "text-multi" then
28 local values = {};
29 for value in tag:childtags("value") do
30 values[#values+1] = value:get_text();
31 end
32 if field.type == "text-multi" then
33 values = table.concat(values, "\n");
34 end
35 field.value = values;
36 end
37
38 if field.type == "boolean" then
39 field.value = field.value == "true" or field.value == "1";
40 end
41
42 layout[#layout+1] = field;
43
44 end
45 return layout;
46 end
47
48 print("dataforms.new " .. require "util.serialization".serialize(from_stanza(require "util.xml".parse(io.read("*a"))), { unquoted = true }))