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 20 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
34 changes: 32 additions & 2 deletions R/html_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#'@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_downlit \code{TRUE} to use the \pkg{downlit} package to
#' highlight R code (including providing hyperlinks to function documentation).
cderv marked this conversation as resolved.
Show resolved Hide resolved
#' Only pandoc highlighting is supported, so with \code{highlight} parameter to
#' "default" or "textmate", "pygments" will be used.
#'@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 Down Expand Up @@ -193,6 +197,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 +282,18 @@ html_document <- function(toc = FALSE,
)
}

# highlight
args <- c(args, pandoc_html_highlight_args(template, highlight))
# highlighting ---------
if (highlight_downlit) {
if (!xfun::loadable("downlit")) {
warning("highlight_downlit=TRUE requires the downlit package",
cderv marked this conversation as resolved.
Show resolved Hide resolved
"downlit highlighting won't be applied",
"and no function linking will be done.", call. = FALSE)
highlight_downlit <- 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 +449,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 +475,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
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
31 changes: 20 additions & 11 deletions R/pandoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,25 +551,34 @@ 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)) {
# deactivate all highlighting if requesting
args <- c(args, "--no-highlight")
}
else if (!identical(template, "default")) {
if (identical(highlight, "default"))
highlight <- "pygments"
} else if (highlight_downlit) {
# use pandoc highlight and add custom css
if (is_highlightjs(highlight)) {
# default to accessible theme a11y - copied from distill
# https://github.com/rstudio/distill/blob/c98d332192ff75f268ddf69bddace34e4db6d89b/inst/rmarkdown/templates/distill_article/resources/a11y.theme
highlight <- pkg_file_highlight("a11y.theme")
cderv marked this conversation as resolved.
Show resolved Hide resolved
}
args <- c(args, "--highlight-style", highlight)
}
else {
args <- c(args, "--variable", "highlight-downlit=1")
} else if (!identical(template, "default")) {
# Use requested pandoc highlighting for non default pandoc template
if (identical(highlight, "default")) highlight <- "pygments"
args <- c(args, "--highlight-style", highlight)
} else {
# for default template only - Pandoc .theme file not supported
highlight <- match.arg(highlight, html_highlighters())
if (is_highlightjs(highlight)) {
# use highlightjs by default and not pandoc highlighting
args <- c(args, "--no-highlight")
args <- c(args, "--variable", "highlightjs=1")
}
else {
} else {
# or specified highlight style
args <- c(args, "--highlight-style", highlight)
}
}
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
211 changes: 211 additions & 0 deletions inst/rmarkdown/highlight/a11y.theme
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"text-color": null,
hadley marked this conversation as resolved.
Show resolved Hide resolved
"background-color": null,
"line-number-color": "#aaaaaa",
"line-number-background-color": null,
"text-styles": {
"Other": {
"text-color": "#007faa",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Attribute": {
"text-color": "#7d9029",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"SpecialString": {
"text-color": "#bb6688",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Annotation": {
"text-color": "#545454",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
},
"Function": {
"text-color": "#4254A7",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"String": {
"text-color": "#008000",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"ControlFlow": {
"text-color": "#007faa",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
},
"Operator": {
"text-color": "#696969",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Error": {
"text-color": "#ff0000",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
},
"BaseN": {
"text-color": "#a1024a",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Alert": {
"text-color": "#ff0000",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
},
"Variable": {
"text-color": "#19177c",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"BuiltIn": {
"text-color": null,
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Extension": {
"text-color": null,
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Preprocessor": {
"text-color": "#bc7a00",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Information": {
"text-color": "#545454",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
},
"VerbatimString": {
"text-color": "#008000",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Warning": {
"text-color": "#545454",
"background-color": null,
"bold": true,
"italic": true,
"underline": false
},
"Documentation": {
"text-color": "#ba2121",
"background-color": null,
"bold": false,
"italic": true,
"underline": false
},
"Import": {
"text-color": null,
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Char": {
"text-color": "#008000",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"DataType": {
"text-color": "#aa5d00",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Float": {
"text-color": "#a1024a",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Comment": {
"text-color": "#545454",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"CommentVar": {
"text-color": "#545454",
"background-color": null,
"bold": true,
"italic": true,
"underline": false
},
"Constant": {
"text-color": "#d91e18",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"SpecialChar": {
"text-color": "#008000",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"DecVal": {
"text-color": "#a1024a",
"background-color": null,
"bold": false,
"italic": false,
"underline": false
},
"Keyword": {
"text-color": "#007faa",
"background-color": null,
"bold": true,
"italic": false,
"underline": false
}
}
}
11 changes: 11 additions & 0 deletions inst/rmd/h/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@
</script>
$endif$

$if(highlight-downlit)$
<style type="text/css">
/* from bookdown::bs4_book() css */
code a:any-link {
color: inherit; /* use colour from syntax highlighting */
text-decoration: underline;
text-decoration-color: #ccc;
}
</style>
$endif$

$if(highlighting-css)$
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
Expand Down
6 changes: 6 additions & 0 deletions man/html_document.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading