Skip to content

Commit

Permalink
Open our own generated html files in the help pane of RStudio
Browse files Browse the repository at this point in the history
Closes #120
  • Loading branch information
lionel- committed Jun 6, 2022
1 parent f9dfa9c commit df83589
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
cli (>= 3.3.0),
crayon,
desc,
fs,
glue,
methods,
rlang (>= 1.0.2),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# pkgload (development version)

* Improved the way help pages are displayed in RStudio. This makes the
behaviour within and outside RStudio consistent and fixes issues
with Rd macros (#120).

* Added support for loading a .so or .dll file from the `inst`
folder via a new `library.dynam()` shim (@ethanplunkett, #48).

Expand Down
27 changes: 20 additions & 7 deletions R/dev-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,32 @@ load_rd_macros <- function(dir) {
print.dev_topic <- function(x, ...) {
cli::cli_inform(c("i" = "Rendering development documentation for {.val {x$topic}}"))

type <- match.arg(x$type %||% "text", c("text", "html"))
out_path <- paste(tempfile("Rtxt"), type, sep = ".")
type <- arg_match0(x$type %||% "text", c("text", "html"))
file <- fs::path_ext_set(fs::path_file(x$path), type)

# This directory structure is necessary for RStudio to open the
# .html file in the help pane (see rstudio/rustdio#11336)
doc_path <- fs::path("doc", "html", file)
path <- fs::path(tempdir(), ".R", doc_path)
fs::dir_create(fs::path_dir(path), recurse = TRUE)

if (type == "text") {
topic_write_text(x, out_path)
file.show(out_path, title = paste(x$pkg, basename(x$path), sep = ":"))
topic_write_text(x, path)
title <- paste(x$pkg, basename(x$path), sep = ":")
file.show(path, title = title)
} else if (type == "html") {
topic_write_html(x, path)

if (is_installed("rstudioapi") && rstudioapi::hasFun("previewRd")) {
rstudioapi::callFun("previewRd", x$path)
# This localhost URL is also part of getting RStudio to open in
# the help pane
port <- exec(env_get(ns_env("tools"), "httpdPort"))
url <- sprintf("http://localhost:%i/%s", port, doc_path)
} else {
topic_write_html(x, out_path)
utils::browseURL(out_path)
url <- path
}

utils::browseURL(url)
}
}

Expand Down

0 comments on commit df83589

Please sign in to comment.