R/html_te.R

#' te R markdown template
#'
#' Personal template for creating an html document from a RMarkdown file.
#'
#' @details Credit to RStudio for the example.
#' @param toc logical.
#' @param toc_depth numeric.
#' @param number_atctions logical.
#' @param fig_width numeric.
#' @param fig_height numeric.
#' @param ... dots. Other arguments to pass to \code{rmarkdown::html_document()}
#' @export
#' @source
#' \url{ttps://rmarkdown.rstudio.com/developer_custom_formats.html }
html_te <- function(toc = FALSE,
                    toc_depth = 3,
                    number_atctions = FALSE,
                    fig_width = 8,
                    fig_height = 8,
                    ...) {
    # get the locations of resource files located within the package
    css <-
      system.file("rmarkdown", "templates", "te", "resources",  package = "temisc")
    require_ns("rmarkdown")
    rmarkdown::html_document(
      toc = toc,
      toc_depth = toc_depth,
      numer_atctions = number_atctions,
      fig_width = fig_width,
      fig_height = fig_height,
      css = css
    )
  }

#
# # Copied from https://github.com/hrbrmstr/hrbrthemes/blob/master/R/utils.r
#
# # Most (if not all) from github.com/rstudio/rmarkdown
#
# from_rmarkdown <- function (implicit_figures = TRUE, extensions = NULL) {
#   extensions <- paste0(extensions, collapse = "")
#   extensions <- gsub(" ", "", extensions)
#   if (!implicit_figures && !grepl("implicit_figures", extensions))
#     extensions <- paste0("-implicit_figures", extensions)
#   rmarkdown::rmarkdown_format(extensions)
# }
#
# from_rst <-function (extensions = NULL) {
#   format <- c("rst")
#   addExtension <- function(extension) {
#     if (length(grep(extension, extensions)) == 0)
#       format <<- c(format, paste0("+", extension))
#   }
#   addExtension("autolink_bare_uris")
#   addExtension("ascii_identifiers")
#   addExtension("tex_math_single_backslash")
#   format <- c(format, extensions, recursive = TRUE)
#   paste(format, collapse = "")
# }
#
# pandoc_html_highlight_args <- function (highlight, template, self_contained,
#                                         files_dir, output_dir)  {
#   args <- c()
#   if (is.null(highlight)) {
#     args <- c(args, "--no-highlight")
#   }
#   else if (!identical(template, "default")) {
#     if (identical(highlight, "default"))
#       highlight <- "pygments"
#     args <- c(args, "--highlight-style", highlight)
#   }
#   else {
#     highlight <- match.arg(highlight, html_highlighters())
#     if (highlight %in% c("default", "textmate")) {
#       highlight_path <- system.file("rmd/h/highlight", package = "rmarkdown")
#       if (self_contained)
#         highlight_path <- pandoc_path_arg(highlight_path)
#       else {
#         highlight_path <- normalized_relative_to(output_dir,
#                                                  rmarkdown::render_supporting_files(highlight_path, files_dir))
#       }
#       args <- c(args, "--no-highlight")
#       args <- c(args, "--variable", paste("highlightjs=",
#                                           highlight_path, sep = ""))
#       if (identical(highlight, "textmate")) {
#         args <- c(args, "--variable", paste("highlightjs-theme=",
#                                             highlight, sep = ""))
#       }
#     }
#     else {
#       args <- c(args, "--highlight-style", highlight)
#     }
#   }
#   args
#
# }
#
# html_highlighters <- function () { c(highlighters(), "textmate") }
# highlighters <- function ()  {
#   c("default", "tango", "pygments", "kate", "monochrome", "espresso",
#     "zenburn", "haddock")
# }
#
# normalized_relative_to <- function (dir, file) {
#   rmarkdown::relative_to(normalizePath(dir, winslash = "/", mustWork = FALSE),
#                          normalizePath(file, winslash = "/", mustWork = FALSE))
# }
#
# normalize_path <- function (path) {
#   if (is.null(path))
#     NULL
#   else normalizePath(path, winslash = "/", mustWork = FALSE)
# }
#
# # te R markdown template
# #
# # Personal template for creating an html document from a RMarkdown file.
# #
# # @details Credit to \code{hrbrthemes::ipsum()} for providing a great example of implementation.
# # @inheritParams rmarkdown::html_document
# # @param toc,toc_depth TOC params
# # @param extra_dependencies,... Additional function arguments to pass to the
# #        base R Markdown HTML output formatter
# # @importFrom htmltools htmlDependency
# # @importFrom rmarkdown pandoc_toc_args pandoc_path_arg knitr_options_html pandoc_options html_document_base rmarkdown_format render_supporting_files relative_to
# # @export
# #
# html_te <- function(number_atctions = FALSE,
#                     fig_width = 8,
#                     fig_height = 8,
#                     fig_retina = if (!fig_caption) 2,
#                     fig_caption = FALSE,
#                     dev = "png",
#                     smart = TRUE,
#                     self_contained = TRUE,
#                     highlight = "default",
#                     mathjax = "default",
#                     extra_dependencies = NULL,
#                     css = NULL,
#                     includes = NULL,
#                     keep_md = FALSE,
#                     lib_dir = NULL,
#                     md_extensions = NULL,
#                     pandoc_args = NULL,
#                     toc = FALSE,
#                     toc_depth = 3,
#                     ...) {
#   theme <- NULL
#   template <- "default"
#   code_folding <- "none"
#
#   dep <- htmltools::htmlDependency(
#     name = "te",
#     version = "0.1.0",
#     system.file("rmarkdown", "templates", "te", "resources", package = "temisc"),
#     # script="rainbow-custom.min.js",
#     stylesheet = "te.css"
#   )
#
#   extra_dependencies <- append(extra_dependencies, list(dep))
#
#
#   args <- c("--standalone")
#   args <- c(args, "--section-divs")
#   args <- c(args, rmarkdown::pandoc_toc_args(toc, toc_depth))
#   args <- c(args,
#             "--template",
#             rmarkdown::pandoc_path_arg(
#               system.file("rmarkdown", "templates", "te", "base.html", package = "temisc")
#             ))
#
#   if (number_atctions)
#     args <- c(args, "--number-sections")
#
#   for (css_file in css)
#     args <- c(args, "--css", rmarkdown::pandoc_path_arg(css_file))
#
#   pre_processor <- function(metadata,
#                             input_file,
#                             runtime,
#                             knit_meta,
#                             files_dir,
#                             output_dir) {
#     if (is.null(lib_dir))
#       lib_dir <- files_dir
#
#     args <- c()
#     args <- c(
#       args,
#       pandoc_html_highlight_args(highlight, template, self_contained, lib_dir, output_dir)
#     )
#     args <- c(
#       args,
#       rmarkdown::includes_to_pandoc_args(
#         includes = includes,
#         filter = if (identical(runtime, "shiny"))
#           normalize_path_te
#         else
#           identity
#       )
#     )
#     args
#
#   }
#
#   rmarkdown::output_format(
#     knitr = rmarkdown::knitr_options_html(fig_width, fig_height, fig_retina, keep_md, dev),
#     pandoc = rmarkdown::pandoc_options(
#       to = "html",
#       from = from_rmarkdown(fig_caption, md_extensions),
#       args = args
#     ),
#     keep_md = keep_md,
#     clean_supporting = self_contained,
#     pre_processor = pre_processor,
#     base_format = rmarkdown::html_document_base(
#       smart = smart,
#       theme = theme,
#       self_contained = self_contained,
#       lib_dir = lib_dir,
#       mathjax = mathjax,
#       template = template,
#       pandoc_args = pandoc_args,
#       extra_dependencies = extra_dependencies,
#       ...
#     )
#   )
# }
aelhabr/temisc documentation built on May 28, 2019, 3:55 p.m.