Skip to content

Commit

Permalink
Merge pull request #6 from kirillseva/add_gh_pages
Browse files Browse the repository at this point in the history
Add gh_pages param
  • Loading branch information
robertzk committed Apr 24, 2015
2 parents 4b80bf3 + 85f6680 commit cc932b6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: rocco
Type: Package
Title: rocco (http://github.com/robertzk/rocco)
Description: Literate Documentation Generator for R.
Version: 0.1.0
Version: 0.1.1
Author: Robert Krzyzanowski <technoguyrob@gmail.com>
Maintainer: Robert Krzyzanowski <technoguyrob@gmail.com>
Authors@R: c(person("Robert", "Krzyzanowski",
Expand All @@ -13,7 +13,7 @@ License: MIT
LazyData: true
Imports:
whisker,
markdown
markdown
Suggests:
knitr,
microbenchmark,
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Version 0.1.1

* Added the ability to push the generated documentation to the
[gh-pages](https://robertzk.github.io/rocco) branch of your repo.
When calling `rocco::rocco`, set `gh_pages = TRUE`.

# Version 0.1.0

* The initial creation of the package.
* The initial creation of the package.
16 changes: 10 additions & 6 deletions R/rocco.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' immediately for browsing. This will be set to \code{\link{interactive()}},
#' that is, TRUE if the R session is running interactive and FALSE
#' otherwise.
#' @param gh_pages logical. If set to true, rocco docs will be served on
#' your gh-pages branch.
#' @export
#' @return TRUE or FALSE according as the documentation process succeeds.
# Additional side effects are the creation of the documentation in the
Expand All @@ -24,25 +26,27 @@
#' # The below will simply create a static HTML site without opening it.
#' rocco("/path/to/package", output_dir = "/my/html/dir", browse = FALSE)
#' }
rocco <- function(directory, output_dir = tempdir(), browse = interactive()) {
rocco <- function(directory, output_dir = tempdir(), browse = interactive(), gh_pages = FALSE) {
if (missing(directory)) directory <- "."
stopifnot(is.character(directory), length(directory) == 1,
is.character(output_dir), length(output_dir) == 1,
isTRUE(browse) || isFALSE(browse),
is_package_directory(directory))

tryCatch({
rocco_(directory, output_dir)
if (isTRUE(gh_pages)) {
`commit_to_gh_pages!`(directory, output_dir)
}

if (browse) browseURL(file.path(output_dir, "index.html"))

TRUE
}, error = function(.) FALSE)
invisible(TRUE)
}, error = function(.) invisible(FALSE))
}

rocco_ <- function(directory, output) {
rocco_skeleton(output)
rocco_skeleton(output)

template <- readLines(file.path(output, "index.html"))
compile(directory, template, file.path(output, "index.html"))
}

30 changes: 30 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,33 @@ description_file_attribute <- function(pkg_path, attribute) {
as.character(read.dcf(file.path(pkg_path, "DESCRIPTION"))[1, attribute])
}

`commit_to_gh_pages!` <- function(directory, dir) {
## A little bit of git magic

## Now that we have the docs, we need to transfer them to the gh-pages
## branch of the repo, commit and push.
## Switch to gh-pages branch
cur_branch <- system('git rev-parse --abbrev-ref HEAD', intern = TRUE)
on.exit({
system(paste('git checkout', cur_branch))
})

st <- system2('git', 'checkout gh-pages')
if (st == 1) system2('git', 'checkout -b gh-pages')

## Copy the docs to the repo folder, and push them upstream
system(paste0('cp -rf ', dir, "/* ", directory))
in_dir(directory, {
system('git add -A .')
system('git commit -m "Updated Rocco docs."')
system('git push origin gh-pages')
})
invisible(TRUE)
}

## Don't want to import devtools because of in_dir
in_dir <- function(new, code) {
old <- setwd(new)
on.exit(setwd(old))
force(code)
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Literate Documentation for R Packages [![Build Status](https://travis-ci.org/robertzk/rocco.svg?branch=master)](https://travis-ci.org/robertzk/rocco) [![Coverage Status](https://coveralls.io/repos/robertzk/rocco/badge.svg?branch=master)](https://coveralls.io/r/robertzk/rocco)
Literate Documentation for R Packages [![Build Status](https://travis-ci.org/robertzk/rocco.svg?branch=master)](https://travis-ci.org/robertzk/rocco) [![Coverage Status](https://coveralls.io/repos/robertzk/rocco/badge.svg?branch=master)](https://coveralls.io/r/robertzk/rocco) [![Documentation](https://img.shields.io/badge/rocco--docs-%E2%9C%93-blue.svg)](http://robertzk.github.io/rocco/)
--------------------

Literate documentation for R packages in the spirit of Coffeescript's [docco](https://github.com/jashkenas/docco)
Expand All @@ -9,6 +9,9 @@ To document your package, simply run
```R
rocco::rocco("/path/to/package") # Will open browser interactively.

# This will build the docs and push them to the repos gh-pages branch
rocco::rocco("/path/to/package", gh_pages = TRUE)

# This will install the rocco docs to /path/to/pkg/inst/docs/rocco
rocco::rocco("/path/to/pkg", "/path/to/pkg/inst/docs/rocco", browse = FALSE)
```
Expand All @@ -35,4 +38,3 @@ Many thanks go to
* [Sindre Sorhus](https://github.com/sindresorhus) for the [Github Markdown style](https://github.com/sindresorhus/github-markdown-css).

* [Jeremy Ashkenas](https://github.com/jashkenas) for [the original](https://github.com/jashkenas/docco) inspiration.

6 changes: 5 additions & 1 deletion man/rocco.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
\alias{rocco-package}
\title{Literate Documentation For R.}
\usage{
rocco(directory, output_dir = tempdir(), browse = interactive())
rocco(directory, output_dir = tempdir(), browse = interactive(),
gh_pages = FALSE)
}
\arguments{
\item{directory}{character. The package directory to document.}
Expand All @@ -18,6 +19,9 @@ By default, an extemporaneously-generated temporary directory.}
immediately for browsing. This will be set to \code{\link{interactive()}},
that is, TRUE if the R session is running interactive and FALSE
otherwise.}

\item{gh_pages}{logical. If set to true, rocco docs will be served on
your gh-pages branch.}
}
\value{
TRUE or FALSE according as the documentation process succeeds.
Expand Down

0 comments on commit cc932b6

Please sign in to comment.