Skip to content
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

Open
wants to merge 50 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
d731a5e
Removed captions_alt_text_template.csv because it's not used in asar.…
sbreitbart-NOAA Dec 12, 2024
d9168fb
changed eval for first chunk to true, and eval for indices table setu…
sbreitbart-NOAA Dec 12, 2024
17bd19a
feature(add new template partial for accessibility and add fxn)
Schiano-NOAA Dec 12, 2024
858a644
Merge branch 'main' into inject-accessibility
Schiano-NOAA Dec 18, 2024
a468891
Merge branch 'main' into inject-accessibility
Schiano-NOAA Jan 7, 2025
186b02e
Update(add_tagging): ability to add alternative text to tex file from…
Schiano-NOAA Jan 7, 2025
d9d2bde
fix(add_taggig): change directory for rda files to arg
Schiano-NOAA Jan 7, 2025
063d706
fix(add_tagging): change pathing to rda_dir
Schiano-NOAA Jan 7, 2025
a024b16
fix(add-tagging): rename accessibility tex for output
Schiano-NOAA Jan 7, 2025
020ad0f
feature(add_accessibility): rename function and load documentation
Schiano-NOAA Jan 8, 2025
67ef216
Initial commit for add_accessibility test - commented out bc unfinished
Schiano-NOAA Jan 8, 2025
6b10916
update(test-add_accessibility): add snapshot for tex file and add pro…
Schiano-NOAA Jan 8, 2025
f3c46ca
feature(alter accessibility fxns): split up funxtion into two and cal…
Schiano-NOAA Jan 9, 2025
b684bfe
Remove nmfspallete from dependencies temporarily (#139)
Schiano-NOAA Jan 13, 2025
46dd4c4
style and docs: run devtools::document() and styler::style_pkg() (#140)
github-actions[bot] Jan 13, 2025
0e18b21
Add nmfspalette back into package
Schiano-NOAA Jan 15, 2025
6b48d9e
remove static NOAA Fishies in authorship
Schiano-NOAA Jan 17, 2025
b9997d0
update NOAA fisheries affiliations
Schiano-NOAA Jan 17, 2025
c07e252
rebase create_table fxn
Schiano-NOAA Jan 17, 2025
88f9112
Merge newer dev branch into old (#148)
Schiano-NOAA Jan 17, 2025
6366d8f
Update accessibility vignette
sbreitbart-NOAA Jan 17, 2025
c3d914c
fix to incorrect reference in .bib file (#149)
iantaylor-NOAA Jan 21, 2025
63a9617
remove replicate nmfs palette and add_theme
Schiano-NOAA Jan 21, 2025
b1c7c77
remove files
Schiano-NOAA Jan 21, 2025
6d2dc77
rebase NAMESPACE
Schiano-NOAA Jan 21, 2025
3fbc82e
fix access guide
Schiano-NOAA Jan 21, 2025
5c33547
remove old package from namespace and add in new
Schiano-NOAA Jan 21, 2025
4ee875d
Fix(create_template):
sbreitbart-NOAA Jan 21, 2025
ec301a3
update(format_quarto): add functionality for html formatting with ass…
Schiano-NOAA Jan 22, 2025
d9be98a
small comment update
sbreitbart-NOAA Jan 22, 2025
a827fe6
update(create_template): custom_sections adds all selected plus refs,…
Schiano-NOAA Jan 22, 2025
248630f
Merge branch 'dev' into inject-accessibility
Schiano-NOAA Jan 22, 2025
85af261
add notes for new alttext test
Schiano-NOAA Jan 24, 2025
03dda0a
Removed captions_alt_text_template.csv because it's not used in asar.…
sbreitbart-NOAA Dec 12, 2024
11bec5b
Remove nmfspallete from dependencies temporarily (#139)
Schiano-NOAA Jan 13, 2025
ab031e5
Add nmfspalette back into package
Schiano-NOAA Jan 15, 2025
e8a225d
Merge newer dev branch into old (#148)
Schiano-NOAA Jan 17, 2025
83fdda2
Custom template fixes (#143)
Schiano-NOAA Jan 21, 2025
ef643e6
feature(alter accessibility fxns): split up funxtion into two and cal…
Schiano-NOAA Jan 9, 2025
fd469b8
Remove nmfspallete from dependencies temporarily (#139)
Schiano-NOAA Jan 13, 2025
4247ba0
Add nmfspalette back into package
Schiano-NOAA Jan 15, 2025
1c6a841
update(create_template): custom_sections adds all selected plus refs,…
Schiano-NOAA Jan 22, 2025
fc050c4
add spp_conout.csv to gitignore during testing
Schiano-NOAA Jan 27, 2025
b9a870e
feature(add_alttext): add accessibility for user to add alt text to f…
Schiano-NOAA Jan 27, 2025
350d370
fix(add_alttext): improve fxning for adding alt text to custom figs e…
Schiano-NOAA Jan 28, 2025
6709e29
feature(add_alttext): add example to function
Schiano-NOAA Jan 28, 2025
628580f
Update documentation
Schiano-NOAA Jan 28, 2025
20fffdb
comment out alttext fxn test
Schiano-NOAA Jan 28, 2025
b986f74
clean up fxns and add examples
Schiano-NOAA Jan 28, 2025
a494c64
add in support_file/accessibilit.tex
Schiano-NOAA Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Generated by roxygen2: do not edit by hand
Copy link
Collaborator

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 )


export(add_accessibility)
export(add_alttext)
export(add_base_section)
export(add_child)
export(add_chunk)
export(add_section)
export(add_tagging)
export(add_theme)
export(convert_output)
export(create_citation)
Expand Down
146 changes: 22 additions & 124 deletions R/add_accessibility.R
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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, alttext_csv_dir requires a directory + the csv name, whereas the other dir-related args (dir, rda_dir) only require a directory. Similarly, I wasn't sure if rename required ".tex" or not. So, I'd make the following changes:

  1. Change dir to tex_dir, for clarity
  2. Change the arg alttext_csv_dir to alttext_csv
  3. Change the alttext_csv arg definition to state that this references the location of the alt text csv
  4. Update the rename definition to specify that this new name shouldn't include the .tex filetype
  5. Add the default value of compile to its definition
  6. Update the x definition to specify that this is the .tex file into which alt text, not "accessibility", is added
  7. Make the "X" within the compile doc lowercase to match the x argument

#' 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(
Expand All @@ -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)))
Expand Down
122 changes: 122 additions & 0 deletions R/add_alttext.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#' Add alternative text into latex
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a draft?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 alttext_csv_dir and rename args.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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______")
}
}
56 changes: 56 additions & 0 deletions R/add_tagging.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Add tagging structure to latex documents produced from quarto
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 rename arg (currently includes rda_dir instead, which isn't an add_tagging() arg)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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______")
}
}
10 changes: 5 additions & 5 deletions man/add_accessibility.Rd

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

Loading