Skip to content

Commit

Permalink
feat(packages): Output bibliography with proper sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Jul 20, 2024
1 parent 598000f commit f6b9b65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
78 changes: 38 additions & 40 deletions packages/bibtex/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ local function crossrefAndXDataResolve (bib, entry)
end
end

local function resolveEntry (bib, key)
local entry = bib[key]
if not entry then
SU.warn("Unknown citation key " .. key)
return
end
if entry.type == "xdata" then
SU.warn("Skipped citation of @xdata entry " .. key)
return
end
crossrefAndXDataResolve(bib, entry)
return entry
end

function package:loadOptPackage (pack)
local ok, _ = pcall(function ()
self:loadPackage(pack)
Expand Down Expand Up @@ -334,16 +348,10 @@ function package:registerCommands ()
if not options.key then
options.key = SU.ast.contentToString(content)
end
local entry = SILE.scratch.bibtex.bib[options.key]
local entry = resolveEntry(SILE.scratch.bibtex.bib, options.key)
if not entry then
SU.warn("Unknown reference in citation " .. options.key)
return
end
if entry.type == "xdata" then
SU.warn("Skipped citation of @xdata entry " .. options.key)
return
end
crossrefAndXDataResolve(SILE.scratch.bibtex.bib, entry)
local style = SILE.settings:get("bibtex.style")
local bibstyle = require("packages.bibtex.styles." .. style)
local cite = Bibliography.produceCitation(options, SILE.scratch.bibtex.bib, bibstyle)
Expand All @@ -354,16 +362,10 @@ function package:registerCommands ()
if not options.key then
options.key = SU.ast.contentToString(content)
end
local entry = SILE.scratch.bibtex.bib[options.key]
local entry = resolveEntry(SILE.scratch.bibtex.bib, options.key)
if not entry then
SU.warn("Unknown reference in citation " .. options.key)
return
end
if entry.type == "xdata" then
SU.warn("Skipped citation of @xdata entry " .. options.key)
return
end
crossrefAndXDataResolve(SILE.scratch.bibtex.bib, entry)
local style = SILE.settings:get("bibtex.style")
local bibstyle = require("packages.bibtex.styles." .. style)
local cite, err = Bibliography.produceReference(options, SILE.scratch.bibtex.bib, bibstyle)
Expand Down Expand Up @@ -463,16 +465,10 @@ function package:registerCommands ()
if not options.key then
options.key = SU.ast.contentToString(content)
end
local entry = SILE.scratch.bibtex.bib[options.key]
local entry = resolveEntry(SILE.scratch.bibtex.bib, options.key)
if not entry then
SU.warn("Unknown reference in citation " .. options.key)
return
end
if entry.type == "xdata" then
SU.warn("Skipped citation of @xdata entry " .. options.key)
return
end
crossrefAndXDataResolve(SILE.scratch.bibtex.bib, entry)

local csljson = bib2csl(entry)
-- csljson.locator = { -- EXPERIMENTAL
Expand All @@ -494,16 +490,10 @@ function package:registerCommands ()
if not options.key then
options.key = SU.ast.contentToString(content)
end
local entry = SILE.scratch.bibtex.bib[options.key]
local entry = resolveEntry(SILE.scratch.bibtex.bib, options.key)
if not entry then
SU.warn("Unknown reference in citation " .. options.key)
return
end
if entry.type == "xdata" then
SU.warn("Skipped citation of @xdata entry " .. options.key)
return
end
crossrefAndXDataResolve(SILE.scratch.bibtex.bib, entry)

local cslentry = bib2csl(entry)
local cite = engine:reference(cslentry)
Expand All @@ -512,19 +502,27 @@ function package:registerCommands ()
end)

self:registerCommand("printbibliography", function (_, _)
if not SILE.scratch.bibtex.engine then
SILE.call("bibliographystyle", { lang = "en-US", style = "chicago-author-date" })
-- SILE.call("bibliographystyle", { lang = "en-US", style = "chicago-fullnote-bibliography" })
-- SILE.call("bibliographystyle", { lang = "en-US", style = "apa" })
end
local engine = SILE.scratch.bibtex.engine

local bib = SILE.scratch.bibtex.bib
-- TEMP: until we implement proper sorting, let's sort by keys
-- for reproducibility.
local tkeys = {}
for k, _ in pairs(bib) do table.insert(tkeys, k) end
table.sort(tkeys)
local count = 0
for _, k in ipairs(tkeys) do
SILE.call("csl:reference", { key = k })
SILE.call("par")
count = count + 1
local entries = {}
for _, entry in pairs(bib) do
if entry.type ~= "xdata" then
crossrefAndXDataResolve(bib, entry)
if entry then
local cslentry = bib2csl(entry)
table.insert(entries, cslentry)
end
end
end
SILE.typesetter:typeset("¤ " .. count .. " references")
print("<bibliography: " .. #entries .. " entries>")
local cite = engine:reference(entries)
SILE.processString(("<sile>%s</sile>"):format(cite), "xml")
end)
end

Expand Down Expand Up @@ -579,7 +577,7 @@ To produce a full reference, use \autodoc:command{\csl:reference{<key>}}.
To produce a complete bibliography, use \autodoc:command{\printbibliography}.
As of yet, this command is for testing purposes only.
It does not handle sorting or filtering of the bibliography.
It does not handle filtering of the bibliography.
\smallskip
\noindent
Expand Down
2 changes: 1 addition & 1 deletion packages/bibtex/support/bib2csl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ local function bib2csl (entry)

-- BibTeX number and issue
-- Tricky, see https://github.com/JabRef/jabref/issues/8372#issuecomment-1023768144
-- Still not sure this is completely corect below.
-- Still not sure this is completely correct below.
if bibtex.series then
-- Series use number
-- BibLaTeX says number is for the series number on books, etc.
Expand Down

0 comments on commit f6b9b65

Please sign in to comment.