Skip to content

Commit

Permalink
Force other language citation check when running render()
Browse files Browse the repository at this point in the history
* Gives warning only if the other language citation is missing
* Must have citation_french: to make it work properly
  • Loading branch information
cgrandin committed May 26, 2024
1 parent 662316a commit 527e668
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(render)
export(resdoc_pdf)
export(resdoc_word)
export(run_pdflatex)
export(set_citations)
export(set_french)
export(sr_pdf)
export(sr_word)
Expand Down
1 change: 1 addition & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ render <- function(yaml_fn = "_bookdown.yml",
# It sets `options(french)` to the value in the file
set_language_option(index_fn, verbose)

set_citations(index_fn, verbose)

# Set up the console Render message
csas_render_type <- gsub("(.*)_\\S+$", "\\1", render_type)
Expand Down
73 changes: 73 additions & 0 deletions R/set-citations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Set the 'other' language citation up so that page 2 has the correct other
#' language based on the value of `fr()`
#'
#' @param fn The bookdown index filename, typically index.Rmd. This file
#' must have a YAML option called 'french' set to either 'true' or 'false'
#' @param verbose Logical. If `TRUE`, print messages
#'
#' @return
#' @export
set_citations <- function(fn = get_index_filename(
system.file("rmarkdown",
"templates",
"resdoc", # All types have the same index filename
"skeleton",
"_bookdown.yml",
package = "csasdown")),
verbose = FALSE){

if(verbose){
notify("Checking file ", fn_color(fn), " for citations ...")
}

if(!file.exists(fn)){
bail("File ", fn_color(fn), " does not exist")
}

rmd <- readLines(fn)
ca_pat <- "^citation_english: *(.*)$"
cf_pat <- "^citation_french: *(.*)$"
ca_ind <- grep(ca_pat, rmd)
cf_ind <- grep(cf_pat, rmd)

if(fr()){
if(!length(ca_ind)){
warning("You are missing the `citation_english:` tag in your YAML file:\n",
fn, ". This citation is required when you build in French as it ",
"is set to the other language citation on page 2 of ",
"the document")
return(invisible())
}
}else{
if(!length(cf_ind)){
warning("You are missing the `citation_french:` tag in your YAML file:\n",
fn, ". This citation is required when you build in English as it ",
"is set to the other language citation on page 2 of ",
"the document")
return(invisible())
}
}
ca <- rmd[ca_ind]
ca <- gsub(ca_pat, "\\1", ca)
cf <- rmd[cf_ind]
cf <- gsub(cf_pat, "\\1", cf)

cother_ind <- grep("^citation_other_language: *", rmd)
cother <- paste0('citation_other_language: ',
ifelse(fr(),
ca,
cf))

if(length(cother_ind)){
rmd[cother_ind] <- cother
}else{
end_citations_ind <- max(ca_ind, cf_ind)
prev <- rmd[1:end_citations_ind]
last <- rmd[(end_citations_ind + 1):length(rmd)]
rmd <- c(prev,
cother,
last)
}

writeLines(rmd, fn)
}
23 changes: 23 additions & 0 deletions man/set_citations.Rd

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

0 comments on commit 527e668

Please sign in to comment.