Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow colformat_md to offset citation note number #50

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 20 additions & 7 deletions R/as-paragraph-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
Expand Down
9 changes: 7 additions & 2 deletions R/colformat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion R/md2ast.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down
273 changes: 136 additions & 137 deletions docs/articles/format_columns.html

Large diffs are not rendered by default.

424 changes: 212 additions & 212 deletions docs/articles/group-rows.html

Large diffs are not rendered by default.

202 changes: 101 additions & 101 deletions docs/articles/transform-headers.html

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions docs/example.html

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

2 changes: 2 additions & 0 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ articles:
format_columns: format_columns.html
group-rows: group-rows.html
transform-headers: transform-headers.html
last_built: 2021-03-07T13:23Z
last_built: 2021-03-11T13:08Z

8 changes: 7 additions & 1 deletion docs/reference/colformat_md.html

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

15 changes: 15 additions & 0 deletions inst/lua/cite-offset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function Meta(meta)
offset = meta.citationNoteNumOffset or 0
end

function add(x)
return math.floor(tonumber(x) + offset)
end

function Cite(cite)
cite.citations[1].note_num = math.floor(offset + cite.citations[1].note_num)
cite.content[1].text = cite.content[1].text:gsub("%d+", add)
return cite
end

return {{Meta = Meta}, {Cite = Cite}}
6 changes: 5 additions & 1 deletion man/colformat_md.Rd

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

9 changes: 3 additions & 6 deletions vignettes/format_columns.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,10 @@ knitr::write_bib("ftExtra")
```

Second, specify it within the YAML front matter.
It may also be required to cite references in the `nocite` field.

```yaml
---
bibliography: ftExtra.bib
nocite: @R-ftExtra
---
```

Expand All @@ -286,10 +284,6 @@ data.frame(
flextable::autofit(add_w = 0.2)
```

---
no-cite: "@R-ftExtra"
---

```{r, echo=FALSE, warning=FALSE}
tf <- tempfile(fileext = ".bib")
knitr::write_bib("ftExtra", tf)
Expand All @@ -302,6 +296,9 @@ data.frame(
flextable::autofit(add_w = 0.2)
```

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)`.

# Math

The rendering of math is also possible.
Expand Down