1if not modules then modules = { } end modules ['lxml-mis'] = {
2 version = 1.001,
3 comment = "this module is the basis for the lxml-* ones",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9local xml, lpeg, string = xml, lpeg, string
10
11local type = type
12local concat = table.concat
13local format, gsub, match = string.format, string.gsub, string.match
14local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
15local P, S, R, C, V, Cc, Cs = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.V, lpeg.Cc, lpeg.Cs
16
17lpegpatterns.xml = lpegpatterns.xml or { }
18local xmlpatterns = lpegpatterns.xml
19
20
21
22
23
24
25local function xmlgsub(t,old,new)
26 local dt = t.dt
27 if dt then
28 for k=1,#dt do
29 local v = dt[k]
30 if type(v) == "string" then
31 dt[k] = gsub(v,old,new)
32 else
33 xmlgsub(v,old,new)
34 end
35 end
36 end
37end
38
39
40
41function xml.stripleadingspaces(dk,d,k)
42 if d and k then
43 local dkm = d[k-1]
44 if dkm and type(dkm) == "string" then
45 local s = match(dkm,"\n(%s+)")
46 xmlgsub(dk,"\n"..rep(" ",#s),"\n")
47 end
48 end
49end
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68local normal = (1 - S("<&>"))^0
69local special = P("<")/"<" + P(">")/">" + P("&")/"&"
70local escaped = Cs(normal * (special * normal)^0)
71
72
73
74local normal = (1 - S"&")^0
75local special = P("<")/"<" + P(">")/">" + P("&")/"&"
76local unescaped = Cs(normal * (special * normal)^0)
77
78
79
80local cleansed = Cs(((P("<") * (1-P(">"))^0 * P(">"))/"" + 1)^0)
81
82xmlpatterns.escaped = escaped
83xmlpatterns.unescaped = unescaped
84xmlpatterns.cleansed = cleansed
85
86function xml.escaped (str) return lpegmatch(escaped,str) end
87function xml.unescaped(str) return lpegmatch(unescaped,str) end
88function xml.cleansed (str) return lpegmatch(cleansed,str) end
89
90
91
92function xml.fillin(root,pattern,str,check)
93 local e = xml.first(root,pattern)
94 if e then
95 local n = #e.dt
96 if not check or n == 0 or (n == 1 and e.dt[1] == "") then
97 e.dt = { str }
98 end
99 end
100end
101 |