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
|
if not modules then modules = { } end modules ['publ-fnd'] = {
version = 1.001,
comment = "this module part of publication support",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
if not characters then
dofile(resolvers.findfile("char-def.lua"))
dofile(resolvers.findfile("char-utf.lua"))
end
-- this tracker is only for real debugging and not for the average user
local trace_match = false trackers.register("publications.match", function(v) trace_match = v end)
local publications = publications
local tonumber, next, type = tonumber, next, type
local find = string.find
local P, R, S, C, Cs, Cp, Cc, Carg, Ct, V = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cs, lpeg.Cp, lpeg.Cc, lpeg.Carg, lpeg.Ct, lpeg.V
local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
local concat = table.concat
local formatters = string.formatters
local lowercase = characters.lower
local topattern = string.topattern
publications = publications or { } -- for testing
local report = logs.reporter("publications","match")
local colon = P(":")
local dash = P("-")
local lparent = P("(")
local rparent = P(")")
local space = lpegpatterns.whitespace
local utf8char = lpegpatterns.utf8character
local valid = 1 - colon - space - lparent - rparent
----- key = C(valid^1)
local key = C(R("az","AZ")^1)
local wildcard = C("*")
local word = Cs(lpegpatterns.unquoted + lpegpatterns.argument + valid^1)
local simple = C(valid^1)
local number = C(valid^1)
local key = C(R("az","AZ")^1)
local contains = S(":~")
local exact = P("=")
local valid = (1 - space - lparent -rparent)^1
local wildcard = P("*") / ".*"
local single = P("?") / "."
local dash = P("-") / "%."
local percent = P("-") / "%%"
local word = Cs(lpegpatterns.unquoted + lpegpatterns.argument + valid)
local range = P("<") * space^0 * C((1-space)^1) * space^1 * C((1-space- P(">"))^1) * space^0 * P(">")
local f_key_fld = formatters[" local kf_%s = get(entry,%q) \n if kf_%s then kf_%s = lower(kf_%s) end"]
local f_key_set = formatters[" local ks_%s = get(entry,%q,categories)\n if ks_%s then ks_%s = lower(ks_%s) end"]
local f_number_fld = formatters[" local nf_%s = tonumber(get(entry,%q))"]
local f_number_set = formatters[" local ns_%s = tonumber(get(entry,%q,categories))"]
local f_fld_exact = formatters["(kf_%s == %q)"]
local f_set_exact = formatters["(ks_%s == %q)"]
local f_fld_contains = formatters["(kf_%s and find(kf_%s,%q))"]
local f_set_contains = formatters["(ks_%s and find(ks_%s,%q))"]
local f_fld_between = formatters["(nf_%s and nf_%s >= %s and nf_%s <= %s)"]
local f_set_between = formatters["(ns_%s and ns_%s >= %s and ns_%s <= %s)"]
local f_all_match = formatters["anywhere(entry,%q)"]
local function test_key_value(keys,where,key,first,last)
if not key or key == "" then
return "(false)"
elseif key == "*" then
last = "^.*" .. topattern(lowercase(last)) .. ".*$" -- todo: make an lpeg
return f_all_match(last)
elseif first == false then
-- exact
last = lowercase(last)
if where == "set" then
keys[key] = f_key_set(key,key,key,key,key)
return f_set_exact(key,last)
else
keys[key] = f_key_fld(key,key,key,key,key)
return f_fld_exact(key,last)
end
elseif first == true then
-- contains
last = "^.*" .. topattern(lowercase(last)) .. ".*$"
if where == "set" then
keys[key] = f_key_set(key,key,key,key,key)
return f_set_contains(key,key,last)
else
keys[key] = f_key_fld(key,key,key,key,key)
return f_fld_contains(key,key,last)
end
else
-- range
if where == "set" then
keys[key] = f_number_set(key,key)
return f_set_between(key,key,tonumber(first),key,tonumber(last))
else
keys[key] = f_number_fld(key,key)
return f_fld_between(key,key,tonumber(first),key,tonumber(last))
end
end
end
local p_compare = P { "all",
all = (V("one") + V("operator") + V("nested") + C(" "))^1,
nested = C("(") * V("all") * C(")"), -- C really needed?
operator = C("and")
+ C("or")
+ C("not"),
one = Carg(1)
* V("where")
* V("key")
* (V("how") * V("word") + V("range"))
/ test_key_value,
key = key
+ C("*"),
where = C("set") * P(":")
+ Cc(""),
how = contains * Cc(true)
+ exact * Cc(false),
word = word,
range = range,
}
-- local p_combine = space^0 * (P(",")/" or ") * space^0
-- local pattern = Cs((P("match")/"" * space^0 * p_compare + p_combine)^1)
local comma = P(",")
local p_spaces = space^0
local p_combine = p_spaces * comma * p_spaces / " or "
local p_expression = P("match")/"" * Cs(p_compare)
+ Carg(1)
* Cc("")
* Cc("tag")
* Cc(false)
* (
P("tag") * p_spaces * P("(") * Cs((1-S(")")-space)^1) * p_spaces * P(")")
+ p_spaces * Cs((1-space-comma)^1) * p_spaces
) / test_key_value
local pattern = Cs {
[1] = V(2) * (p_combine * V(2))^0,
[2] = p_expression,
}
-- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- --
function publications.anywhere(entry,str) -- helpers
for k, v in next, entry do
if find(lowercase(v),str) then
return true
end
end
end
-- todo: use an environment instead of
-- table={
-- { "match", "((kf_editor and find(kf_editor,\"^.*braslau.*$\")))" },
-- { "hash", "foo1234" },
-- { "tag", "bar5678" },
-- }
local f_template = formatters[ [[
local find = string.find
local lower = characters.lower
local anywhere = publications.anywhere
local get = publications.getfuzzy
local specification = publications.currentspecification
local categories = specification and specification.categories
return function(entry)
%s
return %s and true or false
end
]] ]
local function compile(dataset,expr)
local keys = { }
-- local expression = lpegmatch(pattern,expr,start,keys)
local expression = lpegmatch(pattern,expr,1,keys)
if trace_match then
report("compiling expression: %s",expr)
end
local definitions = { }
for k, v in next, keys do
definitions[#definitions+1] = v
end
if #definitions == 0 then
report("invalid expression: %s",expr)
elseif trace_match then
for i=1,#definitions do
report("% 3i : %s",i,definitions[i])
end
end
definitions = concat(definitions,"\n")
local code = f_template(definitions,expression)
if trace_match then
report("generated code: %s",code)
end
local finder = loadstring(code) -- use an environment
if type(finder) == "function" then
finder = finder()
if type(finder) == "function" then
return finder, code
end
end
report("invalid expression: %s",expr)
return false
end
-- local function test(str)
-- local keys = { }
-- local definitions = { }
-- local expression = lpegmatch(pattern,str,1,keys)
-- for k, v in next, keys do
-- definitions[#definitions+1] = v
-- end
-- definitions = concat(definitions,"\n")
-- print(f_template(definitions,expression))
-- end
-- test("match(foo:bar and (foo:bar or foo:bar))")
-- test("match(foo=bar and (foo=bar or foo=bar))")
-- test("match(set:foo:bar),match(set:foo:bar)")
-- test("match(set:foo=bar)")
-- test("match(foo:{bar bar})")
-- test("match(foo={bar bar})")
-- test("match(set:foo:'bar bar')")
-- test("match(set:foo='bar bar')")
-- test("match(set:foo<1000 2000>)")
-- test("match(set:foo<1000 2000>)")
-- test("match(*:foo)")
-- test("match(*:*)")
local trigger = (P("match") + P("tag")) * p_spaces * P("(")
local check = (1-trigger)^0 * trigger
local function finder(dataset,expression)
local found = lpegmatch(check,expression) and compile(dataset,expression) or false
if found then
local okay, message = pcall(found,{})
if not okay then
found = false
report("error in match: %s",message)
end
end
return found
end
-- finder("match(author:foo)")
-- finder("match(author:foo and author:bar)")
-- finder("match(author:foo or (author:bar and page:123))")
-- finder("match(author:foo),match(author:foo)")
publications.finder = finder
function publications.search(dataset,expression)
local find = finder(dataset,expression)
if find then
local ordered = dataset.ordered
local target = { }
for i=1,#ordered do
local entry = ordered[i]
if find(entry) then
local tag = entry.tag
if not target[tag] then
-- we always take the first
target[tag] = entry
end
end
end
return target
else
return { } -- { dataset.luadata[expression] } -- ?
end
end
-- local d = publications.datasets.default
--
-- local d = publications.load {
-- dataset = "default",
-- filename = "t:/manuals/mkiv/hybrid/tugboat.bib"
-- }
--
-- inspect(publications.search(d,[[match(author:hagen)]]))
-- inspect(publications.search(d,[[match(author:hagen and author:hoekwater and year:1990-2010)]]))
-- inspect(publications.search(d,[[match(author:"Bogusław Jackowski")]]))
-- inspect(publications.search(d,[[match(author:"Bogusław Jackowski" and (tonumber(field:year) or 0) > 2000)]]))
-- inspect(publications.search(d,[[Hagen:TB19-3-304]]))
|