forked from mitchelloharawild/vitae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple-bibliographies.lua
133 lines (120 loc) · 4.57 KB
/
multiple-bibliographies.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
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
--[[
multiple-bibliographies – create multiple bibliographies
Copyright © 2018-2020 Albert Krewinkel
Modified 19/10/2020 by Mitchell O'Hara-Wild
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
local List = require 'pandoc.List'
local utils = require 'pandoc.utils'
local stringify = utils.stringify
local run_json_filter = utils.run_json_filter
--- Collection of all cites in the document
local all_cites = {}
--- Document meta value
local doc_meta = pandoc.Meta{}
--- Div used by pandoc-citeproc to insert the bibliography.
local refs_div = pandoc.Div({}, pandoc.Attr('refs'))
local supports_quiet_flag = (function ()
-- We use pandoc instead of pandoc-citeproc starting with pandoc 2.11
if PANDOC_VERSION >= "2.11" then
return true
end
local version = pandoc.pipe('<<PANDOC_PATH>>/pandoc-citeproc', {'--version'}, '')
local major, minor, patch = version:match 'pandoc%-citeproc (%d+)%.(%d+)%.?(%d*)'
major, minor, patch = tonumber(major), tonumber(minor), tonumber(patch)
return major > 0
or minor > 14
or (minor == 14 and patch >= 5)
end)()
local function run_citeproc(doc, quiet)
if PANDOC_VERSION >= "2.11" then
return run_json_filter(
doc,
'<<PANDOC_PATH>>/pandoc',
{'--from=json', '--to=json', '--citeproc', quiet and '--quiet' or nil}
)
else
-- doc = run_json_filter(doc, '<<PANDOC_PATH>>/pandoc-citeproc')
return run_json_filter(
doc,
'<<PANDOC_PATH>>/pandoc-citeproc',
{FORMAT, (quiet and supports_quiet_flag) and '-q' or nil}
)
end
end
--- Resolve citations in the document by combining all bibliographies
-- before running pandoc-citeproc on the full document.
local function resolve_doc_citations (doc)
-- combine all bibliographies
local meta = doc.meta
local orig_bib = meta.bibliography
meta.bibliography = pandoc.MetaList{orig_bib}
for name, value in pairs(meta) do
if name:match('^bibliography_') then
table.insert(meta.bibliography, value)
end
end
-- add dummy div to catch the created bibliography
table.insert(doc.blocks, refs_div)
-- resolve all citations
-- doc = run_json_filter(doc, '<<PANDOC_PATH>>/pandoc-citeproc')
doc = run_citeproc(doc)
-- remove catch-all bibliography
table.remove(doc.blocks)
-- restore bibliography to original value
doc.meta.bibliography = orig_bib
return doc
end
--- Explicitly create a new meta object with all fields relevant for
--- pandoc-citeproc.
local function meta_for_pandoc_citeproc (bibliography)
-- We could just indiscriminately copy all meta fields, but let's be
-- explicit about what's important.
local fields = {
'bibliography', 'references', 'csl', 'citation-style',
'link-citations', 'citation-abbreviations', 'lang',
'suppress-bibliography', 'reference-section-title',
'notes-after-punctuation', 'nocite'
}
local new_meta = pandoc.Meta{}
for _, field in ipairs(fields) do
new_meta[field] = doc_meta[field]
end
new_meta.bibliography = bibliography
return new_meta
end
--- Create a bibliography for a given topic. This acts on all divs whose
-- ID matches "bibliography", and uses the path contained within the div
local function create_topic_bibliography (div)
local is_bib = div.identifier == 'bibliography'
if not is_bib then
return nil
end
local bibfile = div.content[1].content[1].text
local tmp_blocks = {pandoc.Para(all_cites), refs_div}
local tmp_meta = meta_for_pandoc_citeproc(bibfile)
local tmp_doc = pandoc.Pandoc(tmp_blocks, tmp_meta)
local res = run_citeproc(tmp_doc, true) -- try to be quiet
-- First block of the result contains the dummy paragraph, second is
-- the refs Div filled by pandoc-citeproc.
div.content = res.blocks[2].content
return div
end
return {
{
-- Collect all citations and the doc's Meta value for other filters.
Cite = function (c) all_cites[#all_cites + 1] = c end,
Meta = function (m) doc_meta = m end,
},
{ Pandoc = resolve_doc_citations },
{ Div = create_topic_bibliography },
}