Skip to content

Commit

Permalink
Automatically add citations to reference (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy authored Mar 11, 2021
1 parent dbd35a0 commit 50749c3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## New features

* `colformat_md` supports multiple paragprahs by collapsing them with a separator given to the `.sep` argument (default: `"\n\n").
* `colformat_md` supports multiple paragprahs by collapsing them with a separator given to the `.sep` argument (default: `"\n\n") (#43).
* `colformat_md` can now automatically add citations to reference on R Markdown (#48).

## Bug fixes

Expand Down
10 changes: 8 additions & 2 deletions R/colformat.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ colformat_md <- function(x,
return(x)
}

texts <- unlist(dataset[col], use.names = FALSE)

# Must evaluate outside add_footnotes due to lazy evaluation of arguments
ft <- flextable::compose(x,
i = seq(nrow(dataset)), j = col, part = part,
value = as_paragraph_md(
unlist(dataset[col], use.names = FALSE),
texts,
auto_color_link = auto_color_link,
.from = .from,
md_extensions = md_extensions,
Expand All @@ -67,7 +69,11 @@ colformat_md <- function(x,
.sep = .sep
))

add_footnotes(ft, part, .footnote_options)
structure(
add_footnotes(ft, part, .footnote_options),
class = c("ftExtra", class(ft)),
citations = collect_citations(paste(texts, collapse = "\n\n"))
)
}

where <- function(...) {
Expand Down
14 changes: 14 additions & 0 deletions R/collect-citations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
collect_citations <- function(x, .from = 'markdown') {
tf <- tempfile()
xfun::write_utf8(x, tf)
rmarkdown::pandoc_convert(
input = tf,
to = "markdown",
from = .from,
output = tf,
citeproc = FALSE,
options = lua("cite.lua"),
wd = getwd()
)
paste(readLines(tf), collapse = "")
}
27 changes: 27 additions & 0 deletions R/knit-print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' knit_print for extended flextable object
#'
#' Adds a YAML meta data block to cite materials found by `colformat_md()` in
#' the `flextable` object.
#'
#' @inheritParams knitr::knit_print
#' @param label
#' A key to be set in the YAML meta data block to cite materials.
#' This must be unique in the document.
#' Default value is the chunk label prepended by "ftExtra-cite-".
#'
#' @return The `knit_asis` class object.
#' @noRd
knit_print.ftExtra <- function(x,
options,
...,
key = paste0("ftExtra-cite-", options$label)) {
ft <- NextMethod("knit_print", x, options, ...)

cite <- attr(x, "citations", exact = TRUE)

if (cite == "") return(ft)

res <- sprintf('---\n%s: "%s"\n---\n\n%s', key, cite, ft)
attributes(res) <- attributes(ft)
res
}
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.onLoad <- function(libname, pkgname) {
registerS3method("knit_print", "ftExtra", knit_print.ftExtra, asNamespace("knitr"))
}
4 changes: 3 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions inst/lua/cite.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cite = {}

function Cite(elem)
table.insert(cite, elem)
table.insert(cite, pandoc.Space())
end

function Pandoc(doc)
table.remove(cite)
doc.blocks = {pandoc.Para(cite)}
return doc
end

0 comments on commit 50749c3

Please sign in to comment.