Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
jdutant committed Mar 19, 2021
commit d7b74340d9bf6c2a09d5ae1c693dd5f81509069e
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions bibliography-place/README.md → bib-place/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
title: "Bibliography-place"
title: "Bib-place"
author: "Julien Dutant"
---

Bibliography place
Bib-place
=======

Control the placement of a `citeproc`-generated bibliography
via Pandoc templates.
via Pandoc templates. Only works with a single-bibliography
document.

Introduction
------------
Expand Down
77 changes: 77 additions & 0 deletions bib-place/bib-place.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--- # bib-place: template-controlled bibliography
-- placement in Pandoc.
--
-- This Lua filter for Pandoc allows placement of the bibliography
-- to be controlled in pandoc templates.
--
-- @author Julien Dutant <[email protected]>
-- @author Albert Krewinkel
-- @copyright 2021 Julien Dutant, Albert Krewinkel
-- @license MIT - see LICENSE file for details.
-- @release 0.1

local references = pandoc.List({})

--- Filter for the document's blocks
-- Extract the Div with identifer 'refs' if present, as well as
-- any heading that immediately precedes it, and stores them in
-- the variable `references`. To find a heading that precedes
-- the `refs` Div it needs to walk through the document
-- backwards, element by element.
-- @param element a Div element
-- @return an empty list if Div has identifier `refs`
local blocks_filter = {

Pandoc = function (doc)

local previous_was_refs = false

for i = #doc.blocks, 1, -1 do

-- if we have already found the Div `refs` we check
-- whether we have a heading and end the loop
if previous_was_refs then
if doc.blocks[i].t == 'Header' then
references:insert(1, pandoc.MetaBlocks( { doc.blocks[i] } ))
doc.blocks:remove(i) -- remove the heading
break
else
break
end

elseif doc.blocks[i].identifier == 'refs'
or ( doc.blocks[i].classes and
doc.blocks[i].classes:includes('csl-bib-body') ) then

references:insert(pandoc.MetaBlocks( { doc.blocks[i] } ))
doc.blocks:remove(i) -- remove the Div
previous_was_refs = true

end

end

return(doc)

end
}

--- Metadata filter.
-- Places the references block (as string) in the `references`
-- field of the document's metadata. The template can
-- print it by using `$meta.references$`.
-- @param meta the document's metadata block
-- @return the modified metadata block
local metadata_filter = {
Meta = function (meta)
meta.referencesblock = references
return meta
end
}

--- Main code
-- Returns the filters in the desired order of execution
return {
blocks_filter,
metadata_filter
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<body>

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

Seymour Butz
School of Studies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

I enjoyed Doe (1979).

\hypertarget{references}{%
\section*{References}\label{references}}
\addcontentsline{toc}{section}{References}

Seymour Butz
School of Studies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<body>

<p>I enjoyed <span class="citation" data-cites="Doe">Doe (1979)</span>.</p>
<h1 class="unnumbered" id="references">References</h1>
<div id="refs" class="references csl-bib-body hanging-indent" role="doc-bibliography">
<div id="ref-Doe" class="csl-entry" role="doc-biblioentry">
Doe, John. 1979. <em>Short Stories</em>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

I enjoyed Doe (1979).

\hypertarget{references}{%
\section*{References}\label{references}}
\addcontentsline{toc}{section}{References}

\hypertarget{refs}{}
\begin{CSLReferences}{1}{0}
\leavevmode\hypertarget{ref-Doe}{}%
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions bibliography-place/sample.md → bib-place/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ bibliography: sample.bib
...

I enjoyed @Doe.

## Works cited {.refs}

File renamed without changes.
File renamed without changes.
39 changes: 0 additions & 39 deletions bibliography-place/bibliography-place.lua

This file was deleted.