11# ' Print Package Help Documentation
22# '
3- # ' Print the HTML code associated with the help documentation of one or more add-on packages.
3+ # ' Print the HTML code associated with the help documentation of one or more \R packages.
44# '
55# ' @param pkg 'character' vector.
66# ' Package name(s)
77# ' @param file 'connection' or 'character' string.
88# ' Names the file to append output to.
99# ' Prints to the standard output connection by default.
1010# ' @param internal 'logical' flag.
11- # ' Whether to print help topics flagged with the keyword " internal" .
11+ # ' Whether to print help topics flagged with the keyword \dQuote{ internal} .
1212# ' @param toc 'logical' flag.
1313# ' Whether to format level-2 headers (help-topic titles) using a Markdown syntax.
1414# ' This is required when specifying the table-of-contents (toc) format option in R Markdown,
1515# ' see \code{\link[rmarkdown:render]{rmarkdown::render}} function for details.
16- # ' @param replace_title 'logical' flag.
16+ # ' @param title_to_name 'logical' flag.
1717# ' Whether to replace the help-topic \dQuote{title} with its \dQuote{name}.
1818# ' @param sep 'character' string.
1919# ' HTML to separate help topics, a horizontal line by default.
20- # ' @param links 'character' vector (experimental).
21- # ' Names of packages searched when creating internal hyperlinks to help topics.
20+ # ' @param notrun 'logical' flag.
21+ # ' Whether to inlcude \code{## Not run} comments in the Examples section of help documentation.
22+ # ' @param links 'character' vector.
23+ # ' Names of packages searched (level 0) when creating internal hyperlinks to functions and datasets.
2224# ' @param ...
2325# ' Not used
2426# '
4143# ' "---",
4244# ' sep = "\n", file = "help-example.Rmd")
4345# ' PrintPackageHelp("inlmisc", file = "help-example.Rmd",
44- # ' toc = TRUE, replace_title = TRUE)
46+ # ' toc = TRUE, title_to_name = TRUE,
47+ # ' notrun = FALSE)
4548# ' rmarkdown::render("help-example.Rmd")
4649# ' url <- file.path("file:/", getwd(), "help-example.html")
4750# ' utils::browseURL(url)
5053# ' }
5154# '
5255
53- PrintPackageHelp <- function (pkg , file = " " , internal = FALSE ,
54- toc = FALSE , replace_title = FALSE ,
55- sep = " <hr> " , links = NULL , ... ) {
56+ PrintPackageHelp <- function (pkg , file = " " , internal = FALSE , toc = FALSE ,
57+ title_to_name = FALSE , notrun = TRUE , sep = " <hr> " ,
58+ links = pkg , ... ) {
5659
5760 checkmate :: assertCharacter(pkg , unique = TRUE )
5861 checkmate :: assertFlag(internal )
5962 checkmate :: assertFlag(toc )
60- checkmate :: assertFlag(replace_title )
63+ checkmate :: assertFlag(title_to_name )
64+ checkmate :: assertFlag(notrun )
6165 checkmate :: assertString(sep , null.ok = TRUE )
6266 checkmate :: assertCharacter(links , unique = TRUE , null.ok = TRUE )
6367
@@ -88,42 +92,65 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
8892 # identify links
8993 if (! is.null(links )) {
9094 d <- .GetHelpMeta(links )
91- links <- paste0(" #" , d $ name )
95+ links <- paste0(" #" , tolower(gsub( " " , " - " , d $ name )) )
9296 names(links ) <- d $ name
9397 }
9498
9599 # loop through help items
96100 for (i in seq_along(rd )) {
97101
98102 # convert rd to html
99- htm <- utils :: capture.output(tools :: Rd2HTML(rd [[i ]],
100- no_links = is.null(links ),
101- Links = links ,
102- Links2 = links ))
103+ htm <- utils :: capture.output({
104+ suppressWarnings(tools :: Rd2HTML(rd [[i ]],
105+ no_links = is.null(links ),
106+ Links = links ,
107+ Links2 = links ))
108+ })
103109
104- # update level-2 header (title of help documentation)
105- idx <- pmatch(" <h2>" , htm )
110+ # remove broken internal links
111+ pattern <- " <a href=\\\"\\ .\\ ./\\ .\\ ./.*\\\" >.*</a>"
112+ for (j in grep(pattern , htm )) {
113+ repeat {
114+ st <- htm [j ]
115+ re <- regexpr(pattern , st )
116+ if (re == - 1 ) break
117+ st <- substr(st , re , re + attr(re , " match" ) - 1L )
118+ st <- sub(" <a.*\" >" , " " , sub(" </a>" , " " , st ))
119+ htm [j ] <- sub(pattern , st , htm [j ])
120+ }
121+ }
122+
123+ # substitute level-2 header (title of help documentation)
124+ idx <- grep(" <h2>" , htm )
106125 ti <- gsub(" <.*?>" , " " , htm [idx ])
107126 nm <- names(rd )[i ]
127+ id <- tolower(gsub(" " , " -" , nm ))
108128 if (toc ) {
109- if (replace_title )
110- txt <- sprintf(" \n ## %s\n\n *%s*" , nm , ti )
129+ if (title_to_name )
130+ txt <- sprintf(" \n ## %s {#%s} \n\n *%s*" , nm , id , ti )
111131 else
112- txt <- sprintf(" \n ## %s {#%s}" , ti , nm )
132+ txt <- sprintf(" \n ## %s {#%s}" , ti , id )
113133 cat(txt , file = file , sep = " \n\n " , append = TRUE )
114- } else if (replace_title ) {
115- htm [idx ] <- sprintf(" <h2>%s</h2>\n\n <em>%s</em>\n " , nm , ti )
134+ } else if (title_to_name ) {
135+ htm [idx ] <- sprintf(" <h2 id=\" %s\" >%s</h2>\n\n <em>%s</em>\n " ,
136+ id , nm , ti )
116137 }
117138
118139 # remove extraneous lines at beginning and end of help page
119140 htm <- htm [- c(seq_len(idx - ! toc ), length(htm ))]
120141
142+ # remove 'not run' commented lines
143+ if (! notrun ) {
144+ idx <- which(htm %in% c(" ## Not run: " , " ## End(Not run)" ))
145+ if (length(idx ) > 0 ) htm <- htm [- idx ]
146+ }
147+
121148 # edit code-chunk tags to use syntax highlighting
122149 htm_trim <- trimws(htm )
123150 htm [htm_trim == " </pre>" ] <- " </code></pre>"
124151 idx <- which(htm_trim == " <pre>" )
125- htm [ idx + 1L ] <- sprintf( " <pre class=\" lang-r\" ><code class=\" lang-r\" >%s" ,
126- htm [idx + 1L ])
152+ fmt <- " <pre class=\" lang-r\" ><code class=\" lang-r\" >%s"
153+ htm [ idx + 1L ] <- sprintf( fmt , htm [idx + 1L ])
127154 htm [idx ] <- " "
128155
129156 # remove empty lines everywhere but in examples section
@@ -149,11 +176,13 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
149176 src <- sub(" .." , system.file(package = meta $ package [i ]), src )
150177 for (f in src ) checkmate :: assertFileExists(f , access = " r" )
151178 uri <- vapply(src , function (f ) knitr :: image_uri(f ), " " )
152- htm [is ] <- sprintf(" <p><img src=\" %s\" alt=\" %s\" />" , uri , basename(src ))
179+ fmt <- " <p><img src=\" %s\" alt=\" %s\" />"
180+ htm [is ] <- sprintf(fmt , uri , basename(src ))
153181 }
154182
155183 # add seperator
156- if (! is.null(sep ) && i < nrow(meta )) htm <- c(htm , sprintf(" \n %s\n " , sep ))
184+ if (! is.null(sep ) && i < nrow(meta ))
185+ htm <- c(htm , sprintf(" \n %s\n " , sep ))
157186
158187 # preserve html
159188 htm <- htmltools :: htmlPreserve(htm )
@@ -194,9 +223,10 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
194223 stop(gettextf(" invalid %s argument" , sQuote(" file" )), domain = NA )
195224 pkgname <- basename(dirpath )
196225 rddb <- file.path(path , pkgname )
197- if (! file.exists(paste0(rddb , " .rdx" )))
198- stop(gettextf(" package %s exists but was not installed under R >= 2.10.0 so help cannot be accessed" ,
199- sQuote(pkgname )), domain = NA )
226+ if (! file.exists(paste0(rddb , " .rdx" ))) {
227+ fmt <- " package %s exists but was not installed under R >= 2.10.0 so help cannot be accessed"
228+ stop(gettextf(fmt , sQuote(pkgname )), domain = NA )
229+ }
200230 .FetchRdDB(rddb , basename(file ))
201231}
202232
0 commit comments