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

local choose = {}

local element = require("citeproc-element")
local util = require("citeproc-util")


local Choose = element.Element:new()

function Choose:render (item, context)
  self:debug_info(context)
  context = self:process_context(context)
  for i, child in ipairs(self:get_children()) do
    if child:is_element() then
      local result, status = child:render(item, context)
      if status then
        return result
      end
    end
  end
  return nil
end


local If = element.Element:new()

If.render = function (self, item, context)
  self:debug_info(context)
  context = self:process_context(context)
  local results = {}

  local variable_names = context.options["is-numeric"]
  if variable_names then
    for _, variable_name in ipairs(util.split(variable_names)) do
      local variable = self:get_variable(item, variable_name, context)
      table.insert(results, util.is_numeric(variable))
    end
  end

  variable_names = context.options["is-uncertain-date"]
  if variable_names then
    for _, variable_name in ipairs(util.split(variable_names)) do
      local variable = self:get_variable(item, variable_name, context)
      table.insert(results, util.is_uncertain_date(variable))
    end
  end

  local locator_types = context.options["locator"]
  if locator_types then
    for _, locator_type in ipairs(util.split(locator_types)) do
      local locator_label = item.label or "page"
      local res = locator_label == locator_type
      if locator_type == "sub-verbo" then
        res = locator_label == "sub-verbo" or locator_label == "sub verbo"
      end
      table.insert(results, res)
    end
  end

  local positions = context.options["position"]
  if positions then
    for _, position in ipairs(util.split(positions)) do
      local res = false
      if context.mode == "citation" then
        if position == "first" then
          res = (item.position == util.position_map["first"])
        elseif position == "near-note" then
          res = item["near-note"] ~= nil and item["near-note"] ~= false
        else
          res = (item.position >= util.position_map[position])
        end
      end
      table.insert(results, res)
    end
  end

  local type_names = context.options["type"]
  if type_names then
    for _, type_name in ipairs(util.split(type_names)) do
      table.insert(results, item["type"] == type_name)
    end
  end

  variable_names = context.options["variable"]
  if variable_names then
    for _, variable_name in ipairs(util.split(variable_names)) do
      local variable = self:get_variable(item, variable_name, context)
      local res = (variable ~= nil and variable ~= "")
      table.insert(results, res)
    end
  end

  local match = context.options["match"] or "all"
  local status = false
  if match == "any" then
    status = util.any(results)
  elseif match == "none" then
    status = not util.any(results)
  else
    status = util.all(results)
  end
  if status then
    return self:render_children(item, context), status
  else
    return nil, false
  end
end


local ElseIf = If:new()


local Else = element.Element:new()

Else.render = function (self, item, context)
  self:debug_info(context)
  context = self:process_context(context)
  return self:render_children(item, context), true
end


choose.Choose = Choose
choose.If = If
choose.ElseIf = ElseIf
choose.Else = Else

return choose