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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
--
-- Copyright (c) 2021-2023 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--
local irnode = {}
local util
if kpse then
util = require("citeproc-util")
else
util = require("citeproc.util")
end
---@enum GroupVar
local GroupVar = {
Plain = 0,
Important = 1,
Missing = 2,
UnresolvedPlain = 3,
}
---@class IrNode
local IrNode = {
_element = nil,
_element_name = nil,
_type = "IrNode",
_base_class = "IrNode",
text = nil,
formatting = nil,
affixes = nil,
children = nil,
delimiter = nil,
}
function IrNode:new(children, element)
local o = {
_element = element,
_element_name = element.element_name,
_type = self._type,
children = children,
group_var = GroupVar.Plain,
}
o.group_var = GroupVar.Missing
for _, child_ir in ipairs(children) do
if child_ir.group_var == GroupVar.Important then
o.group_var = GroupVar.Important
break
elseif child_ir.group_var == GroupVar.Plain then
o.group_var = GroupVar.Plain
end
end
o.person_name_irs = {}
if children then
for _, child in ipairs(children) do
if child.person_name_irs then
util.extend(o.person_name_irs, child.person_name_irs)
end
end
end
setmetatable(o, self)
self.__index = self
return o
end
function IrNode:derive(type)
local o = {
_type = type,
}
setmetatable(o, self)
self.__index = self
return o
end
function IrNode:_debug(level)
level = level or 0
local ir_info_str = ""
if self.delimiter then
ir_info_str = ir_info_str .. string.format('delimiter: "%s"', self.delimiter)
end
if self.should_inherit_delim then
if ir_info_str ~= "" then
ir_info_str = ir_info_str .. " "
end
ir_info_str = ir_info_str .. "should_inherit_delim: true"
end
if ir_info_str ~= "" then
ir_info_str = string.format("{%s}", ir_info_str)
end
local text = string.format("\n%s [%s] %s <%s> %s", string.rep(" ", level), self.group_var, self._type, self._element_name, ir_info_str)
if self.children and #self.children > 0 then
for _, child_ir in ipairs(self.children) do
text = text .. child_ir:_debug(level + 1)
end
elseif self.inlines then
for _, inline in ipairs(self.inlines) do
text = text .. " " .. inline:_debug()
end
end
return text
end
function IrNode:flatten(format)
return format:flatten_ir(self)
end
function IrNode:capitalize_first_term()
-- util.debug(self)
if self._type == "Rendered" and self.element and (self.element.term == "ibid" or self.element.term == "and") then
self.inlines[1]:capitalize_first_term()
elseif self._type == "SeqIr" and self.children[1] then
self.children[1]:capitalize_first_term()
end
end
function IrNode:collect_year_suffix_irs()
local year_suffix_irs = {}
if self.children then
for i, child_ir in ipairs(self.children) do
if child_ir._type == "YearSuffix" then
table.insert(year_suffix_irs, child_ir)
elseif child_ir.children then
util.extend(year_suffix_irs,
child_ir:collect_year_suffix_irs())
end
end
end
return year_suffix_irs
end
function IrNode:find_first_year_ir()
-- This also find the citation-label IR
if self.is_year then
return self
end
if self.children then
for _, child_ir in ipairs(self.children) do
local year_ir = child_ir:find_first_year_ir()
if year_ir then
return year_ir
end
end
end
return nil
end
---@class Rendered: IrNode
local Rendered = IrNode:derive("Rendered")
function Rendered:new(inlines, element)
local o = {
_element = element,
_element_name = element.element_name,
_type = self._type,
element = element, -- required for capitalizing first term
inlines = inlines,
group_var = GroupVar.Plain,
}
setmetatable(o, self)
self.__index = self
return o
end
---@class YearSuffix: IrNode
local YearSuffix = IrNode:derive("YearSuffix")
function YearSuffix:new(inlines, element)
local o = {
_element = element,
_element_name = element.element_name,
_type = self._type,
element = element,
inlines = inlines,
group_var = GroupVar.Plain,
}
setmetatable(o, self)
self.__index = self
return o
end
---@class NameIr: IrNode
local NameIr = IrNode:derive("NameIr")
---@class PersonNameIr: IrNode
local PersonNameIr = IrNode:derive("PersonNameIr")
function PersonNameIr:new(inlines, element)
local o = {
_element = element,
_element_name = element.element_name,
_type = self._type,
inlines = inlines,
group_var = GroupVar.Plain,
}
setmetatable(o, self)
self.__index = self
return o
end
---@class SeqIr: IrNode
local SeqIr = IrNode:derive("SeqIr")
-- function SeqIr:new(children)
-- o = IrNode.new(self, children)
-- local o = {
-- children = children,
-- group_var = GroupVar.Plain,
-- }
-- setmetatable(o, self)
-- self.__index = self
-- return o
-- end
irnode.IrNode = IrNode
irnode.Rendered = Rendered
irnode.YearSuffix = YearSuffix
irnode.NameIr = NameIr
irnode.PersonNameIr = PersonNameIr
irnode.SeqIr = SeqIr
irnode.GroupVar = GroupVar
return irnode
|