From 23e8fbd664edea9eec75b36ba2afb993e93ebaab Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 26 Jun 2024 15:38:26 +0000 Subject: [PATCH] Delete manuscript folder --- make_screenshots.R | 82 +++++++++++++++++++ .../chapt_screen_images/chapter_urls.tsv | 6 ++ 2 files changed, 88 insertions(+) create mode 100644 make_screenshots.R create mode 100644 resources/chapt_screen_images/chapter_urls.tsv diff --git a/make_screenshots.R b/make_screenshots.R new file mode 100644 index 0000000..5265d31 --- /dev/null +++ b/make_screenshots.R @@ -0,0 +1,82 @@ +#!/usr/bin/env Rscript +# Written by Candace Savonen Jan 2022 + +if (!('devtools' %in% installed.packages())) { + # install.packages("remotes", repos = "http://cran.us.r-project.org") +} + +if (!('optparse' %in% installed.packages())) { + # install.packages("optparse", repos = "http://cran.us.r-project.org") +} + +# Find .git root directory +root_dir <- rprojroot::find_root(rprojroot::has_dir(".git")) + + +library(optparse) +library(magrittr) + +option_list <- list( + optparse::make_option( + c("--repo"), + type = "character", + default = NULL, + help = "GitHub repository name, e.g. jhudsl/OTTR_Template", + ), + optparse::make_option( + c("--git_pat"), + type = "character", + default = NULL, + help = "GitHub personal access token", + ), + optparse::make_option( + c("--output_dir"), + type = "character", + default = "resources/chapt_screen_images", + help = "Output directory where the chapter's screen images should be stored", + ), + optparse::make_option( + c("--base_url"), + type = "character", + default = NULL, + help = "Output directory where the chapter's screen images should be stored", + ) +) + +# Read the arguments passed +opt_parser <- optparse::OptionParser(option_list = option_list) +opt <- optparse::parse_args(opt_parser) + +output_folder <- file.path(opt$output_dir) +if (!dir.exists(output_folder)) { + dir.create(output_folder, recursive = TRUE) +} + +if (is.null(opt$base_url)) { + base_url <- cow::get_pages_url(repo_name = opt$repo, git_pat = opt$git_pat) + 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")) + +# 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 + webshot2::webshot(url, file = file_name) + + return(file_name) + +}) + +# Save file of chapter urls and file_names +chapt_df %>% + dplyr::mutate(img_path = unlist(file_names)) %>% + readr::write_tsv(file.path(output_folder, "chapter_urls.tsv")) + +message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv"))) diff --git a/resources/chapt_screen_images/chapter_urls.tsv b/resources/chapt_screen_images/chapter_urls.tsv new file mode 100644 index 0000000..8f9a82a --- /dev/null +++ b/resources/chapt_screen_images/chapter_urls.tsv @@ -0,0 +1,6 @@ +chapt_titles data_level data_path +Introduction to the Course 1 intro.html +New Chapter 2 first-section-new-chapter.html +New Chapter 3 second-section-new-chapter.html +New Chapter 4 third-section-new-chapter.html +Conclusion 5 conclusion.html