summaryrefslogtreecommitdiff
path: root/lib/core/xml.lua
blob: 14f0511f5ca43ae13d80eaf57eb46e960fbde210 (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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
-- The xml module
xml = {}

function xml:parseargs (s)
  local arg = {}
  gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
    %arg[w] = a
  end)
  return arg
end

-- s is a xml stream, returns a table
function xml:collect (s)
  local stack = {n=0}
  local top = {n=0}
  tinsert(stack, top)
  local ni,c,label,args, empty
  local i, j = 1, 1
  while 1 do
    ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>", j)
    if not ni then break end
    local text = strsub(s, i, ni-1)
    if not strfind(text, "^%s*$") then
      tinsert(top, text)
    end
    if empty == "/" then  -- empty element tag
      tinsert(top, {n=0, label=label, args=xml:parseargs(args), empty=1})
    elseif c == "" then   -- start tag
      top = {n=0, label=label, args=xml:parseargs(args)}
      tinsert(stack, top)   -- new level
    else  -- end tag
      local toclose = tremove(stack)  -- remove top
      top = stack[stack.n]
      if stack.n < 1 then
	error("nothing to close with "..label)
      end
      if toclose.label ~= label then
	error("trying to close "..toclose.label.." with "..label)
      end
      tinsert(top, toclose)
    end 
    i = j+1
  end
  local text = strsub(s, i)
  if not strfind(text, "^%s*$") then
    tinsert(stack[stack.n], text)
  end
  if stack.n > 1 then
    error("unclosed "..stack[stack.n].label)
  end
  return stack[1]
end

-- Viewport coordinates
xml.write_out_y = 0
xml.write_out_x = 0
xml.write_out_h = 24
xml.write_out_w = 80

-- Offsets
xml.write_off_y = 0
xml.write_off_x = 0

-- Current position
xml.write_y = 0
xml.write_x = 0

xml.write_screen = function(color, s)
	local i
	for i = 1, strlen(s) do
		local c = strsub(s, i, i + 1)
		if c ~= "\n" then
			if xml.write_y - xml.write_off_y >= 0 and xml.write_y - xml.write_off_y < xml.write_out_h and xml.write_x - xml.write_off_x >= 0 and xml.write_x - xml.write_off_x < xml.write_out_w then
				Term_putch(xml.write_x - xml.write_off_x + xml.write_out_x, xml.write_y - xml.write_off_y + xml.write_out_y, color, strbyte(c))
			end
			xml.write_x = xml.write_x + 1
		else
			xml.write_x = 0
			xml.write_y = xml.write_y + 1
		end
	end
end

xml.write_file = function (color, s)
	print_hook(s)
end

xml.write = xml.write_screen

xml.rule2string = {
    ['name'] = {"Its ", "name", " is"},
    ['contain'] = {"Its ", "name", " contains"},
    ['symbol'] = {"Its ", "symbol", " is"},
    ['inscribed'] = {"Its ", "inscription", " contains"},
    ['state'] = {"Its ", "state", " is"},
    ['status'] = {"Its ", "status", " is"},
    ['tval'] = {"Its ", "tval", " is"},
    ['race'] = {"Your ", "race", " is"},
    ['subrace'] = {"Your ", "subrace", " is"},
    ['class'] = {"Your ", "class", " is"},
    ['foo1'] = {"The result of ", "test 1 ", "is"},
    ['foo2'] = {"The result of ", "test 2 ", "is"},
    ['foo3'] = {"The result of ", "test 3 ", "is"},
}

xml.display_english = 1
function xml:display_xml(t, tab)
    if xml.display_english then
	xml:english_xml(t, tab)
    else
	xml:print_xml(t, tab)
    end
end

function xml:english_xml(t, tab, not_flag)
	local i, k, e
	local pre, post, recurse
	local children_not_flag
	local nextlevel
	local bcol, ecol = TERM_L_GREEN, TERM_GREEN

	if xml.write_active and t == auto_aux.rule then bcol, ecol = TERM_VIOLET, TERM_VIOLET end

	nextlevel = tab .. "    "

	recurse = 1

	if t.label == "rule" then
	    if t.args.type == "inscribe" then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "A rule named \"")
		xml.write(TERM_WHITE, tostring(t.args.name))
		xml.write(ecol, "\" to ")
		xml.write(bcol, "inscribe")
		xml.write(ecol, " an item with \"")
		xml.write(TERM_WHITE, t.args.inscription)
		xml.write(ecol, "\" when")
		xml.write(TERM_WHITE, "\n")
	    else	
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "A rule named \"")
		xml.write(TERM_WHITE, tostring(t.args.name))
		xml.write(ecol, "\" to ")
		xml.write(bcol, t.args.type)
		xml.write(ecol, " when")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "and" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "At least one of the following is false:")
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "All of the following are true:")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "or" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "All of the following are false:")
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "At least one of the following are true:")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "not" then
	    if bcol == TERM_VIOLET or getn(t) == 0 then
		xml.write(ecol, "(a negating rule)")
		xml.write(TERM_WHITE, "\n")
	    else
		nextlevel = tab
	    end
	    children_not_flag = not nil
	elseif t.label == "inventory" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Nothing in your ")
		xml.write(bcol, "inventory")
		xml.write(ecol, " matches the following:")
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Something in your ")
		xml.write(bcol, "inventory")
		xml.write(ecol, " matches the following:")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "equipment" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Nothing in your ")
		xml.write(bcol, "equipment")
		xml.write(ecol, " matches the following:")
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Something in your ")
		xml.write(bcol, "equipment")
		xml.write(ecol, " matches the following:")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "comment" then
		xml.write(TERM_WHITE, tab)
		xml.write(TERM_WHITE, "(" .. t[1] .. ")")
		xml.write(TERM_WHITE, "\n")
	elseif t.label == "skill" then
	    local s = t[1]
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Your skill in ")
		xml.write(bcol, s)
		xml.write(ecol, " is not from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Your skill in ")
		xml.write(bcol, s)
		xml.write(ecol, " is from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "ability" then
	    local s = t[1]
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "You do not have the ")
		xml.write(bcol, s)
		xml.write(ecol, " ability")
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "You have the ")
		xml.write(bcol, s)
		xml.write(ecol, " ability")
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "level" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Your ")
		xml.write(bcol, "level")
		xml.write(ecol, " is not from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Your ")
		xml.write(bcol, "level")
		xml.write(ecol, " is from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "sval" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Its ")
		xml.write(bcol, "sval")
		xml.write(ecol, " is not from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Its ")
		xml.write(bcol, "sval")
		xml.write(ecol, " is from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    end
	elseif t.label == "discount" then
	    if not_flag then
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Its ")
		xml.write(bcol, "discount")
		xml.write(ecol, " is not from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    else
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, "Its ")
		xml.write(bcol, "discount")
		xml.write(ecol, " is from ")
		xml.write(TERM_WHITE, tostring(t.args.min))
		xml.write(ecol, " to ")
		xml.write(TERM_WHITE, tostring(t.args.max))
		xml.write(TERM_WHITE, "\n")
	    end
	else 
	    if xml.rule2string[t.label] then
		local rule = xml.rule2string[t.label]
		a, b, c = rule[1], rule[2], rule[3]
		if not_flag then c = c .. " not" end
		xml.write(TERM_WHITE, tab)
		xml.write(ecol, a) 
		xml.write(bcol, b) 
		xml.write(ecol, c) 
		xml.write(ecol, " \"") 
		xml.write(TERM_WHITE, t[1])
		xml.write(ecol, "\"") 
		xml.write(TERM_WHITE, "\n")
	    else
		if not_flag then
		    xml.write(bcol, "Not:\n")
		    tab = tab .. "    "
		xml:print_xml(t, tab)
		return
		end
	    end
	end

	for i = 1, getn(t) do
		if type(t[i]) == "string" then
			-- xml.write(TERM_WHITE, t[i].."\n")
		else
			xml:english_xml(t[i], nextlevel, children_not_flag)
		end
	end
end

function xml:print_xml(t, tab)
	local i, k, e
	local inside = nil
	local bcol, ecol = TERM_L_GREEN, TERM_GREEN

	if xml.write_active and t == auto_aux.rule then bcol, ecol = TERM_VIOLET, TERM_VIOLET end

	xml.write(bcol, tab.."<"..t.label)
	for k, e in t.args do
		xml.write(TERM_L_BLUE, " "..k)
		xml.write(TERM_WHITE, "=\"")
		xml.write(TERM_YELLOW, e)
		xml.write(TERM_WHITE, "\"")
	end
	xml.write(bcol, ">")

	for i = 1, getn(t) do
		if type(t[i]) == "string" then
			xml.write(TERM_WHITE, t[i])
		else
			if not inside then xml.write(TERM_WHITE, "\n") end
			inside = not nil
			xml:print_xml(t[i], tab.."    ")
		end
	end

	if not inside then
		xml.write(ecol, "</"..t.label..">\n")
	else
		xml.write(ecol, tab.."</"..t.label..">\n")
	end
end

-- t is a table representing xml, outputs the xml code via xml.write()
function xml:output(t)
	local i
	for i = 1, getn(t) do
		xml:print_xml(t[i], "")
	end
end