From 2aab394b7da414c0eb74962b221769975db52bff Mon Sep 17 00:00:00 2001 From: Chris Grandin Date: Fri, 8 Nov 2024 10:03:41 -0800 Subject: [PATCH] Clean up code formatting for readability for some functions --- R/fix-envs.R | 284 ++++++++++++++++++++++++++------------------- R/resdoc-pdf.R | 12 +- R/techreport-pdf.R | 33 +++++- 3 files changed, 202 insertions(+), 127 deletions(-) diff --git a/R/fix-envs.R b/R/fix-envs.R index 04fd501..6648028 100644 --- a/R/fix-envs.R +++ b/R/fix-envs.R @@ -34,49 +34,52 @@ 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 = x[region_line], - m = regexec( - pattern = pat, - text = x[region_line] - ) - )[[1]] + region_vec <- regmatches(x[region_line], + regexec(pat, + 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( - pattern = "AddressPlaceholder", replacement = contact_info$address, - x = x - ) - x <- sub( - pattern = "EmailPlaceholder", - replacement = paste0( - "\\\\link\\{mailto:", contact_info$email, - "\\}\\{", contact_info$email, "\\}" - ), x = x - ) + x <- sub("AddressPlaceholder", + contact_info$address, + x) + + x <- sub("EmailPlaceholder", + paste0("\\\\link\\{mailto:", + contact_info$email, + "\\}\\{", + contact_info$email, + "\\}"), + x) } # End if region exists (SRs) ## Change csas-style to use the sty file found in csasdown repo g <- grep("csas-style", x) @@ -100,13 +103,17 @@ 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 <- c( - i3, - if (length(i2 <- grep(end_reg, x))) (i2 + 1)[grepl("^\\s*$", x[i2 + 1])] - ) - if (length(i3)) x <- x[-i3] + 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] + } g <- grep("\\\\Appendices$", x) if (identical(length(g), 0L)) { @@ -125,15 +132,13 @@ 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]) } } @@ -150,8 +155,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: @@ -161,11 +166,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 <- @@ -174,20 +179,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 } # ---------------------------------------------------------------------- @@ -195,13 +200,19 @@ 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, function(.x) grep(.x, toupper(x), perl = TRUE) + 1) + "^\\\\SECTION{SOURCES OF INFORMATION}") + + .matches <- lapply(regexs, \(.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) { @@ -225,16 +236,24 @@ fix_envs <- function(x, "\\setlength{\\parskip}{8pt}", "", x[seq(references_begin, references_end)], - "\\setlength{\\parindent}{0in} \\setlength{\\leftskip}{0in} \\setlength{\\parskip}{4pt}", + paste0("\\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("\\\\section\\*\\{REFERENCES\\}\\\\label\\{references\\}\\}", x) + starred_references_line <- grep(paste0("\\\\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("\\\\addcontentsline\\{toc\\}\\{section\\}\\{REFERENCES\\}", x) + add_toc_contents_line <- grep(paste0("\\\\addcontentsline\\{toc\\}\\", + "{section\\}\\{REFERENCES\\}"), + x) x[add_toc_contents_line] <- "" } # Modify References section name here @@ -242,14 +261,15 @@ 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 { @@ -273,93 +293,113 @@ 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(pattern = "\\% Report number", x = x) + 1 - rn_loc_2 <- grep(pattern = "\\% End of report number", x = x) - 1 + rn_loc_1 <- grep("\\% Report number", x) + 1 + rn_loc_2 <- grep("\\% End of report number", 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(pattern = "\\}+$", replacement = "", x = rn_text) + rn_text_clean <- gsub("\\}+$", + "", + 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(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_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_text <- x[st_loc_1] - st_text_clean <- gsub(pattern = "\\}+$", replacement = "", x = st_text) + st_text_clean <- gsub("\\}+$", "", 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( - 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 + 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 + } x[cite_head_fr] <- "Cite comme ceci (jusqu'\u00E0 la publication)~:" cite_loc_fr <- grep( - pattern = "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x - ) - if (length(cite_loc_fr) == 0) bail("Can't find French citation") # nocov + "\\\\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( - pattern = "\\\\emph\\{Also available in English:\\}", - x = x - ) - if (length(cite_head_eng) == 0) bail("Can't find English citation header") # nocov + "\\\\emph\\{Also available in English:\\}", + x) + if (length(cite_head_eng) == 0) { + bail("Can't find English citation header") # nocov + } x[cite_head_eng] <- "" cite_loc_eng <- grep( - pattern = "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x - ) - if (length(cite_loc_eng) == 0) bail("Can't find English citation") # nocov + "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", + 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( - pattern = "Correct Citation for this Publication:", - x = x - ) - if (length(cite_head_eng) == 0) bail("Can't find English citation header") # nocov + "Correct Citation for this Publication:", + 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( - pattern = "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x - ) + "\\\\citeEng\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", + 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( - 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 + "\\\\emph\\{Aussi disponible en fran\\\\c\\{c\\}ais~:\\}", + x) + if (length(cite_head_fr) == 0){ + bail("Can't find French citation header") # nocov + } x[cite_head_fr] <- "" cite_loc_fr <- grep( - pattern = "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", x = x - ) + "\\\\citeFr\\{\\\\rdWorkDoneYear\\{\\}/\\\\rdNumber\\{\\}\\}", + 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 @@ -375,7 +415,8 @@ 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.", call. = FALSE) + # stop("csasdown currently only works with pandoc < 3.1.7." + # "Please revert to an older pandoc version.") # } x <- add_appendix_subsection_refs(x) @@ -383,25 +424,30 @@ 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 ee871a5..c32c4aa 100644 --- a/R/resdoc-pdf.R +++ b/R/resdoc-pdf.R @@ -76,9 +76,13 @@ 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( @@ -103,7 +107,9 @@ 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/techreport-pdf.R b/R/techreport-pdf.R index 42fd7de..5092f7b 100644 --- a/R/techreport-pdf.R +++ b/R/techreport-pdf.R @@ -50,11 +50,30 @@ 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) @@ -70,10 +89,13 @@ 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( @@ -82,6 +104,7 @@ techreport_pdf <- function(latex_engine = "pdflatex", ) }) on.exit(options(bookdown.post.late = old_opt)) + base }