Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more highlighting support for HTML output #1941

Merged
merged 62 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
734c1bd
add an argument defaulting to FALSE and check for downlit package
cderv Oct 6, 2020
c89fc5c
if using downlit, default to pygments and not highlight.js
cderv Oct 6, 2020
f2c8892
Add downlit as post-processing step
cderv Oct 6, 2020
b666a2a
missing bracket
cderv Oct 6, 2020
6cc1989
use dev downlit for Pandoc highlighting support
cderv Oct 29, 2020
b20a651
Correct wording.
cderv Oct 29, 2020
dbc9c43
use xfun here
cderv Nov 4, 2020
559f5f8
Add tests for pandoc syntax highlighting argument
cderv Nov 4, 2020
e35a921
style and add comment about the current logic
cderv Nov 4, 2020
eb1e04d
explicitly add the pandoc classes from downlit
cderv Nov 4, 2020
e7bfead
style
cderv Nov 4, 2020
41ee41f
typo
cderv Nov 4, 2020
5fe312a
separate highlight logic to add specific css in template
cderv Nov 4, 2020
b501a89
modify tests and refactor
cderv Nov 4, 2020
d24216c
document
cderv Nov 4, 2020
5ce2725
textmate does not work with downlit
cderv Nov 4, 2020
859be92
new downlit is on CRAN now
cderv Nov 4, 2020
0e0ed1c
use a11y.theme from distill by default for downlit.
cderv Nov 5, 2020
0cbcfd0
missing theme file
cderv Nov 5, 2020
a3dfe4a
and update test
cderv Nov 5, 2020
ffe52e7
throw an error insted of a warning if downlit engine for syntax highl…
cderv Nov 5, 2020
fa59bd1
new helper to get less wrapping in tests
cderv Nov 5, 2020
fab4098
there is a wrapper for pandoc highlight arg already
cderv Nov 5, 2020
450b02d
rewrite the logic separating highlighting engine from each other
cderv Nov 5, 2020
7fecaa8
allow to provide highlight = "a11y" to get the accessible theme
cderv Nov 5, 2020
6aa0b32
also add rstudio theme from distill
cderv Nov 5, 2020
509ca7a
Document downlit engine better
cderv Nov 6, 2020
b6e5c4c
more details on highlighting in the doc
cderv Nov 6, 2020
37ab96e
Apply suggestions from code review on wording
cderv Nov 6, 2020
41b04c4
missing space after \cr
cderv Nov 6, 2020
7f11dc9
document param on several paragraphs.
cderv Nov 6, 2020
50eb603
Add a section on Highlighting to document what behavior a user should…
cderv Nov 6, 2020
e2007dc
custom themes file for syntax highlighting style are only available f…
cderv Nov 9, 2020
83abaf3
Merge branch 'master' into downlit-support
cderv Nov 24, 2020
fba6bc2
use arrow.theme from https://github.com/rstudio/distill/pull/221
cderv Nov 24, 2020
98e7af9
\href is url then text
cderv Nov 24, 2020
e315e03
texmate can be used with non default template now.
cderv Nov 27, 2020
38a86cf
in fact stick with current behavior for highlight.js as it is safer t…
cderv Nov 27, 2020
1a33c36
Fix an issue with custom template and highlight=textmate
cderv Nov 27, 2020
3ee0e70
update documentation
cderv Nov 27, 2020
8cf69d9
Merge branch 'master' into downlit-support
cderv Nov 27, 2020
a0160b7
Merge branch 'master' into downlit-support
cderv Nov 27, 2020
5a8e474
Merge branch 'master' into downlit-support
cderv Nov 27, 2020
994a7b5
remove comment lines introduced by copy pasting the theme json file
cderv Nov 27, 2020
d88d9e3
reformart rstudio.theme correctly
cderv Nov 27, 2020
a1d40ef
Apply suggestions from code review
cderv Nov 30, 2020
827bfad
Other changes after @hadley's review
cderv Nov 30, 2020
c7e331c
update arrow.theme to last changes
cderv Dec 2, 2020
518d8ae
Add a reminder comment for #1976
cderv Dec 9, 2020
6f32c42
Merge branch 'master' into downlit-support
cderv Dec 15, 2020
deb612d
Merge branch 'master' into downlit-support
cderv Dec 18, 2020
a558848
Arrow theme tweaks from rstudio/distill#338
cderv Mar 10, 2021
26fc6ff
Merge branch 'master' into downlit-support
cderv May 20, 2021
b65bca4
Merge branch 'main' into downlit-support
cderv Jan 13, 2022
b373d09
Merge branch 'main' into downlit-support
cderv Jan 13, 2022
932b0dd
missing word in documentation
cderv Jan 13, 2022
36ce5d2
Update requirement for downlit in Suggests
cderv Jan 13, 2022
d1a9a6b
typo
cderv Jan 13, 2022
2345a80
Correctly test as `template` can be NULL
cderv Jan 13, 2022
d63608d
Add NEWS bullet and bump version
cderv Jan 14, 2022
5f9299d
Rebuild documentation
cderv Jan 14, 2022
0e3863f
Add correct support for highlight_downlit in html_notebook
cderv Jan 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ Suggests:
vctrs,
tibble,
fs,
rsconnect
rsconnect,
downlit (>= 0.2.1)
SystemRequirements: pandoc (>= 1.14) - http://pandoc.org
URL: https://github.com/rstudio/rmarkdown
BugReports: https://github.com/rstudio/rmarkdown/issues
Expand Down
125 changes: 109 additions & 16 deletions R/html_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,30 @@
#' "darkly", "readable", "spacelab", "united", "cosmo", "lumen", "paper",
#' "sandstone", "simplex", or "yeti"). Pass \code{NULL} for no theme (in this
#' case you can use the \code{css} parameter to add your own styles).
#'@param highlight Syntax highlighting style. Supported styles include
#' "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn",
#' "haddock", and "textmate". Pass \code{NULL} to prevent syntax highlighting.
#'@param highlight Syntax highlight engine and style. See the
#' \emph{Highlighting} section below for details.
#'
#' "default" (and "textmate") will use highlightjs as syntax highlighting
#' engine instead of Pandoc.
#'
#' Any other value will be passed to Pandoc's
#' \href{https://pandoc.org/MANUAL.html#syntax-highlighting}{highlighting
#' style}. Pandoc's built-in styles include "tango", "pygments", "kate",
#' "monochrome", "espresso", "zenburn", "haddock" and "breezedark". Two custom
#' are also included, "arrow", for accessible color scheme and "rstudio",
#' to replicate RStudio editor theme. A path to a \samp{.theme} file can also be
#' passed to be used as a custom highlighting style for Pandoc. Note that custom
#' style are available for Pandoc 2.0+.
cderv marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Pass \code{NULL} to prevent syntax highlighting.
#'
#'@param highlight_downlit \code{TRUE} to use the \pkg{downlit} package as
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' syntax highlight engine to highlight inline code and R code chunks
#' (including providing hyperlinks to function documentation). Only Pandoc
#' color schemes are supported with this engine. With \code{highlight}
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' "default", it will use the accessible theme called "arrow". To learn more
#' about \pkg{downlit} highlighting engine, see
#' \url{https://downlit.r-lib.org/}.
#'@param mathjax Include mathjax. The "default" option uses an https URL from a
#' MathJax CDN. The "local" option uses a local version of MathJax (which is
#' copied into the output directory). You can pass an alternate URL or pass
Expand All @@ -78,6 +99,54 @@
#'@param extra_dependencies,... Additional function arguments to pass to the
#' base R Markdown HTML output formatter \code{\link{html_document_base}}
#'@return R Markdown output format to pass to \code{\link{render}}
#'@section Highlighting:
#'
#' Different highlighting engines can be used with a R Markdown HTML documents:
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' highlightjs and Pandoc for all languages they support, \pkg{downlit} only
#' for R code.
#'
#' highlightjs engine can only be used with the default template (i.e
#' \code{template = "default"}) and it has two styles ("default" and
#' "textmate"). When activated, a JS script and a CSS file are inserted into
#' the output HTML to make it works. For now, this is the default engine for
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' the default template - this could change in the future.
#'
#' Pandoc built-in highlighting engine works with any template, default or
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' custom, and style can be chosen among the built-in ones ("tango", "pygments",
#' "kate", "monochrome", "espresso", "zenburn", "haddock" and "breezedark") or
#' among some custom theme passed as ".theme" files (see Details in the
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' \href{https://pandoc.org/MANUAL.html#syntax-highlighting}{Pandoc Manual}).
#' \pkg{rmarkdown} includes two custom themes to select with \code{highlight}
#' parameter:
#' \itemize{
#' \item{"arrow", an accessible color theme derived from \href{https://www.a11yproject.com/}{optimized for
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' accessibility and color contrast}}
#' \item{"rstudio", a color scheme close to RStudio's default highlighting and
#' highglightjs's textmate.}
#' }
#' Custom theme are only available for Pandoc 2.0 and above.
cderv marked this conversation as resolved.
Show resolved Hide resolved
#'
#' \href{https://downlit.r-lib.org/}{\pkg{downlit}} is an R package that
#' provides a syntax highlighting engine in R. It will also do automatic
#' linking of R code (requires internet connectivity). It is activated only if
#' \code{highlight_downlit} is \code{TRUE} and will pass after Pandoc engine to
#' replace only R code syntax highlighting and will use any selected Pandoc's
#' style. Default color scheme is the accessible theme "arrow". If default
#' template is used, some CSS is added to get the link introduced by
#' autolinking to use the color scheme chosen. If you want to use with a custom
#' template, add this to your template:
cderv marked this conversation as resolved.
Show resolved Hide resolved
#'
#' \preformatted{
#' $if(highlight-downlit)$
#' <style type="text/css">
#' code a:any-link {
#' color: inherit; /* use colour from syntax highlighting */
#' text-decoration: underline;
#' text-decoration-color: #ccc;
#' }
#' </style>
#' $endif$}
#'
#'@section Navigation Bars:
#'
#' If you have a set of html documents which you'd like to provide a common
Expand Down Expand Up @@ -153,16 +222,19 @@
#' Note however that if you choose not to use the "default" HTML template then
#' several aspects of HTML document rendering will behave differently:
#'
#' \itemize{ \item{The \code{theme} parameter does not work (you can still
#' provide styles using the \code{css} parameter). } \item{For the
#' \code{highlight} parameter, the default highlighting style will resolve to
#' "pygments" and the "textmate" highlighting style is not available }
#' \item{The \code{toc_float} parameter will not work. } \item{The
#' \code{code_folding} parameter will not work. } \item{Tabbed sections (as
#' described above) will not work.} \item{Navigation bars (as described above)
#' will not work. }\item{MathJax will not work if \code{self_contained} is
#' \code{TRUE} (these two options can't be used together in normal pandoc
#' templates). } }
#' \itemize{
#' \item{The \code{theme} parameter does not work (you can still provide styles
#' using the \code{css} parameter). }
#' \item{For the \code{highlight} parameter, the default highlighting engine
#' will resolve to Pandoc instead of highlightjs and highlighting style will default to
#' "pygments". "textmate" style is not available as related to highlightjs}
#' \item{The \code{toc_float} parameter will not work. }
#' \item{The \code{code_folding} parameter will not work. }
#' \item{Tabbed sections (as described above) will not work.}
#' \item{Navigation bars (as described above) will not work. }
#' \item{MathJax will not work if \code{self_contained} is \code{TRUE} (these
#' two options can't be used together in normal pandoc templates). }
#' }
#'
#' Due to the above restrictions, you might consider using the \code{includes}
#' parameter as an alternative to providing a fully custom template.
Expand Down Expand Up @@ -193,6 +265,7 @@ html_document <- function(toc = FALSE,
self_contained = TRUE,
theme = "default",
highlight = "default",
highlight_downlit = FALSE,
cderv marked this conversation as resolved.
Show resolved Hide resolved
mathjax = "default",
template = "default",
extra_dependencies = NULL,
Expand Down Expand Up @@ -277,8 +350,14 @@ html_document <- function(toc = FALSE,
)
}

# highlight
args <- c(args, pandoc_html_highlight_args(template, highlight))
# highlighting ---------
if (highlight_downlit && !xfun::loadable("downlit")) {
stop("highlight_downlit=TRUE requires the downlit package to be installed.",
call. = FALSE)
}
args <- c(args,
pandoc_html_highlight_args(template, highlight, highlight_downlit)
)

# add highlight.js html_dependency if required
extra_dependencies <- append(
Expand Down Expand Up @@ -434,6 +513,20 @@ html_document <- function(toc = FALSE,
args
}

# post-processor that use the output file from pandoc
post_processor <- function(metadata, input_file, output_file, clean, verbose) {
cderv marked this conversation as resolved.
Show resolved Hide resolved

# add a post processor for syntax highlighting with downlit if requested
if (highlight_downlit) {
output_file <- downlit::downlit_html_path(
output_file, output_file,
classes = downlit::classes_pandoc()
)
}

output_file
}

# return format
output_format(
knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md, dev),
Expand All @@ -446,6 +539,7 @@ html_document <- function(toc = FALSE,
pre_knit = pre_knit,
post_knit = post_knit,
pre_processor = pre_processor,
post_processor = post_processor,
on_exit = on_exit,
base_format = html_document_base(theme = theme,
self_contained = self_contained,
Expand Down Expand Up @@ -652,4 +746,3 @@ navbar_link_text <- function(x, ...) {
tagList(x$text, ...)
}


2 changes: 1 addition & 1 deletion R/md_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' widely used retina displays, but will also result in the output of
#' \code{<img>} tags rather than markdown images due to the need to set the
#' width of the image explicitly.
#' @param ext Extention of the output document (defaults to ".md").
#' @param ext Extension of the output document (defaults to ".md").
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' @return R Markdown output format to pass to \code{\link{render}}
#' @examples
#' \dontrun{
Expand Down
70 changes: 54 additions & 16 deletions R/pandoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,29 +551,67 @@ unix_mathjax_path <- function() {


pandoc_html_highlight_args <- function(template,
cderv marked this conversation as resolved.
Show resolved Hide resolved
highlight) {
highlight,
highlight_downlit = FALSE) {

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 (is_highlightjs(highlight)) {
args <- c(args, "--no-highlight")
args <- c(args, "--variable", "highlightjs=1")
# no highlighting engine
if (is.null(highlight)) return(pandoc_highlight_args(NULL))

# TODO: move out so that it works also for other formats
resolve_highlight <- function(highlight) {
# if Pandoc built-in highlighter, do no nothing
if (highlight %in% highlighters()) return(highlight)
if (!pandoc2.0()) {
stop("Using a custom highlighting style requires Pandoc 2.0 and above",
call. = FALSE)
}
else {
args <- c(args, "--highlight-style", highlight)
custom <- list(
# from distill
# https://raw.githubusercontent.com/apreshill/distill/arrow/inst/rmarkdown/templates/distill_article/resources/arrow.theme
arrow = pkg_file_highlight("arrow.theme"),
# from distill
# https://github.com/rstudio/distill/blob/c98d332192ff75f268ddf69bddace34e4db6d89b/inst/rmarkdown/templates/distill_article/resources/rstudio.theme
rstudio = pkg_file_highlight("rstudio.theme")
)
# if not an alias use the provided custom path
custom[[highlight]] %||% highlight
}
highlight <- resolve_highlight(highlight)

check_highlightjs <- function(highlight, engine) {
if (highlight != "default" && is_highlightjs(highlight)) {
stop(
sprintf(c(
"'%s' theme is for highlightjs highlighting engine ",
"and can't be used with %s engine."), c(highlight, engine)),
call. = FALSE
)
}
}

# downlit engine
if (highlight_downlit) {
check_highlightjs(highlight, "downlit")
default <- if (pandoc2.0()) resolve_highlight("arrow") else "pygments"
args <- c(
pandoc_highlight_args(highlight, default = default),
# variable used to insert some css in a Pandoc template
pandoc_variable_arg("highlight-downlit")
cderv marked this conversation as resolved.
Show resolved Hide resolved
)
} else if (template == "default" && is_highlightjs(highlight)) {
# highlightjs engine for default template only
args <- c(pandoc_highlight_args(NULL),
# variable used to insert some css and js
# in the Pandoc default template
pandoc_variable_arg("highlightjs", "1"))
} else {
# Pandoc engine
check_highlightjs(highlight, "Pandoc")
args <- pandoc_highlight_args(highlight, default = "pygments")
}

args
}

Expand Down
4 changes: 4 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pkg_file_lua <- function(filters = NULL, package = "rmarkdown") {
pandoc_path_arg(files)
}

pkg_file_highlight <- function(file) {
pkg_file("rmarkdown", "highlight", file, mustWork = TRUE)
}

#' @rdname rmarkdown_format
#' @export
from_rmarkdown <- function(implicit_figures = TRUE, extensions = NULL) {
Expand Down
Loading