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

local layout = {}

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 SeqIr = ir_node.SeqIr


---@class Layout: Element
---@field prefix string?
---@field suffix string?
---@field delimiter string?
---@field locale string?
local Layout = Element:derive("layout")

function Layout:from_node(node)
  local o = Layout:new()
  o:set_affixes_attributes(node)
  o:set_formatting_attributes(node)
  o:get_delimiter_attribute(node)

  -- CSL-M: layout
  o:set_attribute(node, "locale")

  o:process_children_nodes(node)

  return o
end

function Layout:build_ir(engine, state, context)
  local ir = self:build_children_ir(engine, state, context)
  if not ir then
    return nil
  end
  if context.in_bibliography then
    ir.delimiter = self.delimiter

    -- Move affixes of `bibliography > layout` into the right-inline element
    -- bugreports_SmallCapsEscape.txt
    local has_right_inline = false
    if self.affixes or self.formatting then
      for i, child_ir in ipairs(ir.children) do
        if child_ir.display == "right-inline" then
          has_right_inline = true
          local right_inline_with_affixes = SeqIr:new({child_ir}, self)
          right_inline_with_affixes.affixes = util.clone(self.affixes)
          right_inline_with_affixes.formatting = util.clone(self.formatting)
          child_ir.display = nil
          right_inline_with_affixes.display = "right-inline"
          ir.children[i] = right_inline_with_affixes
          break
        end
      end
    end

    if not has_right_inline then
      ir.affixes = util.clone(self.affixes)
      ir.formatting = util.clone(self.formatting)
    end
  end
  -- util.debug(ir)
  return ir
end


layout.Layout = Layout

return layout