summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citeproc-node-number.lua
blob: cc2bc45c7d3568d34657826f9c885e77436e918b (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
--
-- Copyright (c) 2021-2023 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--

local number_module = {}

local element
local ir_node
local util

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

local Element = element.Element
local Rendered = ir_node.Rendered
local GroupVar = ir_node.GroupVar


---@class Number: Element
---@field variable string
---@field form string?
---@field prefix string?
---@field suffix string?
---@field display string?
---@field text_case string?
local Number = Element:derive("number")

function Number:new(node)
  local o = {
    element_name = "number",
    form = "numeric",
  }
  setmetatable(o, self)
  self.__index = self
  return o
end

function Number:from_node(node)
  local o = Number:new()
  o:set_attribute(node, "variable")
  o:set_attribute(node, "form")
  o:set_affixes_attributes(node)
  o:set_display_attribute(node)
  o:set_formatting_attributes(node)
  o:set_text_case_attribute(node)
  return o
end

function Number:build_ir(engine, state, context)
  local number
  if not state.suppressed[self.variable] then
    number = context:get_variable(self.variable, self.form)
  end
  if not number then
    local ir = Rendered:new({}, self)
    ir.group_var = GroupVar.Missing
    return ir
  end

  if type(number) == "number" then
    number = tostring(number)
  end
  number = self:format_number(number, self.variable, self.form, context)

  local inlines = self:render_text_inlines(number, context)
  local ir = Rendered:new(inlines, self)
  ir.group_var = GroupVar.Important

  -- Suppress substituted name variable
  if state.name_override and not context.sort_key then
    state.suppressed[self.variable] = true
  end
  return ir
end


number_module.Number = Number

return number_module