diff --git a/DESCRIPTION b/DESCRIPTION index 5b03dac..751a796 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: csasdown Title: Reproducible CSAS Reports with Bookdown -Version: 0.1.7 +Version: 0.1.8 Authors@R: c(person(given = c("Sean", "C."), family = "Anderson", diff --git a/NEWS.md b/NEWS.md index 8813d30..b5d5b1b 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # csasdown +# csasdown 0.1.8 + +* Revert some recent commits so the main branch works. + # csasdown 0.1.7 * Merge in in-progress fsar template. diff --git a/R/fix-envs.R b/R/fix-envs.R index 6648028..04fd501 100644 --- a/R/fix-envs.R +++ b/R/fix-envs.R @@ -34,52 +34,49 @@ fix_envs <- function(x, region_line <- grep(pattern = "% Region", x) + 1 # If region is specified if (length(region_line) > 0) { - # Get region name and contact info pat <- "\\\\rdRegion\\}\\{(.*?)\\}+$" - region_vec <- regmatches(x[region_line], - regexec(pat, - x[region_line]))[[1]] - + region_vec <- regmatches( + x = x[region_line], + m = regexec( + pattern = pat, + text = x[region_line] + ) + )[[1]] region <- region_supplied <- region_vec[2] region_def_ind <- grep(pat, x) - - if(fr()) { + if(fr()){ # If the author supplied an English region name for a French doc, convert it eng_match <- grep(region_supplied, region_info$Region) if(length(eng_match)){ region <- region_info[eng_match, ]$RegionFr if(length(region_def_ind)){ - x[region_def_ind] <- gsub(region_supplied, - region, - x[region_def_ind]) + x[region_def_ind] <- gsub(region_supplied, region, x[region_def_ind]) } } - } else { + }else{ # If the author supplied an French region name for a English doc, convert it fr_match <- grep(region_supplied, region_info$RegionFr) if(length(fr_match)){ region <- region_info[fr_match, ]$Region if(length(region_def_ind)){ - x[region_def_ind] <- gsub(region_supplied, - region, - x[region_def_ind]) + x[region_def_ind] <- gsub(region_supplied, region, x[region_def_ind]) } } } contact_info <- get_contact_info(region = region) # Insert contact info - x <- sub("AddressPlaceholder", - contact_info$address, - x) - - x <- sub("EmailPlaceholder", - paste0("\\\\link\\{mailto:", - contact_info$email, - "\\}\\{", - contact_info$email, - "\\}"), - x) + x <- sub( + pattern = "AddressPlaceholder", replacement = contact_info$address, + x = x + ) + x <- sub( + pattern = "EmailPlaceholder", + replacement = paste0( + "\\\\link\\{mailto:", contact_info$email, + "\\}\\{", contact_info$email, "\\}" + ), x = x + ) } # End if region exists (SRs) ## Change csas-style to use the sty file found in csasdown repo g <- grep("csas-style", x) @@ -103,17 +100,13 @@ fix_envs <- function(x, } beg_reg <- "^\\s*\\\\begin\\{.*\\}" end_reg <- "^\\s*\\\\end\\{.*\\}" + i3 <- if (length(i1 <- grep(beg_reg, x))) (i1 - 1)[grepl("^\\s*$", x[i1 - 1])] - i3 <- if (length(i1 <- grep(beg_reg, x))) - (i1 - 1)[grepl("^\\s*$", x[i1 - 1])] - - i3 <- c(i3, - if (length(i2 <- grep(end_reg, x))) - (i2 + 1)[grepl("^\\s*$", x[i2 + 1])]) - - if (length(i3)) { - x <- x[-i3] - } + i3 <- c( + i3, + if (length(i2 <- grep(end_reg, x))) (i2 + 1)[grepl("^\\s*$", x[i2 + 1])] + ) + if (length(i3)) x <- x[-i3] g <- grep("\\\\Appendices$", x) if (identical(length(g), 0L)) { @@ -132,13 +125,15 @@ fix_envs <- function(x, for (i in seq(appendix_line + 1, length(x))) { x[i] <- gsub("\\\\section\\{", "\\\\appsection\\{", x[i]) if (!fr()) { - x[i] <- gsub("\\\\chapter\\{", - "\\\\starredchapter\\{APPENDIX~\\\\thechapter. ", - x[i]) + x[i] <- gsub( + "\\\\chapter\\{", + "\\\\starredchapter\\{APPENDIX~\\\\thechapter. ", x[i] + ) } else { - x[i] <- gsub("\\\\chapter\\{", - "\\\\starredchapter\\{ANNEXE~\\\\thechapter. ", - x[i]) + x[i] <- gsub( + "\\\\chapter\\{", + "\\\\starredchapter\\{ANNEXE~\\\\thechapter. ", x[i] + ) } } @@ -155,8 +150,8 @@ fix_envs <- function(x, x <- gsub("^.*\\\\tightlist$", "", x) - # \eqref needs to be \ref so the equation references don't have () - # around them: https://tex.stackexchange.com/a/107425 + # \eqref needs to be \ref so the equation references don't have () around them + # https://tex.stackexchange.com/a/107425 x <- gsub("\\\\eqref\\{", "\\\\ref\\{", x) # Non-breaking spaces: @@ -166,11 +161,11 @@ fix_envs <- function(x, # ---------------------------------------------------------------------- # Add tooltips so that figures have alternative text for read-out-loud figlabel_lines <- x[grep("\\\\label\\{fig:", x)] - fig_labels <- gsub("\\\\caption\\{(.*?)\\}\\\\label\\{fig:(.*?)\\}", - "\\2", - figlabel_lines) - all_include_graphics <- grep("(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", - x) + fig_labels <- gsub( + "\\\\caption\\{(.*?)\\}\\\\label\\{fig:(.*?)\\}", + "\\2", figlabel_lines + ) + all_include_graphics <- grep("(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", x) # is this a true figure with a caption in Pandoc style? all_include_graphics <- @@ -179,20 +174,20 @@ fix_envs <- function(x, if (identical(length(fig_labels), length(all_include_graphics))) { for (i in seq_along(all_include_graphics)) { x[all_include_graphics[i]] <- - gsub("(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", - paste0("\\\\pdftooltip{\\1}{", - "Figure \\\\ref{fig:", - fig_labels[i], "}}"), - x[all_include_graphics[i]]) + gsub( + "(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", + paste0("\\\\pdftooltip{\\1}{", "Figure \\\\ref{fig:", fig_labels[i], "}}"), + x[all_include_graphics[i]] + ) } } else { # nocov start - alert("The number of detected figure captions did not match ", - "the number of detected figures. Reverting to unnumbered ", - "alternative text figures.") - x <- gsub("(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", - "\\\\pdftooltip{\\1}{Figure}", - x) + alert("The number of detected figure captions did not match the number of ", + "detected figures. Reverting to unnumbered alternative text figures.") + x <- gsub( + "(\\\\includegraphics\\[(.*?)\\]\\{(.*?)\\})", + "\\\\pdftooltip{\\1}{Figure}", x + ) # nocov end } # ---------------------------------------------------------------------- @@ -200,19 +195,13 @@ fix_envs <- function(x, regexs <- c( "^\\\\CHAPTER\\*\\{R\\p{L}F\\p{L}RENCES", # English or French "^\\\\SECTION{SOURCES DE RENSEIGNEMENTS}", - "^\\\\SECTION{SOURCES OF INFORMATION}") - - .matches <- lapply(regexs, \(.x){ - grep(.x, toupper(x), perl = TRUE) + 1}) - + "^\\\\SECTION{SOURCES OF INFORMATION}" + ) + .matches <- lapply(regexs, function(.x) grep(.x, toupper(x), perl = TRUE) + 1) references_insertion_line <- unlist(.matches) - x[references_insertion_line - 1] <- sub("chapter", - "section", - x[references_insertion_line - 1]) - x[references_insertion_line] <- sub("chapter", - "section", - x[references_insertion_line]) + x[references_insertion_line - 1] <- sub("chapter", "section", x[references_insertion_line - 1]) + x[references_insertion_line] <- sub("chapter", "section", x[references_insertion_line]) # Move the bibliography to before the appendices: if (length(references_insertion_line) > 0) { @@ -236,24 +225,16 @@ fix_envs <- function(x, "\\setlength{\\parskip}{8pt}", "", x[seq(references_begin, references_end)], - paste0("\\setlength{\\parindent}{0in} ", - "\\setlength{\\leftskip}{0in} ", - "\\setlength{\\parskip}{4pt}"), + "\\setlength{\\parindent}{0in} \\setlength{\\leftskip}{0in} \\setlength{\\parskip}{4pt}", x[seq(references_insertion_line + 1, references_begin - 1)], x[length(x)] ) # Modify References from starred chapter to regular chapter so that it is numbered - starred_references_line <- grep(paste0("\\\\section\\*\\{REFERENCES\\}", - "\\\\label\\{references\\}\\}"), - x) + starred_references_line <- grep("\\\\section\\*\\{REFERENCES\\}\\\\label\\{references\\}\\}", x) if(length(starred_references_line)){ - x[starred_references_line] <- gsub("\\*", - "", - x[starred_references_line]) + x[starred_references_line] <- gsub("\\*", "", x[starred_references_line]) # Remove the add contents line which was used to add the unnumbered section before - add_toc_contents_line <- grep(paste0("\\\\addcontentsline\\{toc\\}\\", - "{section\\}\\{REFERENCES\\}"), - x) + add_toc_contents_line <- grep("\\\\addcontentsline\\{toc\\}\\{section\\}\\{REFERENCES\\}", x) x[add_toc_contents_line] <- "" } # Modify References section name here @@ -261,15 +242,14 @@ fix_envs <- function(x, ref_ind <- grep("\\{REFERENCES", x) if(!length(ref_ind)){ # nocov start - bail("REFERENCES section header not found in the document. ", - "Make sure you haven't commented out that section in ", + bail("REFERENCES section header not found in the document. Make sure you ", + "haven't commented out that section in ", fn_color("_bookdown.yml"), " or changed the header name") # nocov end } - x[ref_ind] <- gsub("REFERENCES", - ifelse(fr(), - "R\u00c9F\u00c9RENCES CIT\u00c9ES", - "REFERENCES CITED"), x[ref_ind]) + x[ref_ind] <- gsub("REFERENCES", ifelse(fr(), + "R\u00c9F\u00c9RENCES CIT\u00c9ES", + "REFERENCES CITED"), x[ref_ind]) } } else { @@ -293,113 +273,93 @@ fix_envs <- function(x, label_app <- grep("^\\\\label\\{app:", x) for (i in seq_along(label_app)) { if (grepl("^\\\\section\\{", x[label_app[i] + 1])) { - x[seq(label_app[i], label_app[i] + 1)] <- - x[seq(label_app[i] + 1, label_app[i])] + x[seq(label_app[i], label_app[i] + 1)] <- x[seq(label_app[i] + 1, label_app[i])] } } # Implement "Approved pre-publication" version (science response) if (prepub) { # Text to add - addText <- ifelse(fr(), - " -- \\hl{\\,Ne\\:pas\\:distribuer\\,}}", - " -- \\hl{\\,Not\\:for\\:further\\:distribution\\,}}") + addText <- ifelse(fr(), " -- \\hl{\\,Ne\\:pas\\:distribuer\\,}}", + " -- \\hl{\\,Not\\:for\\:further\\:distribution\\,}}" + ) # 1. Modify header first page (report number) - rn_loc_1 <- grep("\\% Report number", x) + 1 - rn_loc_2 <- grep("\\% End of report number", x) - 1 + rn_loc_1 <- grep(pattern = "\\% Report number", x = x) + 1 + rn_loc_2 <- grep(pattern = "\\% End of report number", x = x) - 1 if (rn_loc_1 != rn_loc_2) { bail("Can't find report number (report_number)") # nocov } rn_text <- x[rn_loc_1] - rn_text_clean <- gsub("\\}+$", - "", - rn_text) + rn_text_clean <- gsub(pattern = "\\}+$", replacement = "", x = rn_text) rn_text_new <- paste0(rn_text_clean, "}", addText) x[rn_loc_1] <- rn_text_new # 2. Modify short title - st_loc_1 <- grep("\\% Title short", x) + 1 - st_loc_2 <- grep("\\% End of title short", x) - 1 - if (st_loc_1 != st_loc_2) { - bail("Can't find short title (title_short)") # nocov - } + st_loc_1 <- grep(pattern = "\\% Title short", x = x) + 1 + st_loc_2 <- grep(pattern = "\\% End of title short", x = x) - 1 + if (st_loc_1 != st_loc_2) bail("Can't find short title (title_short)") # nocov st_text <- x[st_loc_1] - st_text_clean <- gsub("\\}+$", "", st_text) + st_text_clean <- gsub(pattern = "\\}+$", replacement = "", x = st_text) st_text_new <- paste0(st_text_clean, addText) x[st_loc_1] <- st_text_new # 3. Modify citation (2 things) if (fr()) { # Edit french citation cite_head_fr <- grep( - paste0("La pr\\\\\'\\{e\\}sente publication ", - "doit \\\\\\^\\{e\\}tre cit\\\\\'\\{e\\}e ", - "comme suit~:"), - x) - if (length(cite_head_fr) == 0) { - bail("Can't find French citation header") # nocov - } + pattern = "La pr\\\\\'\\{e\\}sente publication doit \\\\\\^\\{e\\}tre cit\\\\\'\\{e\\}e comme suit~:", + x = x + ) + if (length(cite_head_fr) == 0) bail("Can't find French citation header") # nocov x[cite_head_fr] <- "Cite comme ceci (jusqu'\u00E0 la publication)~:" cite_loc_fr <- grep( - "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", - x = x) - if (length(cite_loc_fr) == 0) { - bail("Can't find French citation") # nocov - } + pattern = "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x + ) + if (length(cite_loc_fr) == 0) bail("Can't find French citation") # nocov x[cite_loc_fr] <- "\\citeFr{Sous presse}" # Nuke english citation cite_head_eng <- grep( - "\\\\emph\\{Also available in English:\\}", - x) - if (length(cite_head_eng) == 0) { - bail("Can't find English citation header") # nocov - } + pattern = "\\\\emph\\{Also available in English:\\}", + x = x + ) + if (length(cite_head_eng) == 0) bail("Can't find English citation header") # nocov x[cite_head_eng] <- "" cite_loc_eng <- grep( - "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", - x) - if (length(cite_loc_eng) == 0) { - bail("Can't find English citation") # nocov - } + pattern = "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x + ) + if (length(cite_loc_eng) == 0) bail("Can't find English citation") # nocov x[cite_loc_eng] <- "" } else { # Edit english citation cite_head_eng <- grep( - "Correct Citation for this Publication:", - x) - if (length(cite_head_eng) == 0) { - bail("Can't find English citation header") # nocov - } + pattern = "Correct Citation for this Publication:", + x = x + ) + if (length(cite_head_eng) == 0) bail("Can't find English citation header") # nocov x[cite_head_eng] <- "Correct citation (until published):" cite_loc_eng <- grep( - "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", - x) + pattern = "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x + ) if (length(cite_loc_eng) == 0) bail("Can't find English citation") # nocov x[cite_loc_eng] <- "\\citeEng{In press}" # Nuke french citation cite_head_fr <- grep( - "\\\\emph\\{Aussi disponible en fran\\\\c\\{c\\}ais~:\\}", - x) - if (length(cite_head_fr) == 0){ - bail("Can't find French citation header") # nocov - } + pattern = "\\\\emph\\{Aussi disponible en fran\\\\c\\{c\\}ais~:\\}", + x = x + ) + if (length(cite_head_fr) == 0) bail("Can't find French citation header") # nocov x[cite_head_fr] <- "" cite_loc_fr <- grep( - "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", - x) + pattern = "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x + ) if (length(cite_loc_fr) == 0) bail("Can't find French citation") # nocov x[cite_loc_fr] <- "" } # End modify citations } # End if prepub # Fix Res. Doc. 2013/092: -> Res. Doc. 2013/092. - x <- gsub("Res\\. Doc\\. ([0-9]{4}/[0-9]{2,}):", - "Res. Doc. \\1.", - x) - x <- gsub("MPO\\. Doc\\. de rech ([0-9]{4}/[0-9]{2,}):", - "MPO. Doc. de rech \\1.", - x) + x <- gsub("Res\\. Doc\\. ([0-9]{4}/[0-9]{2,}):", "Res. Doc. \\1.", x) + x <- gsub("MPO\\. Doc\\. de rech ([0-9]{4}/[0-9]{2,}):", "MPO. Doc. de rech \\1.", x) - # Pandoc now turns DOIs into href in references but must be \link{} - # to have underline: + # Pandoc now turns DOIs into href in references but must be \link{} to have underline: x <- gsub("\\\\href\\{", "\\\\link\\{", x) # Fix Pandoc/LaTeX bug as of 2021-04-07 where @@ -415,8 +375,7 @@ fix_envs <- function(x, } # Enable reference linking to subsections of appendices # if (!pandoc_curr_ver_is_before()) { - # stop("csasdown currently only works with pandoc < 3.1.7." - # "Please revert to an older pandoc version.") + # 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) @@ -424,30 +383,25 @@ fix_envs <- function(x, document_start_ind <- grep("^\\\\documentclass", x) pre_start <- x[1:document_start_ind] post_start <- x[(document_start_ind + 1):length(x)] - inp_lines <- - c("\\makeatletter", - "\\def\\@seccntformat#1{", - " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@section\\else", - " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@subsection\\else", - " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@subsubsection\\else", - " \\csname the#1\\endcsname\\quad", - " \\fi\\fi\\fi}", - "\\makeatother") + inp_lines <- c("\\makeatletter", + "\\def\\@seccntformat#1{", + " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@section\\else", + " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@subsection\\else", + " \\expandafter\\ifx\\csname c@#1\\endcsname\\c@subsubsection\\else", + " \\csname the#1\\endcsname\\quad", + " \\fi\\fi\\fi}", + "\\makeatother") x <- c(pre_start, inp_lines, post_start) } # Add the latex chunk for code highlighting theme_ind <- grep("^% Add theme here$", x) if(length(theme_ind)){ - themes <- c("pygments", "tango", "espresso", - "zenburn", "kate", "monochrome", - "breezedark", "haddock") + themes <- c("pygments", "tango", "espresso", "zenburn", "kate", "monochrome", "breezedark", "haddock") pre_theme <- x[1:(theme_ind - 1)] post_theme <- x[(theme_ind + 1):length(x)] if(highlight %in% themes){ - theme_latex <- readLines(system.file("themes", - paste0(highlight, ".latex"), - package = "csasdown")) + theme_latex <- readLines(system.file("themes", paste0(highlight, ".latex"), package = "csasdown")) }else{ theme_latex <- readLines(here(highlight)) # nocov } diff --git a/R/resdoc-pdf.R b/R/resdoc-pdf.R index c32c4aa..369739b 100644 --- a/R/resdoc-pdf.R +++ b/R/resdoc-pdf.R @@ -62,6 +62,8 @@ resdoc_pdf <- function(toc = TRUE, "zenburn", "kate", "monochrome", "breezedark", "haddock") + fr <- function() if (french) TRUE else FALSE + if(is.null(highlight)){ highlight = "monochrome" # nocov } @@ -76,13 +78,9 @@ resdoc_pdf <- function(toc = TRUE, } if (fr()) { - file <- system.file("csas-tex", - "res-doc-french.tex", - package = "csasdown") + file <- system.file("csas-tex", "res-doc-french.tex", package = "csasdown") } else { - file <- system.file("csas-tex", - "res-doc.tex", - package = "csasdown") + file <- system.file("csas-tex", "res-doc.tex", package = "csasdown") } base <- pdf_book( @@ -107,9 +105,7 @@ resdoc_pdf <- function(toc = TRUE, line_nums_mod = line_nums_mod, draft_watermark = draft_watermark, lot_lof = lot_lof, - which_sty = ifelse(fr(), - "res-doc-french.sty", - "res-doc.sty") + which_sty = ifelse(fr(), "res-doc-french.sty", "res-doc.sty") ) # Mostly copied from knitr::render_sweave diff --git a/R/sr-pdf.R b/R/sr-pdf.R index 41dca6f..7219cdf 100644 --- a/R/sr-pdf.R +++ b/R/sr-pdf.R @@ -13,6 +13,8 @@ sr_pdf <- function(latex_engine = "pdflatex", "--default-image-extension=png"), ...) { + fr <- function() if (french) TRUE else FALSE + themes <- c("pygments", "tango", "espresso", "zenburn", "kate", "monochrome", "breezedark", "haddock") diff --git a/R/techreport-pdf.R b/R/techreport-pdf.R index 5092f7b..d38329f 100644 --- a/R/techreport-pdf.R +++ b/R/techreport-pdf.R @@ -21,23 +21,20 @@ techreport_pdf <- function(latex_engine = "pdflatex", highlight = "monochrome" # nocov } + fr <- function() if (french) TRUE else FALSE + if((!highlight %in% themes) && !file.exists(here(highlight))){ bail("in YAML, ", tag_color("csasdown:techreport_pdf: highlight"), "must be one of ", csas_color(paste(themes, collapse = ", ")), "\nor a filename for a custom latex theme file.", - "\nSee pandoc documentation, ", - csas_color("--highlight-style argument.")) + "\nSee pandoc documentation, ", csas_color("--highlight-style argument.")) } if (fr()) { - file <- system.file("csas-tex", - "tech-report-french.tex", - package = "csasdown") # nocov + file <- system.file("csas-tex", "tech-report-french.tex", package = "csasdown") # nocov } else { - file <- system.file("csas-tex", - "tech-report.tex", - package = "csasdown") + file <- system.file("csas-tex", "tech-report.tex", package = "csasdown") } base <- pdf_book( @@ -50,30 +47,11 @@ techreport_pdf <- function(latex_engine = "pdflatex", tmp_hl <- grep("--highlight-style", base$pandoc$args) base$pandoc$args <- base$pandoc$args[-c(tmp_hl[1], tmp_hl[1] + 1)] - cover_file_pdf <- if (fr()) { - "tech-report-cover-french.pdf" - } else { - "tech-report-cover.pdf" - } - cover_file_docx <- if (fr()) { - "tech-report-cover-french.docx" - } else { - "tech-report-cover.docx" - } - + cover_file_pdf <- if (fr()) "tech-report-cover-french.pdf" else "tech-report-cover.pdf" + cover_file_docx <- if (fr()) "tech-report-cover-french.docx" else "tech-report-cover.docx" if (!file.exists(cover_file_pdf)) { - cover_docx <- system.file("rmarkdown", - "templates", - "techreport", - "skeleton", - cover_file_docx, - package = "csasdown") - cover_pdf <- system.file("rmarkdown", - "templates", - "techreport", - "skeleton", - cover_file_pdf, - package = "csasdown") + cover_docx <- system.file("rmarkdown", "templates", "techreport", "skeleton", cover_file_docx, package = "csasdown") + cover_pdf <- system.file("rmarkdown", "templates", "techreport", "skeleton", cover_file_pdf, package = "csasdown") alert("Missing the Tech Report cover page. Copying in the files...") file.copy(cover_docx, ".", overwrite = FALSE) file.copy(cover_pdf, ".", overwrite = FALSE) @@ -89,13 +67,10 @@ techreport_pdf <- function(latex_engine = "pdflatex", line_nums_mod = line_nums_mod, lot_lof = lot_lof, draft_watermark = draft_watermark, - which_sty = ifelse(fr(), - "tech-report-french.sty", - "tech-report.sty") + which_sty = ifelse(fr(), "tech-report-french.sty", "tech-report.sty") ) base$knitr$opts_chunk$comment <- NA - old_opt <- getOption("bookdown.post.latex") options(bookdown.post.latex = function(x) { fix_envs( @@ -104,7 +79,6 @@ techreport_pdf <- function(latex_engine = "pdflatex", ) }) on.exit(options(bookdown.post.late = old_opt)) - base } diff --git a/R/update-csasstyle.R b/R/update-csasstyle.R index d16ab0e..d951f4d 100644 --- a/R/update-csasstyle.R +++ b/R/update-csasstyle.R @@ -31,95 +31,103 @@ update_csasstyle <- function(copy = TRUE, draft_watermark = FALSE, which_sty = "res-doc.sty") { - if (pandoc_version == "3.1.7") { - bail("csasdown does not work with pandoc 3.1.7. Either update pandoc ", - "or revert to an older version.") + if (pandoc_version == '3.1.7') { + bail("csasdown does not work with pandoc 3.1.7. Either update pandoc or revert to an older version.") } - dr_sty <- "csas-style" - dr_sty_sys <- system.file(dr_sty, package = "csasdown") - fn_sty <- system.file(file.path(dr_sty, - which_sty), - package = "csasdown") + fn <- system.file("csas-style", package = "csasdown") + if(!copy && line_nums){ + bail("You have set ", csas_color("copy"), " to ", csas_color("FALSE"), + " and ", csas_color("line_nums"), " to ", csas_color("TRUE"), + " in the ", fn_color("index.Rmd"), " YAML header. The permanent ", + "style file cannot be modified as needed to include line numbering. ", + "Either set ", csas_color("copy"), " to ", csas_color("TRUE"), + " or ", csas_color("line_nums"), " to ", csas_color("FALSE"), + " to build.") + } + if(!copy && lot_lof){ + bail("You have set ", csas_color("copy"), " to ", csas_color("FALSE"), + " and ", csas_color("lot_lof"), " to ", csas_color("TRUE"), + " in the ", fn_color("index.Rmd"), " YAML header. The permanent ", + "style file cannot be modified as needed to include the lists of ", + "tables and figures. Either set ", + csas_color("copy"), " to ", csas_color("TRUE"), " or ", + csas_color("lot_lof"), " to ", csas_color("FALSE"), + " to build.") + } + if(!copy && draft_watermark){ + bail("You have set ", csas_color("copy"), " to ", csas_color("FALSE"), + " and ", csas_color("draft_watermark"), " to ", csas_color("TRUE"), + " in the ", fn_color("index.Rmd"), " YAML header. The permanent ", + "style file cannot be modified as needed to include the DRAFT ", + "watermark. Either set ", + csas_color("copy"), " to ", csas_color("TRUE"), " or ", + csas_color("draft_watermark"), " to ", csas_color("FALSE"), + " to build.") + } - if(copy || !dir.exists("csas-style")){ - if(fn_sty == ""){ - bail("You have either set ", csas_color("copy"), " to ", - csas_color("true"), " or the `csas-style` directory is missing ", - "and the system file needed to copy ") + if (copy || !dir.exists("csas-style")) { + dir.create("csas-style", showWarnings = FALSE) + ignore <- file.copy(fn, ".", overwrite = TRUE, recursive = TRUE) + if(line_nums || lot_lof || draft_watermark){ + csas_style <- readLines(file.path("csas-style", which_sty)) } - dir.create(dr_sty, showWarnings = FALSE) - - copied <- file.copy(dr_sty_sys, - here(), - overwrite = TRUE, - recursive = TRUE) - if(!copied){ - alert("File ", fn_color(fn_sty), ", not copied, `file.copy()` ", - "returned ", csas_color("FALSE"), ".") + if (line_nums) { + if (grepl("res-doc", which_sty)) { + frontmatter_loc <- grep("frontmatter\\{", csas_style) + beg_of_file <- csas_style[seq(1, (frontmatter_loc - 1))] + end_of_file <- csas_style[seq(frontmatter_loc, length(csas_style))] + modulo <- paste0("\\modulolinenumbers[", line_nums_mod, "]") + csas_style <- c(beg_of_file, "\\linenumbers", modulo, end_of_file) + writeLines(csas_style, file.path("csas-style", which_sty)) + } else { + modulo <- paste0("\\modulolinenumbers[", line_nums_mod, "]") + csas_style <- c(csas_style, "\\linenumbers", modulo) + writeLines(csas_style, file.path("csas-style", which_sty)) + } } - } - - if(line_nums || lot_lof || draft_watermark){ - csas_style <- readLines(fn_sty) - } - if(line_nums){ - if(grepl("res-doc", which_sty)){ - frontmatter_loc <- grep("frontmatter\\{", csas_style) - beg_of_file <- csas_style[seq(1, (frontmatter_loc - 1))] - end_of_file <- csas_style[seq(frontmatter_loc, length(csas_style))] - modulo <- paste0("\\modulolinenumbers[", line_nums_mod, "]") - csas_style <- c(beg_of_file, "\\linenumbers", modulo, end_of_file) - writeLines(csas_style, file.path("csas-style", which_sty)) - }else{ - modulo <- paste0("\\modulolinenumbers[", line_nums_mod, "]") - csas_style <- c(csas_style, "\\linenumbers", modulo) + if (lot_lof) { + if (grepl("res-doc", which_sty) | grepl("tech-report", which_sty) | grepl("manu-report", which_sty)) { + pagenumbering_loc <- grep("pagenumbering\\{arabic", csas_style) + beg_of_file <- csas_style[seq(1, (pagenumbering_loc - 1))] + end_of_file <- csas_style[seq(pagenumbering_loc, length(csas_style))] + lot <- "\\listoftables" + cp <- "\\clearpage" + lof <- "\\listoffigures" + csas_style <- c(beg_of_file, lot, cp, lof, cp, end_of_file) + writeLines(csas_style, file.path("csas-style", which_sty)) + } else { # nocov start + alert(csas_color("lot_lof"), " is only implemented for Res Docs, ", + "ManuReports and TechReports.") + } # nocov end + } + if(draft_watermark){ + last_usepackage_ind <- tail(grep("usepackage", csas_style), 1) + beg_of_file <- csas_style[seq(1, last_usepackage_ind)] + end_of_file <- csas_style[seq(last_usepackage_ind + 1, length(csas_style))] + draft_watermark_include <- "\\usepackage{draftwatermark}" + if(last_usepackage_ind == length(csas_style)){ + csas_style <- c(beg_of_file, draft_watermark_include) # nocov + }else{ + csas_style <- c(beg_of_file, draft_watermark_include, end_of_file) + } writeLines(csas_style, file.path("csas-style", which_sty)) } - } - if(lot_lof){ - if(grepl("res-doc", which_sty) | - grepl("tech-report", which_sty) | - grepl("manu-report", which_sty)){ - pagenumbering_loc <- grep("pagenumbering\\{arabic", csas_style) - beg_of_file <- csas_style[seq(1, (pagenumbering_loc - 1))] - end_of_file <- csas_style[seq(pagenumbering_loc, length(csas_style))] - lot <- "\\listoftables" - cp <- "\\clearpage" - lof <- "\\listoffigures" - csas_style <- c(beg_of_file, lot, cp, lof, cp, end_of_file) + + if (pandoc_version < '3.1.7') { + # default is for pandoc > 3.1.8 + # 3.1.7 has already been checked, requires its own template, and errors + csas_style <- readLines(file.path("csas-style", which_sty)) + pandoc_3.1.8_start <- grep("^% START-PANDOC-3.1.8", csas_style) + pandoc_3.1.8_end <- grep("^% END-PANDOC-3.1.8", csas_style) + csas_style[seq(pandoc_3.1.8_start, pandoc_3.1.8_end)] <- + paste("%", csas_style[seq(pandoc_3.1.8_start, pandoc_3.1.8_end)]) + pandoc_pre_3.1.7_start <- grep("^% % START-PANDOC-BEFORE-3.1.7", csas_style) + pandoc_pre_3.1.7_end <- grep("^% % END-PANDOC-BEFORE-3.1.7" , csas_style) + csas_style[seq(pandoc_pre_3.1.7_start, pandoc_pre_3.1.7_end)] <- + gsub("^% ", "", csas_style[seq(pandoc_pre_3.1.7_start, pandoc_pre_3.1.7_end)]) writeLines(csas_style, file.path("csas-style", which_sty)) - }else{ # nocov start - alert(csas_color("lot_lof"), " is only implemented for Res Docs, ", - "ManuReports and TechReports.") - } # nocov end - } - if(draft_watermark){ - last_usepackage_ind <- tail(grep("usepackage", csas_style), 1) - beg_of_file <- csas_style[seq(1, last_usepackage_ind)] - end_of_file <- csas_style[seq(last_usepackage_ind + 1, length(csas_style))] - draft_watermark_include <- "\\usepackage{draftwatermark}" - if(last_usepackage_ind == length(csas_style)){ - csas_style <- c(beg_of_file, draft_watermark_include) # nocov - }else{ - csas_style <- c(beg_of_file, draft_watermark_include, end_of_file) } - writeLines(csas_style, file.path("csas-style", which_sty)) } - - # if(pandoc_version < "3.1.7"){ - # # default is for pandoc > 3.1.8 - # # 3.1.7 has already been checked, requires its own template, and errors - # csas_style <- readLines(fn_sty) - # pandoc_3.1.8_start <- grep("^% START-PANDOC-3.1.8", csas_style) - # pandoc_3.1.8_end <- grep("^% END-PANDOC-3.1.8", csas_style) - # csas_style[seq(pandoc_3.1.8_start, pandoc_3.1.8_end)] <- - # paste("%", csas_style[seq(pandoc_3.1.8_start, pandoc_3.1.8_end)]) - # pandoc_pre_3.1.7_start <- grep("^% % START-PANDOC-BEFORE-3.1.7", csas_style) - # pandoc_pre_3.1.7_end <- grep("^% % END-PANDOC-BEFORE-3.1.7" , csas_style) - # csas_style[seq(pandoc_pre_3.1.7_start, pandoc_pre_3.1.7_end)] <- - # gsub("^% ", "", csas_style[seq(pandoc_pre_3.1.7_start, pandoc_pre_3.1.7_end)]) - # writeLines(csas_style, fn_sty) - # } - } + diff --git a/inst/csas-style/res-doc.sty b/inst/csas-style/res-doc.sty index e146e31..bf645f8 100644 --- a/inst/csas-style/res-doc.sty +++ b/inst/csas-style/res-doc.sty @@ -718,59 +718,59 @@ DFO Can.\ Sci.\ Advis.\ Sec.\ Res.\ Doc. \rdYear{}/\rdNumber{}. }% End of frontmatter %% ------------------------------------------------------------------------------ -% % START-PANDOC-3.1.8 -% % definitions for citeproc citations -% \NewDocumentCommand\citeproctext{}{} -% \NewDocumentCommand\citeproc{mm}{% -% \begingroup\def\citeproctext{#2}\cite{#1}\endgroup} -% \makeatletter -% % allow citations to break across lines -% \let\@cite@ofmt\@firstofone -% % avoid brackets around text for \cite: -% \def\@biblabel#1{} -% \def\@cite#1#2{{#1\if@tempswa , #2\fi}} -% \makeatother -% \newlength{\cslhangindent} -% \setlength{\cslhangindent}{1.5em} -% \newlength{\csllabelwidth} -% \setlength{\csllabelwidth}{3em} -% \newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing -% {\begin{list}{}{% -% \setlength{\itemindent}{0pt} -% \setlength{\leftmargin}{0pt} -% \setlength{\parsep}{0pt} -% % turn on hanging indent if param 1 is 1 -% \ifodd #1 -% \setlength{\leftmargin}{\cslhangindent} -% \setlength{\itemindent}{-1\cslhangindent} -% \fi -% % set entry spacing -% \setlength{\itemsep}{#2\baselineskip}}} -% {\end{list}} -% \usepackage{calc} -% \newcommand{\CSLBlock}[1]{\hfill\break#1\hfill\break} -% \newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}} -% \newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}} -% \newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1} -% % END-PANDOC-3.1.8 - -% START-PANDOC-BEFORE-3.1.7 -\newenvironment{CSLReferences}[3] % #1 hanging-ident, #2 entry spacing - {% don't indent paragraphs - \setlength{\parindent}{0pt} - % turn on hanging indent if param 1 is 1 - \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi - % set entry spacing - \ifnum #2 > 0 - \setlength{\parskip}{#2\baselineskip} - \fi - }% - {} -%Copied from https://github.com/rstudio/rmarkdown/issues/1649 +% START-PANDOC-3.1.8 +% definitions for citeproc citations +\NewDocumentCommand\citeproctext{}{} +\NewDocumentCommand\citeproc{mm}{% + \begingroup\def\citeproctext{#2}\cite{#1}\endgroup} +\makeatletter + % allow citations to break across lines + \let\@cite@ofmt\@firstofone + % avoid brackets around text for \cite: + \def\@biblabel#1{} + \def\@cite#1#2{{#1\if@tempswa , #2\fi}} +\makeatother \newlength{\cslhangindent} \setlength{\cslhangindent}{1.5em} -\newenvironment{cslreferences}% - {$if(csl-hanging-indent)$\setlength{\parindent}{0pt}% - \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}% - {\par} -% END-PANDOC-BEFORE-3.1.7 +\newlength{\csllabelwidth} +\setlength{\csllabelwidth}{3em} +\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing + {\begin{list}{}{% + \setlength{\itemindent}{0pt} + \setlength{\leftmargin}{0pt} + \setlength{\parsep}{0pt} + % turn on hanging indent if param 1 is 1 + \ifodd #1 + \setlength{\leftmargin}{\cslhangindent} + \setlength{\itemindent}{-1\cslhangindent} + \fi + % set entry spacing + \setlength{\itemsep}{#2\baselineskip}}} + {\end{list}} +\usepackage{calc} +\newcommand{\CSLBlock}[1]{\hfill\break#1\hfill\break} +\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}} +\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}} +\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1} +% END-PANDOC-3.1.8 + +% % START-PANDOC-BEFORE-3.1.7 +% \newenvironment{CSLReferences}[3] % #1 hanging-ident, #2 entry spacing +% {% don't indent paragraphs +% \setlength{\parindent}{0pt} +% % turn on hanging indent if param 1 is 1 +% \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi +% % set entry spacing +% \ifnum #2 > 0 +% \setlength{\parskip}{#2\baselineskip} +% \fi +% }% +% {} +% %Copied from https://github.com/rstudio/rmarkdown/issues/1649 +% \newlength{\cslhangindent} +% \setlength{\cslhangindent}{1.5em} +% \newenvironment{cslreferences}% +% {$if(csl-hanging-indent)$\setlength{\parindent}{0pt}% +% \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}% +% {\par} +% % END-PANDOC-BEFORE-3.1.7 \ No newline at end of file diff --git a/tests/testthat/test-update-csasstyle.R b/tests/testthat/test-update-csasstyle.R index 1ffc5ed..0d1175a 100644 --- a/tests/testthat/test-update-csasstyle.R +++ b/tests/testthat/test-update-csasstyle.R @@ -33,18 +33,20 @@ test_that("update_csasstyle() works", { # --------------------------------------------------------------------------- # Set lot_lof (toggle show List of tables/List of Figures in doc) installed_3_1_2 <- pandoc::pandoc_available("3.1.2") - if(!installed_3_1_2){ - pandoc::pandoc_install("3.1.2") - } - pandoc::pandoc_activate("3.1.2") - rmd <- readLines("index.Rmd") - ind <- grep("lot_lof:", rmd) - rmd[ind] <- " lot_lof: true" - writeLines(rmd, "index.Rmd") - csasdown::render() - expect_true(file.exists("_book/resdoc-english.pdf")) - if(!installed_3_1_2){ - pandoc::pandoc_uninstall("3.1.2") + if (installed_3_1_2) { + # if (!installed_3_1_2){ + # pandoc::pandoc_install("3.1.2") + # } + pandoc::pandoc_activate("3.1.2") + rmd <- readLines("index.Rmd") + ind <- grep("lot_lof:", rmd) + rmd[ind] <- " lot_lof: true" + writeLines(rmd, "index.Rmd") + csasdown::render() + expect_true(file.exists("_book/resdoc-english.pdf")) + # if (!installed_3_1_2){ + # pandoc::pandoc_uninstall("3.1.2") + # } } # ---------------------------------------------------------------------------