Skip to content

Commit

Permalink
Update bibliography_entries() lua filter and internals for pandoc change
Browse files Browse the repository at this point in the history
Resolves #246
  • Loading branch information
mitchelloharawild committed Nov 23, 2023
1 parent bcb45bb commit 738a98c
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 127 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# vitae (development version)

* Fixed `bibliography_entries()` failing to render PDF outputs with recent
versions of pandoc (#246).

# vitae 0.5.3

Small release to resolve CRAN checks and removed dependency on the unmaintained
Expand Down
7 changes: 3 additions & 4 deletions R/bibliography.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,16 @@ knit_print.vitae_bibliography <- function(x, options = knitr::opts_current$get()
endlabel <- x %@% "endlabel"

# Convert file separator to format expected by citeproc
file <- gsub("\\", "/", file, fixed = TRUE)
# file <- gsub("\\", "/", file, fixed = TRUE)

out <- glue(
'
::: {#bibliography}
<< file >>
::: {#refs-<< rlang::hash_file(file) >>}
:::
',
.open = "<<", .close = ">>"
)
knitr::asis_output(out, meta = list(structure(x$id, class = "vitae_nocite")))
knitr::asis_output(out, meta = list(structure(list(file = file, id = x$id), class = "vitae_nocite")))
}
23 changes: 9 additions & 14 deletions R/cv_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,8 @@ cv_document <- function(..., pandoc_args = NULL, pandoc_vars = NULL,
pandoc_args <- c(pandoc_args, rmarkdown::pandoc_variable_arg(names(pandoc_vars)[[i]], pandoc_vars[[i]]))
}

# Inject multiple-bibliographies lua filter
mult_bib <- file.path(tempdir(), "multiple-bibliographies.lua")
if(rmarkdown::pandoc_version() <= package_version("2.7.3")) {
warn(sprintf("Detected pandoc version %s, which may cause issues with bibliography_entries().
Please update pandoc if you have any issues knitting bibliographies (this can be done by updating RStudio).", rmarkdown::pandoc_version()))
}
cat(
gsub("<<PANDOC_PATH>>", rmarkdown::find_pandoc()$dir, fixed = TRUE,
readLines(system.file("multiple-bibliographies.lua", package = "vitae", mustWork = TRUE), encoding = "UTF-8")),
file = mult_bib, sep = "\n"
)

pandoc_args <- c(
c(rbind("--lua-filter", mult_bib)),
c(rbind("--lua-filter", system.file("multiple-bibliographies.lua", package = "vitae", mustWork = TRUE))),
pandoc_args
)

Expand All @@ -41,8 +29,15 @@ Please update pandoc if you have any issues knitting bibliographies (this can be
files_dir, output_dir)

# Add citations to front matter yaml, there may be a better way to do this.
# For example, @* wildcard. Keeping as is to avoid unintended side effects.
meta_nocite <- vapply(knit_meta, inherits, logical(1L), "vitae_nocite")
metadata$nocite <- c(metadata$nocite, paste0("@", do.call(c, knit_meta[meta_nocite]), collapse = ", "))

bib_files <- lapply(knit_meta[meta_nocite], function(x) x$file)
names(bib_files) <- vapply(bib_files, rlang::hash_file, character(1L))
metadata$bibliography <- bib_files

bib_ids <- unique(unlist(lapply(knit_meta[meta_nocite], function(x) x$id)))
metadata$nocite <- c(metadata$nocite, paste0("@", bib_ids, collapse = ", "))
if(is.null(metadata$csl)) metadata$csl <- system.file("vitae.csl", package = "vitae", mustWork = TRUE)

body <- partition_yaml_front_matter(xfun::read_utf8(input_file))$body
Expand Down
104 changes: 56 additions & 48 deletions inst/multiple-bibliographies.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--[[
multiple-bibliographies – create multiple bibliographies
multibib – create multiple bibliographies
Copyright © 2018-2020 Albert Krewinkel
Modified 19/10/2020 by Mitchell O'Hara-Wild
Copyright © 2018-2022 Albert Krewinkel
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,69 +15,63 @@ 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.
]]
PANDOC_VERSION:must_be_at_least '2.11'

local List = require 'pandoc.List'
local utils = require 'pandoc.utils'
local stringify = utils.stringify
local run_json_filter = utils.run_json_filter

--- get the type of meta object
local metatype = pandoc.utils.type or
function (v)
local metatag = type(v) == 'table' and v.t and v.t:gsub('^Meta', '')
return metatag and metatag ~= 'Map' and metatag or type(v)
end

--- 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.
--- Div used by 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)()
-- Div filled by citeproc with properties set according to
-- the output format and the attributes of cs:bibliography
local refs_div_with_properties

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}
)
--- Run citeproc on a pandoc document
local citeproc
if utils.citeproc then
-- Built-in Lua function
citeproc = utils.citeproc
else
-- Use pandoc as a citeproc processor
citeproc = function (doc)
local opts = {'--from=json', '--to=json', '--citeproc', '--quiet'}
return run_json_filter(doc, 'pandoc', opts)
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)
local bibconf = meta.bibliography
meta.bibliography = pandoc.MetaList{}
if metatype(bibconf) == 'table' then
for _, value in pairs(bibconf) do
table.insert(meta.bibliography, stringify(value))
end
end
-- add dummy div to catch the created bibliography
-- add refs 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)
doc = citeproc(doc)
-- remove catch-all bibliography and keep it for future use
refs_div_with_properties = table.remove(doc.blocks)
-- restore bibliography to original value
doc.meta.bibliography = orig_bib
return doc
Expand All @@ -103,22 +96,37 @@ local function meta_for_pandoc_citeproc (bibliography)
return new_meta
end

local function remove_duplicates(classes)
local seen = {}
return classes:filter(function(x)
if seen[x] then
return false
else
seen[x] = true
return true
end
end)
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
-- ID starts with "refs", followed by nothing but underscores and
-- alphanumeric characters.
local function create_topic_bibliography (div)

local is_bib = div.identifier == 'bibliography'
if not is_bib then
local name = div.identifier:match('^refs[-_]?([-_%w]*)$')
local bibfile = name and (doc_meta.bibliography or {})[name]
if not bibfile 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
local res = citeproc(tmp_doc)
-- First block of the result contains the dummy paragraph, second is
-- the refs Div filled by pandoc-citeproc.
-- the refs Div filled by citeproc.
div.content = res.blocks[2].content
-- Set the classes and attributes as citeproc did it on refs_div
div.classes = remove_duplicates(refs_div_with_properties.classes)
div.attributes = refs_div_with_properties.attributes
return div
end

Expand Down
41 changes: 28 additions & 13 deletions inst/rmarkdown/templates/awesomecv/resources/awesome-cv.tex
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,41 @@
$endfor$

% Pandoc CSL macros
$if(csl-refs)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{2em}
\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
{% don't indent paragraphs
\setlength{\parindent}{0pt}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{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}
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
}%
{}
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\honortitlestyle{#1}}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\honordatestyle{#1}}}
\newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$

\begin{document}

Expand Down
39 changes: 27 additions & 12 deletions inst/rmarkdown/templates/hyndman/resources/hyndmantemplate.tex
Original file line number Diff line number Diff line change
Expand Up @@ -270,26 +270,41 @@
\setlength\LTright{0pt}

% Pandoc CSL macros
$if(csl-refs)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[3] % #1 hanging-ident, #2 entry spacing
{% don't indent paragraphs
\setlength{\parindent}{0pt}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{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}
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
}%
{}
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\hfill #1~}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \cslhangindent - \csllabelwidth}{#1}\vspace{0.8ex}}
\newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$


\def\endfirstpage{\newpage}
Expand Down
39 changes: 27 additions & 12 deletions inst/rmarkdown/templates/latexcv/resources/classic/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,41 @@


% Pandoc CSL macros
$if(csl-refs)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[3] % #1 hanging-ident, #2 entry spacing
{% don't indent paragraphs
\setlength{\parindent}{0pt}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{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}
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
}%
{}
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}}
\newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$


%============================================================================%
Expand Down
Loading

0 comments on commit 738a98c

Please sign in to comment.