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

Use realfavicongenerator API to build high-res PNG favicons #883

Merged
merged 24 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9289ffe
Update HEAD template to use PNG favicons instead of ICO
Bisaloo Nov 7, 2018
73d7311
Generate favicon using realfavicongenerator API
Bisaloo Nov 9, 2018
85db937
Update changelog with favicon changes
Bisaloo Nov 11, 2018
728031d
Remove dependency to magick
Bisaloo Nov 11, 2018
d348b2c
Read an exact number of bits instead of using 99999
Bisaloo Nov 11, 2018
923de17
Use identical instead of ==
Bisaloo Nov 11, 2018
6639bf1
Delete default favicon.ico
Bisaloo Nov 12, 2018
e71e5cc
Use list instead of template for json request
Bisaloo Nov 12, 2018
5305e0d
Remove config-favicon.json template
Bisaloo Nov 12, 2018
b88e3b3
Use utils::unzip instead of unzip
Bisaloo Nov 12, 2018
76c56a1
Add timeout and http_error check
Bisaloo Nov 12, 2018
4984fd9
Create build_favicon() function
Bisaloo Nov 13, 2018
93f1b23
Fix API failure check
Bisaloo Nov 13, 2018
cdcee75
Use fs instead of base for dir existence test
Bisaloo Nov 13, 2018
05c51bd
Update find-logo to find and prioritize svg icons
Bisaloo Nov 13, 2018
6cd21df
Add missing parenthesis
Bisaloo Nov 13, 2018
fdb0f29
Remove old favicon.ico
Bisaloo Nov 13, 2018
2c7f7d5
Check in API output
Bisaloo Nov 13, 2018
270759e
Update docs
Bisaloo Nov 13, 2018
4931de9
Edit docs again to remove all mentions of favicon.ico
Bisaloo Nov 13, 2018
8fa6f27
Add more formats in head template
Bisaloo Nov 13, 2018
06abab7
Add username to NEWS item
Bisaloo Nov 13, 2018
d05833a
Do not declare ico favicon
Bisaloo Nov 13, 2018
8ac1864
Remove unnecessary message
Bisaloo Nov 13, 2018
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
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Suggests:
jsonlite,
knitr,
leaflet,
magick,
testthat,
rticles
VignetteBuilder: knitr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export(as_pkgdown)
export(autolink_html)
export(build_article)
export(build_articles)
export(build_favicon)
export(build_home)
export(build_news)
export(build_reference)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# pkgdown 1.1.0.9000

* New function `build_favicon()` creates high resolution favicons, which are
better suited for modern web usage (e.g. retina display screens, desktop
shortcuts, etc.). For this, it uses the <http://realfavicongenerator.net>
API. As a side effect, this change also removes the dependency to the
magick package (@bisaloo, #883).

* Navbar version now gets class "version" so you can more easily control the
display if you want (#680).

Expand Down
2 changes: 1 addition & 1 deletion R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' # pkgdown <img src="man/figures/logo.png" align="right" />
#' ```
#'
#' [init_site()] will also automatically create a favicon.ico from your package
#' [init_site()] will also automatically create a favicon set from your package
#' logo.
#'
#' @section YAML config - home:
Expand Down
108 changes: 95 additions & 13 deletions R/build-logo.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,111 @@
build_logo <- function(pkg = ".") {

pkg <- as_pkgdown(pkg)

logo_path <- find_logo(pkg$src_path)
if (is.null(logo_path))
if (!dir_exists(path(pkg$src_path, "pkgdown", "favicon"))) {
return()
}

dir_copy_to(pkg, path(pkg$src_path, "pkgdown", "favicon"), pkg$dst_path)
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved

file_copy_to(pkg, logo_path, from_dir = path_dir(logo_path))
}

if (!requireNamespace("magick", quietly = TRUE)) {
message("magick not available, using default pkgdown favicon.ico")

if (!file_exists(path(pkg$dst_path, "favicon.ico"))) {
file_copy(path_pkgdown("assets", "favicon.ico"), pkg$dst_path)
}
return()
#' Create a complete set of favicons with different sizes from your package logo
#'
#' This function auto-detects the location of your package logo (with the name
#' `logo.csv` (recommended format) or `logo.png`) and runs it through the
#' <https://realfavicongenerator.net> API to build a complete set of favicons
#' with different sizes, as needed for modern web usage.
#'
#' You only need to run the function once. The favicon set will be stored in
#' `pkgdown/favicon` and copied by [init_site()] to the relevant location each
#' time the website is rebuilt.
#'
#' @inheritParams as_pkgdown
#'
#' @export
build_favicon <- function(pkg = ".") {

pkg <- as_pkgdown(pkg)

logo_path <- find_logo(pkg$src_path)

if (is.null(logo_path)) {
stop("Package logo could not be found. Aborting favicon creation.",
call. = FALSE)
}

logo <- readBin(logo_path, what = "raw", n = fs::file_info(logo_path)$size)

json_request <- list(
"favicon_generation" = list(
"api_key" = "87d5cd739b05c00416c4a19cd14a8bb5632ea563",
"master_picture" = list(
"type"= "inline",
"content"= openssl::base64_encode(logo)
),
"favicon_design" = list(
"desktop_browser" = list(),
"ios" = list(
"picture_aspect" = "no_change",
"assets" = list(
"ios6_and_prior_icons" = FALSE,
"ios7_and_later_icons" = TRUE,
"precomposed_icons" = FALSE,
"declare_only_default_icon" = TRUE
)
)
)
)
)

# It may take some time to generate the whole favicon set so we need to set
# a high timeout value.
request <- httr::POST("https://realfavicongenerator.net/api/favicon",
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved
body = json_request, encode = "json",
httr::timeout(10000)
)

if (httr::http_error(request)) {
stop("The API could not be reached. Please check your internet connection ",
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved
"or try again later.",
call. = FALSE
)
}

api_answer <- httr::content(request)
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved

if (!identical(api_answer$favicon_generation_result$result$status, "success")) {
stop("API request failed: please check that you are using supported file ",
"formats",
call. = FALSE
)
}

cat_line("Creating ", dst_path("favicon.ico"))
magick::image_read(logo_path) %>%
magick::image_scale("32x32") %>%
magick::image_write(path(pkg$dst_path, "favicon.ico"), format = "png")
tmp <- tempfile()

result <- httr::GET(api_answer$favicon_generation_result$favicon$package_url,
httr::write_disk(tmp)
)

utils::unzip(tmp, exdir = path(pkg$src_path, "pkgdown", "favicon"))

unlink(tmp)

}


find_logo <- function(path) {

logo_path <- path(path, "logo.svg")
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved
if (file_exists(logo_path))
return(logo_path)

logo_path <- path(path, "man", "figures", "logo.svg")
if (file_exists(logo_path))
return(logo_path)

logo_path <- path(path, "logo.png")
if (file_exists(logo_path))
return(logo_path)
Expand Down
11 changes: 5 additions & 6 deletions R/init.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#' Initialise site infrastructure
#'
#' This creates the output directory (`docs/`), `favicon.ico` (from the package
#' logo), a machine readable description of the site, and copies CSS/JS
#' assets and extra files.
#' This creates the output directory (`docs/`), a machine readable description
#' of the site, and copies CSS/JS assets and extra files.
#'
#' @section Build-ignored files:
#' We recommend using `usethis::use_pkgdown()` to build-ignore `docs/` and
Expand All @@ -18,9 +17,9 @@
#' `<HEAD>` after the default pkgdown CSS and JS.
#'
#' @section Favicon:
#' If you include you package logo in the standard location of
#' `man/figures/logo.png`, a favicon will be automatically created for
#' you.
#' You should manually run [build_favicon()] once to generate the favicon set
#' from your logo. The result is stored in `pkgdown/favicon` and will
#' automatically be copied to the relevant location when you run [init_site()].
#'
#' @inheritParams build_articles
#' @export
Expand Down
Binary file removed docs/favicon.ico
Binary file not shown.
Binary file removed inst/assets/favicon.ico
Binary file not shown.
7 changes: 6 additions & 1 deletion inst/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

<title>{{{pagetitle}}} • {{#site}}{{title}}{{/site}}</title>

<link rel="shortcut icon" type="image/x-icon" href="{{#site}}{{root}}{{/site}}favicon.ico">
<link rel="icon" type="image/png" sizes="16x16" href="{{#site}}{{root}}{{/site}}favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="{{#site}}{{root}}{{/site}}favicon-32x32.png">
Bisaloo marked this conversation as resolved.
Show resolved Hide resolved
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="{{#site}}{{root}}{{/site}}apple-touch-icon.png" />
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="{{#site}}{{root}}{{/site}}apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="{{#site}}{{root}}{{/site}}apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="{{#site}}{{root}}{{/site}}apple-touch-icon-60x60.png" />

<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
Expand Down
22 changes: 22 additions & 0 deletions man/build_favicon.Rd

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

2 changes: 1 addition & 1 deletion man/build_home.Rd

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

11 changes: 5 additions & 6 deletions man/init_site.Rd

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

Binary file added pkgdown/favicon/apple-touch-icon-120x120.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/apple-touch-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/apple-touch-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkgdown/favicon/favicon.ico
Binary file not shown.