summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/bibl-bib.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/bibl-bib.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/bibl-bib.lua115
1 files changed, 60 insertions, 55 deletions
diff --git a/Master/texmf-dist/tex/context/base/bibl-bib.lua b/Master/texmf-dist/tex/context/base/bibl-bib.lua
index 3c0dad2fa4e..e0e5a6c3a42 100644
--- a/Master/texmf-dist/tex/context/base/bibl-bib.lua
+++ b/Master/texmf-dist/tex/context/base/bibl-bib.lua
@@ -16,20 +16,28 @@ local utf = unicode.utf8
local lower, format, gsub, concat = string.lower, string.format, string.gsub, table.concat
local next = next
local utfchar = utf.char
-local lpegmatch = lpeg.match
+local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
local textoutf = characters and characters.tex.toutf
local variables = interfaces and interfaces.variables
-
+local settings_to_hash = utilities.parsers.settings_to_hash
local finalizers = xml.finalizers.tex
-local xmlfilter, xmltext = xml.filter, xml.text
+local xmlfilter, xmltext, getid = xml.filter, xml.text, lxml.getid
+
+local P, R, S, C, Cc, Cs, Ct = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.Ct
local trace_bibxml = false trackers.register("publications.bibxml", function(v) trace_bibtex = v end)
-bibtex = bibtex or { }
+local report_xml = logs.reporter("publications","xml")
-bibtex.size = 0
-bibtex.definitions = 0
-bibtex.shortcuts = 0
+bibtex = bibtex or { }
+local bibtex = bibtex
+
+bibtex.statistics = bibtex.statistics or { }
+local bibtexstats = bibtex.statistics
+
+bibtexstats.nofbytes = 0
+bibtexstats.nofdefinitions = 0
+bibtexstats.nofshortcuts = 0
local defaultshortcuts = {
jan = "1",
@@ -55,7 +63,7 @@ local entries
-- hashed again.
local function do_shortcut(tag,key,value)
- bibtex.shortcuts = bibtex.shortcuts + 1
+ bibtexstats.nofshortcuts = bibtexstats.nofshortcuts + 1
if lower(tag) == "@string" then
shortcuts[key] = value
end
@@ -63,7 +71,7 @@ end
local function do_definition(tag,key,tab) -- maybe check entries here (saves memory)
if not entries or entries[key] then
- bibtex.definitions = bibtex.definitions + 1
+ bibtexstats.nofdefinitions = bibtexstats.nofdefinitions + 1
local t = { }
for i=1,#tab,2 do
t[tab[i]] = tab[i+1]
@@ -81,8 +89,6 @@ local function resolve(s)
return shortcuts[s] or defaultshortcuts[s] or s -- can be number
end
-local P, R, S, C, Cc, Cs, Ct = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.Ct
-
local percent = P("%")
local start = P("@")
local comma = P(",")
@@ -101,10 +107,10 @@ local collapsed = (space^1)/ " "
local function add(a,b) if b then return a..b else return a end end
-local keyword = C((lpeg.R("az","AZ","09") + S("@_:-"))^1) -- lpeg.C((1-space)^1)
+local keyword = C((R("az","AZ","09") + S("@_:-"))^1) -- C((1-space)^1)
local s_quoted = ((escape*single) + collapsed + (1-single))^0
local d_quoted = ((escape*double) + collapsed + (1-double))^0
-local balanced = lpeg.patterns.balanced
+local balanced = lpegpatterns.balanced
local s_value = (single/"") * s_quoted * (single/"")
local d_value = (double/"") * d_quoted * (double/"")
@@ -116,7 +122,7 @@ local value = Cs((somevalue * ((spacing * hash * spacing)/"" * somevalue)^0
local assignment = spacing * keyword * spacing * equal * spacing * value * spacing
local shortcut = keyword * spacing * left * spacing * (assignment * comma^0)^0 * spacing * right
-local definition = keyword * spacing * left * spacing * keyword * comma * lpeg.Ct((assignment * comma^0)^0) * spacing * right
+local definition = keyword * spacing * left * spacing * keyword * comma * Ct((assignment * comma^0)^0) * spacing * right
local comment = keyword * spacing * left * (1-right)^0 * spacing * right
local forget = percent^1 * (1-lineending)^0
@@ -127,21 +133,20 @@ local grammar = (space + forget + shortcut/do_shortcut + definition/do_definitio
function bibtex.convert(session,content)
statistics.starttiming(bibtex)
data, shortcuts, entries = session.data, session.shortcuts, session.entries
- -- session.size = session.size + #content
- bibtex.size = bibtex.size + #content
- session.size = session.size + #content
+ bibtexstats.nofbytes = bibtexstats.nofbytes + #content
+ session.nofbytes = session.nofbytes + #content
lpegmatch(grammar,content or "")
statistics.stoptiming(bibtex)
end
function bibtex.load(session,filename)
- local filename = resolvers.find_file(filename,"bib")
+ local filename = resolvers.findfile(filename,"bib")
if filename ~= "" then
local data = io.loaddata(filename) or ""
if data == "" then
- logs.report("publications","empty file '%s', no conversion to xml",filename)
+ report_xml("empty file '%s', no conversion to xml",filename)
elseif trace_bibxml then
- logs.report("publications","converting file '%s' to xml",filename)
+ report_xml("converting file '%s' to xml",filename)
end
bibtex.convert(session,data)
end
@@ -149,16 +154,16 @@ end
function bibtex.new()
return {
- data = { },
+ data = { },
shortcuts = { },
- xml = xml.convert("<?xml version='1.0' standalone='yes'?>\n<bibtex></bibtex>"),
- size = 0,
- entries = nil,
- loaded = false,
+ xml = xml.convert("<?xml version='1.0' standalone='yes'?>\n<bibtex></bibtex>"),
+ nofbytes = 0,
+ entries = nil,
+ loaded = false,
}
end
-local escaped_pattern = xml.escaped_pattern
+local escaped_pattern = lpegpatterns.xml.escaped
local ihatethis = {
f = "\\f",
@@ -193,12 +198,12 @@ function bibtex.toxml(session,options)
-- we can always speed this up if needed
-- format slows down things a bit but who cares
statistics.starttiming(bibtex)
- local result = { }
- local options = aux.settings_to_hash(options)
+ local result, r = { }, 0
+ local options = settings_to_hash(options)
local convert = options.convert -- todo: interface
local strip = options.strip -- todo: interface
local entries = session.entries
- result[#result+1] = format("<?xml version='1.0' standalone='yes'?>")
+ r = r + 1 ; result[r] = format("<?xml version='1.0' standalone='yes'?>")
result[#result+1] = format("<bibtex>")
for id, categories in next, session.data do
id = lower(gsub(id,"^@",""))
@@ -218,14 +223,14 @@ function bibtex.toxml(session,options)
-- kind of hackery ... bibtex databases are quite unportable
value = lpegmatch(filter,value) or value
end
- result[#result+1] = format(" <field name='%s'>%s</field>",key,value)
+ r = r + 1 ; result[r] = format(" <field name='%s'>%s</field>",key,value)
end
end
- result[#result+1] = format("</entry>")
+ r = r + 1 ; result[r] = format("</entry>")
end
end
end
- result[#result+1] = format("</bibtex>")
+ r = r + 1 ; result[r] = format("</bibtex>")
result = concat(result,"\n")
-- alternatively we could use lxml.convert
session.xml = xml.convert(result, {
@@ -240,10 +245,10 @@ function bibtex.toxml(session,options)
end
statistics.register("bibtex load time", function()
- local size = bibtex.size
- if size > 0 then
+ local nofbytes = bibtexstats.nofbytes
+ if nofbytes > 0 then
return format("%s seconds (%s bytes, %s definitions, %s shortcuts)",
- statistics.elapsedtime(bibtex),size,bibtex.definitions,bibtex.shortcuts)
+ statistics.elapsedtime(bibtex),nofbytes,bibtexstats.nofdefinitions,bibtexstats.nofshortcuts)
else
return nil
end
@@ -264,14 +269,14 @@ end)
--~ local session = bibtex.new()
--~ bibtex.convert(session,str)
--~ bibtex.toxml(session)
---~ print(session.size,statistics.elapsedtime(bibtex))
+--~ print(session.nofbytes,statistics.elapsedtime(bibtex))
--~ local session = bibtex.new()
--~ bibtex.load(session,"IEEEabrv.bib")
--~ bibtex.load(session,"IEEEfull.bib")
--~ bibtex.load(session,"IEEEexample.bib")
--~ bibtex.toxml(session)
---~ print(session.size,statistics.elapsedtime(bibtex))
+--~ print(session.nofbytes,statistics.elapsedtime(bibtex))
--~ local session = bibtex.new()
--~ bibtex.load(session,"gut.bib")
@@ -284,27 +289,29 @@ end)
--~ bibtex.load(session,"texnique.bib")
--~ bibtex.load(session,"tugboat.bib")
--~ bibtex.toxml(session)
---~ print(session.size,statistics.elapsedtime(bibtex))
+--~ print(session.nofbytes,statistics.elapsedtime(bibtex))
--~ print(table.serialize(session.data))
--~ print(table.serialize(session.shortcuts))
--~ print(xml.serialize(session.xml))
-if not characters then dofile(resolvers.find_file("char-def.lua")) end
+if not characters then dofile(resolvers.findfile("char-def.lua")) end
local chardata = characters.data
local concat = table.concat
-local P, Ct, lpegmatch = lpeg.P, lpeg.Ct, lpeg.match
+local lpeg = lpeg
+
+local P, Ct, lpegmatch, lpegpatterns = lpeg.P, lpeg.Ct, lpeg.match, lpeg.patterns
local space, comma = P(" "), P(",")
local andsplitter = Ct(lpeg.splitat(space^1 * "and" * space^1))
local commasplitter = Ct(lpeg.splitat(space^0 * comma * space^0))
local spacesplitter = Ct(lpeg.splitat(space^1))
-local firstcharacter = lpeg.patterns.utf8byte
+local firstcharacter = lpegpatterns.utf8byte
-function is_upper(str)
+local function is_upper(str)
local first = lpegmatch(firstcharacter,str)
local okay = chardata[first]
return okay and okay.category == "lu"
@@ -578,8 +585,8 @@ end
-- pays off.
local function collectauthoryears(id,list)
- list = aux.settings_to_hash(list)
- id = lxml.get_id(id)
+ list = settings_to_hash(list)
+ id = getid(id)
local found = { }
for e in xml.collected(id,"/bibtex/entry") do
if list[e.at.tag] then
@@ -631,9 +638,6 @@ end
if commands then
- local texsprint = tex and tex.sprint
- local ctxcatcodes = tex and tex.ctxcatcodes
-
local sessions = { }
function commands.definebibtexsession(name)
@@ -667,7 +671,7 @@ if commands then
if collected then
local author = collected[1].dt[1] or ""
if author ~= "" then
- texsprint(ctxcatcodes,authors.concat(author,method,what,settings))
+ context(authors.concat(author,method,what,settings))
end
end
end
@@ -677,7 +681,7 @@ if commands then
local c = collected[1]
local year = xmlfilter(c,"xml://field[@name='year']/text()")
local author = xmlfilter(c,"xml://field[@name='author']/text()")
- texsprint(ctxcatcodes,authors.short(author,year))
+ context(authors.short(author,year))
end
end
@@ -711,31 +715,32 @@ if commands then
function bibtex.authorref(id,list)
local result = collectauthoryears(id,list,method,what)
for author, years in next, result do
- texsprint(ctxcatcodes,authors.concat(author,method,what,settings))
+ context(authors.concat(author,method,what,settings))
end
end
function bibtex.authoryearref(id,list)
local result = collectauthoryears(id,list,method,what)
for author, years in next, result do
- texsprint(ctxcatcodes,authors.concat(author,method,what,settings)," (",concat(years,", "),")")
+ context("%s (%s)",authors.concat(author,method,what,settings),concat(years,", "))
end
end
function bibtex.authoryearsref(id,list)
local result = collectauthoryears(id,list,method,what)
for author, years in next, result do
- texsprint(ctxcatcodes,"(",authors.concat(author,method,what,settings),", ",concat(years,", "),")")
+ context("(%s, %s)",authors.concat(author,method,what,settings),concat(years,", "))
end
end
- function bibtex.singular_or_plural(singular,plural)
+ function bibtex.singularorplural(singular,plural)
if lastconcatsize and lastconcatsize > 1 then
- texsprint(ctxcatcodes,plural)
+ context(plural)
else
- texsprint(ctxcatcodes,singular)
+ context(singular)
end
end
+
end