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

local sort = {}

local unicode = require("unicode")

local Element = require("citeproc-element").Element
local Date = require("citeproc-node-date").Date
local names = require("citeproc-node-names")
local InlineElement = require("citeproc-output").InlineElement
local util = require("citeproc-util")


-- [Sorting](https://docs.citationstyles.org/en/stable/specification.html#sorting)
local Sort = Element:derive("sort")

function Sort:from_node(node)
  local o = Sort:new()
  o.children = {}

  o:process_children_nodes(node)
  o.sort_directions = {}
  for i, key in ipairs(o.children) do
    o.sort_directions[i] = (key.sort ~= "descending")
  end
  table.insert(o.sort_directions, true)

  return o
end

function Sort:sort(items, state, context)
  -- key_map = {
  --   id1 = {key1, key2, ...},
  --   id2 = {key1, key2, ...},
  --   ...
  -- }
  local key_map = {}
  local sort_directions = self.sort_directions
  -- true: ascending
  -- false: descending

  if not Sort.collator then
    local lang = context.engine.lang
    local language = string.sub(lang, 1, 2)
    -- It's 6 seconds slower to run the whole test-suite if these package
    -- loading statements are put in the header.
    local uca_ducet = require("lua-uca.lua-uca-ducet")
    local uca_collator = require("lua-uca.lua-uca-collator")
    Sort.collator = uca_collator.new(uca_ducet)
    if language ~= "en" then
      local uca_languages = require("lua-uca.lua-uca-languages")
      if uca_languages[language] then
        Sort.collator = uca_languages[language](Sort.collator)
      else
        util.warning(string.format('Locale "%s" is not provided by lua-uca. The sorting order may be incorrect.', lang))
      end
    end
  end

  -- TODO: optimize: use cached values for fixed keys
  for i, item in ipairs(items) do
    key_map[item.id] = {}

    context.id = item.id
    context.cite = item
    context.reference = context.engine.registry.registry[item.id]

    for j, key in ipairs(self.children) do
      if context.reference then
        context.sort_key = key
        local key_str = key:eval(context.engine, state, context)
        key_map[item.id][j] = key_str
      else
        -- The entry is missing
        key_map[item.id][j] = false
      end
    end
    -- To preserve the original order of items with same sort keys
    -- sort_NameImplicitSortOrderAndForm.txt
    table.insert(key_map[item.id], i)
  end

  -- util.debug(key_map)

  local function compare_entry(item1, item2)
    return self.compare_entry(key_map, sort_directions, item1, item2)
  end
  -- util.debug(items)
  table.sort(items, compare_entry)

  return items
end

function Sort.compare(value1, value2)
  if type(value1) == "string" then
    return Sort.compare_strings(value1, value2)
  else
    return value1 < value2
  end
end

function Sort.compare_strings(str1, str2)
  if Sort.collator then
    return Sort.collator:compare_strings(str1, str2)
  else
    return str1 < str2
  end
end

function Sort.compare_entry(key_map, sort_directions, item1, item2)
  for i, value1 in ipairs(key_map[item1.id]) do
    local ascending = sort_directions[i]
    local value2 = key_map[item2.id][i]
    if value1 and value2 then
      local res
      if ascending then
        res = Sort.compare(value1, value2)
      else
        res = Sort.compare(value2, value1)
      end
      if res or value1 ~= value2 then
        return res
      end
    elseif value1 then
      return true
    elseif value2 then
      return false
    end
  end
end

local Key = Element:derive("key")

function Key:new()
  local o = Element.new(self)
  Key.sort = "ascending"
  return o
end

function Key:from_node(node)
  local o = Key:new()
  o:set_attribute(node, "sort")
  o:set_attribute(node, "variable")
  o:set_attribute(node, "macro")
  o:set_number_attribute(node, "names-min")
  o:set_number_attribute(node, "names-use-first")
  o:set_bool_attribute(node, "names-use-last")
  return o
end

function Key:eval(engine, state, context)
  local res
  if self.variable then
    local variable_type = util.variable_types[self.variable]
    if variable_type == "name" then
      res = self:eval_name(engine, state, context)
    elseif variable_type == "date" then
      res = self:eval_date(context)
    elseif variable_type == "number" then
      local value = context:get_variable(self.variable)
      if type(value) == "string" and string.match(value, "%s+") then
        value = tonumber(value)
      end
      res = value
    else
      res = context:get_variable(self.variable)
      if type(res) == "string" then
        local inlines = InlineElement:parse(res, context)
        res = context.format:output(inlines)
      end
    end
  elseif self.macro then
    local macro = context:get_macro(self.macro)
    state:push_macro(self.macro)
    local ir = macro:build_ir(engine, state, context)
    state:pop_macro(self.macro)
    if ir.name_count then
      return ir.name_count
    elseif ir.sort_key ~= nil then
      return ir.sort_key
    end
    local output_format = context.format
    local inlines = ir:flatten(output_format)
    -- util.debug(inlines)
    local str = output_format:output(inlines)
    return str
  end
  if res == nil then
    -- make table.insert(_, nil) work
    res = false
  end
  return res
end

function Key:eval_name(engine, state, context)
  if not self.name_inheritance then
    self.name_inheritance = util.clone(context.name_inheritance)
  end
  local name = context:get_variable(self.variable)
  if not name then
    return false
  end
  local ir = self.name_inheritance:build_ir(self.variable, nil, nil, engine, state, context)
  if ir.name_count then
    -- name count
    return ir.name_count
  end
  local output_format = context.format
  local inlines = ir:flatten(output_format)

  local str = output_format:output(inlines)

  return str
end

function Key:eval_date(context)
  if not self.date then
    self.date = Date:new()
    self.date.variable = self.variable
    self.date.form = "numeric"
    self.date.date_parts = "year-month-day"
  end
  return self.date:render_sort_key(context.engine, nil, context)
end


sort.Sort = Sort
sort.Key = Key

return sort