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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
#' @importFrom htmltools htmlDependency copyDependencyToDir
#' makeDependencyRelative renderDependencies tagList tags
#' restorePreserveChunks extractPreserveChunks HTML
NULL
#' Provide common HTML dependencies for R Markdown formats
#'
#' These functions provide common HTML dependencies (e.g. jquery, bootstrap)
#' for re-use by other R Markdown formats.
#'
#' @inheritParams html_document
#' @name html-dependencies
NULL
# Create an HTML dependency for jQuery
#' @rdname html-dependencies
#' @export
html_dependency_jquery <- function() {
htmlDependency(
name = "jquery",
version = "1.11.3",
src = pkg_file("rmd/h/jquery"),
script = "jquery.min.js")
}
# Create an HTML dependency for jQuery UI
#' @rdname html-dependencies
#' @export
html_dependency_jqueryui <- function() {
htmlDependency(
name = "jqueryui",
version = "1.11.4",
src = pkg_file("rmd/h/jqueryui"),
script = "jquery-ui.min.js")
}
# Create an HTML dependency for Bootstrap
#' @rdname html-dependencies
#' @export
html_dependency_bootstrap <- function(theme) {
if (identical(theme, "default")) {
theme <- "bootstrap"
}
htmlDependency(
name = "bootstrap",
version = "3.3.5",
src = pkg_file("rmd/h/bootstrap"),
meta = list(viewport = "width=device-width, initial-scale=1"),
script = c(
"js/bootstrap.min.js",
# These shims are necessary for IE 8 compatibility
"shim/html5shiv.min.js",
"shim/respond.min.js"),
stylesheet = paste0("css/", theme, ".min.css"))
}
# Create an HTML dependency for tocify
#' @rdname html-dependencies
#' @export
html_dependency_tocify <- function() {
htmlDependency(
name = "tocify",
version = "1.9.1",
src = pkg_file("rmd/h/tocify"),
script = "jquery.tocify.js",
stylesheet = "jquery.tocify.css")
}
# Create an HTML dependency for Pandoc code block accessibility
html_dependency_accessible_code_block <- function() {
ver <- pandoc_version()
if (ver < '2.7.3' || ver > '2.9.2.1') return()
htmlDependency(
name = "accessible-code-block",
version = "0.0.1",
src = pkg_file("rmd/h/accessibility"),
script = "empty-anchor.js"
)
}
# Create an HTML dependency for FontAwesome
#' @rdname html-dependencies
#' @export
html_dependency_font_awesome <- function() {
htmlDependency(
"font-awesome",
"5.1.0",
src = pkg_file("rmd/h/fontawesome"),
stylesheet = c(
"css/all.css",
"css/v4-shims.css")
)
}
# Create an HTML dependency for Ionicons
#' @rdname html-dependencies
#' @export
html_dependency_ionicons <- function() {
htmlDependency(
name = "ionicons",
version = "2.0.1",
src = pkg_file("rmd/h/ionicons"),
stylesheet = "css/ionicons.min.css")
}
html_dependency_navigation <- function(code_menu, source_embed) {
# dynamically build script list
script <- c("tabsets.js")
if (code_menu)
script <- c(script, "codefolding.js")
if (source_embed)
script <- c(script, "sourceembed.js")
htmlDependency(name = "navigation",
version = "1.1",
src = pkg_file("rmd/h/navigation-1.1"),
script = script)
}
html_dependency_anchor_sections <- function() {
htmlDependency(name = "anchor-sections",
version = "1.0.1",
src = pkg_file("rmd/h/anchor-sections"),
script = "anchor-sections.js",
stylesheet = "anchor-sections.css")
}
# analyze navbar html source for icon dependencies
navbar_icon_dependencies <- function(navbar) {
# read the navbar source
source <- read_utf8(navbar)
# find icon references
res <- regexec('<(span|i) +class *= *("|\') *(fa\\w fa|ion ion)-', source)
matches <- regmatches(source, res)
libs <- c()
for (match in matches) {
if (length(match) > 0)
libs <- c(libs, match[[4]])
}
libs <- unique(libs)
# return their dependencies
any_fa <- any(grepl("fa\\w fa", libs))
any_ion <- any(grepl("ion ion", libs))
html_dependencies_fonts(any_fa, any_ion)
}
# utility function to return a list of font dependencies based
# whether we are including font_awesome and/or ionicons
html_dependencies_fonts <- function(font_awesome, ionicons) {
deps <- list()
if (font_awesome)
deps <- append(deps, list(html_dependency_font_awesome()))
if (ionicons)
deps <- append(deps, list(html_dependency_ionicons()))
deps
}
# flattens an arbitrarily nested list and returns all of the dependency
# objects it contains
flatten_dependencies <- function(knit_meta, test) {
all_dependencies <- list()
# knit_meta is a list of 'meta' attributes returned from custom knit_print
# functions. since the 'meta' attribute could either be an html dependency or
# a list of dependencies we recurse on lists that aren't named
for (dep in knit_meta) {
if (is.null(names(dep)) && is.list(dep)) {
inner_dependencies <- flatten_dependencies(dep, test)
all_dependencies <- append(all_dependencies, inner_dependencies)
} else if (test(dep)) {
all_dependencies[[length(all_dependencies) + 1]] <- dep
}
}
all_dependencies
}
flatten_html_dependencies <- function(knit_meta) {
flatten_dependencies(knit_meta, is_html_dependency)
}
# consolidate dependencies (use latest versions and remove duplicates). this
# routine is the default implementation for version dependency resolution;
# formats may specify their own.
html_dependency_resolver <- function(all_dependencies) {
dependencies <- htmltools::resolveDependencies(all_dependencies)
# validate each surviving dependency
dependencies <- lapply(dependencies, validate_html_dependency)
# return the consolidated dependencies
dependencies
}
html_reference_path <- function(path, lib_dir, output_dir) {
# write the full OS-specific path if no library
if (is.null(lib_dir))
pandoc_path_arg(path)
else
relative_to(output_dir, path)
}
# return the html dependencies as an HTML string suitable for inclusion
# in the head of a document
html_dependencies_as_string <- function(dependencies, lib_dir, output_dir) {
if (!is.null(lib_dir)) {
dependencies <- lapply(dependencies, copyDependencyToDir, lib_dir)
dependencies <- lapply(dependencies, makeDependencyRelative, output_dir)
}
return(renderDependencies(dependencies, "file", encodeFunc = identity,
hrefFilter = function(path) {
html_reference_path(path, lib_dir, output_dir)
})
)
}
# check class of passed list for 'html_dependency'
is_html_dependency <- function(list) {
inherits(list, "html_dependency")
}
# validate that the passed list is a correctly formed html_dependency
validate_html_dependency <- function(list) {
# ensure it's the right class
if (!is_html_dependency(list))
stop("passed object is not of class html_dependency", call. = FALSE)
# validate required fields
if (is.null(list$name))
stop("name for html_dependency not provided", call. = FALSE)
if (is.null(list$version))
stop("version for html_dependency not provided", call. = FALSE)
list <- fix_html_dependency(list)
if (is.null(list$src$file))
stop("path for html_dependency not provided", call. = FALSE)
file <- list$src$file
if (!is.null(list$package)) {
file <- system.file(file, package = list$package)
warning("validate_html_dependency: transformed package '",list$script,"' to file '",file)
list$src$file <- file
}
if (is.na(file) || "" == file) {
if (!is.null(list$script)) {
if (file.exists(paste(system.file(package="rmarkdown"),"rmd/h",list$script,sep="/"))) {
list$src$file <- paste(system.file(package="rmarkdown"),"rmd/h",sep="/")
return(list)
}
for(d in dir(paste(system.file(package="rmarkdown"),"rmd/h",sep="/"))) {
if (file.exists(paste(system.file(package="rmarkdown"),"rmd/h",d,list$script,sep="/"))) {
list$src$file <- paste(system.file(package="rmarkdown"),"rmd/h",d,sep="/")
return(list)
}
}
for(d in dir("/usr/share/javascript")) {
file <- paste("/usr/share/javascript",d,list$script,sep="/")
if (file.exists(file)) {
list$src$file <- d
return(list)
}
}
file <- paste(getwd(),list$script,sep="/")
warning("validate_html_dependency: transformed script '",list$script,"' to file '",file)
list$src$file <- getwd()
}
}
if (is.na(file) || "" == file) {
utils::str(list)
warning("validate_html_dependency was asked to search for a file dependency with no file name")
} else if (!file.exists(file)) {
utils::str(list)
stop("path for html_dependency not found: '", file, "', expected in '", getwd(), "'", call. = FALSE)
}
list
}
# monkey patch HTML dependencies; currently only supports highlight.js
fix_html_dependency <- function(list) {
if (!identical(list$name, 'highlightjs') || !identical(list$version, '1.1'))
return(list)
if (!identical(list$src$file, '')) return(list)
html_dependency_highlightjs(gsub('[.]css$', '', list$stylesheet))
}
# check if the passed knit_meta has any (e.g. html/latex) dependencies
has_dependencies <- function(knit_meta, class) {
if (inherits(knit_meta, class))
return(TRUE)
if (is.list(knit_meta)) {
for (dep in knit_meta) {
if (is.null(names(dep))) {
if (has_dependencies(dep, class))
return(TRUE)
} else {
if (inherits(dep, class))
return(TRUE)
}
}
}
FALSE
}
has_html_dependencies <- function(knit_meta) {
has_dependencies(knit_meta, "html_dependency")
}
# create an html_dependency for pagedtable
#' @rdname html-dependencies
#' @export
html_dependency_pagedtable <- function() {
htmlDependency(
"pagedtable",
version = "1.1",
src = pkg_file("rmd/h/pagedtable-1.1"),
script = "js/pagedtable.js",
stylesheet = "css/pagedtable.css"
)
}
#' @param highlight Highlighter to use
#' @rdname html-dependencies
#' @export
html_dependency_highlightjs <- function(highlight) {
htmlDependency(
"highlightjs",
version = "9.12.0",
src = pkg_file("rmd/h/highlightjs"),
script = "highlight.js",
stylesheet = paste0(highlight, ".css")
)
}
# create an html_dependency for rsiframe
html_dependency_rsiframe <- function() {
# add session port to meta if we have it (note that RStudio will
# only provide this in desktop mode)
meta <- NULL
session_port <- Sys.getenv("RSTUDIO_SESSION_PORT")
if (nzchar(session_port))
meta <- list(rstudio_origin = paste0("127.0.0.1:", session_port))
htmlDependency(
"rstudio-iframe",
version = "1.1",
src = pkg_file("rmd/h/rsiframe-1.1"),
script = "rsiframe.js",
meta = meta
)
}
# Pandoc 2.9 adds attributes on both headers and their parent divs. We remove
# the ones on headers since they are unnecessary (#1723).
html_dependency_header_attrs <- function() {
if (pandoc_available('2.9')) list(
htmlDependency(
"header-attrs",
version = packageVersion("rmarkdown"),
src = pkg_file("rmd/h/pandoc"),
script = "header-attrs.js"
)
)
}
|