Skip to content

Commit

Permalink
Allow full url in theme arg in install_theme (#270) (#271)
Browse files Browse the repository at this point in the history
* Allow full URL in theme arg in new_site and install_theme (#270)

* Unzip theme into tmpdir to respect force arg

* Strip tmpdir from zipdir after finding expdir

* Refactor zipdir/newdir in install_theme

* Clean SHA and branch from theme install dir

* Hard OR if branch is empty string

* Add contributor and news item for theme arg update
  • Loading branch information
gadenbuie authored and yihui committed Mar 6, 2018
1 parent 051efca commit 4266c33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Beilei", "Bian", role = "ctb"),
person("Forest", "Fang", role = "ctb"),
person("Garrick", "Aden-Buie", role = "ctb"),
person("Hiroaki", "Yutani", role = "ctb"),
person("Ian", "Lyttle", role = "ctb"),
person("JJ", "Allaire", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- The `new_post` addin now lets you choose an archetype. See https://gohugo.io/content-management/archetypes/ for more details (thanks, @lcolladotor, #173).

- The `theme` argument of `install_theme()` and `new_site()` now accepts a full URL to a theme's repository zip file. This can be used to install themes from other web-based git hosts, like GitLab and Bitbucket (thanks @gadenbuie, #271).

## BUG FIXES

- The `kind` argument (i.e., the archetype) of `new_content()` now works with files that end in `.Rmd` and `.Rmarkdown`. The archetype still has to end in `.md` for Hugo to work with it (thanks, @lcolladotor, #261).
Expand Down
36 changes: 25 additions & 11 deletions R/hugo.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ change_config = function(name, value) {
#' @param sample Whether to add sample content. Hugo creates an empty site by
#' default, but this function adds sample content by default).
#' @param theme A Hugo theme on Github (a chararacter string of the form
#' \code{user/repo}, and you can optionally sepecify a GIT branch or tag name
#' \code{user/repo}, and you can optionally specify a GIT branch or tag name
#' after \code{@@}, i.e. \code{theme} can be of the form
#' \code{user/repo@@branch}). If \code{theme = NA}, no themes will be
#' installed, and you have to manually install a theme.
#' \code{user/repo@@branch}). You can also specify a full URL to the zip file of
#' the theme. If \code{theme = NA}, no themes will be installed, and you have
#' to manually install a theme.
#' @param hostname Where to find the theme. Defaults to \code{github.com}; specify
#' if you wish to use an instance of GitHub Enterprise.
#' if you wish to use an instance of GitHub Enterprise. You can also specify the
#' full URL of the zip file in \code{theme}, in which case this argument is ignored.
#' @param theme_example Whether to copy the example in the \file{exampleSite}
#' directory if it exists in the theme. Not all themes provide example sites.
#' @param serve Whether to start a local server to serve the site.
Expand Down Expand Up @@ -142,19 +144,28 @@ install_theme = function(
theme, hostname = 'github.com', theme_example = FALSE, update_config = TRUE, force = FALSE
) {
r = '^([^/]+/[^/@]+)(@.+)?$'
if (!is.character(theme) || length(theme) != 1 || !grepl(r, theme)) {
warning("'theme' must be a character string of the form 'user/repo' or 'user/repo@branch'")
r_zip = "\\.zip$"
theme_is_url = grepl(r_zip, theme)
if (!is.character(theme) || length(theme) != 1 || (!grepl(r, theme) & !theme_is_url)) {
warning("'theme' must be a character string of the form 'user/repo' or 'user/repo@branch', or a full URL to the .zip file")
return(invisible())
}
branch = sub('^@', '', gsub(r, '\\2', theme))
if (branch == '') branch = 'master'
if (branch == '' || theme_is_url) branch = 'master'
theme = gsub(r, '\\1', theme)
dir_create('themes')
in_dir('themes', {
url = sprintf('https://%s/%s/archive/%s.zip', hostname, theme, branch)
zipfile = sprintf('%s.zip', basename(theme))
if (theme_is_url) {
url = theme
zipfile = gsub(".+/(.+\\.zip)", "\\1", theme)
} else {
url = sprintf('https://%s/%s/archive/%s.zip', hostname, theme, branch)
zipfile = sprintf('%s.zip', basename(theme))
}
download2(url, zipfile, mode = 'wb')
files = utils::unzip(zipfile)
tmpdir = tempfile("", ".")
on.exit(in_dir('themes', unlink(tmpdir, recursive = TRUE)))
files = utils::unzip(zipfile, exdir = tmpdir)
zipdir = dirname(files)
zipdir = zipdir[which.min(nchar(zipdir))]
expdir = file.path(zipdir, 'exampleSite')
Expand All @@ -167,14 +178,17 @@ install_theme = function(
'and at least take a look at the config file config.toml of the example site, ',
'because not all Hugo themes work with any config files.'
)
newdir = gsub(sprintf('-%s$', branch), '', zipdir)
newdir = gsub(tmpdir, ".", zipdir)
newdir = gsub("-[a-f0-9]{12,40}$", "", newdir)
newdir = gsub(sprintf('-%s$', branch), '', newdir)
if (!force && dir_exists(newdir)) stop(
'The theme already exists. Try install_theme("', theme, '", force = TRUE) ',
'after you read the help page ?blogdown::install_theme.', call. = FALSE
)
unlink(newdir, recursive = TRUE)
file.rename(zipdir, newdir)
unlink(zipfile)
if (theme_is_url) theme = newdir
})
if (update_config) {
change_config('theme', sprintf('"%s"', basename(theme)))
Expand Down

0 comments on commit 4266c33

Please sign in to comment.