-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcairoxml2cairo.lua
executable file
·223 lines (197 loc) · 6.4 KB
/
cairoxml2cairo.lua
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env lua
--[[
Copyright (c) 2010-2014 Andreas Krinke
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--]]
local formats = "c lua-oocairo scrupp"
-- default export filter
local format = "lua-oocairo"
local infile, outfile
-- collect command line argument
if #arg == 2 then
infile = arg[1]
outfile = arg[2]
elseif #arg == 4 and arg[1] == "-f" and string.find(formats, arg[2], 1, true) then
format = arg[2]
infile = arg[3]
outfile = arg[4]
else
print([[
Usage: lua cairoxml2cairo.lua [-f format] xml-file source-file
Available formats: ]] .. formats)
os.exit()
end
-- try to load the source code format definitions
local success, replacements = pcall(require, "formats." .. format)
if not success then
print(string.format([[
Error loading format %s:
%s
Is %s.lua missing in directory 'formats'?]], format, replacements, format))
os.exit()
end
-- list of tags that start/end a gradient definition
local gradient_tags = {
solid = true,
linear = true,
radial = true
}
-- list of tags that start/end a pattern definition
local pattern_tags = {
["source-pattern"] = true,
["mask-pattern"] = true
}
-- stores the current state of cairo
local state = {}
-----------------------------------------------------------------------------
-- Function Definitions
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Simple XML Parser
-- source: http://lua-users.org/wiki/LuaXml
local function parseargs(s)
local arg = {}
string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
local function collect(s)
local stack = {}
local top = {}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w%-:]+)(.-)(%/?)>", i)
if not ni then break end
local text = string.sub(s, i, ni-1)
if not string.find(text, "^%s*$") then
table.insert(top, text)
end
if empty == "/" then -- empty element tag
table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
elseif c == "" then -- start tag
top = {label=label, xarg=parseargs(xarg)}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
if #stack < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
table.insert(top, toclose)
end
i = j+1
end
local text = string.sub(s, i)
if not string.find(text, "^%s*$") then
table.insert(stack[#stack], text)
end
if #stack > 1 then
error("unclosed "..stack[stack.n].label)
end
return stack[1]
end
-- End Simple XML Parser
-----------------------------------------------------------------------------
--- Extracts the basename (without suffix) from a path
-- @patam path Path to extract the basename from.
-- @return basename of the file
function basename(path)
local file = path:match("([^\\/:]*)$")
local basename, ext = file:match("^%.*(.+)%.([^%.]-)$")
if not basename then
basename = file:match("^%.*(.*)$")
end
return (basename:gsub("[%.-]", "_"))
end
--- Outputs the source code corresponding to the current tag.
-- For each start and end tag, different source code can be defined.
-- @param fh Output filehandle.
-- @param basename Basename of the output file.
-- @param kind Either "pre" (start tag was parsed)
-- or "post" (stop tag was parsed).
-- @param t Table containing information about the current tag.
local function output(fh, basename, kind, t)
local label = t.label
if label == "svg" then
error("svg tag detected\nPlease provide a cairo xml file, not an svg file.")
end
local rep = replacements[label]
if not rep then
error("not supported tag: " .. label)
end
local s
if kind == "pre" then
if type(rep) == "table" then
rep = rep.pre
end
else
if type(rep) == "table" then
rep = rep.post
else
rep = nil
end
end
if type(rep) == "string" then
s = rep
elseif type(rep) == "function" then
s = rep(state, t[1])
end
if s then
s = string.gsub(s, "$basename", basename)
s = string.gsub(s, "$value", t[1])
s = string.gsub(s, "$(%w+)", t.xarg)
fh:write(s, "\n")
end
if kind == "post" then
if label == "surface" then
state.last_environment = "surface"
elseif gradient_tags[label] then
state.last_environment = "gradient"
elseif pattern_tags[label] then
state.last_environment = nil
end
end
end
--- Processes the table generated by the XML parser.
-- @param fh Output filehandle.
-- @param t Table generated by collect().
-- @see collect.
local function process(fh, basename, t)
for _,child in ipairs(t) do
if type(child) == "table" then
output(fh, basename, "pre", child)
process(fh, basename, child)
output(fh, basename, "post", child)
end
end
end
-----------------------------------------------------------------------------
-- Main
-----------------------------------------------------------------------------
-- open the input and output file
local fh_in = assert(io.open(infile))
local fh_out = assert(io.open(outfile, "w"))
-- read and parse the whole xml file
local t = collect(fh_in:read("*a"))
-- process the resulting table
process(fh_out, basename(outfile), t)