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

Change to webshot2 #61

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Imports:
R.utils,
readr,
stringr,
webshot
webshot2
Suggests:
knitr,
rcrossref,
rcrossref (>= 1.2),
rmarkdown,
testthat (>= 3.0.0),
withr
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ importFrom(stringr,str_remove_all)
importFrom(stringr,str_replace)
importFrom(stringr,str_replace_all)
importFrom(stringr,str_trunc)
importFrom(webshot,webshot)
importFrom(webshot2,webshot)
9 changes: 5 additions & 4 deletions R/drop_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#' In other words, either one, many or no BibTeX citation keys can be specified.
#' @param export_as A string specifying the desired output format. For now supports PNG and HTML.
#' Use "html" to include the 'bare' taglist (recommended for inclusion in Rmarkdown documents) or "html_full" to write a standalone .html file including <head> etc.
#' The PNG is a screenshot of the rendered HTML via the 'webshot' package. The filename represents this two step approach on purpose.
#' For webshot you need to install phantomJS once (see 'webshot' documentation).
#' The PNG is a screenshot of the rendered HTML via the 'webshot2' package. The filename represents this two step approach on purpose.
#' @param output_dir A string specifying the relative path, where the rendered output files should be stored.
#' @param max_authors Integer number of maximum authors to print. If the number of authors exceeds this, the list is cropped accordingly.
#' @param include_qr Character string specifying the way the QR code should be included or if no QR code should be included.
Expand Down Expand Up @@ -83,7 +82,7 @@
#' @import dplyr
#' @importFrom htmltools tags save_html
#' @importFrom lubridate year ymd
#' @importFrom webshot webshot
#' @importFrom webshot2 webshot
#' @importFrom stringr str_detect str_remove_all str_trunc str_replace_all

drop_name <- function(bib, cite_key,
Expand Down Expand Up @@ -293,7 +292,9 @@ drop_name <- function(bib, cite_key,
# handle different input options for bibtex keys
if (missing(cite_key)) {
n_bib <- nrow(clean_bib)
message(paste0("No cite_key specified. Working through all possible ", n_bib, " entries in the bibliography."))
if (n_bib > 1) {
message(paste0("No cite_key specified. Working through all possible ", n_bib, " entries in the bibliography."))
}
work_list <- clean_bib
} else if (!is.character(cite_key)) {
stop("cite_key must be of type 'caracter'.")
Expand Down
13 changes: 5 additions & 8 deletions R/write_vc.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' }
#'
#' @importFrom htmltools tags save_html
#' @importFrom webshot webshot
#' @importFrom webshot2 webshot

write_vc <- function(work_item, path_absolute, output_dir, export_as) {

Expand Down Expand Up @@ -73,10 +73,7 @@ write_vc <- function(work_item, path_absolute, output_dir, export_as) {
}
)
} else if (export_as == "png") {
if (!webshot::is_phantomjs_installed()) {
message("You need to download and install phantomJS to save output as PNG. Try running 'webshot::install_phantomjs()' once.")
} else {
# renders as "complete" html to get the white background for PNG snapshot.
# renders as "complete" html to get the white background for PNG snapshot.
tryCatch(
expr = {
htmltools::save_html(work_item$vcs, file = output_file)
Expand All @@ -91,10 +88,9 @@ write_vc <- function(work_item, path_absolute, output_dir, export_as) {
}
)


tryCatch(
expr = {
webshot::webshot(output_file, paste0(output_file, ".png"), selector = ".visual-citation", zoom = 2)
webshot2::webshot(output_file, paste0(output_file, ".png"), selector = ".visual-citation", zoom = 2)
},
error = function(e) {
message("Could not take a screenshot of the intermediate HTML.")
Expand All @@ -116,7 +112,7 @@ write_vc <- function(work_item, path_absolute, output_dir, export_as) {
R.utils::getRelativePath(png_out)
)
return(return_path_png)
}

} else {
stop("Output format unknown")
}
Expand All @@ -128,4 +124,5 @@ write_vc <- function(work_item, path_absolute, output_dir, export_as) {
)

return(return_path_html)

}
3 changes: 1 addition & 2 deletions man/drop_name.Rd

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

3 changes: 1 addition & 2 deletions man/drop_name_crossref.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-write_vc.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ test_that("saving as html works as expected", {
# "Some 2023", "Alice; Bob; Charlie", "Journal of Unnecessary R Packages", "Alice2023", "2023", VCS
# )
#
# # writes a file, this test can only be run, if phantomJS is installed:
# if(webshot::is_phantomjs_installed()) {
# # writes a file, this test can only be run, if a chromium browser is installed:
# if(!is.null(chromote::find_chrome())) {
# withr::with_dir(
# new = new,
# code = {
Expand Down