diff --git a/bib-place/LICENSE b/bib-place/LICENSE new file mode 100644 index 00000000..b4e8923c --- /dev/null +++ b/bib-place/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Julien Dutant + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/bib-place/Makefile b/bib-place/Makefile new file mode 100644 index 00000000..839ecc21 --- /dev/null +++ b/bib-place/Makefile @@ -0,0 +1,35 @@ +DIFF ?= diff --strip-trailing-cr -u + +.PHONY: test + +test: latex html + +latex: sample.md expected_filtered.tex expected_unfiltered.tex bib-place.lua + @pandoc -s --citeproc --to latex --template template $< \ + | $(DIFF) expected_unfiltered.tex - + @pandoc -s --citeproc --lua-filter bib-place.lua --to=latex \ + --template template $< | $(DIFF) expected_filtered.tex - + +html: sample.md expected_filtered.html expected_unfiltered.html + @pandoc -s --citeproc --to=html --template template $< \ + | $(DIFF) expected_unfiltered.html - + @pandoc -s --citeproc --lua-filter bib-place.lua --to=html \ + --template template $< | $(DIFF) expected_filtered.html - + +expected_filtered.html : sample.md bib-place.lua + @pandoc -s --citeproc --lua-filter bib-place.lua \ + --output expected_filtered.html \ + --template template $< \ + +expected_filtered.tex : sample.md bib-place.lua + @pandoc -s --citeproc --lua-filter bib-place.lua \ + --output expected_filtered.tex \ + --template template $< + +expected_unfiltered.html : sample.md bib-place.lua + @pandoc -s --citeproc --output expected_unfiltered.html \ + --template template $< + +expected_unfiltered.tex : sample.md bib-place.lua + @pandoc -s --citeproc --output expected_unfiltered.tex \ + --template template $< diff --git a/bib-place/README.md b/bib-place/README.md new file mode 100644 index 00000000..84eceb29 --- /dev/null +++ b/bib-place/README.md @@ -0,0 +1,82 @@ +--- +title: "Bib-place" +author: "Julien Dutant" +--- + +Bib-place +======= + +Control the placement of a `citeproc`-generated bibliography +via Pandoc templates. Only works with a single-bibliography +document. + +The filter does not affect biblatex / natbib outputs, so it can be +used with a custom template that caters for all bibliography engines. + +Introduction +------------ + +In Pandoc templates the main text is contained in the variable +`$body$`. Suppose you want your document template to add something +just below the body, such as the author's name and affiliation: + +``` +$body$ + +$for(author)$$author$$endfor$ +$if(institute)$ +$for(institute)$$institute$$endfor$ +$endif$ + +``` + +When using `citeproc` to generate a bibliography you will not get +the desired result, because `citeproc`-generated bibliographies +are inserted at the end of the `$body$`` variable. The template +above will print the author's name and institute after the +bibliography. + +This filter takes the `citeproc` bibliography out of `$body$` and +places it in a `$referencesblock$` variable instead. + +Usage +---- + +Call the filter at the command line or in a defaults file (see Pandoc's +manual for detail). **Important**: the filter must be called after *citeproc*. From the command line: + +``` +pandoc -s --citeproc -L bib-place.lua sample.md -t html + +pandoc -s --citeproc --lua-filter bib-place.lua sample.md -t html +``` + +In a default file: + +``` +filters: +- citeproc +- bib-place.lua +``` + +In you custom Pandoc template you can then place the references block with the `referencesblock` variable: + +``` +$body$ + +$for(author)$$author$$endfor$ +$if(institute)$ +$for(institute)$$institute$$endfor$ +$endif$ + +$if(referencesblock)$$referencesblock$$endif$ +``` + +Notes +----- + +The template can be agnostic on which bibliography engine your run. If you process the document with other bibliography engines (natbib, biblatex) the filter will leave them untouched and you can place them by moving the +`\printbibliography` commands in the template. + +If you use the filter with the default pandoc templates or with a template +that does not use `$referencesblock$` your bibliography will not be printed. diff --git a/bib-place/bib-place.lua b/bib-place/bib-place.lua new file mode 100644 index 00000000..d1fdcaaf --- /dev/null +++ b/bib-place/bib-place.lua @@ -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 +-- @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 +} diff --git a/bib-place/expected_filtered.html b/bib-place/expected_filtered.html new file mode 100644 index 00000000..ed326b22 --- /dev/null +++ b/bib-place/expected_filtered.html @@ -0,0 +1,22 @@ + + + Sample document + + + +

I enjoyed Doe (1979).

+ +Seymour Butz +School of Studies + + + +

Works cited

+
+Doe, John. 1979. Short Stories. +
+
+ + + + diff --git a/bib-place/expected_filtered.tex b/bib-place/expected_filtered.tex new file mode 100644 index 00000000..ba253a94 --- /dev/null +++ b/bib-place/expected_filtered.tex @@ -0,0 +1,53 @@ +% A minimal template + +% Preamble commands for each bibliography engine +% These are included in Pandoc's default template +% +\newlength{\cslhangindent} +\setlength{\cslhangindent}{1.5em} +\newlength{\csllabelwidth} +\setlength{\csllabelwidth}{3em} +\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing + {% don't indent paragraphs + \setlength{\parindent}{0pt} + % turn on hanging indent if param 1 is 1 + \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi + % set entry spacing + \ifnum #2 > 0 + \setlength{\parskip}{#2\baselineskip} + \fi + }% + {} +\usepackage{calc} +\newcommand{\CSLBlock}[1]{#1\hfill\break} +\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}} +\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break} +\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1} + +\begin{document} + +I enjoyed Doe (1979). + +Seymour Butz +School of Studies + +% Refs should be below this + +\hypertarget{works-cited}{% +\subsection*{Works cited}\label{works-cited}} +\addcontentsline{toc}{subsection}{Works cited}\hypertarget{refs}{} +\begin{CSLReferences}{1}{0} +\leavevmode\vadjust pre{\hypertarget{ref-Doe}{}}% +Doe, John. 1979. \emph{Short Stories}. + +\end{CSLReferences} + + +% +% From pandoc's template: placement +% of bibliography with natbib and biblatex + + + + +\end{document} diff --git a/bib-place/expected_unfiltered.html b/bib-place/expected_unfiltered.html new file mode 100644 index 00000000..0957e6d3 --- /dev/null +++ b/bib-place/expected_unfiltered.html @@ -0,0 +1,24 @@ + + + Sample document + + + +

I enjoyed Doe (1979).

+

Works cited

+
+
+Doe, John. 1979. Short Stories. +
+
+ +Seymour Butz +School of Studies + + + + + + + + diff --git a/bib-place/expected_unfiltered.tex b/bib-place/expected_unfiltered.tex new file mode 100644 index 00000000..454dfc5e --- /dev/null +++ b/bib-place/expected_unfiltered.tex @@ -0,0 +1,57 @@ +% A minimal template + +% Preamble commands for each bibliography engine +% These are included in Pandoc's default template +% +\newlength{\cslhangindent} +\setlength{\cslhangindent}{1.5em} +\newlength{\csllabelwidth} +\setlength{\csllabelwidth}{3em} +\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing + {% don't indent paragraphs + \setlength{\parindent}{0pt} + % turn on hanging indent if param 1 is 1 + \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi + % set entry spacing + \ifnum #2 > 0 + \setlength{\parskip}{#2\baselineskip} + \fi + }% + {} +\usepackage{calc} +\newcommand{\CSLBlock}[1]{#1\hfill\break} +\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}} +\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break} +\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1} + +\begin{document} + +I enjoyed Doe (1979). + +\hypertarget{works-cited}{% +\subsection*{Works cited}\label{works-cited}} +\addcontentsline{toc}{subsection}{Works cited} + +\hypertarget{refs}{} +\begin{CSLReferences}{1}{0} +\leavevmode\vadjust pre{\hypertarget{ref-Doe}{}}% +Doe, John. 1979. \emph{Short Stories}. + +\end{CSLReferences} + +Seymour Butz +School of Studies + +% Refs should be below this + + + + +% +% From pandoc's template: placement +% of bibliography with natbib and biblatex + + + + +\end{document} diff --git a/bib-place/sample.bib b/bib-place/sample.bib new file mode 100644 index 00000000..c63b34e1 --- /dev/null +++ b/bib-place/sample.bib @@ -0,0 +1,5 @@ +@book{Doe, + title = "Short Stories", + author = "Doe, John", + year = 1979, +} diff --git a/bib-place/sample.md b/bib-place/sample.md new file mode 100644 index 00000000..0a4e364d --- /dev/null +++ b/bib-place/sample.md @@ -0,0 +1,11 @@ +--- +title: Sample document +author: Seymour Butz +institute: School of Studies +bibliography: sample.bib +... + +I enjoyed @Doe. + +## Works cited {.refs} + diff --git a/bib-place/template.html b/bib-place/template.html new file mode 100644 index 00000000..063a9d98 --- /dev/null +++ b/bib-place/template.html @@ -0,0 +1,20 @@ + + + $if(title-prefix)$$title-prefix$ – $endif$$pagetitle$ + + + +$body$ + +$for(author)$$author$$endfor$ +$if(institute)$ +$for(institute)$$institute$$endfor$ +$endif$ + + + +$if(referencesblock)$$referencesblock$$endif$ + + + + diff --git a/bib-place/template.latex b/bib-place/template.latex new file mode 100644 index 00000000..8f55572c --- /dev/null +++ b/bib-place/template.latex @@ -0,0 +1,89 @@ +% A minimal template + +% Preamble commands for each bibliography engine +% These are included in Pandoc's default template +% +$if(natbib)$ +\usepackage[$natbiboptions$]{natbib} +\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$} +$endif$ +$if(biblatex)$ +\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex} +$for(bibliography)$ +\addbibresource{$bibliography$} +$endfor$ +$endif$ +$if(csl-refs)$ +\newlength{\cslhangindent} +\setlength{\cslhangindent}{1.5em} +\newlength{\csllabelwidth} +\setlength{\csllabelwidth}{3em} +\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing + {% don't indent paragraphs + \setlength{\parindent}{0pt} + % turn on hanging indent if param 1 is 1 + \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi + % set entry spacing + \ifnum #2 > 0 + \setlength{\parskip}{#2\baselineskip} + \fi + }% + {} +\usepackage{calc} +\newcommand{\CSLBlock}[1]{#1\hfill\break} +\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}} +\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break} +\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1} +$endif$ + +\begin{document} + +$body$ + +$for(author)$$author$$endfor$ +$if(institute)$ +$for(institute)$$institute$$endfor$ +$endif$ + +% Refs should be below this + +$if(referencesblock)$$referencesblock$$endif$ + + +% +% From pandoc's template: placement +% of bibliography with natbib and biblatex + +$if(natbib)$ + $if(bibliography)$ + $if(biblio-title)$ + $if(has-chapters)$ + \renewcommand\bibname{$biblio-title$} + $else$ + \renewcommand\refname{$biblio-title$} + $endif$ + $endif$ + $if(beamer)$ + \begin{frame}[allowframebreaks]{$biblio-title$} + \bibliographytrue + $endif$ + \bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} + $if(beamer)$ + \end{frame} + $endif$ + $endif$ +$endif$ + +$if(biblatex)$ + $if(beamer)$ + \begin{frame}[allowframebreaks]{$biblio-title$} + \bibliographytrue + \printbibliography[heading=none] + \end{frame} + $else$ +\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$ + $endif$ +$endif$ + + +\end{document}