summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citeproc-node-label.lua
blob: 27cafa2f18993ab91ee4ca0e906f51f41d69af1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--
-- Copyright (c) 2021-2022 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--

local label = {}

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")


-- [Label](https://docs.citationstyles.org/en/stable/specification.html#label)
local Label = Element:derive("label")

Label.form = "long"
Label.plural = "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

function Label:build_ir(engine, state, context)
  -- local variable = context:get_variable(self.variable, self.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 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
  end
  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_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
    return #variable > 1
  elseif variable_type == "number" then
    if util.startswith(variable, "number-of-") then
      return tonumber(value) > 1
    else
      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
  end
  return false
end


label.Label = Label

return label