commit | author | age
|
6a4200
|
1 |
-- lua filter to add backlinks to citations |
JUH |
2 |
-- run after --citeproc on the command line |
|
3 |
|
|
4 |
-- cites is a table mapping citation item identifiers |
|
5 |
-- to an array of cite identifiers |
|
6 |
local cites = {} |
|
7 |
|
|
8 |
-- counter for cite identifiers |
|
9 |
local cite_number = 1 |
|
10 |
|
|
11 |
local function with_latex_label(s, el) |
|
12 |
if FORMAT == "latex" then |
|
13 |
return {pandoc.RawInline("latex", "\\label{" .. s .. "}"), el} |
|
14 |
else |
|
15 |
return {el} |
|
16 |
end |
|
17 |
end |
|
18 |
|
|
19 |
function Cite(el) |
|
20 |
local cite_id = "cite_" .. cite_number |
|
21 |
cite_number = cite_number + 1 |
|
22 |
for _,citation in ipairs(el.citations) do |
|
23 |
if cites[citation.id] then |
|
24 |
table.insert(cites[citation.id], cite_id) |
|
25 |
else |
|
26 |
cites[citation.id] = {cite_id} |
|
27 |
end |
|
28 |
end |
|
29 |
return pandoc.Span(with_latex_label(cite_id, el), pandoc.Attr(cite_id)) |
|
30 |
end |
|
31 |
|
|
32 |
function Div(el) |
|
33 |
local citation_id = el.identifier:match("ref%-(.+)") |
|
34 |
if citation_id then |
|
35 |
local backlinks = {pandoc.Str("Cited:"),pandoc.Space()} |
|
36 |
for i,cite_id in ipairs(cites[citation_id]) do |
|
37 |
local marker = pandoc.Str(i) |
|
38 |
if FORMAT == "latex" then |
|
39 |
marker = pandoc.RawInline("latex", "\\pageref{" .. cite_id .. "}") |
|
40 |
end |
|
41 |
if #backlinks > 2 then |
|
42 |
table.insert(backlinks, pandoc.Str(",")) |
|
43 |
table.insert(backlinks, pandoc.Space()) |
|
44 |
end |
|
45 |
table.insert(backlinks, pandoc.Link(marker, "#"..cite_id)) |
|
46 |
end |
|
47 |
table.insert(el.content, pandoc.Para({pandoc.Span(backlinks, |
|
48 |
pandoc.Attr("",{"csl-indent"}))})) |
|
49 |
return el |
|
50 |
end |
|
51 |
end |