diff options
author | Marius <mariausol@gmail.com> | 2013-10-20 01:20:14 +0300 |
---|---|---|
committer | Marius <mariausol@gmail.com> | 2013-10-20 01:20:14 +0300 |
commit | 965214d981e6129b782c67adcaf3a81aedcb0bac (patch) | |
tree | 84f5945aae8efc9b6eb1898b873be5453cafe43d /tex/context/base/m-database.lua | |
parent | e7d0d90a434e5452ff9e86c8abab5a4cac35e2f1 (diff) | |
download | context-965214d981e6129b782c67adcaf3a81aedcb0bac.tar.gz |
stable 2013.05.28 00:36
Diffstat (limited to 'tex/context/base/m-database.lua')
-rw-r--r-- | tex/context/base/m-database.lua | 81 |
1 files changed, 43 insertions, 38 deletions
diff --git a/tex/context/base/m-database.lua b/tex/context/base/m-database.lua index 91e9636ee..47854daa0 100644 --- a/tex/context/base/m-database.lua +++ b/tex/context/base/m-database.lua @@ -6,22 +6,20 @@ if not modules then modules = { } end modules ['m-database'] = { license = "see context related readme files" } -local sub, gmatch = string.sub, string.gmatch +local sub, gmatch, format = string.sub, string.gmatch, string.format local concat = table.concat local lpegpatterns, lpegmatch, lpegsplitat = lpeg.patterns, lpeg.match, lpeg.splitat -local lpegP, lpegC, lpegS, lpegCt, lpegCc, lpegCs = lpeg.P, lpeg.C, lpeg.S, lpeg.Ct, lpeg.Cc, lpeg.Cs +local lpegP, lpegC, lpegS, lpegCt = lpeg.P, lpeg.C, lpeg.S, lpeg.Ct 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") -local context = context +buffers.database = buffers.database or { } local l_tab = lpegpatterns.tab local l_space = lpegpatterns.space @@ -38,7 +36,7 @@ local separators = { -- not interfaced spaces = l_space^1, } -function moduledata.database.csv.process(settings) +function buffers.database.process(settings) local data if settings.type == "file" then local filename = resolvers.finders.byscheme("any",settings.database) @@ -48,8 +46,6 @@ function moduledata.database.csv.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 @@ -59,7 +55,7 @@ function moduledata.database.csv.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 or "" + local command = settings.command 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) @@ -67,7 +63,7 @@ function moduledata.database.csv.process(settings) local quotedata = nil for chr in gmatch(quotechar,".") do local quotechar = lpegP(chr) - local quoteword = lpegCs(((l_space^0 * quotechar)/"") * (1 - quotechar)^0 * ((quotechar * l_space^0)/"")) + local quoteword = l_space^0 * quotechar * lpegC((1 - quotechar)^0) * quotechar * l_space^0 if quotedata then quotedata = quotedata + quoteword else @@ -77,34 +73,12 @@ function moduledata.database.csv.process(settings) whatever = quotedata + whatever end local checker = commentchar ~= "" and lpegS(commentchar) - 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 splitter = lpegCt(whatever * (separator * whatever)^0) 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() @@ -113,7 +87,39 @@ function moduledata.database.csv.process(settings) context(before) found = true end - context(lpegmatch(whatever,line)) + 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 end end if found then @@ -122,7 +128,6 @@ function moduledata.database.csv.process(settings) context.endgroup() end end - context.popcatcodes() if trace_flush then context.poplogger() end |