diff --git a/NEWS.md b/NEWS.md index 09ebd79..51174bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ * `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). +* `colformat_md` gains `.cite_offset` option (default: 0) to offset note number when the citation style such as vancouver requires the number be consistent with the body (#50). ## Bug fixes diff --git a/R/as-paragraph-md.R b/R/as-paragraph-md.R index 771dd96..8d5b9d5 100644 --- a/R/as-paragraph-md.R +++ b/R/as-paragraph-md.R @@ -34,7 +34,11 @@ lua <- function(...) { c("--lua-filter", system.file("lua", ..., package = "ftExtra")) } -lua_filters <- function(.sep) { +meta <- function(key, val) { + sprintf("--metadata=%s:%s", key, val) +} + +lua_filters <- function(.sep = "\n\n", .cite_offset = 0) { if (!rmarkdown::pandoc_available("2")) return(NULL) c( @@ -43,33 +47,42 @@ lua_filters <- function(.sep) { if (rmarkdown::pandoc_available("2.7.3")) { c( lua("math.lua"), - paste0("--metadata=pandoc-path:", rmarkdown::pandoc_exec()), + meta("pandoc_path", rmarkdown::pandoc_exec()), if (!rmarkdown::pandoc_available("2.10")) { - paste0("--metadata=temporary-directory:", tempdir()) + meta("temporary-directory", tempdir()) } ) }, + if (.cite_offset != 0) { + c( + rmarkdown::pandoc_citeproc_args(), + lua("cite-offset.lua"), + meta("citationNoteNumOffset", .cite_offset) + ) + }, if (rmarkdown::pandoc_available("2.2.3")) { - c(lua("blocks-to-inlines.lua"), paste0("--metadata=sep_blocks:", .sep)) + c(lua("blocks-to-inlines.lua"), meta("sep_blocks", .sep)) } ) } - +#' @param ... +#' parameters passed to lua_filters +#' @noRd parse_md <- function(x, auto_color_link = "blue", pandoc_args = NULL, .from = "markdown", .footnote_options = NULL, - .sep = "\n\n") { + ...) { if (!is.character(auto_color_link) || length(auto_color_link) != 1L) { stop("`auto_color_link` must be a string") } md_df <- md2df( x, - pandoc_args = c(lua_filters(.sep = .sep), pandoc_args), + pandoc_args = c(lua_filters(...), pandoc_args), .from = .from, .check = TRUE ) diff --git a/R/colformat.R b/R/colformat.R index 8176d60..4d1452d 100644 --- a/R/colformat.R +++ b/R/colformat.R @@ -10,6 +10,9 @@ #' Options for footnotes generated by `footnote_options()`. #' @param .sep #' A separator of paragraphs (default: `"\n\n"`) +#' @param .cite_offset +#' A offset value of citation note number (default: 0). +#' Use this to start number higher 0 for the consistency with the body text. #' @inheritParams as_paragraph_md #' #' @examples @@ -30,7 +33,8 @@ colformat_md <- function(x, pandoc_args = NULL, .from = "markdown+autolink_bare_uris", .footnote_options = footnote_options(), - .sep = "\n\n" + .sep = "\n\n", + .cite_offset = 0 ) { .j <- rlang::enexpr(j) part <- match.arg(part) @@ -66,7 +70,8 @@ colformat_md <- function(x, md_extensions = md_extensions, pandoc_args = pandoc_args, .footnote_options = .footnote_options, - .sep = .sep + .sep = .sep, + .cite_offset = .cite_offset )) structure( diff --git a/R/md2ast.R b/R/md2ast.R index 217eb67..da286cb 100644 --- a/R/md2ast.R +++ b/R/md2ast.R @@ -20,7 +20,11 @@ md2ast <- function(x, to = "json", from = .from, output = tf, - citeproc = !is.null(yaml$bibliography) || any(grepl("^--bibliography", pandoc_args)), + citeproc = ( + !all(rmarkdown::pandoc_citeproc_args() %in% pandoc_args) && + !is.null(yaml$bibliography) || + any(grepl("^--bibliography", pandoc_args)) + ), options = pandoc_args, wd = getwd() ) diff --git a/docs/articles/format_columns.html b/docs/articles/format_columns.html index fc24698..c0275a1 100644 --- a/docs/articles/format_columns.html +++ b/docs/articles/format_columns.html @@ -125,7 +125,7 @@

"Al", as_sub("2"), "O", as_sub("3") ) ) -
+
+

If citation style such as Vancouver requires citations be numbered sequentially and consistently with the body, manually offset the number for example by colformat_md(.cite_offset = 5).

@@ -1049,7 +1048,7 @@

as_flextable() %>% colformat_md() %>% flextable::autofit(add_w = 0.2)

-
+
 grouped_iris %>% as_flextable(hide_grouplabel = TRUE)
-
+

The sep argument defines delimiters by regular expression. The default delimiters are . and _. Let’s see what happens when seop = "e"

 ft %>% separate_header(sep = "e")
-
+