forked from quarto-dev/quarto-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.R
86 lines (83 loc) · 2.52 KB
/
preview.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#' Quarto Preview
#'
#' Render and preview a Quarto document or website project.
#'
#' Automatically reloads the browser when input files are re-rendered or
#' document resources (e.g. CSS) change.
#'
#' @param file The document or website project directory to preview (defaults to
#' current working directory)
#' @param render For website preview, the most recent execution results of
#' computational documents are used to render the site (this is to optimize
#' startup time). If you want to perform a full render prior to serving pass
#' "all" or a vector of specific formats to render. Pass "default" to render
#' the default format for the site. For document preview, the document is
#' rendered prior to preview (pass `FALSE` to override this).
#' @param port Port to listen on (defaults to 4848)
#' @param host Hostname to bind to (defaults to 127.0.0.1)
#' @param browse Open a browser to preview the content. Defaults to using the
#' RStudio Viewer when running within RStudio.Pass a function (e.g.
#' `utils::browseURL` to override this behavior).
#' @param watch Watch for changes and automatically reload browser.
#' @param navigate Automatically navigate the preview browser to the most
#' recently rendered document.
#'
#' @importFrom processx process
#' @importFrom rstudioapi isAvailable
#' @importFrom rstudioapi viewer
#' @importFrom utils browseURL
#' @importFrom later later
#'
#' @examples
#' \dontrun{
#' # Preview the project in the current directory
#' quarto_preview()
#'
#' # Preview a document
#' quarto_preview("document.qmd")
#'
#' # Preview the project in "myproj" directory and use external browser
#' # (rather than RStudio Viewer)
#' quarto_preview("myproj", open = utils::browseURL)
#'
#' # Stop any running quarto preview
#' quarto_preview_stop()
#' }
#'
#' @export
quarto_preview <- function(file = NULL,
render = "auto",
port = "auto",
host = "127.0.0.1",
browse = TRUE,
watch = TRUE,
navigate = TRUE) {
# default for file
if (is.null(file)) {
file <- getwd()
}
# handle extra_args
args <- c()
if (isFALSE(watch)) {
args <- c(args, "--no-watch")
}
if (isFALSE(navigate)) {
args <- c("--no-navigate")
}
# serve
run_serve_daemon(
"preview",
file,
NULL,
args,
render,
port,
host,
browse
)
}
#' @rdname quarto_preview
#' @export
quarto_preview_stop <- function() {
stop_serve_daemon("preview")
}