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

Render Rmd for non-episode files #172

Closed
fmichonneau opened this issue Sep 24, 2021 · 1 comment
Closed

Render Rmd for non-episode files #172

fmichonneau opened this issue Sep 24, 2021 · 1 comment
Milestone

Comments

@fmichonneau
Copy link
Contributor

With styles only Rmd files in the _episodes_rmd folder were knitted.
Does/Could {sandpaper} support rendering Rmd files anywhere? (e.g., in the leaner/ or instructor/ folders as suggested here)

@fmichonneau fmichonneau added this to the 2. Beta Phase milestone Sep 24, 2021
@zkamvar
Copy link
Contributor

zkamvar commented Sep 24, 2021

Yes, we do support rendering Rmd files anywhere.

The reason is because all source files are discovered through get_resource_list() and all Markdown and R Markdown sources are subsequently passed through this function that will render R Markdown sources or simply copy plain Markdown sources:

callr_build_episode_md <- function(path, hash, workenv, outpath, workdir, root, quiet) {
# Shortcut if the source is a markdown file
# Taken directly from tools::file_ext
file_ext <- function (x) {
pos <- regexpr("\\.([[:alnum:]]+)$", x)
ifelse(pos > -1L, substring(x, pos + 1L), "")
}
# Also taken directly from tools::file_path_sans_ext
file_path_sans_ext <- function (x) {
sub("([^.]+)\\.[[:alnum:]]+$", "\\1", x)
}
if (file_ext(path) == "md") {
file.copy(path, outpath, overwrite = TRUE)
return(NULL)
}
# Load required packages if it's an RMarkdown file and we know the root
# directory.
if (root != "") {
renv::load(root)
on.exit(invisible(utils::capture.output(renv::deactivate(root), type = "message")), add = TRUE)
}
# Set knitr options for output ---------------------------
ochunk <- knitr::opts_chunk$get()
oknit <- knitr::opts_knit$get()
on.exit(knitr::opts_chunk$restore(ochunk), add = TRUE)
on.exit(knitr::opts_knit$restore(oknit), add = TRUE)
slug <- file_path_sans_ext(basename(outpath))
knitr::opts_chunk$set(
comment = "",
fig.align = "center",
class.output = "output",
class.error = "error",
class.warning = "warning",
class.message = "output",
fig.path = file.path("fig", paste0(slug, "-rendered-"))
)
knitr::opts_knit$set(
# set our working directory to not pollute source
root.dir = workdir,
# Ensure HTML options like caption are respected by code chunks
rmarkdown.pandoc.to = "markdown"
)
# Set the working directory -----------------------------
wd <- getwd()
on.exit(setwd(wd), add = TRUE)
setwd(workdir)
# Generate markdown -------------------------------------
knitr::knit(
input = path,
output = outpath,
envir = workenv,
quiet = quiet,
encoding = "UTF-8"
)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants