summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citeproc-node-label.lua
blob: 5c0ee9bf98e3409e282111c23accb935d9a0711e (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
--
-- Copyright (c) 2021-2023 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--

local label = {}

local element
local ir_node
local output
local util

if kpse then
  element = require("citeproc-element")
  ir_node = require("citeproc-ir-node")
  output = require("citeproc-output")
  util = require("citeproc-util")
else
  element = require("citeproc.element")
  ir_node = require("citeproc.ir-node")
  output = require("citeproc.output")
  util = require("citeproc.util")
end

local Element = element.Element
local IrNode = ir_node.IrNode
local Rendered = ir_node.Rendered
local PlainText = output.PlainText


-- [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
      -- Issue #27: "number-of-pages": "91–129"
      value = string.match(tostring(value), "%d+")
      return value and tonumber(value) > 1
    else
      value = tostring(value)
      local tokens = self:parse_number_tokens(value, context)
      local num_numeric_tokens = 0
      for _, token in ipairs(tokens) do
        if token.type == "number" then
          num_numeric_tokens = num_numeric_tokens + 1
        end
      end
      return num_numeric_tokens > 1
    end
  end
  return false
end


label.Label = Label

return label