-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
Add shadow_clone() to use themes #233
Open
tcgriffith
wants to merge
2
commits into
yihui:master
Choose a base branch
from
tcgriffith:dev_usetheme
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#' Use a new xaringan theme. | ||
#' | ||
#' This function clones an xaringan theme for you to use. Include it in a code chunk in the xaringan slide source. | ||
#' | ||
#' | ||
#' @param theme Either a built-in theme shipped with xaringan, or the repository address of the theme in the format of username/repo | ||
#' @param ref desired git reference. could be a commit SHA, or a branch name. Defauts to master | ||
#' | ||
#' | ||
#' @return The cloned theme | ||
#' @export | ||
#' | ||
#' @examples | ||
#' # built-in | ||
#' shadow_clone("kunoichi") | ||
#' # remote | ||
#' shadow_clone("tcgriffith/xaringan_theme_example") | ||
#' | ||
shadow_clone = function(theme=NULL, ref="master"){ | ||
|
||
## retrieve built-in themes | ||
builtin_css = list_css() | ||
builtin_theme = names(builtin_css) | ||
|
||
## similar to remotes::install_github, convert repo into full url on github | ||
## theme=tcgriffith/xaringan_theme_example -> | ||
## css_remote = https://raw.githubusercontent.com/tcgriffith/xaringan_theme_example/master/xaringan_theme_example.css | ||
|
||
if (is.null(theme)) return(NULL) | ||
|
||
bn = basename(theme) | ||
css_remote = sprintf("https://raw.githubusercontent.com/%s/%s/%s.css", | ||
theme, | ||
ref, | ||
bn) | ||
css_local = basename(css_remote) | ||
|
||
## built-in theme | ||
if (theme %in% builtin_theme) { | ||
message("Theme: ", theme) | ||
message(sprintf("To use the same theme, try shadow_clone(%s)",theme)) | ||
css_local = builtin_css[theme] | ||
} | ||
## remote theme | ||
else { | ||
## extract git hash as ref and print out as message | ||
repo = paste0("https://github.com/", theme) | ||
cmd = paste0("git ls-remote ",repo," refs/heads/master") | ||
rslt = system(command=cmd, intern=TRUE) | ||
ref = substr(rslt, 1,8) | ||
|
||
message("Theme repo:", repo) | ||
message("ref: ", ref) | ||
message(sprintf("To use the same theme, try shadow_clone(%s,%s)",theme, ref)) | ||
|
||
## download remote css when css_local doesn't exist | ||
if (!file.exists(css_local)){ | ||
try_dl = try(xfun::download_file(css_remote)) | ||
## if download failed, fallback to default CSS. warning | ||
if(try_dl != 0 ) { | ||
warning("# Shadow clone failed. Theme not found.") | ||
css_local = NULL | ||
} | ||
} | ||
} | ||
|
||
## include CSS in the final xaringan html. | ||
|
||
if(!is.null(css_local)){ | ||
htmltools::includeCSS(css_local) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the name sounds fancy, I am worried about if it will be descriptive for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buuut, it fits the naruto theme!