-
Notifications
You must be signed in to change notification settings - Fork 4
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
Inject accessibility #124
base: dev
Are you sure you want to change the base?
Inject accessibility #124
Changes from 1 commit
d731a5e
d9168fb
17bd19a
858a644
a468891
186b02e
d9d2bde
063d706
a024b16
020ad0f
67ef216
6b10916
f3c46ca
b684bfe
46dd4c4
0e18b21
6b48d9e
b9997d0
c07e252
88f9112
6366d8f
c3d914c
63a9617
b1c7c77
6d2dc77
3fbc82e
5c33547
4ee875d
ec301a3
d9be98a
a827fe6
248630f
85af261
03dda0a
11bec5b
ab031e5
e8a225d
83fdda2
ef643e6
fd469b8
4247ba0
1c6a841
fc050c4
b9a870e
350d370
6709e29
628580f
20fffdb
b986f74
a494c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function isn't working for me. I'm using the same args as for add_tagging and add_alttext, but I get an error I think is about incorrect filepaths ("Error in file(file, "rt") : cannot open the connection"). The error arises at line 73, when add_alttext is called. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,11 @@ | |
#' @param compile TRUE/FALSE - indicate whether the document (X) should be rendered after these files are changed | ||
#' @param rename change the name of the latex file for final compilation or saving | ||
#' | ||
#' @return DRAFT: This function was made to help add in latex packages and content | ||
#' associated with PDF tagging. Quarto does not allow the user to edit anything | ||
#' before documentclass, so this function alters the rendered .tex file. Users | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify what these arguments mean and what they could look like. I had to look into the source code to figure out what some of the args were actually asking for. For instance,
|
||
#' should either compile directly through the function or run | ||
#' tinytex::lualatex(...) afterwards in the console. | ||
#' @return DRAFT: This function was made to help add in | ||
Schiano-NOAA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' latex packages and content associated with PDF | ||
#' tagging as well as alternative text for latex | ||
#' documents. Quarto does not allow the user to edit anything | ||
#' before documentclass, so this function alters the rendered .tex file. | ||
#' @export | ||
Schiano-NOAA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
add_accessibility <- function( | ||
|
@@ -22,127 +22,25 @@ add_accessibility <- function( | |
compile = TRUE, | ||
rename = NULL | ||
) { | ||
# Read latex file | ||
tex_file <- readLines(file.path(dir, x)) | ||
|
||
# Identify line where the new accessibility content should be added after | ||
# line_after <- grep("\\PassOptionsToPackage\\{dvipsnames\\,svgnames\\,x11names\\}\\{xcolor\\}", tex_file) | ||
# # Acessibility additions before /documentclass | ||
# line_to_add <- "\\input{accessibility.tex}" | ||
# # Add line into file | ||
# tex_file <- append(line_to_add, tex_file, after = line_after) | ||
# # DO NOT UNCOMMENT FOLLOWING LINES WHEN OPERATING FXN AS A WHOLE | ||
# # We want to keep tex_file open so we can make changes to figures later down the line | ||
# # Export file | ||
# # write(tex_file, file = paste(dir, x, sep = "/")) | ||
# | ||
# # Add accessibility.tex to directory | ||
# accessibility <- paste0( | ||
# "\\DocumentMetadata{%", "\n", | ||
# " ", "% uncompress, %only for debugging!!", "\n", | ||
# " ", "pdfversion=2.0,", "\n", | ||
# " ", "testphase={phase-II, tabular, graphic}%", "\n", | ||
# " ", "% testphase={phase-II,math, tabular, graphic}% TOC Does not work", "\n", | ||
# " ", "% testphase={phase-III,math}% TOC works", "\n", | ||
# "}", "\n", | ||
# "\\tagpdfsetup{activate, tabsorder=structure}", "\n", | ||
# "% Use the following to fix bug in November 2023 download of LaTeX", "\n", | ||
# "\\ExplSyntaxOn", "\n", | ||
# "\\cs_generate_variant:Nn__tag_prop_gput:Nnn{cnx}", "\n", | ||
# "\\ExplSyntaxOff", "\n", | ||
# "%", "\n" | ||
# ) | ||
# Save accessibility partial | ||
# utils::capture.output(cat(acceesibility), file = file.path(dir, "accessibility.tex"), append = FALSE) | ||
|
||
# Check: count instances of pattern | ||
# sub_count <- length( | ||
# grep("1", | ||
# stringr::str_count( | ||
# tex_file, | ||
# pattern = stringr::coll("\\pandocbounded{\\includegraphics[")) | ||
# ) | ||
# ) | ||
|
||
# Identify lines with figures | ||
# this approach allows us to not mistake the replacement for other figures | ||
fig_lines <- grep("fig-([a-z]+|[a-z]+_[a-z]+)-plot-1.pdf", tex_file) | ||
# TODO: | ||
# create check to see if there are any instances where the suffix is not plot-1 | ||
# Replace instances of macro in the tex file | ||
# replace_macro <- gsub( | ||
# "\\pandocbounded", | ||
# "\\pdftooltip", | ||
# tex_file | ||
# ) | ||
|
||
# Replace pandocbounded with pdftooltip so alt text can be added | ||
tex_file[fig_lines] <- lapply( | ||
tex_file[fig_lines], | ||
function(line) { | ||
gsub("\\pandocbounded", "\\pdftooltip", line) | ||
} | ||
# Add tagpdf pkg to template and create accessibility.tex | ||
add_tagging( | ||
x = x, | ||
dir = dir, | ||
compile = FALSE, | ||
rename = rename | ||
) | ||
|
||
# Insert alt text for figures | ||
# Call alt text in list with names | ||
obj_files <- list.files(file.path(rda_dir, "rda_files")) | ||
|
||
# read all files in obj_files and put into list | ||
alt_text_list <- list() | ||
for (i in seq_along(obj_files)) { | ||
load(file.path(rda_dir, "rda_files", obj_files[i])) | ||
# extract name to add into the list for placement | ||
rda_name <- stringr::str_replace(obj_files[i], "_figure.rda", "") | ||
# if name is >1 word then replace the _ with - to follow naming convention for | ||
# figures in tex file | ||
if (grepl("_", rda_name)) rda_name <- stringr::str_replace(rda_name, "_", "-") | ||
# convert to name in tex file to find where the line is located | ||
tex_name <- glue::glue("fig-{rda_name}-plot-1.pdf") | ||
# extract alt. text with figure | ||
alt_text <- rda$alt_text | ||
# names(alt_text) <- tex_name | ||
# place obj into list | ||
alt_text_list[[tex_name]] <- alt_text | ||
# call tex obj name using names() | ||
# call alt text using list[[i]] | ||
# remove rda file to declutter | ||
rm(rda) | ||
} | ||
|
||
# Find where figure is located and append the alt. text | ||
for (i in seq_along(alt_text_list)) { | ||
fig_line <- grep(names(alt_text_list[i]), tex_file) | ||
# Check that line we are adding the alt text to is for correct fig | ||
if (!grepl(names(alt_text_list[i]), tex_file[fig_line])) { | ||
warning(glue::glue("Non-matching object name to tex file line.")) | ||
next | ||
} | ||
# Check that selected tex_line contains a marked figure - aka correct placement | ||
file_name <- stringr::str_remove(x, ".tex") | ||
if (!grepl(glue::glue("{file_name}_files/figure-pdf/fig-"), tex_file[fig_line])) { | ||
warning(glue::glue("Improper line for appendment: \n Skipped adding alternative text for {names(alt_text_list[i])}")) | ||
next | ||
} | ||
tex_file[fig_line] <- paste(tex_file[fig_line], "{", alt_text_list[[i]], "}", sep = "") | ||
# tex_file[fig_line] <- strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "")) # remove strwrap if does not render | ||
} | ||
# tex_file[430] <- paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "") | ||
# strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = ""), width = 80) | ||
|
||
# Checks | ||
# add check if there are more plots that did not have alt text added | ||
# if (length(obj_files) != length(fig_lines)) { | ||
# # Find which ones were not changed | ||
# # figs_miss_alt <- | ||
# warning("Missing alternative text for the followiing figures:") | ||
# } | ||
# TODO: test case where additional figure is added into the .tex file that is not included in the rda files | ||
|
||
# Save overwrite tex file | ||
write(unlist(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
# utils::capture.output(cat(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x)), append = FALSE) | ||
# Render the .tex file after edits | ||
message("______Tagging structure added to tex file.______") | ||
# add alternative text to template | ||
add_alttext( | ||
x = ifelse(is.null(rename), x, glue::glue("{rename}.tex")), | ||
dir = dir, | ||
rda_dir = rda_dir, | ||
compile = FALSE, | ||
rename = rename | ||
) | ||
message("______Alternative text added to tex file.______") | ||
# Render the .tex file after edits | ||
if (compile) { | ||
# test if this can be done when skeleton is in different folder than the wd | ||
tinytex::lualatex(file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#' Add alternative text into latex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: please change the filetype from .r to .R for consistency |
||
#' | ||
#' @inheritParams add_accessibility | ||
#' | ||
#' @return DRAFT: This function was made to help add in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still a draft? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is and is outdated somehow after your review which makes no sense because I pushed all of these changes on tuesday |
||
#' alternative text to latex documents generated from | ||
#' quarto. Quarto does not currently contain a way to | ||
#' add alternative text to PDF documents, so this function | ||
#' was developed as a work around. The addition of alternative | ||
#' text needs to be in the form found in rda files made | ||
#' from satf plotting functions. If users want to use this | ||
#' function as a way to add alternative text to all | ||
#' figures, please leave an issue to request this function. | ||
#' | ||
#' @export | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add examples if they're absent (I can see an example in Rstudio but not here... ?), and update the example to include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added on tuesday...not sure why none of my changes are there. Git is telling me that everything is updated |
||
#' | ||
add_alttext <- function( | ||
x = list.files(getwd())[grep("skeleton.tex", list.files(getwd()))], | ||
dir = getwd(), | ||
rda_dir = getwd(), | ||
compile = TRUE, | ||
rename = NULL | ||
) { | ||
# Read latex file | ||
tex_file <- readLines(file.path(dir, x)) | ||
|
||
# Check: count instances of pattern | ||
# sub_count <- length( | ||
# grep("1", | ||
# stringr::str_count( | ||
# tex_file, | ||
# pattern = stringr::coll("\\pandocbounded{\\includegraphics[")) | ||
# ) | ||
# ) | ||
|
||
# Identify lines with figures | ||
# this approach allows us to not mistake the replacement for other figures | ||
fig_lines <- grep("fig-([a-z]+|[a-z]+_[a-z]+)-plot-1.pdf", tex_file) | ||
# TODO: | ||
# create check to see if there are any instances where the suffix is not plot-1 | ||
# Replace instances of macro in the tex file | ||
# replace_macro <- gsub( | ||
# "\\pandocbounded", | ||
# "\\pdftooltip", | ||
# tex_file | ||
# ) | ||
|
||
# Replace pandocbounded with pdftooltip so alt text can be added | ||
tex_file[fig_lines] <- lapply( | ||
tex_file[fig_lines], | ||
function(line) { | ||
gsub("\\pandocbounded", "\\pdftooltip", line) | ||
} | ||
) | ||
|
||
# Insert alt text for figures | ||
# Call alt text in list with names | ||
obj_files <- list.files(file.path(rda_dir, "rda_files")) | ||
|
||
# read all files in obj_files and put into list | ||
alt_text_list <- list() | ||
for (i in seq_along(obj_files)) { | ||
load(file.path(rda_dir, "rda_files", obj_files[i])) | ||
# extract name to add into the list for placement | ||
rda_name <- stringr::str_replace(obj_files[i], "_figure.rda", "") | ||
# if name is >1 word then replace the _ with - to follow naming convention for | ||
# figures in tex file | ||
if (grepl("_", rda_name)) rda_name <- stringr::str_replace(rda_name, "_", "-") | ||
# convert to name in tex file to find where the line is located | ||
tex_name <- glue::glue("fig-{rda_name}-plot-1.pdf") | ||
# extract alt. text with figure | ||
alt_text <- rda$alt_text | ||
# names(alt_text) <- tex_name | ||
# place obj into list | ||
alt_text_list[[tex_name]] <- alt_text | ||
# call tex obj name using names() | ||
# call alt text using list[[i]] | ||
# remove rda file to declutter | ||
rm(rda) | ||
} | ||
|
||
# Find where figure is located and append the alt. text | ||
for (i in seq_along(alt_text_list)) { | ||
fig_line <- grep(names(alt_text_list[i]), tex_file) | ||
# Check that line we are adding the alt text to is for correct fig | ||
if (!grepl(names(alt_text_list[i]), tex_file[fig_line])) { | ||
warning(glue::glue("Non-matching object name to tex file line.")) | ||
next | ||
} | ||
# Check that selected tex_line contains a marked figure - aka correct placement | ||
file_name <- stringr::str_remove(x, ".tex") | ||
if (!grepl(glue::glue("{file_name}_files/figure-pdf/fig-"), tex_file[fig_line])) { | ||
warning(glue::glue("Improper line for appendment: \n Skipped adding alternative text for {names(alt_text_list[i])}")) | ||
next | ||
} | ||
tex_file[fig_line] <- paste(tex_file[fig_line], "{", alt_text_list[[i]], "}", sep = "") | ||
# tex_file[fig_line] <- strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "")) # remove strwrap if does not render | ||
} | ||
# tex_file[430] <- paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "") | ||
# strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = ""), width = 80) | ||
|
||
# Checks | ||
# add check if there are more plots that did not have alt text added | ||
# if (length(obj_files) != length(fig_lines)) { | ||
# # Find which ones were not changed | ||
# # figs_miss_alt <- | ||
# warning("Missing alternative text for the followiing figures:") | ||
# } | ||
# TODO: test case where additional figure is added into the .tex file that is not included in the rda files | ||
|
||
# Save overwrite tex file | ||
write(unlist(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
# utils::capture.output(cat(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x)), append = FALSE) | ||
message("______Alternative text added to tex file.______") | ||
# Render the .tex file after edits | ||
if (compile) { | ||
message("______Compiling in progress - This can take a while...______") | ||
# test if this can be done when skeleton is in different folder than the wd | ||
tinytex::lualatex(file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
message("______Compiling finished______") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Add tagging structure to latex documents produced from quarto | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: please change the filetype from .r to .R for consistency |
||
#' | ||
#' @inheritParams add_accessibility | ||
#' | ||
#' @return DRAFT: This function was made to help add in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still a draft? (for this and the other 2 identical comments, please remove "DRAFT: " if it's ready to merge) |
||
#' latex packages and content associated with PDF | ||
#' tagging. Quarto does not allow the user to edit anything | ||
#' before documentclass, so this function alters the rendered .tex file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also don't see the example here, but I do see it in my Rstudio file ...? Please update the example to include the fxn's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto above |
||
#' @export | ||
#' | ||
add_tagging <- function( | ||
x = list.files(getwd())[grep("skeleton.tex", list.files(getwd()))], | ||
dir = getwd(), | ||
compile = TRUE, | ||
rename = NULL | ||
) { | ||
# Read latex file | ||
tex_file <- readLines(file.path(dir, x)) | ||
|
||
# Identify line where the new accessibility content should be added after | ||
line_after <- grep("\\PassOptionsToPackage\\{dvipsnames\\,svgnames\\,x11names\\}\\{xcolor\\}", tex_file) | ||
# Acessibility additions before /documentclass | ||
line_to_add <- "\\input{accessibility.tex}" | ||
# Add line into file | ||
tex_file <- append(line_to_add, tex_file, after = line_after) | ||
# DO NOT UNCOMMENT FOLLOWING LINES WHEN OPERATING FXN AS A WHOLE | ||
# We want to keep tex_file open so we can make changes to figures later down the line | ||
# Export file | ||
write(tex_file, file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
|
||
# Add accessibility.tex to directory | ||
accessibility <- paste0( | ||
"\\DocumentMetadata{%", "\n", | ||
" ", "% uncompress, %only for debugging!!", "\n", | ||
" ", "pdfversion=2.0,", "\n", | ||
" ", "testphase={phase-II, tabular, graphic}%", "\n", | ||
" ", "% testphase={phase-II,math, tabular, graphic}% TOC Does not work", "\n", | ||
" ", "% testphase={phase-III,math}% TOC works", "\n", | ||
"}", "\n", | ||
"\\tagpdfsetup{activate, tabsorder=structure}", "\n", | ||
"% Use the following to fix bug in November 2023 download of LaTeX", "\n", | ||
"\\ExplSyntaxOn", "\n", | ||
"\\cs_generate_variant:Nn__tag_prop_gput:Nnn{cnx}", "\n", | ||
"\\ExplSyntaxOff", "\n", | ||
"%", "\n" | ||
) | ||
# Save accessibility partial | ||
utils::capture.output(cat(accessibility), file = file.path(dir, "accessibility.tex"), append = FALSE) | ||
message("______Tagging structure added to tex file.______") | ||
if (compile) { | ||
message("______Compiling in progress - This can take a while...______") | ||
# test if this can be done when skeleton is in different folder than the wd | ||
tinytex::lualatex(file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x))) | ||
message("______Compiling finished______") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, meant to mention this: Initially, I had some issues rendering the 08_tables.qmd. Based on conversations from a couple of weeks ago, I'm sure this will be resolved with rebasing (these errors arose for you before, I believe, as explained here: #153 )