From bce00b56b40c5ed0467d4c2552bbb81bdef2da76 Mon Sep 17 00:00:00 2001 From: raulossada Date: Sat, 12 Jul 2014 09:49:09 -0300 Subject: [PATCH 1/7] Fixed the error of not finding npm on Windows 8.1 --- R/checkForGitbook.R | 2 +- R/gitbookInfo.R | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/checkForGitbook.R b/R/checkForGitbook.R index 80c6744..cfe6927 100644 --- a/R/checkForGitbook.R +++ b/R/checkForGitbook.R @@ -6,7 +6,7 @@ #' @param quiet logical indicating whether messages should be printed. #' @export checkForGitbook <- function(quiet=FALSE) { - if(system('npm', ignore.stdout=TRUE) != 0) { + if(system('npm -v', ignore.stdout=TRUE) != 0) { stop("Cannot find node.js. You can install it from http://nodejs.org/download/") } if(system('gitbook', ignore.stdout=TRUE) != 0) { diff --git a/R/gitbookInfo.R b/R/gitbookInfo.R index 48a95c0..430d9ab 100644 --- a/R/gitbookInfo.R +++ b/R/gitbookInfo.R @@ -7,7 +7,10 @@ #' @export gitbookInfo <- function() { checkForGitbook(quiet=TRUE) - installed <- system('gitbook --version', intern=TRUE) + listInstalledModules <- system("npm list -g", intern=TRUE); + gitbookIndex <- grep("gitbook@", listInstalledModules, fixed=TRUE); + position <- regexpr("gitbook@", listInstalledModules[gitbookIndex], fixed=TRUE); + installed <- substr( listInstalledModules[gitbookIndex], position+8, nchar(listInstalledModules[gitbookIndex]) ); current <- system('npm view gitbook version', intern=TRUE) if(length(current) > 0) { current <- current[1] From a266d11cbbab5ad72127802bb9439141d2cdca00 Mon Sep 17 00:00:00 2001 From: raulossada Date: Sat, 12 Jul 2014 15:45:47 -0300 Subject: [PATCH 2/7] Update the initGitbook function. Using the version proposed by benmarwick. --- R/initGitbook.R | 77 ++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/R/initGitbook.R b/R/initGitbook.R index 4c6bd96..4023845 100644 --- a/R/initGitbook.R +++ b/R/initGitbook.R @@ -8,44 +8,41 @@ #' #' @export initGitbook <- function(dir=getwd()) { - dir <- normalizePath(dir) - checkForGitbook(quiet=TRUE) - oldwd <- setwd(dir) - test <- system(paste0('gitbook init ', dir)) - if(test != 0) { stop("gitbook initalization failed") } - mdfiles <- list.files(dir, '*.md', recursive=TRUE, full.names=TRUE) - mdfiles <- mdfiles[-c(grep('README.md$', mdfiles), - grep('SUMMARY.md$', mdfiles))] - mdfiles2 <- gsub('/.md$', '.Rmd', mdfiles) - file.rename(mdfiles, mdfiles2) - - knitr.header <- c( # TODO: make a package option? - "```{r knitsetup, echo=FALSE, results='hide', warning=FALSE, message=FALSE, cache=FALSE}", - "opts_knit$set(base.dir='./', fig.path='', out.format='md')", - "opts_chunk$set(prompt=TRUE, comment='', results='markup')", - "# See yihui.name/knitr/options for more Knitr options.", - "##### Put other setup R code here", - "", - "```", - "" - ) - for(rmd in mdfiles2) { - file <- file(rmd) - lines <- readLines(file) - close(file) - - #if the knitsetup block isn't already in the file, then add it - suppressWarnings( - if(grepl("r knitsetup.+\n", lines)) { - lines <- c(knitr.header, lines) - } - ) - - file <- file(rmd) - writeLines(lines, file(rmd)) - close(file) - } - - setwd(oldwd) - invisible() + dir <- normalizePath(dir) + checkForGitbook(quiet = TRUE) + oldwd <- setwd(dir) + test <- system(paste0("gitbook init ", dir)) + if (test != 0) { + stop("gitbook initalization failed") + } + mdfiles <- list.files(dir, "*.md", recursive = TRUE, full.names = TRUE) + mdfiles <- mdfiles[-c(grep("README.md$", mdfiles), grep("SUMMARY.md$", mdfiles))] + mdfiles2 <- gsub("\\.md$", ".Rmd", mdfiles) + file.rename(mdfiles, mdfiles2) + knitr.header <- c("```{r knitsetup, echo=FALSE, results='hide', warning=FALSE, message=FALSE, cache=FALSE}", + "opts_knit$set(base.dir='./', fig.path='', out.format='md')", + "opts_chunk$set(prompt=TRUE, comment='', results='markup')", + "# See yihui.name/knitr/options for more Knitr options.", + "##### Put other setup R code here", "", "", + "# end setup chunk", + "```") + + for (rmd in mdfiles2) { + file <- file(rmd) + lines <- readLines(file) + close(file) + # don't inject knitr setup chunk if Rmd file + # already has one, ie. editing an existing Rmd + # or the references Rmd, which has a different setup + toMatch <- c("r knitsetup", "r setup") + matches <- grepl(paste(toMatch,collapse="|"), lines) + suppressWarnings(if (!any(matches)) { + lines <- c(knitr.header, lines) + }) + file <- file(rmd) + writeLines(lines, file(rmd)) + close(file) + } + setwd(oldwd) + invisible() } From d0afb0d230d4d9ff47d7a58194f7509c33c719e5 Mon Sep 17 00:00:00 2001 From: raulossada Date: Sat, 12 Jul 2014 19:40:02 -0300 Subject: [PATCH 3/7] Update gitbookInfo.R Using `npm list -g` instead of `gitbook --version` since it seems that the later one needs some special permission to execute directly from RStudio in Windows 8.1 x64. Although running `cmd` and using `gitbook --version` works fine. --- R/gitbookInfo.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/gitbookInfo.R b/R/gitbookInfo.R index 430d9ab..fbcc7b0 100644 --- a/R/gitbookInfo.R +++ b/R/gitbookInfo.R @@ -7,7 +7,8 @@ #' @export gitbookInfo <- function() { checkForGitbook(quiet=TRUE) - listInstalledModules <- system("npm list -g", intern=TRUE); + # Use npm instead of gitbook. + listInstalledModules <- system("npm list -g", intern=TRUE); gitbookIndex <- grep("gitbook@", listInstalledModules, fixed=TRUE); position <- regexpr("gitbook@", listInstalledModules[gitbookIndex], fixed=TRUE); installed <- substr( listInstalledModules[gitbookIndex], position+8, nchar(listInstalledModules[gitbookIndex]) ); From 990d4de23449bc687927700f728829bdf04b2d35 Mon Sep 17 00:00:00 2001 From: raulossada Date: Tue, 15 Jul 2014 12:14:56 -0300 Subject: [PATCH 4/7] Update publishGitbook.R Using https instead of ssh to avoid firewalss and proxy issues --- R/publishGitbook.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/publishGitbook.R b/R/publishGitbook.R index d270220..5e55848 100644 --- a/R/publishGitbook.R +++ b/R/publishGitbook.R @@ -24,6 +24,6 @@ publishGitbook <- function(repo, "git checkout -b gh-pages \n", "git add . \n", "git commit -am '", message, "' \n", - "git push git@github.com:", repo, " gh-pages --force ") + "git push https://github.com/", repo, " gh-pages --force ") system(cmd) } From fd6bcfe756b5aa280a39706a3d765c925dab00a2 Mon Sep 17 00:00:00 2001 From: raulossada Date: Wed, 16 Jul 2014 00:27:50 -0300 Subject: [PATCH 5/7] Update publishGitbook.R --- R/publishGitbook.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/publishGitbook.R b/R/publishGitbook.R index 5e55848..6c487fd 100644 --- a/R/publishGitbook.R +++ b/R/publishGitbook.R @@ -14,16 +14,16 @@ #' @export publishGitbook <- function(repo, out.dir=paste0(getwd(), '/_book'), - message='Update built gitbook') { + msg='Update built gitbook') { test <- system('git --version', ignore.stderr=TRUE, ignore.stdout=TRUE, show.output.on.console=FALSE) if(test != 0) { stop('Git does not appear to be installed.')} cmd <- paste0( "cd ", out.dir, " \n", "git init \n", - "git commit --allow-empty -m '", message,"' \n", + "git commit --allow-empty -m '", msg,"1' \n", "git checkout -b gh-pages \n", "git add . \n", - "git commit -am '", message, "' \n", + "git commit -a -m '", msg, "2' \n", "git push https://github.com/", repo, " gh-pages --force ") system(cmd) } From 008b2bf5436267e63490cf42c5269b995f11b537 Mon Sep 17 00:00:00 2001 From: raulossada Date: Sat, 29 Jul 2017 16:52:16 -0300 Subject: [PATCH 6/7] Testing a new buildGitbook version --- R/buildGitbook2.R | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 R/buildGitbook2.R diff --git a/R/buildGitbook2.R b/R/buildGitbook2.R new file mode 100644 index 0000000..c2d743e --- /dev/null +++ b/R/buildGitbook2.R @@ -0,0 +1,63 @@ +#' This will build a gitbook from the source markdown files. +#' +#' This function is simply a wrapper to a system call to \code{gitbook}. +#' +#' \url{https://github.com/GitbookIO/gitbook} +#' +#' @param source.dir location containing the source files. +#' @param out.dir location of the built book. +#' @param format the format of book. Options are pdf or ebook. If omitted, +#' this will build a website. +#' @param buildRmd should \code{\link{buildRmd}} be called first. +#' @param gitbook.params other parameters passed to the gitbook command. +#' @param ... other parameters passed to \code{\link{buildRmd}}. +#' @export +buildGitbook2 <- function(source.dir=getwd(), + out.dir=paste0(getwd(), '/_book'), + buildRmd = TRUE, + format, + gitbook.params, ...) { + + if(buildRmd) { + message('Building R markdown files...') + buildRmd(source.dir, ...) + message('R Markdown files successfully built!') + } + + checkForGitbook(quiet=TRUE) + + buildCmd <- 'build' + if(!missing(format)) { buildCmd <- format } + ### cmd <- paste0("gitbook ", buildCmd, " ", source.dir, " --output=", out.dir) + + ro_args <- paste0(buildCmd, " --gitbook=2.6.4 ", source.dir, " --output=", out.dir); + + + #if(!missing(title)) { cmd <- paste0(cmd, ' --title="', title, '"') } + #if(!missing(intro)) { cmd <- paste0(cmd, ' --intro="', intro, '"') } + #if(!missing(github)) { cmd <- paste0(cmd, ' --github=', github) } + #if(mathjax) { cmd <- paste0(cmd, " --plugins plugin-mathjax") } + if(!missing(gitbook.params)) { cmd <- paste0(cmd, " ", gitbook.params)} + ### system(cmd) + + system2(command="gitbook", args=ro_args); + + # Post-process hack to fix broken img urls. + # https://github.com/GitbookIO/gitbook/issues/99 + # Will also fix links to the Introduction + # https://github.com/GitbookIO/gitbook/issues/113 + #dirs <- list.dirs(out.dir, recursive=FALSE, full.names=FALSE) + #for(i in seq_along(dirs)) { + files <- list.files(out.dir, '*.html', recursive=TRUE) + for(j in seq_along(files)) { + fconn <- file(paste0(out.dir, '/', files[j])) + file <- readLines(fconn) + close(fconn) + #file <- gsub(paste0(dirs[i], '/', dirs[i], '/'), '', file) + file <- gsub('./">', './index.html">', file) + fconn <- file(paste0(out.dir, '/', files[j])) + writeLines(file, fconn) + close(fconn) + } + #} +} From 622ebacb29cb3a0ffa14150c54f5c963f6a976e5 Mon Sep 17 00:00:00 2001 From: raulossada Date: Sat, 29 Jul 2017 17:22:23 -0300 Subject: [PATCH 7/7] Removing test version --- R/buildGitbook2.R | 63 ----------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 R/buildGitbook2.R diff --git a/R/buildGitbook2.R b/R/buildGitbook2.R deleted file mode 100644 index c2d743e..0000000 --- a/R/buildGitbook2.R +++ /dev/null @@ -1,63 +0,0 @@ -#' This will build a gitbook from the source markdown files. -#' -#' This function is simply a wrapper to a system call to \code{gitbook}. -#' -#' \url{https://github.com/GitbookIO/gitbook} -#' -#' @param source.dir location containing the source files. -#' @param out.dir location of the built book. -#' @param format the format of book. Options are pdf or ebook. If omitted, -#' this will build a website. -#' @param buildRmd should \code{\link{buildRmd}} be called first. -#' @param gitbook.params other parameters passed to the gitbook command. -#' @param ... other parameters passed to \code{\link{buildRmd}}. -#' @export -buildGitbook2 <- function(source.dir=getwd(), - out.dir=paste0(getwd(), '/_book'), - buildRmd = TRUE, - format, - gitbook.params, ...) { - - if(buildRmd) { - message('Building R markdown files...') - buildRmd(source.dir, ...) - message('R Markdown files successfully built!') - } - - checkForGitbook(quiet=TRUE) - - buildCmd <- 'build' - if(!missing(format)) { buildCmd <- format } - ### cmd <- paste0("gitbook ", buildCmd, " ", source.dir, " --output=", out.dir) - - ro_args <- paste0(buildCmd, " --gitbook=2.6.4 ", source.dir, " --output=", out.dir); - - - #if(!missing(title)) { cmd <- paste0(cmd, ' --title="', title, '"') } - #if(!missing(intro)) { cmd <- paste0(cmd, ' --intro="', intro, '"') } - #if(!missing(github)) { cmd <- paste0(cmd, ' --github=', github) } - #if(mathjax) { cmd <- paste0(cmd, " --plugins plugin-mathjax") } - if(!missing(gitbook.params)) { cmd <- paste0(cmd, " ", gitbook.params)} - ### system(cmd) - - system2(command="gitbook", args=ro_args); - - # Post-process hack to fix broken img urls. - # https://github.com/GitbookIO/gitbook/issues/99 - # Will also fix links to the Introduction - # https://github.com/GitbookIO/gitbook/issues/113 - #dirs <- list.dirs(out.dir, recursive=FALSE, full.names=FALSE) - #for(i in seq_along(dirs)) { - files <- list.files(out.dir, '*.html', recursive=TRUE) - for(j in seq_along(files)) { - fconn <- file(paste0(out.dir, '/', files[j])) - file <- readLines(fconn) - close(fconn) - #file <- gsub(paste0(dirs[i], '/', dirs[i], '/'), '', file) - file <- gsub('./">', './index.html">', file) - fconn <- file(paste0(out.dir, '/', files[j])) - writeLines(file, fconn) - close(fconn) - } - #} -}