# File src/library/tools/R/dynamicHelp.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2019 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
## Helper function used to declare mime-type for files served by
## dynamic help, and for base64-encoded files embedded in example
## output (see code2html.R).
mime_type <- function(path, ext = NULL)
{
stopifnot(length(path) == 1L)
if (missing(ext)) ext <- file_ext(path)
switch(ext,
"css" = "text/css",
"js" = "text/javascript", # for katex etc
"sgml" = "text/sgml", # in RGtk2
"xml" = "text/xml", # in RCurl (RFC 7303 recommends "application/xml")
"html" = "text/html",
"htm" = "text/html",
"xhtml" = "application/xhtml+xml",
"php" = "application/x-httpd-php",
"epub" = "application/epub+zip",
"csv" = "text/csv",
"json" = "application/json",
"jsonld" = "application/ld+json",
"mjs" = "text/javascript",
## common types (see https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
"gif" = "image/gif", # in R2HTML
"jpg" = "image/jpeg",
"jpeg" = "image/jpeg",
"png" = "image/png",
"svg" = "image/svg+xml",
"apng" = "image/apng",
"avif" = "image/avif",
"webp" = "image/webp",
"bmp" = "image/bmp",
"ico" = "image/x-icon",
"tiff" = "image/tiff",
"tif" = "image/tiff",
"pdf" = "application/pdf",
"eps" =,
"ps" = "application/postscript", # in GLMMGibbs, mclust
## fonts
"eot" = "application/vnd.ms-fontobject",
"otf" = "font/otf",
"ttf" = "font/ttf",
"woff" = "font/woff",
"woff2" = "font/woff2",
## media
"aac" = "audio/aac",
"avi" = "video/x-msvideo",
"cda" = "application/x-cdf",
"mid" = "audio/x-midi",
"midi" = "audio/x-midi",
"mp3" = "audio/mpeg",
"mp4" = "video/mp4",
"mpeg" = "video/mpeg",
"oga" = "audio/ogg",
"ogv" = "video/ogg",
"ogx" = "application/ogg",
"opus" = "audio/opus",
"3gp" = "video/3gpp",
"3g2" = "video/3gpp2",
"wav" = "audio/wav",
"weba" = "audio/webm",
"webm" = "video/webm",
## archive / compression
"bz" = "application/x-bzip",
"bz2" = "application/x-bzip2",
"gz" = "application/gzip",
"rar" = "application/vnd.rar",
"zip" = "application/zip",
"7z" = "application/x-7z-compressed",
"tar" = "application/x-tar",
## default
"text/plain")
}
## This may be asked for
## R.css, favicon.ico
## searches with path = "/doc/html/Search"
## documentation with path = "/doc/....", possibly updated under tempdir()/.R
## demos with path "/demo/*"
## Running demos, using path "/Demo/*"
## html help, either by topic, /library//help/ (pkg=NULL means any)
## or by file, /library//html/.html
httpd <- function(path, query, ...)
{
logHelpRequests <-
config_val_to_logical(Sys.getenv("_R_HTTPD_LOG_MESSAGES_", "FALSE"))
if (logHelpRequests) {
message(sprintf("HTTPD-REQUEST %s%s", path,
if (is.null(query)) ""
else { # query is a named chr vector
paste(paste(names(query), query, sep = "="),
collapse = ",")
}))
}
linksToTopics <-
config_val_to_logical(Sys.getenv("_R_HELP_LINKS_TO_TOPICS_", "TRUE"))
.HTMLdirListing <- function(dir, base, up) {
files <- list.files(dir) # note, no hidden files are listed
out <- HTMLheader(paste0("Listing of directory
", dir),
headerTitle = paste("R:", dir), logo=FALSE,
up = up)
if(!length(files))
out <- c(out, gettext("No files in this directory"))
else {
urls <- paste0('', files, '')
out <- c(out, "",
paste0("- ", mono(iconv(urls, "", "UTF-8")), "
"),
"
")
}
out <- c(out, "
\n