diff options
Diffstat (limited to 'biblio/citation-style-language/citeproc-node-label.lua')
-rw-r--r-- | biblio/citation-style-language/citeproc-node-label.lua | 142 |
1 files changed, 63 insertions, 79 deletions
diff --git a/biblio/citation-style-language/citeproc-node-label.lua b/biblio/citation-style-language/citeproc-node-label.lua index 828f5e9122..27cafa2f18 100644 --- a/biblio/citation-style-language/citeproc-node-label.lua +++ b/biblio/citation-style-language/citeproc-node-label.lua @@ -6,104 +6,88 @@ local label = {} -local element = require("citeproc-element") +local Element = require("citeproc-element").Element +local IrNode = require("citeproc-ir-node").IrNode +local Rendered = require("citeproc-ir-node").Rendered +local PlainText = require("citeproc-output").PlainText local util = require("citeproc-util") -local Label = element.Element:new() +-- [Label](https://docs.citationstyles.org/en/stable/specification.html#label) +local Label = Element:derive("label") -function Label:render (item, context) - self:debug_info(context) - context = self:process_context(context) +Label.form = "long" +Label.plural = "contextual" - local variable_name - if context.names_element then - -- The `variable` attribute of names may hold multiple roles. - -- Each of them may call `Label:render()` to render the term. - -- When used in `names` element, the role name is the first argument - -- and the item is accessed via `context.item`. - -- Bad design - -- TODO: Redesign the arguments of render() - variable_name = item - else - variable_name = context.options["variable"] - end - - local form = context.options["form"] - local plural = context.options["plural"] or "contextual" +function Label:from_node(node) + local o = Label:new() + o:set_attribute(node, "variable") + o:set_attribute(node, "form") + o:set_attribute(node, "plural") + o:set_affixes_attributes(node) + o:set_formatting_attributes(node) + o:set_text_case_attribute(node) + o:set_strip_periods_attribute(node) + return o +end - if not context.names_element then - local variable_type = util.variable_types[variable_name] - -- variable must be or one of the number variables. - if variable_type ~= "number" then - return nil - end - -- The term is only rendered if the selected variable is non-empty - local variable = item[variable_name] - if not variable then - return nil - end - if type(variable) == "string" then - if not (string.match(variable, "^%d") or util.is_numeric(variable)) then - return nil - end - end - end +function Label:build_ir(engine, state, context) + -- local variable = context:get_variable(self.variable, self.form) - local term - if variable_name == "locator" then - local locator_type = item.label or "page" - term = self:get_term(locator_type, form) - else - term = self:get_term(variable_name, form) + local is_plural = false + if self.plural == "always" then + is_plural = true + elseif self.plural == "never" then + is_plural = false + elseif self.plural == "contextual" then + is_plural = self:_is_variable_plural(self.variable, context) end - local res = nil - if term then - if plural == "contextual" and self:_is_plural(variable_name, context) or plural == "always" then - res = term:render(context, true) - else - res = term:render(context, false) + local variable = self.variable + if variable == "locator" then + variable = context:get_variable("label") or "page" + if variable == "sub verbo" then + -- bugreports_MovePunctuationInsideQuotesForLocator.txt + variable = "sub-verbo" end - - res = self:strip_periods(res, context) - res = self:case(res, context) - res = self:format(res, context) - res = self:wrap(res, context) end - return res + local text = context:get_simple_term(variable, self.form, is_plural) + if not text or text == "" then + return nil + end + + local inlines = self:render_text_inlines(text, context) + return Rendered:new(inlines, self) end -function Label:_is_plural (variable_name, context) - local variable_type = util.variable_types[variable_name] - -- Don't use self:get_variable here - local variable = context.item[variable_name] - local res = false +function Label:_is_variable_plural(variable, context) + local value = context:get_variable(variable) + if not value then + return false + end + local variable_type = util.variable_types[variable] if variable_type == "name" then - -- Label inside `names` - res = #variable > 1 - + return #variable > 1 elseif variable_type == "number" then - if util.startswith(variable_name, "number-of-") then - res = tonumber(variable) > 1 + if util.startswith(variable, "number-of-") then + return tonumber(value) > 1 else - variable = tostring(variable) - variable = string.gsub(variable, "\\%-", "") - if #util.split(variable, "%s*[,&-]%s*") > 1 then - -- check if contains multiple numbers - -- "i–ix": true - -- res = string.match(tostring(variable), "%d+%D+%d+") ~= nil - res = true - elseif string.match(variable, "%Aand%A") or string.match(variable, "%Aet%A") then - res = true - else - res = false + value = tostring(value) + -- label_CollapsedPageNumberPluralDetection.txt + -- 327\-30 => single + value = string.gsub(value, "\\%-", "") + if string.match(value, "[,&-]") then + return true + elseif string.match(value, util.unicode["en dash"]) then + return true + elseif string.match(value, "%Wand%W") then + return true + elseif string.match(value, "%Wet%W") then + return true end end - else - util.warning("Invalid attribute \"variable\".") end - return res + return false end |