-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add citations to reference (#48)
- Loading branch information
Showing
7 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |