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

Allow full url in theme arg in install_theme (#270) #271

Merged
merged 7 commits into from Mar 6, 2018

Conversation

gadenbuie
Copy link
Member

@gadenbuie gadenbuie commented Mar 6, 2018

This PR allows the theme argument in new_site() and install_theme() to accept a full URL to the zip file of the theme, in line with the discussions in #264 and #270.

Some notes:

I did not add any validation on the URL other than to check that it ends in .zip. This lets the process be as flexible as possible and download2() will throw the error if the URL is invalid or doesn't work. This also means the user can specify a local file, like file:///home/user/archive.zip or any other unusual configuration.

Because I didn't attempt to guess at the structure of the URL, we don't know the theme's base name and we have to rely on the first level folder in the zip file. This is in line with how install_theme() currently works, but previously you used knowledge of the branch (parsed from the theme argument) to remove the trailing -<branch> from the theme name. The end result here is that the theme may end up with a long, unique name depending on how it's zipped by the repo host, but this will be transparent to the user (no warning or message emitted).

To illustrate the process, in the use case described in #270,

  1. the full URL is https://gitlab.com/syui/hugo-theme-wave/repository/master/archive.zip,

  2. the theme is downloaded, extracted and stored in themes/hugo-theme-wave-master-13b859a257213881038ebeb1021a5578c243653b

  3. and the config file is changed to theme = "hugo-theme-wave-master-13b859a257213881038ebeb1021a5578c243653b"

Finally, there is the question of force = TRUE. Sticking to minimal changes, specifying the full URL is equivalent to force = TRUE because the folder name is only changed using the user/repo style name. Previously, you were reasonably certain that the zip file would extract into a different folder than the final folder, so there aren't any checks that the unzipping will overwrite. This is the expected behavior in d198998.

I added a second commit, 4bda539, where the zip file is extracted into a temp folder inside themes/. The tmpdir name is stripped from the zip file list so that we still get the theme folder, and if force = FALSE and the theme dir (zipdir) exists inside themes/ the error is thrown, tmpdir is unlinked, and the zip file remains (as this was the previous behavior). I've separated this into a second commit so that you can see behavior with and without the usage of a tmpdir. Feel free to revert or ask for changes as needed.

p.s. will be happy to add news entry and finalize PR, etc. after your review.

@gadenbuie
Copy link
Member Author

FYI here are three example URLs

github_url = "https://github.com/road2stat/hugo-tanka/archive/master.zip"

gitlab_url = "https://gitlab.com/syui/hugo-theme-wave/repository/master/archive.zip"

bitbucket_url = "https://bitbucket.org/thunderrabbit/hugo-theme-thunderrabbit/get/master.zip"

R/hugo.R Outdated
zipdir = dirname(files)
zipdir = gsub(tmpdir, ".", zipdir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too early. The dir expdir on line 173 won't be found if you strip tmpdir from zipdir at this point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I just moved line 170 down so that it's stripped after finding expdir.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, on second thought, it's only needed for newdir, so I'm going to refactor a bit.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks good to me now.

For newdir, do you think it make sense to remove -[a-f0-9]{32,}$ (which "apparently" looks like SHA)?

@yihui
Copy link
Member

yihui commented Mar 6, 2018

Please also add yourself to DESCRIPTION as ctb (alphabetical order by first names) and an item in NEWS.md. Thanks!

R/hugo.R Outdated
@@ -179,15 +178,16 @@ 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(sprintf('-%s$', branch), '', newdir)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yihui Should I add an if (!theme_is_url) here so that it doesn't even try to change the filename if the zip file is given? The alternative would be to add some logic around the branch regex and then hope we get lucky at this stage ... so seems to me to be better to skip this if theme_is_url. I didn't do this because I was trying not to add a ton of logic and branching paths, but it's been nagging at me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditioning on theme_is_url makes sense to me.

@yihui yihui added this to the v0.6 milestone Mar 6, 2018
@gadenbuie
Copy link
Member Author

Re SHA, that's something I played with too. Getting rid of it is easy with that regex, it's easier than guessing the branch. But maybe it won't work depending on how the repo is bundled? So I kinda settled on consistency in the sense that the theme dir is the directory given by the bundler (see my comment on removing branch)... that said I'm happy to add commit hashes removal if you think it's a good idea.

@gadenbuie
Copy link
Member Author

gadenbuie commented Mar 6, 2018

Here's a quick sketch for trying to clean up the theme directory name. If the theme is a URL, guess that branch = 'master'.

if (branch == '' | theme_is_url) branch = 'master'

Then remove SHA hash if at end of directory name, and then remove branch name if at the end.

newdir = gsub(tmpdir, ".", zipdir)
newdir = gsub("-[a-f0-9]{7,40}$", "", newdir)
newdir = gsub(sprintf('-%s$', branch), '', newdir)

Note the regex is from https://stackoverflow.com/a/468378/2022615 -- bitbucket uses shortened hashes, like thunderrabbit-hugo-theme-thunderrabbit-c418b1a1584d.

To make this easy, give this a thumbs up and I'll push above changes. thumbs down is don't change folder name at all if theme is a URL.

@yihui
Copy link
Member

yihui commented Mar 6, 2018

👍 I feel 5 is probably a little risky... Removing the SHA hash is a feature "good to have", but not really necessary. For now, we can probably just stick with 40, and change it when other users actually request it.

R/hugo.R Outdated
@@ -151,7 +151,7 @@ install_theme = function(
return(invisible())
}
branch = sub('^@', '', gsub(r, '\\2', theme))
if (branch == '') branch = 'master'
if (branch == '' | theme_is_url) branch = 'master'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| -> ||

@gadenbuie
Copy link
Member Author

Cool and I agree! I kinda went middle of the road and set lower limit to 12 so that bitbucket zips work. There's no chance for clashes with any English words at that limit. Using branch = "master" when the theme_is_url works really well for GitHub and Gitlab.

@yihui yihui merged commit 4266c33 into rstudio:master Mar 6, 2018
@yihui
Copy link
Member

yihui commented Mar 6, 2018

Perfect. Thanks @gadenbuie!

@yihui
Copy link
Member

yihui commented Mar 6, 2018

Actually could you do me one more favor to reply to #270?

@gadenbuie
Copy link
Member Author

Of course, thanks for the reminder!

yihui added a commit that referenced this pull request Mar 7, 2018
@gadenbuie gadenbuie deleted the theme-full-url branch March 9, 2018 17:07
@yihui
Copy link
Member

yihui commented Mar 11, 2018

This PR probably introduced a bug on Windows: https://stackoverflow.com/q/49212335/559676 Do you mind following up with that post and see if you are able to fix it? @gadenbuie (Unfortunately I've been sick for a few days)

@gadenbuie
Copy link
Member Author

@yihui Sure thing, I'll take a look tonight. Sorry about the bug, Windows is a bit of a blind spot for me. And hope you feel better soon!

@gadenbuie
Copy link
Member Author

@yihui I was able to reproduce the issue, which I believe I've solved with #276. I replied in a comment to the SO question, pointing them to the new PR and with instructions to get back to previous behavior in the meantime. Let me know if there's anything else I can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants