Skip to content

Commit

Permalink
refactor(packages): Better handling of page ranges in bibliographies
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Sep 14, 2024
1 parent 95ecae9 commit dea1be4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions csl/core/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local CslLocale = require("csl.core.locale").CslLocale

local superfolding = require("csl.core.utils.superfolding")
local endash = luautf8.char(0x2013)
local emdash = luautf8.char(0x2014)

local CslEngine = pl.class()

Expand Down Expand Up @@ -61,12 +62,26 @@ function CslEngine:_init (style, locale, extras)
close_quote = self:_render_term("close-quote") or luautf8.char(0x201D), -- 0x201D curly right quote
open_inner_quote = self:_render_term("open-inner-quote") or luautf8.char(0x2018), -- 0x2018 curly left single quote
close_inner_quote = self:_render_term("close-inner-quote") or luautf8.char(0x2019), -- 0x2019 curly right single quote
page_range_delimiter = self:_render_term("page-range-delimiter") or endash, -- FIXME: UNUSED AS OF NOW
page_range_delimiter = self:_render_term("page-range-delimiter") or endash,
[","] = self:_render_term("comma") or ",",
[";"] = self:_render_term("semicolon") or ";",
[":"] = self:_render_term("colon") or ":",
}

-- For page ranges, see text processing for <text variable="page">
local sep = self.punctuation.page_range_delimiter
local dashes = "%-" .. endash .. emdash
if sep ~= "-" and sep ~= endash and sep ~= emdash then
local escape = function (str)
local escapeInLua = "[%.%+%*%[%?%-%%]"
return string.gsub(str, "([%.%+%*%[%?%-%%])", "%%%1")
end
dashes = dashes .. string.gsub(sep, "([%.%+%*%?%-%]%[%%])", "%%%1")
end
local textinrange = "[^" .. dashes .. "]+"
local dashinrange = "[" .. dashes .. "]+"
self.page_range_capture = "(" .. textinrange .. ")%s*" .. dashinrange .. "%s*(" .. textinrange .. ")"

-- Inheritable variables
-- There's a long list of such variables, but let's be dumb and just merge everything.
self.inheritable = {
Expand Down Expand Up @@ -410,12 +425,19 @@ function CslEngine:_text (options, content, entry)
elseif options.term then
t = self:_render_term(options.term, options.form, options.plural)
elseif options.variable then
local variable = SU.required(options, "variable", "CSL text")
local variable = options.variable
t = entry[variable]
self:_addGroupVariable(variable, t)
if variable == "locator" then
t = t and t.value
variable = entry.locator.label
end
if variable == "page" and t then
-- Replace any dash in page ranges
local sep = self.punctuation.page_range_delimiter
t = luautf8.gsub(t, self.page_range_capture, "%1" .. sep .. "%2")
end

-- FIXME NOT IMPLEMENTED SPEC:
-- "May be accompanied by the form attribute to select the “long”
-- (default) or “short” form of a variable (e.g. the full or short
Expand Down

0 comments on commit dea1be4

Please sign in to comment.