Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rd_to_qmd_to_md #53

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
fs,
here,
htmltools,
quarto,
rmarkdown,
rstudioapi,
servr,
Expand Down
86 changes: 86 additions & 0 deletions R/rd_to_qmd_to_md.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
.rd_to_qmd <- function(source_file, target_dir) {

if (missing(source_file) || !file.exists(source_file)) {
stop("source_file must be a valid file path.", call. = FALSE)
}
if (missing(source_file) || !dir.exists(target_dir)) {
stop("target_dir must be a valid directory.", call. = FALSE)
}

# Rd -> html
rd = tools::parse_Rd(here::here(source_file))
tmp_html = paste0(tempfile(), ".html")
tools::Rd2HTML(rd, out = tmp_html)

# superfluous header and footer
tmp = readLines(tmp_html)
tmp = tmp[(grep("</table>$", tmp)[1] + 1):length(tmp)]
tmp = tmp[seq_len(which("</div>" == tmp) - 3)]

# first column (odd entries) of table in Arguments should not be wrapped
idx = grep("<td>", tmp)
idx = idx[seq_along(idx) %% 2 == 1]
tmp[idx] = sub("<td>", '<td style = "white-space: nowrap; font-family: monospace; vertical-align: top">', tmp[idx])

# math in Equivalence section
idx = grepl("<.code", tmp)

# examples: evaluate code blocks (assume examples are always last)
idx = which(tmp == "<h3>Examples</h3>")
if (length(idx) == 1) {
ex = tmp[(idx + 1):length(tmp)]
ex = gsub("<.*>", "", ex)
ex = gsub("&lt;", "<", ex)
ex = gsub("&gt;", ">", ex)
ex = gsub("&gt;", ">", ex)
ex = ex[!grepl("## Not run:", ex)]
ex = ex[!grepl("## End", ex)]
tmp = c(tmp[2:idx], "```{r, warning=FALSE, message=FALSE}", "library(marginaleffects)", ex, "```")
}

# cleanup equations
tmp <- gsub(
'<code class="reqn">(.*?)&gt;(.*?)</code>',
'<code class="reqn">\\1>\\2</code>',
tmp)
tmp <- gsub(
'<code class="reqn">(.*?)&lt;(.*?)</code>',
'<code class="reqn">\\1<\\2</code>',
tmp)
tmp <- gsub('<code class="reqn">(.*?)</code>', '\\$\\1\\$', tmp)

# title
funname = tools::file_path_sans_ext(basename(source_file))
if (!is.null(title)) {
tmp = tmp[!grepl("h1", tmp)]
tmp = c(paste("#", funname, "{.unnumbered}\n"), tmp)
}

# Fix title level (use ## and not <h2> so that the TOC can be generated by
# mkdocs)
tmp = gsub("<h2[^>]*>", "", tmp, perl = TRUE)
tmp = gsub("<.h2>", "", tmp)
tmp = gsub("<h3>", "## ", tmp)
tmp = gsub("</h3>", "", tmp)

# paragraph tags are unnecessary in markdown
tmp <- gsub("<p>", "", tmp, fixed = TRUE)
tmp <- gsub("</p>", "", tmp, fixed = TRUE)

# write to file
fn = file.path(target_dir, sub("Rd$", "qmd", basename(source_file)))
writeLines(tmp, con = fn)
}


.qmd_to_md <- function(source_file) {

if (missing(source_file) || !file.exists(source_file)) {
stop("source_file must be a valid file path.", call. = FALSE)
}

quarto::quarto_render(
input = path.expand(source_file),
output_format = "md"
)
}
Loading