From 25323f2bff8327c9185385e1b43fb055e33244a8 Mon Sep 17 00:00:00 2001 From: tcgriffith Date: Thu, 31 Oct 2019 11:34:47 +1000 Subject: [PATCH 1/2] shadow_clone done --- NAMESPACE | 1 + R/ninjutsu.R | 72 +++++++++++++++++++++++++++++++++++++++++++++ man/shadow_clone.Rd | 26 ++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 R/ninjutsu.R create mode 100644 man/shadow_clone.Rd diff --git a/NAMESPACE b/NAMESPACE index bed722e3..3ecb4897 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(decktape) export(inf_mr) export(infinite_moon_reader) export(moon_reader) +export(shadow_clone) export(summon_remark) export(tsukuyomi) import(stats) diff --git a/R/ninjutsu.R b/R/ninjutsu.R new file mode 100644 index 00000000..fb3935eb --- /dev/null +++ b/R/ninjutsu.R @@ -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 = xaringan:::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 = stringr::str_sub(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) + } +} diff --git a/man/shadow_clone.Rd b/man/shadow_clone.Rd new file mode 100644 index 00000000..bbece526 --- /dev/null +++ b/man/shadow_clone.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ninjutsu.R +\name{shadow_clone} +\alias{shadow_clone} +\title{Use a new xaringan theme.} +\usage{ +shadow_clone(theme = NULL, ref = "master") +} +\arguments{ +\item{theme}{Either a built-in theme shipped with xaringan, or the repository address of the theme in the format of username/repo} + +\item{ref}{desired git reference. could be a commit SHA, or a branch name. Defauts to master} +} +\value{ +The cloned theme +} +\description{ +This function clones an xaringan theme for you to use. Include it in a code chunk in the xaringan slide source. +} +\examples{ +# built-in +shadow_clone("kunoichi") +# remote +shadow_clone("tcgriffith/xaringan_theme_example") + +} From e20b94a49486f17c7a69411580b9620f6b605408 Mon Sep 17 00:00:00 2001 From: tcgriffith Date: Thu, 31 Oct 2019 12:14:57 +1000 Subject: [PATCH 2/2] remove stringr dependency --- R/ninjutsu.R | 4 ++-- man/decktape.Rd | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/R/ninjutsu.R b/R/ninjutsu.R index fb3935eb..1afad699 100644 --- a/R/ninjutsu.R +++ b/R/ninjutsu.R @@ -19,7 +19,7 @@ shadow_clone = function(theme=NULL, ref="master"){ ## retrieve built-in themes - builtin_css = xaringan:::list_css() + builtin_css = list_css() builtin_theme = names(builtin_css) ## similar to remotes::install_github, convert repo into full url on github @@ -47,7 +47,7 @@ shadow_clone = function(theme=NULL, ref="master"){ repo = paste0("https://github.com/", theme) cmd = paste0("git ls-remote ",repo," refs/heads/master") rslt = system(command=cmd, intern=TRUE) - ref = stringr::str_sub(rslt, 1,8) + ref = substr(rslt, 1,8) message("Theme repo:", repo) message("ref: ", ref) diff --git a/man/decktape.Rd b/man/decktape.Rd index 61fadc7d..ce6a5431 100644 --- a/man/decktape.Rd +++ b/man/decktape.Rd @@ -43,8 +43,7 @@ For some operating systems you may need to } \examples{ if (interactive()) { - xaringan::decktape("https://slides.yihui.name/xaringan", "xaringan.pdf", - docker = FALSE) + xaringan::decktape('https://slides.yihui.name/xaringan', 'xaringan.pdf', docker = FALSE) } } \references{