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

Update gitbookInfo.R and checkForGitbook.R #22

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/checkForGitbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion R/gitbookInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#' @export
gitbookInfo <- function() {
checkForGitbook(quiet=TRUE)
installed <- system('gitbook --version', 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]) );
current <- system('npm view gitbook version', intern=TRUE)
if(length(current) > 0) {
current <- current[1]
Expand Down
77 changes: 37 additions & 40 deletions R/initGitbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
8 changes: 4 additions & 4 deletions R/publishGitbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 push git@github.com:", repo, " gh-pages --force ")
"git commit -a -m '", msg, "2' \n",
"git push https://github.com/", repo, " gh-pages --force ")
system(cmd)
}