# defaults.R -- internal options and code for handling pygments styles
parse_PyList <- function(x) {
tmp <- strsplit(gsub("\\[(.*)\\]", "\\1", x), ", ")[[1]]
gsub("'", "", tmp, fixed=TRUE)
}
#' List all styles (including plugins) available to Pygments
#'
#' @export
pygmentr_styles <- function() {
cmd <- paste0("python -c 'from pygments.styles import get_all_styles;",
"print list(get_all_styles())'")
# greedy is ok here; formatted as Python list
parse_PyList(pipe_in(cmd))
}
#' Is the supplied syntax a vaild syntax?
#'
#' @param style a style to validate that it's available.
check_valid_style <- function(style) {
if (!(style %in% pygments_styles()))
stop(sprintf("'%s' is not an available Pygments style.", style))
}
new_defaults <- function(opts=list()) {
opts$validators <- list(style=check_valid_style)
opts$get <- function(name) return(opts[[name]])
# simple, but there's only two options!
opts$set <- function(...) {
dots <- list(...)
if (length(dots) == 0) return()
for (name in intersect(names(dots), names(opts$validators)))
opts$validators[[name]](dots[[name]]) # validation for some options
opts[names(dots)] <<- dots
}
opts$set("formatter", "latex")
opts$set("style", "default")
return(opts)
}
#' Global options for pygmentr
#'
#' @export
pygmentr_opts <- new_defaults()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.