Skip to content

Commit

Permalink
close #29: make it possible to refer to files in other directories
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Jan 25, 2017
1 parent 205484f commit 50d6bb7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- A class `title-slide` was added to the automatically generated title slide (`moon_reader(seal = TRUE)`) so that you can customize the this slide using CSS (thanks, @ekstroem, #7).

- Added an argument `cast_from` to `infinite_moon_reader()` to specify the root directory of the server. Previously the root directory is the directory of the Rmd input file, which makes it impossible for the Rmd document to use resources in upper-level directories (e.g. `![](../gif/cute-kittens.gif)`). Now you can set the working directory to the upper-level directory and call `inf_mr('relative/path/to/input.Rmd')`, so that `input.Rmd` can use any files under the current working directory `./` (thanks, @pat-s, #29).

## BUG FIXES

- A local copy of MathJax should work with `moon_reader()` (thanks, @bnicenboim, #13).
Expand Down
24 changes: 19 additions & 5 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,20 @@ tsukuyomi = function(...) moon_reader(...)
#' \code{inf_mr()} is an alias of \code{infinite_moon_reader()}.
#'
#' The Rmd document is compiled continuously to trap the world in the Infinite
#' Tsukuyomi.
#' Tsukuyomi. The genjutsu is cast from the directory specified by
#' \code{cast_from}, and the Rinne Sharingan will be reflected off of the
#' \code{moon}.
#' @param moon The input Rmd file path (if missing and in RStudio, the current
#' active document is used).
#' @param cast_from The root directory of the server.
#' @references \url{http://naruto.wikia.com/wiki/Infinite_Tsukuyomi}
#' @note This function is not really tied to the output format
#' \code{\link{moon_reader}()}. You can use it to serve any single-HTML-file R
#' Markdown output.
#' @seealso \code{servr::\link{httw}}
#' @export
#' @rdname inf_mr
infinite_moon_reader = function(moon) {
infinite_moon_reader = function(moon, cast_from = '.') {
# when this function is called via the RStudio addin, use the dir of the
# current active document
if (missing(moon) && requireNamespace('rstudioapi', quietly = TRUE)) {
Expand All @@ -183,14 +186,25 @@ infinite_moon_reader = function(moon) {
basename(moon), '".'
)
}
moon = normalizePath(moon, mustWork = TRUE)
moon = normalize_path(moon)
rebuild = function(...) {
if (moon %in% normalizePath(c(...))) rmarkdown::render(
moon, envir = globalenv(), encoding = 'UTF-8'
)
}
html = rebuild(moon) # render slides initially
servr::httw(dirname(moon), initpath = basename(html), handler = rebuild)
html = normalize_path(rebuild(moon)) # render slides initially
d = normalize_path(cast_from)
f = rmarkdown::relative_to(d, html)
# see if the html output file is under the dir cast_from
if (f == html) {
d = dirname(html)
f = basename(html)
warning(
"Cannot use '", cast_from, "' as the root directory of the server because ",
"the HTML output is not under this directory. Using '", d, "' instead."
)
}
servr::httw(d, initpath = f, handler = rebuild)
}

#' @export
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ example_css = function() htmltools::htmlDependency(
'remark-css', '0.0.1', pkg_resource(), stylesheet = 'example.css', all_files = FALSE
)

normalize_path = function(path) {
normalizePath(path, winslash = '/', mustWork = TRUE)
}

# a simple JSON serializer
tojson = function(x) {
if (is.null(x)) return('null')
Expand Down

0 comments on commit 50d6bb7

Please sign in to comment.