From 6ffce3b8f07d19f22250098175eb7ab45747c2e9 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 26 Aug 2020 18:29:30 +0200 Subject: [PATCH 1/4] expose the mathjax argument in gitbook to customize pandoc argument This allows to modified what is done on rmarkdown side. --- R/gitbook.R | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/R/gitbook.R b/R/gitbook.R index 09fce6ca5..a3f239bd6 100644 --- a/R/gitbook.R +++ b/R/gitbook.R @@ -21,7 +21,7 @@ #' @export gitbook = function( fig_caption = TRUE, number_sections = TRUE, self_contained = FALSE, - lib_dir = 'libs', pandoc_args = NULL, ..., template = 'default', + lib_dir = 'libs', pandoc_args = NULL, ..., template = 'default', mathjax = "default", split_by = c('chapter', 'chapter+number', 'section', 'section+number', 'rmd', 'none'), split_bib = TRUE, config = list(), table_css = TRUE ) { @@ -37,9 +37,33 @@ gitbook = function( config = html_document2( toc = TRUE, number_sections = number_sections, fig_caption = fig_caption, self_contained = self_contained, lib_dir = lib_dir, theme = NULL, - template = template, pandoc_args = pandoc_args2(pandoc_args), ... + template = template, pandoc_args = pandoc_args2(pandoc_args), + mathjax = mathjax, ... ) split_by = match.arg(split_by) + pre = config$pre_processor # in case a pre processor have been defined + config$pre_processor = function( + metadata, input_file, runtime, knit_meta, files_dir, output_dir + ) { + if (is.function(pre)) + args = pre(metadata, input_file, runtime, knit_meta, files_dir, output_dir) + + # adjusting mathjax default handling by rmarkdown in pandoc_mathjax_args() + # if a custom url is passed + if (!is.null(mathjax) && !identical(mathjax, "default")) { + # if no mathjax arg, rmarkdown did not included it and it stays that way + if(length(i <- grep("--mathjax", args))) { + # if local url is inserted by rmarkdown + if (identical(mathjax, "local")) + mathjax = gsub("^--mathjax=(.*)$", "\\1", args[i]) + # replacing the mathjax and setting a variable for the gitbook template + if (length(i)) args = args[-i] + args = c(args, "--mathjax", + "--variable", paste0("mathjax-url:", mathjax)) + } + } + args + } post = config$post_processor # in case a post processor have been defined config$post_processor = function(metadata, input, output, clean, verbose) { if (is.function(post)) output = post(metadata, input, output, clean, verbose) @@ -285,3 +309,7 @@ download_filenames = function(config) { } if (length(downloads)) I(downloads) } + +gitbook_mathjax_arg <- function (mathjax, template, self_contained, files_dir, output_dir) +{ +} From 7198969ea53c35a97bc1d7278fa083202adf48fc Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 26 Aug 2020 18:30:08 +0200 Subject: [PATCH 2/4] use mathjax-url variable in gitbook template if set --- inst/templates/gitbook.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/templates/gitbook.html b/inst/templates/gitbook.html index cef9c1fae..a686f2d6e 100644 --- a/inst/templates/gitbook.html +++ b/inst/templates/gitbook.html @@ -172,7 +172,7 @@

(function () { var script = document.createElement("script"); script.type = "text/javascript"; - var src = "$if(mathjax)$$mathjax$$endif$"; + var src = "$if(mathjax)$$if(mathjax-url)$$mathjax-url$$else$$mathjax$$endif$$endif$"; if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML"; if (location.protocol !== "file:") if (/^https?:/.test(src)) From eea4e9cc38aa866006b1c24d08262f7369fdf976 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 26 Aug 2020 19:08:38 +0200 Subject: [PATCH 3/4] document to make travis happy --- man/gitbook.Rd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/gitbook.Rd b/man/gitbook.Rd index 24dfa28ee..c84559b4e 100644 --- a/man/gitbook.Rd +++ b/man/gitbook.Rd @@ -12,6 +12,7 @@ gitbook( pandoc_args = NULL, ..., template = "default", + mathjax = "default", split_by = c("chapter", "chapter+number", "section", "section+number", "rmd", "none"), split_bib = TRUE, config = list(), From 9e14a5b29ef7d802dc7f54060d1ae1ee1b374172 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 26 Aug 2020 19:12:58 +0200 Subject: [PATCH 4/4] add mathjax in doc params --- R/gitbook.R | 2 +- man/gitbook.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/gitbook.R b/R/gitbook.R index a3f239bd6..fa8d984ba 100644 --- a/R/gitbook.R +++ b/R/gitbook.R @@ -3,7 +3,7 @@ #' This output format function ported a style provided by GitBook #' (\url{https://www.gitbook.com}) for R Markdown. #' @inheritParams html_chapters -#' @param fig_caption,number_sections,self_contained,lib_dir,pandoc_args ... +#' @param fig_caption,number_sections,self_contained,lib_dir,pandoc_args,mathjax ... #' Arguments to be passed to \code{rmarkdown::\link{html_document}()} #' (\code{...} not including \code{toc}, and \code{theme}). #' @param template Pandoc template to use for rendering. Pass \code{"default"} diff --git a/man/gitbook.Rd b/man/gitbook.Rd index c84559b4e..35150732e 100644 --- a/man/gitbook.Rd +++ b/man/gitbook.Rd @@ -20,7 +20,7 @@ gitbook( ) } \arguments{ -\item{fig_caption, number_sections, self_contained, lib_dir, pandoc_args}{... +\item{fig_caption, number_sections, self_contained, lib_dir, pandoc_args, mathjax}{... Arguments to be passed to \code{rmarkdown::\link{html_document}()} (\code{...} not including \code{toc}, and \code{theme}).}