R/utils.R

Defines functions run_servr to_json merge_list check_css list_css lua_filters pkg_resource

pkg_resource = function(...) {
  system.file('resources', ..., package = 'pagedown', mustWork = TRUE)
}

lua_filters = function(...) {
  c(rbind("--lua-filter", pkg_resource('lua', c(...))))
}

list_css = function() {
  css = list.files(pkg_resource('css'), '[.]css$', full.names = TRUE)
  setNames(css, gsub('.css$', '', basename(css)))
}

check_css = function(css) {
  valid = names(list_css())
  if (length(invalid <- setdiff(css, valid)) == 0) return()
  invalid = invalid[1]
  maybe = sort(agrep(invalid, valid, value = TRUE))[1]
  hint = if (is.na(maybe)) '' else paste0('; did you mean "', maybe, '"?')
  stop(
    '"', invalid, '" is not a valid built-in CSS filename', if (hint != "") hint else ".",
    " Use `pagedown:::list_css()` to view all built-in CSS filenames.", call. = FALSE
  )
}

merge_list = function(x, y) {
  x[names(y)] = y
  x
}

to_json = function(x, ..., auto_unbox = TRUE, null = 'null') {
  jsonlite::toJSON(x, ..., auto_unbox = auto_unbox, null = null)
}

`%n%` = knitr:::`%n%`

run_servr = function() {
  # see https://github.com/rstudio/httpuv/issues/250
  later::with_loop(later::global_loop(), httpuv::service(NA))
}

Try the pagedown package in your browser

Any scripts or data that you put into this service are public.

pagedown documentation built on Dec. 28, 2022, 1:16 a.m.