| 1 |
#' Knit package documentation |
| 2 |
#' |
| 3 |
#' Run examples in a package and insert output into the examples code; |
| 4 |
#' \code{knit_rd_all()} is a wrapper around \code{knit_rd()} to build static |
| 5 |
#' HTML help pages for all packages under the \file{html} directory of them. |
| 6 |
#' @param pkg Package name. |
| 7 |
#' @param links A character vector of links to be passed to |
| 8 |
#' \code{tools::\link{Rd2HTML}()}. |
| 9 |
#' @param frame Boolean: whether to put a navigation frame on the left of the |
| 10 |
#' index page. |
| 11 |
#' @return All HTML pages corresponding to topics in the package are written |
| 12 |
#' under the current working directory. An \file{index.html} is also written |
| 13 |
#' as a table of content. |
| 14 |
#' @note Ideally the html pages should be put under the \file{html} directory of |
| 15 |
#' an installed package which can be found via \code{system.file('html', |
| 16 |
#' package = 'your_package_name')}, otherwise some links may not work (e.g. |
| 17 |
#' the link to the DESCRITION file). |
| 18 |
#' @examples library(knitr) |
| 19 |
#' \dontrun{ |
| 20 |
#' |
| 21 |
#' knit_rd('maps') |
| 22 |
#' knit_rd('rpart') |
| 23 |
#' setwd(system.file('html', package = 'ggplot2')) |
| 24 |
#' knit_rd('ggplot2') # time-consuming! |
| 25 |
#' |
| 26 |
#' knit_rd_all() # this may take really long time if you have many packages installed |
| 27 |
#' } |
| 28 |
#' @export |
| 29 |
knit_rd = function(pkg, links = tools::findHTMLlinks(), frame = TRUE) { |
| 30 |
library(pkg, character.only = TRUE) |
| 31 |
optc = opts_chunk$get(); on.exit(opts_chunk$set(optc)) |
| 32 |
file.copy(system.file('misc', 'R.css', package = 'knitr'), './') |
| 33 |
pkgRdDB = getFromNamespace('fetchRdDB', 'tools')(file.path(find.package(pkg), 'help', pkg)) |
| 34 |
force(links); topics = names(pkgRdDB) |
| 35 |
for (p in topics) { |
| 36 |
message('** knitting documentation of ', p) |
| 37 |
tools::Rd2HTML(pkgRdDB[[p]], f <- tempfile(), |
| 38 |
package = pkg, Links = links, no_links = is.null(links), stages = 'render') |
| 39 |
txt = read_utf8(f) |
| 40 |
unlink(f) |
| 41 |
if (length(i <- grep('<h3>Examples</h3>', txt)) == 1L && |
| 42 |
length(grep('</pre>', txt[i:length(txt)]))) { |
| 43 |
i0 = grep('<pre>', txt); i0 = i0[i0 > i][1L] - 1L |
| 44 |
i1 = grep('</pre>', txt); i1 = i1[i1 > i0][1L] + 1L |
| 45 |
tools::Rd2ex(pkgRdDB[[p]], ef <- tempfile()) |
| 46 |
ex = read_utf8(ef) |
| 47 |
unlink(ef) |
| 48 |
ex = ex[-(1L:grep('### ** Examples', ex, fixed = TRUE))] |
| 49 |
ex = c('```{r}', ex, '```') |
| 50 |
opts_chunk$set(fig.path = paste0('figure/', p, '-'), tidy = FALSE) |
| 51 |
res = try(knit2html(text = ex, envir = parent.frame(2), template = FALSE, quiet = TRUE)) |
| 52 |
if (inherits(res, 'try-error')) { |
| 53 |
res = ex; res[1] = '<pre><code class="r">'; res[length(res)] = '</code></pre>' |
| 54 |
} |
| 55 |
txt = c(txt[1:i0], res, txt[i1:length(txt)]) |
| 56 |
txt = sub('</head>', ' |
| 57 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/github.min.css"> |
| 58 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script> |
| 59 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/languages/r.min.js"></script> |
| 60 |
<script>hljs.initHighlightingOnLoad();</script> |
| 61 |
</head>', txt) |
| 62 |
} else message('no examples found for ', p) |
| 63 |
write_utf8(txt, paste0(p, '.html')) |
| 64 |
} |
| 65 |
unlink('figure/', recursive = TRUE) |
| 66 |
toc = sprintf('- <a href="%s" target="content">%s</a>', paste0(topics, '.html'), topics) |
| 67 |
toc = c(paste0('# ', pkg), '', toc, '', |
| 68 |
paste('Generated with [knitr](https://yihui.org/knitr/) ', packageVersion('knitr'))) |
| 69 |
markdown::mark_html(text = toc, output = '00frame_toc.html', meta = list( |
| 70 |
title = paste('R Documentation of', pkg), css = 'R.css' |
| 71 |
)) |
| 72 |
txt = read_utf8(file.path(find.package(pkg), 'html', '00Index.html')) |
| 73 |
unlink('00Index.html') |
| 74 |
# fix image links |
| 75 |
write_utf8(gsub('../../../doc/html/', 'http://stat.ethz.ch/R-manual/R-devel/doc/html/', |
| 76 |
txt, fixed = TRUE), '00Index.html') |
| 77 |
if (!frame) { |
| 78 |
unlink(c('00frame_toc.html', 'index.html')) |
| 79 |
# do not need a navigation frame, so make 00Index the real homepage |
| 80 |
(if (is_windows()) file.copy else file.symlink)('00Index.html', 'index.html') |
| 81 |
return(invisible()) |
| 82 |
} |
| 83 |
write_utf8(sprintf( |
| 84 |
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> |
| 85 |
<html> |
| 86 |
<head><title>Documentation of the %s package</title></head> |
| 87 |
<frameset cols="15%%,*"> |
| 88 |
<frame src="00frame_toc.html"> |
| 89 |
<frame src="00Index.html" name="content"> |
| 90 |
</frameset> |
| 91 |
</html> |
| 92 |
', pkg), 'index.html') |
| 93 |
} |
| 94 |
|
| 95 |
#' @rdname knit_rd |
| 96 |
#' @export |
| 97 |
knit_rd_all = function() { |
| 98 |
owd = getwd(); on.exit(setwd(owd)) |
| 99 |
links = tools::findHTMLlinks() |
| 100 |
for (p in .packages(TRUE)) { |
| 101 |
message('* Making static html help pages for ', p) |
| 102 |
setwd(system.file('html', package = p)) |
| 103 |
knit_rd(p, links, frame = FALSE) |
| 104 |
} |
| 105 |
} |