Skip to content

Commit

Permalink
add add_pkg_deps fn for #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Apr 6, 2022
1 parent 0e85261 commit 3b7a256
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: r2readthedocs
Title: Convert R Package Documentation to a 'readthedocs' Website
Version: 0.0.1.005
Version: 0.0.1.006
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"))
Description: Convert R package documentation to a 'readthedocs' website.
Expand Down
123 changes: 123 additions & 0 deletions R/dependency-docs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

#' Add documentation of all dependency packages
#' @noRd
add_pkg_deps <- function (path = here::here ()) {

desc <- data.frame (read.dcf (file.path (path, "DESCRIPTION")))
imps <- gsub ("\\n", "", strsplit (desc$Imports, ",") [[1]])

dep_dir <- normalizePath (file.path (path, "docs", "dependencies"),
mustWork = FALSE)
if (!dir.exists (dep_dir)) {
dir.create (dep_dir)
}
fout <- tempfile (fileext = ".md")
for (i in imps) {
pkg_dir <- file.path (dep_dir, i)
if (!dir.exists (pkg_dir)) {
dir.create (pkg_dir)
}
rd <- tools::Rd_db (i)
nms <- names (rd)

# make index for that pkg:
rd_titles <- tools::file_path_sans_ext (nms)
myst_targets <-
pkg_index <- c (
paste0 ("# ", i),
"",
paste0 ("- [",
rd_titles,
"](",
paste0 (i, "_", nms),
")")
)

brio::write_lines (pkg_index,
file.path (dep_dir, paste0 (i, ".md")))

add_dep_fns (path, i, rd)
}

index <- add_deps_to_index_rst (path, imps)
brio::write_lines (index,
file.path (path, "docs", "index.rst"))
}

add_deps_to_index_rst <- function (path, dep_pkgs) {

f <- file.path (path, "docs", "index.rst")
if (!file.exists (f)) {
stop ("file [", f, "] does not exist")
}

index <- brio::read_lines (f)
deps <- grep ("^\\s+dependencies\\/", index)

if (length (deps) > 0L) {

toc <- grep ("^\\.\\.\\stoctree\\:\\:$", index)
toc <- toc [max (which (toc < min (deps)))]
deps_index <- seq (toc, max (deps))
index <- index [-deps_index]
}

# remove any text at the end of toctree items, to be replaced later:
toc <- grep ("^\\.\\.\\stoctree\\:\\:$", index)
txtlines <- grep ("^\\w", index)
txtlines <- txtlines [which (txtlines > max (toc))]
end_txt <- NULL

if (length (txtlines) > 0L) {
index_end <- seq (min (txtlines), length (index))
end_txt <- index [index_end]
index <- index [-index_end]
}

index <- c (index,
"",
".. toctree::",
" :maxdepth: 1",
" :caption: Dependencies",
"")
for (d in dep_pkgs) {
index <- c (index,
paste0 (" dependencies/", d, ".md"))
}

c (index,
"",
end_txt)
}


#' Add functions for one dependency package
#' @noRd
add_dep_fns <- function (path, pkg, rd) {

fout <- tempfile (fileext = ".md")
dep_dir <- normalizePath (file.path (path, "docs", "dependencies"),
mustWork = FALSE)
pkg_dir <- file.path (dep_dir, pkg)

for (n in names (rd)) {

out <- Rd2md::Rd2markdown (rd [[n]], outfile = fout)
md <- brio::read_lines (fout)
md <- gsub ("^\\#\\#\\#\\s", "#### ", md)
md <- gsub ("^\\#\\#\\s", "### ", md)
md <- gsub ("^\\#\\s", "## ", md)

# insert MyST link target:
md <- c (paste0 ("(", pkg, "_", n, ")="),
"",
md)

mdout <- file.path (pkg_dir,
paste0 (tools::file_path_sans_ext (n),
".md"))
brio::write_lines (md, mdout)
}

file.remove (fout)
}
8 changes: 7 additions & 1 deletion R/readthedocs.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' Convert package documentation to `readthedocs` format
#'
#' @param path Path to local R package with documentation to be converted
#' @param dev If `TRUE`, include function documentation of all dependency
#' packages in site.
#' @param open If `TRUE`, open the documentation site in default browser.
#' @return TRUE (invisibly) if documentation successfully converted.
#' @export
r2readthedocs <- function (path = here::here (), open = TRUE) {
r2readthedocs <- function (path = here::here (), dev = FALSE, open = TRUE) {

path <- convert_path (path)

Expand Down Expand Up @@ -34,6 +36,10 @@ r2readthedocs <- function (path = here::here (), open = TRUE) {
if (!dir.exists (static_dir))
dir.create (static_dir)

if (dev) {
add_pkg_deps (path)
}

rtd_clean (path)
rtd_build (path)

Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/ropenscilabs/r2readthedocs",
"issueTracker": "https://github.com/ropenscilabs/r2readthedocs/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.0.1.005",
"version": "0.0.1.006",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
5 changes: 4 additions & 1 deletion man/r2readthedocs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b7a256

Please sign in to comment.