diff options
author | pixel <pixel> | 2007-05-29 09:26:14 +0000 |
---|---|---|
committer | pixel <pixel> | 2007-05-29 09:26:14 +0000 |
commit | e05a7635275b5a217a07c3beb5204e16cfa12ac0 (patch) | |
tree | d3d6b4f15de1d7f8d5abda54a6fae1e18dc4965f | |
parent | 2f80dce604f3eaea8429c104e97ff11e0d121283 (diff) |
Creating basic XmlMarkup object.
-rw-r--r-- | lib/xmllib.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/xmllib.lua b/lib/xmllib.lua new file mode 100644 index 0000000..c3d6358 --- /dev/null +++ b/lib/xmllib.lua @@ -0,0 +1,80 @@ +local function xml_escape(str) + return string.gsub(str, "&", "&") +end + +local function process_comment(table, a) + return "<!-- " .. a .. " -->" +end + +local function process_tag(table, key, a) + local middle, options, options_str, k, v, b + + middle = "" + options = {} + + if type(a) == "table" then + for k, v in ipairs(a) do + middle = middle .. v + end + for k, v in pairs(a) do + if (type(k) == "string") then + options[k] = string.gsub(v, '"', '\\"') + end + end + elseif type(a) == "string" then + middle = xml_escape(a) + end + + if middle ~= "" then + options_str = "" + for k, v in pairs(options) do + if (v == "") then + options_str = " " .. options_str .. k + else + options_str = " " .. options_str .. k .. '="' .. v .. '"' + end + end + return "<" .. key .. options_str .. ">" .. middle .. "</" .. key .. ">" + else + return "<" .. key .. " />" + end +end + +local xmlmarkup_metatable = { + __index = function(table, key) + local k = key + return function(t, a) return process_tag(t, k, a) end + end +} + +function NewXmlMarkup() + local r = { + comment = process_comment, + } + + setmetatable(r, xmlmarkup_metatable) + + return r +end + +--[[ + +xm = NewXmlMarkup() + +s = xm:html { + xm:head { + xm:title "Test" + }, + xm:body { + bgcolor = "#ffeedd", + + "Normal test", + xm:br(), + xm:b "Bold test.", + xm:comment "Hi there...", + } +} + +print(s) + +]]-- |