summaryrefslogtreecommitdiff
path: root/lib/xmllib.lua
blob: 114dc0844935c5b7831c77bb948756c940f2be06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
local xml_replacements = {
    { "&", "&" },
    { "<", "&lt;" },
    { ">", "&gt;" },
}

local attribute_replacements = {
    { '"', '\\"' },
}

local function generic_escape(str, replacements)
    local _, r
    
    for _, r in pairs(replacements) do
        str = string.gsub(str, r[1], r[2])
    end
    
    return str
end

local function xml_escape(str)
    return generic_escape(str, xml_replacements)
end

local function attribute_escape(str)
    return generic_escape(str, attribute_replacements)
end

local function process_comment(xm, a)
    return {
        istop = true,
        nodes = {
            a
        },
        type = "comment",
    }
end

local function process_lf(xm)
    return {
        istop = true,
        type = "lf",
    }
end

local function burrow(t)
    local k, v
    
    if type(t) ~= "table" then return end
    if type(t.istop) ~= "boolean" then return end
    
    t.istop = nil
end

local function clean_tops(t)
    local k, v
    
    for k, v in pairs(t) do
        if not v.istop then
            t[k] = nil
        end
    end
end

local function process_tag(xm, key, a)
    local ret, attrs, nodes, has_nodes, has_attrs, k, v, b = { istop = true, key = key }, {}, {}, false, false
    
    if type(a) == "table" then
        for k, v in ipairs(a) do
            if type(v) == "table" then
                burrow(v)
                has_nodes = true
                table.insert(nodes, v)
            elseif type(v) == "string" then
                has_nodes = true
                table.insert(nodes, xml_escape(v))
            else
                error "Unsupported argument type."
            end
        end
        for k, v in pairs(a) do
            if (type(k) == "string") then
                has_attrs = true
                attrs[k] = attribute_escape(xml_escape(v))
            end
        end
    elseif type(a) == "string" then
        has_nodes = true
        table.insert(nodes, xml_escape(a))
    end

    if (has_nodes) then ret.nodes = nodes end
    if (has_attrs) then ret.attrs = attrs end

    table.insert(xm.tops, ret)
    
    clean_tops(xm.tops)
    
    return ret
end

local function render_attrs(t)
    local r, k, v = ""
    
    if type(t) ~= "table" then return "" end
    
    for k, v in pairs(t) do
        r = r .. " " .. k .. '="' .. v .. '"'
    end
    
    return r
end

local function do_render_r(v)
    local middle, i, n = ""
    
    if v.type == "comment" then
        return "<!-- " .. v.nodes[1] .. " -->"
    elseif v.type == "lf" then
        return "\n"
    elseif type(v.key) == "string" then
        if type(v.nodes) == "table" then
            for i, n in ipairs(v.nodes) do
                if type(n) == "string" then
                    middle = middle .. n
                elseif type(n) == "table" then
                    middle = middle .. do_render_r(n)
                end
            end
            return "<" .. v.key .. render_attrs(v.attrs) .. ">" .. middle .. "</" .. v.key .. ">"
        else
            return "<" .. v.key .. render_attrs(v.attrs) .. " />"
        end
    else
        error "Inconsistancy in the XML structure."
    end
    
    return ""
end

local function do_render(xm)
    local r, i, n = ""
    
    for i, n in pairs(xm.tops) do
        r = r .. do_render_r(n)
    end

    return xm.doctype .. "\n" .. r
end

local xmlmarkup_metatable = {
    __index = function(xm, key)
        local k = key
        return function(xm, a) return process_tag(xm, k, a) end
    end
}

function NewXmlMarkup()
    local r = {
        comment = process_comment,
        lf = process_lf,
        tops = {},
        render = do_render,
        doctype = '<?xml version="1.0" encoding="UTF-8"?>',
    }
    
    setmetatable(r, xmlmarkup_metatable)
    
    return r
end


--[[ example of use:

-- result is:
-- <html><head><title>Test</title></head><body bgcolor="#ffeedd">Normal test<br /><b>Bold test.</b><!-- Hi there... --></body></html>


xm = NewXmlMarkup()

xm:html {
    xm:head {
        xm:title "Test"
    },
    xm:body {
        bgcolor = "#ffeedd",
    
        "Normal test",
        xm:br(),
        xm:b "Bold test.",
        xm:comment "Hi there...",
    }
}

print(xm:render())

]]--