-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pandoc-unique-identifiers.lua
executable file
·50 lines (41 loc) · 1.19 KB
/
pandoc-unique-identifiers.lua
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
--- Make all identifiers heading indetifiers unique.
--- Useful in combination with `include-files.lua`.
---
--- Copyright: © 2019–2020 Gabriel Nützi
--- License: MIT – see LICENSE file for details
local ids = {}
-- Get the key in the table, otherwise return nil
local function get(obj, field, ...)
if obj == nil or field == nil then
return obj
else
return get(obj[field], ...)
end
end
function make_unique(block)
local id = get(block, "identifier")
if id == nil or id == "" then return nil end
local index = ids[id]
-- get master count if possible
local baseId = id:match("^(.*)-%d+$")
if baseId then
id = baseId
index = ids[id]
end
if index == nil then
ids[id] = 0 -- initialize counter
end
-- we have duplicate id, e.g 'id: name-3', 'index: 3', baseId = "name"
if index then
-- Replace id
index = index + 1
newId = id .. "-" .. tostring(index)
ids[id] = index
ids[newId] = index
io.stderr:write("Adjust existing identifier " .. id .. " to: '" .. newId .. "'\n")
block.identifier = newId
return block
end
return nil
end
return {{Header = make_unique}}