-
Notifications
You must be signed in to change notification settings - Fork 1
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
start functionalizing make screenshots #153
Changes from 2 commits
62c8ae6
af2d370
23f0fa5
0aabec8
4f85a8c
edddb94
0a38e72
8dd642d
d8f1622
55005af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ Imports: | |
yaml | ||
Suggests: | ||
remotes, | ||
cow, | ||
testthat (>= 3.0.0), | ||
tibble, | ||
utils | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' A function to make screenshots from an OTTR bookdown website | ||
#' @description This function creates screenshots of course chapters that are stored in a created output directory | ||
#' @param git_pat default is NULL; required argument; a Git secret | ||
#' @param repo default is NULL; required argument; GitHub repository name, e.g., jhudsl/OTTR_Template | ||
#' @param output_dir default is "resources/chapt_screen_images"; Output directory where the chapter's screen images should be stored | ||
kweav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' @param base_url default is NULL; rendered bookdown URL where screenshots are taken from, if NULL, the function will use the repo_name and and git_pat to find the base_url | ||
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. Had to edit this description to change it from the original in the script. How is it? 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. Looks like all the essentials are here! Only thing you could add if you wanted to be extra nice is a link to the page in GitHub about PATs: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens |
||
#' @import cow | ||
#' @import dplyr | ||
#' @importFrom webshot2 webshot | ||
#' @importFrom magrittr %>% | ||
#' @importFrom rprojroot find_root has_dir | ||
#' @author Candace Savonen | ||
make_screenshots <- function(git_pat = NULL, repo = NULL, output_dir = "resources/chapt_screen_images", base_url = NULL){ | ||
|
||
# Find .git root directory | ||
root_dir <- find_root(has_dir(".git")) | ||
|
||
output_folder <- file.path(output_dir) | ||
if (!dir.exists(output_folder)) { | ||
dir.create(output_folder, recursive = TRUE) | ||
} | ||
|
||
if (is.null(base_url)){ | ||
base_url <- cow::get_pages_url(repo_name = repo, git_pat = git_pat) #what if these arguments are still NULL/not supplied? | ||
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. Do we want to make this cow function part of ottrpal? 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. Yes! That's next up! It's mentioned in #137 but I didn't realize I didn't make it its own issue so there is one now: jhudsl/cow#27 |
||
base_url <- gsub("/$", "", base_url) | ||
} | ||
|
||
# Collect all the chapter pages for the url given | ||
chapt_df <- ottrpal::get_chapters(html_page = file.path(root_dir, "docs", "index.html"), | ||
base_url = base_url) | ||
|
||
# Now take screenshots for each | ||
file_names <- lapply(chapt_df$url, function(url){ | ||
file_name <- gsub(".html", | ||
".png", | ||
file.path(output_folder, basename(url)) | ||
) | ||
|
||
# Get rid of special characters because leanpub no like | ||
file_name <- gsub(":|?|!|\\'", | ||
"", | ||
file_name | ||
) | ||
|
||
# Take the screenshot | ||
webshot(url, file = file_name) | ||
|
||
return(file_name) | ||
}) | ||
|
||
# Save file of chapter urls and file_names | ||
chapt_df <- chapt_df %>% | ||
dplyr::mutate(img_path = unlist(file_names)) | ||
|
||
chapt_df %>% | ||
readr::write_tsv(file.path(output_folder, "chapter_urls.tsv")) | ||
|
||
message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv"))) | ||
|
||
} | ||
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. Do I need to return something from this function? 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. I think returning the file path to |
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.
What's the best way to communicate that these are required arguments, but by default NULL?
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.
I would just not have them be NULL if they are required because then it will automatically throw an error if no argument is provided. And docs wise the way you say its "required" is fine 👍