summaryrefslogtreecommitdiff
path: root/tex/context/base/m-database.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tex/context/base/m-database.lua')
-rw-r--r--tex/context/base/m-database.lua81
1 files changed, 38 insertions, 43 deletions
diff --git a/tex/context/base/m-database.lua b/tex/context/base/m-database.lua
index 47854daa0..91e9636ee 100644
--- a/tex/context/base/m-database.lua
+++ b/tex/context/base/m-database.lua
@@ -6,20 +6,22 @@ if not modules then modules = { } end modules ['m-database'] = {
license = "see context related readme files"
}
-local sub, gmatch, format = string.sub, string.gmatch, string.format
+local sub, gmatch = string.sub, string.gmatch
local concat = table.concat
local lpegpatterns, lpegmatch, lpegsplitat = lpeg.patterns, lpeg.match, lpeg.splitat
-local lpegP, lpegC, lpegS, lpegCt = lpeg.P, lpeg.C, lpeg.S, lpeg.Ct
+local lpegP, lpegC, lpegS, lpegCt, lpegCc, lpegCs = lpeg.P, lpeg.C, lpeg.S, lpeg.Ct, lpeg.Cc, lpeg.Cs
local stripstring = string.strip
+moduledata.database = moduledata.database or { }
+moduledata.database.csv = moduledata.database.csv or { }
+
-- One also needs to enable context.trace, here we only plug in some code (maybe
-- some day this tracker will also toggle the main context tracer.
-local trace_flush = false trackers.register("module.database.flush", function(v) trace_flush = v end)
-
+local trace_flush = false trackers.register("module.database.flush", function(v) trace_flush = v end)
local report_database = logs.reporter("database")
-buffers.database = buffers.database or { }
+local context = context
local l_tab = lpegpatterns.tab
local l_space = lpegpatterns.space
@@ -36,7 +38,7 @@ local separators = { -- not interfaced
spaces = l_space^1,
}
-function buffers.database.process(settings)
+function moduledata.database.csv.process(settings)
local data
if settings.type == "file" then
local filename = resolvers.finders.byscheme("any",settings.database)
@@ -46,6 +48,8 @@ function buffers.database.process(settings)
data = buffers.getlines(settings.database)
end
if data and #data > 0 then
+ local catcodes = tonumber(settings.catcodes) or tex.catcodetable
+ context.pushcatcodes(catcodes)
if trace_flush then
context.pushlogger(report_database)
end
@@ -55,7 +59,7 @@ function buffers.database.process(settings)
local left, right = settings.left or "", settings.right or ""
local setups = settings.setups or ""
local strip = settings.strip == v_yes or false
- local command = settings.command
+ local command = settings.command or ""
separatorchar = (not separatorchar and ",") or separators[separatorchar] or separatorchar
local separator = type(separatorchar) == "string" and lpegS(separatorchar) or separatorchar
local whatever = lpegC((1 - separator)^0)
@@ -63,7 +67,7 @@ function buffers.database.process(settings)
local quotedata = nil
for chr in gmatch(quotechar,".") do
local quotechar = lpegP(chr)
- local quoteword = l_space^0 * quotechar * lpegC((1 - quotechar)^0) * quotechar * l_space^0
+ local quoteword = lpegCs(((l_space^0 * quotechar)/"") * (1 - quotechar)^0 * ((quotechar * l_space^0)/""))
if quotedata then
quotedata = quotedata + quoteword
else
@@ -73,12 +77,34 @@ function buffers.database.process(settings)
whatever = quotedata + whatever
end
local checker = commentchar ~= "" and lpegS(commentchar)
- local splitter = lpegCt(whatever * (separator * whatever)^0)
+ if strip then
+ whatever = whatever / stripstring
+ end
+ if left ~= "" then
+ whatever = lpegCc(left) * whatever
+ end
+ if right ~= "" then
+ whatever = whatever * lpegCc(right)
+ end
+ if command ~= "" then
+ whatever = lpegCc("{") * whatever * lpegCc("}")
+ end
+ whatever = whatever * (separator/"" * whatever)^0
+ if first ~= "" then
+ whatever = lpegCc(first) * whatever
+ end
+ if last ~= "" then
+ whatever = whatever * lpegCc(last)
+ end
+ if command ~= "" then
+ whatever = lpegCs(lpegCc(command) * whatever)
+ else
+ whatever = lpegCs(whatever)
+ end
local found = false
for i=1,#data do
local line = data[i]
if not lpegmatch(l_empty,line) and (not checker or not lpegmatch(checker,line)) then
- local list = lpegmatch(splitter,line)
if not found then
if setups ~= "" then
context.begingroup()
@@ -87,39 +113,7 @@ function buffers.database.process(settings)
context(before)
found = true
end
- if trace_flush then
- local result, r = { }, 0
- r = r + 1 ; result[r] = first
- for j=1,#list do
- local str = strip and stripstring(list[j]) or list[j]
- r = r + 1 ; result[r] = left
- if command == "" then
- r = r + 1 ; result[r] = str
- else
- r = r + 1 ; result[r] = command
- r = r + 1 ; result[r] = "{"
- r = r + 1 ; result[r] = str
- r = r + 1 ; result[r] = "}"
- end
- r = r + 1 ; result[r] = right
- end
- r = r + 1 ; result[r] = last
- context(concat(result))
- else
- context(first)
- for j=1,#list do
- local str = strip and stripstring(list[j]) or list[j]
- context(left)
- if command == "" then
- context(str)
- else
- context(command)
- context(false,str)
- end
- context(right)
- end
- context(last)
- end
+ context(lpegmatch(whatever,line))
end
end
if found then
@@ -128,6 +122,7 @@ function buffers.database.process(settings)
context.endgroup()
end
end
+ context.popcatcodes()
if trace_flush then
context.poplogger()
end