Skip to content

Commit d7b7434

Browse files
committed
Captures the bibliography heading as well
Modified the filter so that both the bibliography and the heading that immediately precedes it (if any) are placed in a metadata element.
1 parent ed684bd commit d7b7434

13 files changed

+94
-42
lines changed
File renamed without changes.
File renamed without changes.

bibliography-place/README.md renamed to bib-place/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
2-
title: "Bibliography-place"
2+
title: "Bib-place"
33
author: "Julien Dutant"
44
---
55

6-
Bibliography place
6+
Bib-place
77
=======
88

99
Control the placement of a `citeproc`-generated bibliography
10-
via Pandoc templates.
10+
via Pandoc templates. Only works with a single-bibliography
11+
document.
1112

1213
Introduction
1314
------------

bib-place/bib-place.lua

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--- # bib-place: template-controlled bibliography
2+
-- placement in Pandoc.
3+
--
4+
-- This Lua filter for Pandoc allows placement of the bibliography
5+
-- to be controlled in pandoc templates.
6+
--
7+
-- @author Julien Dutant <[email protected]>
8+
-- @author Albert Krewinkel
9+
-- @copyright 2021 Julien Dutant, Albert Krewinkel
10+
-- @license MIT - see LICENSE file for details.
11+
-- @release 0.1
12+
13+
local references = pandoc.List({})
14+
15+
--- Filter for the document's blocks
16+
-- Extract the Div with identifer 'refs' if present, as well as
17+
-- any heading that immediately precedes it, and stores them in
18+
-- the variable `references`. To find a heading that precedes
19+
-- the `refs` Div it needs to walk through the document
20+
-- backwards, element by element.
21+
-- @param element a Div element
22+
-- @return an empty list if Div has identifier `refs`
23+
local blocks_filter = {
24+
25+
Pandoc = function (doc)
26+
27+
local previous_was_refs = false
28+
29+
for i = #doc.blocks, 1, -1 do
30+
31+
-- if we have already found the Div `refs` we check
32+
-- whether we have a heading and end the loop
33+
if previous_was_refs then
34+
if doc.blocks[i].t == 'Header' then
35+
references:insert(1, pandoc.MetaBlocks( { doc.blocks[i] } ))
36+
doc.blocks:remove(i) -- remove the heading
37+
break
38+
else
39+
break
40+
end
41+
42+
elseif doc.blocks[i].identifier == 'refs'
43+
or ( doc.blocks[i].classes and
44+
doc.blocks[i].classes:includes('csl-bib-body') ) then
45+
46+
references:insert(pandoc.MetaBlocks( { doc.blocks[i] } ))
47+
doc.blocks:remove(i) -- remove the Div
48+
previous_was_refs = true
49+
50+
end
51+
52+
end
53+
54+
return(doc)
55+
56+
end
57+
}
58+
59+
--- Metadata filter.
60+
-- Places the references block (as string) in the `references`
61+
-- field of the document's metadata. The template can
62+
-- print it by using `$meta.references$`.
63+
-- @param meta the document's metadata block
64+
-- @return the modified metadata block
65+
local metadata_filter = {
66+
Meta = function (meta)
67+
meta.referencesblock = references
68+
return meta
69+
end
70+
}
71+
72+
--- Main code
73+
-- Returns the filters in the desired order of execution
74+
return {
75+
blocks_filter,
76+
metadata_filter
77+
}

bibliography-place/expected_filtered.html renamed to bib-place/expected_filtered.html

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<body>
66

77
<p>I enjoyed <span class="citation" data-cites="Doe">Doe (1979)</span>.</p>
8+
<h1 class="unnumbered" id="references">References</h1>
89

910
Seymour Butz
1011
School of Studies

bibliography-place/expected_filtered.tex renamed to bib-place/expected_filtered.tex

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
I enjoyed Doe (1979).
3030

31+
\hypertarget{references}{%
32+
\section*{References}\label{references}}
33+
\addcontentsline{toc}{section}{References}
34+
3135
Seymour Butz
3236
School of Studies
3337

bibliography-place/expected_unfiltered.html renamed to bib-place/expected_unfiltered.html

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<body>
66

77
<p>I enjoyed <span class="citation" data-cites="Doe">Doe (1979)</span>.</p>
8+
<h1 class="unnumbered" id="references">References</h1>
89
<div id="refs" class="references csl-bib-body hanging-indent" role="doc-bibliography">
910
<div id="ref-Doe" class="csl-entry" role="doc-biblioentry">
1011
Doe, John. 1979. <em>Short Stories</em>.

bibliography-place/expected_unfiltered.tex renamed to bib-place/expected_unfiltered.tex

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
I enjoyed Doe (1979).
3030

31+
\hypertarget{references}{%
32+
\section*{References}\label{references}}
33+
\addcontentsline{toc}{section}{References}
34+
3135
\hypertarget{refs}{}
3236
\begin{CSLReferences}{1}{0}
3337
\leavevmode\hypertarget{ref-Doe}{}%
File renamed without changes.

bibliography-place/sample.md renamed to bib-place/sample.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ bibliography: sample.bib
66
...
77

88
I enjoyed @Doe.
9+
10+
## Works cited {.refs}
11+
File renamed without changes.
File renamed without changes.

bibliography-place/bibliography-place.lua

-39
This file was deleted.

0 commit comments

Comments
 (0)