Skip to content

Commit

Permalink
Generatte separate pages for readme and reference
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 29, 2016
1 parent 70a666c commit c53e6fe
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 206 deletions.
77 changes: 16 additions & 61 deletions R/build-index.r
Original file line number Diff line number Diff line change
@@ -1,73 +1,28 @@
# Must be called after all other build functions.
build_index <- function(pkg) {
out <- file.path(pkg$site_path, "index.html")
message("Generating index.html")

index <- pkg$index
topic_index <- pkg$topics[pkg$topics$in_index, , drop = FALSE]
pkg$topic_index <- rows_list(topic_index)
pkg$readme <- readme(pkg)
pkg$pagetitle <- "Home"

# Cross-reference complete list of topics vs. topics found in index page
topics <- unlist(lapply(index, "[[", "topics"))
missing <- !(topics %in% topic_index$name)
if (any(missing)) {
warning("Can't find index topics: ", paste(topics[missing],
collapse = ", "), call. = FALSE)
topics <- topics[!missing]
}

other <- !(topic_index$name %in% topics)
if (any(other)) {
title <- if(length(topics)) 'Other' else ''
index <-
c(index, list(sd_section(title, NULL, sort(topic_index$name[other]))))
}

# Render each section
sections <- lapply(index, build_section, pkg = pkg)
pkg$sections <- sections
pkg$rd <- NULL

render_icons(pkg)
pkg$pagetitle <- "Index"
render_page(pkg, "index", pkg, out)
render_page(pkg, "readme", pkg, out)
}

build_section <- function(section, pkg) {
find_info <- function(item) {
match <- pkg$topics$name == item$name
if (!any(match)) return(NULL)

row <- pkg$topics[match, , drop = FALSE]
item$file_out <- row$file_out

aliases <- setdiff(row$alias[[1]], row$name)
if (length(aliases) > 0) {
item$aliases <- str_c("(", str_c(aliases, collapse = ", "), ")")
}
readme <- function(pkg = ".") {
pkg <- as.sd_package(pkg)

if (is.null(item$title)) {
rd <- pkg$rd[[row$file_in]]
item$title <- extract_title(rd, pkg)
}

item$icon <- icon_path(pkg, item$name)
item
# First look in staticdocs path
path <- file.path(pkg$sd_path, "README.md")
if (file.exists(path)) {
return(markdown(path = path))
}

desc <- section$description

list(
title = section$name %||% "Missing section title",
description = markdown(desc),
items = compact(lapply(section$elements, find_info))
)
}
# Then look in the package root
path <- file.path(pkg$path, "README.md")
if (file.exists(path)) {
return(markdown(path = path))
}

extract_title <- function(x, pkg) {
title <- Find(function(x) attr(x, "Rd_tag") == "\\title", x)
to_html(title, pkg = pkg)
# Otherwise fallback to description
pkg$description
}

compact <- function (x) Filter(function(x) !is.null(x) & length(x), x)

73 changes: 73 additions & 0 deletions R/build-reference.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Must be called after all other build functions.
build_reference <- function(pkg) {
out <- file.path(pkg$site_path, "reference.html")
message("Generating reference.html")

index <- pkg$index
topic_index <- pkg$topics[pkg$topics$in_index, , drop = FALSE]
pkg$topic_index <- rows_list(topic_index)

# Cross-reference complete list of topics vs. topics found in index page
topics <- unlist(lapply(index, "[[", "topics"))
missing <- !(topics %in% topic_index$name)
if (any(missing)) {
warning("Can't find index topics: ", paste(topics[missing],
collapse = ", "), call. = FALSE)
topics <- topics[!missing]
}

other <- !(topic_index$name %in% topics)
if (any(other)) {
title <- if(length(topics)) 'Other' else ''
index <-
c(index, list(sd_section(title, NULL, sort(topic_index$name[other]))))
}

# Render each section
sections <- lapply(index, build_section, pkg = pkg)
pkg$sections <- sections
pkg$rd <- NULL

render_icons(pkg)
pkg$pagetitle <- "Function reference"
render_page(pkg, "index", pkg, out)
}

build_section <- function(section, pkg) {
find_info <- function(item) {
match <- pkg$topics$name == item$name
if (!any(match)) return(NULL)

row <- pkg$topics[match, , drop = FALSE]
item$file_out <- row$file_out

aliases <- setdiff(row$alias[[1]], row$name)
if (length(aliases) > 0) {
item$aliases <- str_c("(", str_c(aliases, collapse = ", "), ")")
}

if (is.null(item$title)) {
rd <- pkg$rd[[row$file_in]]
item$title <- extract_title(rd, pkg)
}

item$icon <- icon_path(pkg, item$name)
item
}

desc <- section$description

list(
title = section$name %||% "Missing section title",
description = markdown(desc),
items = compact(lapply(section$elements, find_info))
)
}

extract_title <- function(x, pkg) {
title <- Find(function(x) attr(x, "Rd_tag") == "\\title", x)
to_html(title, pkg = pkg)
}

compact <- function (x) Filter(function(x) !is.null(x) & length(x), x)

32 changes: 10 additions & 22 deletions R/build.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#' Currently, \code{build_site} builds documentation for help topics,
#' vignettes, demos, and a \code{README.md}, if present.
#'
#' @section Home page:
#'
#' The home page is generated from \code{inst/staticdocs/README.md},
#' \code{README.md}, or the \code{DESCRIPTION} if neither readme file is
#' present. On the homepage, you should show how to install the package,
#' describe what it does, and provide an example of how to use it.
#'
#' @param pkg path to source version of package. See
#' \code{\link[devtools]{as.package}} for details on how paths and package
#' names are resolved.
Expand All @@ -17,7 +24,6 @@
#' @param mathjax Use mathjax to render math symbols?
#' @param with_vignettes If \code{TRUE}, will build vignettes.
#' @param with_demos If \code{TRUE}, will build demos.
#' @param with_readme If \code{TRUE}, will build the README.
#' @param launch If \code{TRUE}, will open freshly generated site in web
#' browser.
#' @export
Expand All @@ -37,7 +43,6 @@ build_site <- function(pkg = ".",
mathjax = TRUE,
with_vignettes = TRUE,
with_demos = TRUE,
with_readme = TRUE,
launch = interactive()
) {
pkg <- as.sd_package(
Expand All @@ -56,11 +61,13 @@ build_site <- function(pkg = ".",
}
copy_bootstrap(pkg)


pkg$topics <- build_topics(pkg)
if (with_vignettes) pkg$vignettes <- build_vignettes(pkg)
if (with_demos) pkg$demos <- build_demos(pkg)
if (with_readme) pkg$readme <- readme(pkg)

build_index(pkg)
build_reference(pkg)

if (launch) launch(pkg)
invisible(TRUE)
Expand Down Expand Up @@ -115,25 +122,6 @@ build_topics <- function(pkg = ".") {
index
}

readme <- function(pkg = ".") {
pkg <- as.sd_package(pkg)

# First look in staticdocs path
path <- file.path(pkg$sd_path, "README.md")
if (file.exists(path)) {
return(markdown(path = path))
}

# Then look in the package root
path <- file.path(pkg$path, "README.md")
if (file.exists(path)) {
return(markdown(path = path))
}

# Otherwise fallback to description
pkg$description
}

copy_bootstrap <- function(pkg = ".") {
pkg <- as.sd_package(pkg)
user_bootstrap <- pkg$bootstrap_path
Expand Down
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![Travis-CI Build Status](https://travis-ci.org/hadley/staticdocs.svg?branch=master)](https://travis-ci.org/hadley/staticdocs)

staticdocs provides a way to conveniently render R package documentation into html pages suitable for stand-alone viewing, such as on a package webpage. You can see staticdocs in action at <http://hadley.github.io/staticdocs/>: this is the output of staticdocs applied to the latest version of staticdocs.
staticdocs is designed to make it quick and easy to build a website for your package. You can see staticdocs in action at <http://hadley.github.io/staticdocs/>: this is the output of staticdocs applied to the latest version of staticdocs.

## Installation

staticdocs is not currently available from CRAN, but you can install the development version from github with:

Expand All @@ -11,22 +13,14 @@ staticdocs is not currently available from CRAN, but you can install the develop
devtools::install_github("hadley/staticdocs")
```

# Features

* Attractive defaults: staticdocs uses [bootstrap]
(https://getbootstrap.com/2.0.4/) to provide an attractive website.

* Customisable: you can override the default templates to provide
alternative rendering
## Usage

* Flexible ways to specify the index page so you can group related
functions together.
Run staticdocs from the package directory each time you release your package:

Compared to `Rd2html`, staticdocs:

* Makes it easier to customise the output.
```R
staticdocs::build_site()
```

* Runs examples, so users see both input and output.
This will generate a `docs/` directory. The home page will be generated from your package's `README.md`, and a function reference will be generated from the documentation in the `man/` direcftory.

* Assumes only one package is being rendered - links to documentation in
other packages are forwarded to [Rdocumentation](http://www.rdocumentation.org/).
If you are using GitHub, the easiest way to make this your package website is to check into git, then go settings for your repo and make sure that the __GitHub pages__ source is set to "master branch /docs folder".
20 changes: 15 additions & 5 deletions docs/build_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<a class="brand" href="index.html">staticdocs 0.1.0.9000</a>
<div class="nav">
<ul class="nav">
<li><a href="index.html"><i class="icon-home icon-white"></i> Index</a></li>
<li><a href="index.html">Home</a></li>
<li><a href="reference.html">Reference</a></li>
</ul>
</div>
</div>
Expand All @@ -56,8 +57,7 @@ <h2>Usage</h2>
<pre><span class="functioncall"><a href='build_site.html'>build_site</a></span><span class="keyword">(</span><span class="argument">pkg</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="string">"."</span><span class="keyword">,</span> <span class="argument">site_path</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="string">"docs"</span><span class="keyword">,</span> <span class="argument">examples</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span>
<span class="argument">run_dont_run</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">FALSE</span><span class="keyword">,</span> <span class="argument">templates_path</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="string">"inst/staticdocs/templates"</span><span class="keyword">,</span>
<span class="argument">bootstrap_path</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="string">"inst/staticdocs/bootstrap"</span><span class="keyword">,</span> <span class="argument">mathjax</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span>
<span class="argument">with_vignettes</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span> <span class="argument">with_demos</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span> <span class="argument">with_readme</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span>
<span class="argument">launch</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="functioncall"><a href='http://www.rdocumentation.org/packages/base/topics/interactive'>interactive</a></span><span class="keyword">(</span><span class="keyword">)</span><span class="keyword">)</span></pre>
<span class="argument">with_vignettes</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span> <span class="argument">with_demos</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="number">TRUE</span><span class="keyword">,</span> <span class="argument">launch</span>&nbsp;<span class="argument">=</span>&nbsp;<span class="functioncall"><a href='http://www.rdocumentation.org/packages/base/topics/interactive'>interactive</a></span><span class="keyword">(</span><span class="keyword">)</span><span class="keyword">)</span></pre>

<h2>Arguments</h2>
<dl>
Expand All @@ -84,8 +84,6 @@ <h2>Arguments</h2>
<dd>If <code>TRUE</code>, will build vignettes.</dd>
<dt>with_demos</dt>
<dd>If <code>TRUE</code>, will build demos.</dd>
<dt>with_readme</dt>
<dd>If <code>TRUE</code>, will build the README.</dd>
<dt>launch</dt>
<dd>If <code>TRUE</code>, will open freshly generated site in web
browser.</dd>
Expand All @@ -98,6 +96,18 @@ <h2>Description</h2>
vignettes, demos, and a <code>README.md</code>, if present.</p>

</div>

<div class="Home page">
<h2>Home page</h2>

<p></p>

<p>The home page is generated from <code>inst/staticdocs/README.md</code>,
<code>README.md</code>, or the <code>DESCRIPTION</code> if neither readme file is
present. On the homepage, you should show how to install the package,
describe what it does, and provide a brief example of use.</p>

</div>

<h2 id="examples">Examples</h2>
<pre class="examples"><div class='input'><span class="comment">## Not run: ------------------------------------</span>
Expand Down
3 changes: 2 additions & 1 deletion docs/demo-plot.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<a class="brand" href="index.html">staticdocs 0.1.0.9000</a>
<div class="nav">
<ul class="nav">
<li><a href="index.html"><i class="icon-home icon-white"></i> Index</a></li>
<li><a href="index.html">Home</a></li>
<li><a href="reference.html">Reference</a></li>
</ul>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion docs/demo-stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<a class="brand" href="index.html">staticdocs 0.1.0.9000</a>
<div class="nav">
<ul class="nav">
<li><a href="index.html"><i class="icon-home icon-white"></i> Index</a></li>
<li><a href="index.html">Home</a></li>
<li><a href="reference.html">Reference</a></li>
</ul>
</div>
</div>
Expand Down
Loading

0 comments on commit c53e6fe

Please sign in to comment.