diff options
author | Kim Alvefur <zash@zash.se> | 2020-10-28 23:15:52 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2020-10-28 23:15:52 +0100 |
commit | b7ecfa2c96dfe7f2d7708631d9e358ec5b257a94 (patch) | |
tree | ca0bacf107decc765a88e286aeaf8a68ed641fa9 /tools/form2table.lua | |
parent | 0219adfe3e83d6916a038e4b80b9754e37dcdac3 (diff) | |
download | prosody-b7ecfa2c96dfe7f2d7708631d9e358ec5b257a94.tar.gz prosody-b7ecfa2c96dfe7f2d7708631d9e358ec5b257a94.zip |
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
Diffstat (limited to 'tools/form2table.lua')
-rw-r--r-- | tools/form2table.lua | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tools/form2table.lua b/tools/form2table.lua new file mode 100644 index 00000000..49a6972b --- /dev/null +++ b/tools/form2table.lua @@ -0,0 +1,48 @@ +-- Read an XML dataform and spit out a serialized Lua table of it + +local function from_stanza(stanza) + local layout = { + title = stanza:get_child_text("title"); + instructions = stanza:get_child_text("instructions"); + }; + for tag in stanza:childtags("field") do + local field = { + name = tag.attr.var; + type = tag.attr.type; + label = tag.attr.label; + desc = tag:get_child_text("desc"); + required = tag:get_child("required") and true or nil; + value = tag:get_child_text("value"); + options = nil; + }; + + if field.type == "list-single" or field.type == "list-multi" then + local options = {}; + for option in tag:childtags("option") do + options[#options+1] = { label = option.attr.label, value = option:get_child_text("value") }; + end + field.options = options; + end + + if field.type == "jid-multi" or field.type == "list-multi" or field.type == "text-multi" then + local values = {}; + for value in tag:childtags("value") do + values[#values+1] = value:get_text(); + end + if field.type == "text-multi" then + values = table.concat(values, "\n"); + end + field.value = values; + end + + if field.type == "boolean" then + field.value = field.value == "true" or field.value == "1"; + end + + layout[#layout+1] = field; + + end + return layout; +end + +print("dataforms.new " .. require "util.serialization".serialize(from_stanza(require "util.xml".parse(io.read("*a"))), { unquoted = true })) |