Skip to content

Commit

Permalink
Create robust function to compare Pandoc version numbers
Browse files Browse the repository at this point in the history
* Cannot just compare strings, eg: "3.1.12" < "3.1.8" is TRUE
* Split into chunks and directly compare as numeric values
* Add argument so it is easier to change the comparison version in the future
* Create docs
  • Loading branch information
cgrandin committed Mar 21, 2024
1 parent c7d3522 commit ac8e2dd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(fr)
export(get_french)
export(manureport_pdf)
export(manureport_word)
export(pandoc_curr_ver_is_before)
export(render)
export(resdoc_pdf)
export(resdoc_word)
Expand Down
55 changes: 50 additions & 5 deletions R/fix-envs.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fix_envs <- function(x,
}
x <- inject_refstepcounters(x)

if (pandoc_before_3.1.8()) {
if (pandoc_curr_ver_is_before()) {
# Need to remove hypertarget for references to appendices to work:
# rs_line <- grep("\\\\refstepcounter", x)
# FIXME: make more robust
Expand Down Expand Up @@ -206,7 +206,7 @@ fix_envs <- function(x,

# Move the bibliography to before the appendices:
if (length(references_insertion_line) > 0) {
if (pandoc_before_3.1.8()) {
if (pandoc_curr_ver_is_before()) {
references_begin <- grep("^\\\\hypertarget\\{refs\\}\\{\\}$", x)
} else {
references_begin <- grep("^\\\\phantomsection\\\\label\\{refs\\}$", x)
Expand Down Expand Up @@ -371,11 +371,11 @@ fix_envs <- function(x,
# ! You can't use `\vadjust' in vertical mode.
# \leavevmode\vadjust
# pre{\hypertarget{ref-edwards2013}{}}%
if (pandoc_before_3.1.8()) {
if (pandoc_curr_ver_is_before()) {
x <- gsub("\\\\vadjust pre", "", x)
}
# Enable reference linking to subsections of appendices
# if (!pandoc_before_3.1.8()) {
# if (!pandoc_curr_ver_is_before()) {
# stop("csasdown currently only works with pandoc < 3.1.7. Please revert to an older pandoc version.", call. = FALSE)
# }
x <- add_appendix_subsection_refs(x)
Expand Down Expand Up @@ -423,4 +423,49 @@ region_info <- tibble::tribble(
"Quebec Region", "R\u00E9gion du Qu\u00E9bec", "[email protected]", "850 route de la Mer, P.O. Box 1000\\\\\\\\Mont-Joli, QC\\\\enspace G5H 3Z4", "850 route de la Mer, P.O. Box 1000\\\\\\\\Mont-Joli (QC) G5H 3Z4"
)

pandoc_before_3.1.8 <- function() rmarkdown::pandoc_version() < '3.1.8'
#' Checks whether or not the current version of Pandoc installed is below a
#' given version
#'
#' @param ver The version to compare the current version against. If `NULL`,
#' an error is thrown
#'
#' @return If the current version of Pandoc is prior to this dotted string
#' version, `TRUE`. If not `FALSE` is returned.
#' @export
pandoc_curr_ver_is_before <- function(ver = "3.1.8"){

if(is.null(ver)){
stop("Must supply a version string to compare (`ver`)")
}
ver_curr <- as.character(rmarkdown::pandoc_version())
ver_curr_vec <- strsplit(ver_curr, "\\.")[[1]] |> as.numeric()
ver_curr_len <- length(ver_curr_vec)

ver_vec <- strsplit(ver, "\\.")[[1]] |> as.numeric()
ver_vec_len <- length(ver_vec)
# Add NAs to the end of the comparison version if the current pandoc
# version has more dotted numbers than the comparison version
length(ver_vec) <- length(ver_curr_vec)
# Change the NAs to zeros if there are any
# For example if ver originally was 3.1.8 and pandoc version (ver_curr) was
# 3.1.12.1 then ver will be 3.1.8.0 to match in length the pandoc version
# after this next call
ver_vec[is.na(ver_vec)] <- 0

# Compare each chunk of the dotted version number. Can return `TRUE` before
# finishing all the compares if the current version chunk's value is less than
# the comparison chunk's value or `FALSE` if the comparison chunk's value is
# less than the current version chunk's value
for(i in seq_along(ver_vec)){
if(ver_curr_vec[i] != ver_vec[i]){
if(ver_curr_vec[i] < ver_vec[i]){
return(TRUE)
}
if(ver_vec[i] < ver_curr_vec[i]){
return(FALSE)
}
}
}

FALSE
}
21 changes: 21 additions & 0 deletions man/pandoc_curr_ver_is_before.Rd

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

0 comments on commit ac8e2dd

Please sign in to comment.