Title: | Syntax Highlighting and Automatic Linking |
---|---|
Description: | Syntax highlighting of R code, specifically designed for the needs of 'RMarkdown' packages like 'pkgdown', 'hugodown', and 'bookdown'. It includes linking of function calls to their documentation on the web, and automatic translation of ANSI escapes in output to the equivalent HTML. |
Authors: | Hadley Wickham [aut, cre], Posit Software, PBC [cph, fnd] |
Maintainer: | Hadley Wickham <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.4.9000 |
Built: | 2024-11-07 04:57:38 UTC |
Source: | https://github.com/r-lib/downlit |
Automatically link inline code
autolink(text) autolink_url(text)
autolink(text) autolink_url(text)
text |
String of code to highlight and link. |
If text
is linkable, an HTML link for autolink()
, and or just
the URL for autolink_url()
. Both return NA
if the text is not
linkable.
downlit provides a number of options to control the details of the linking. They are particularly important if you want to generate "local" links.
downlit.package
: name of the current package. Determines when
topic_index
and article_index
downlit.topic_index
and downlit.article_index
: named character
vector that maps from topic/article name to path.
downlit.rdname
: name of current Rd file being documented (if any);
used to avoid self-links.
downlit.attached
: character vector of currently attached R packages.
downlit.local_packages
: named character vector providing relative
paths (value) to packages (name) that can be reached with relative links
from the target HTML document.
downlit.topic_path
and downlit.article_path
: paths to reference
topics and articles/vignettes relative to the "current" file.
autolink("stats::median()") autolink("vignette('grid', package = 'grid')") autolink_url("stats::median()")
autolink("stats::median()") autolink("vignette('grid', package = 'grid')") autolink_url("stats::median()")
Code blocks, identified by <pre>
tags with class sourceCode r
or any <pre>
tag inside of <div class='downlit'>
, are
processed with highlight()
.
Inline code, identified by <code>
tags that contain only text
(and don't have a header tag (e.g. <h1>
) or <a>
as an ancestor)
are processed processed with autolink()
.
Use downlit_html_path()
to process an .html
file on disk;
use downlit_html_node()
to process an in-memory xml_node
as part of a
larger pipeline.
downlit_html_path(in_path, out_path, classes = classes_pandoc()) downlit_html_node(x, classes = classes_pandoc())
downlit_html_path(in_path, out_path, classes = classes_pandoc()) downlit_html_node(x, classes = classes_pandoc())
in_path , out_path
|
Input and output paths for HTML file |
classes |
A mapping between token names and CSS class names.
Bundled |
x |
An |
downlit_html_path()
invisibly returns output_path
;
downlit_html_node()
modifies x
in place and returns nothing.
node <- xml2::read_xml("<p><code>base::t()</code></p>") node # node is modified in place downlit_html_node(node) node
node <- xml2::read_xml("<p><code>base::t()</code></p>") node # node is modified in place downlit_html_node(node) node
downlit_md_*
works by traversing the markdown AST generated by Pandoc.
It applies highlight()
to CodeBlock
s and autolink()
to inline Code
.
Use downlit_md_path()
to transform a file on disk; use
downlit_md_string()
to transform a string containing markdown as part
of a larger pipeline.
Needs pandoc 1.19 or later.
downlit_md_path(in_path, out_path, format = NULL) downlit_md_string(x, format = NULL)
downlit_md_path(in_path, out_path, format = NULL) downlit_md_string(x, format = NULL)
in_path , out_path
|
Input and output paths for markdown file. |
format |
Pandoc format; defaults to "gfm" if you have pandoc 2.0.0 or greater, otherwise "markdown_github". |
x |
A string containing markdown. |
downlit_md_path()
invisibly returns output_path
;
downlit_md_string()
returns a string containing markdown.
if (rmarkdown::pandoc_available("1.19")) { downlit_md_string("`base::t()`") downlit_md_string("`base::t`") downlit_md_string("* `base::t`") # But don't highlight in headings downlit_md_string("## `base::t`") }
if (rmarkdown::pandoc_available("1.19")) { downlit_md_string("`base::t()`") downlit_md_string("`base::t`") downlit_md_string("* `base::t`") # But don't highlight in headings downlit_md_string("## `base::t`") }
This function runs code
and captures the output using
evaluate::evaluate()
. It syntax higlights code with highlight()
, and
intermingles it with output.
evaluate_and_highlight( code, fig_save, classes = downlit::classes_pandoc(), env = NULL, output_handler = evaluate::new_output_handler(), highlight = TRUE )
evaluate_and_highlight( code, fig_save, classes = downlit::classes_pandoc(), env = NULL, output_handler = evaluate::new_output_handler(), highlight = TRUE )
code |
Code to evaluate (as a string). |
fig_save |
A function with arguments |
classes |
A mapping between token names and CSS class names.
Bundled |
env |
Environment in which to evaluate code; if not supplied, defaults to a child of the global environment. |
output_handler |
Custom output handler for |
highlight |
Optionally suppress highlighting. This is useful for tests. |
An string containing HTML with a dependencies
attribute
giving an additional htmltools dependencies required to render the HTML.
cat(evaluate_and_highlight("1 + 2")) cat(evaluate_and_highlight("x <- 1:10\nmean(x)")) # ----------------------------------------------------------------- # evaluate_and_highlight() powers pkgdown's documentation formatting so # here I include a few examples to make sure everything looks good # ----------------------------------------------------------------- blue <- function(x) paste0("\033[34m", x, "\033[39m") f <- function(x) { cat("This is some output. My favourite colour is ", blue("blue"), ".\n", sep = "") message("This is a message. My favourite fruit is ", blue("blueberries")) warning("Now at stage ", blue("blue"), "!") } f() plot(1:10)
cat(evaluate_and_highlight("1 + 2")) cat(evaluate_and_highlight("x <- 1:10\nmean(x)")) # ----------------------------------------------------------------- # evaluate_and_highlight() powers pkgdown's documentation formatting so # here I include a few examples to make sure everything looks good # ----------------------------------------------------------------- blue <- function(x) paste0("\033[34m", x, "\033[39m") f <- function(x) { cat("This is some output. My favourite colour is ", blue("blue"), ".\n", sep = "") message("This is a message. My favourite fruit is ", blue("blueberries")) warning("Now at stage ", blue("blue"), "!") } f() plot(1:10)
This function:
syntax highlights code
links function calls to their documentation (where possible)
in comments, translates ANSI escapes in to HTML equivalents.
highlight(text, classes = classes_chroma(), pre_class = NULL, code = FALSE) classes_pandoc() classes_chroma()
highlight(text, classes = classes_chroma(), pre_class = NULL, code = FALSE) classes_pandoc() classes_chroma()
text |
String of code to highlight and link. |
classes |
A mapping between token names and CSS class names.
Bundled |
pre_class |
Class(es) to give output |
code |
If |
If text
is valid R code, an HTML <pre>
tag. Otherwise,
NA
.
A string containing syntax highlighted HTML or NA
(if text
isn't parseable).
downlit provides a number of options to control the details of the linking. They are particularly important if you want to generate "local" links.
downlit.package
: name of the current package. Determines when
topic_index
and article_index
downlit.topic_index
and downlit.article_index
: named character
vector that maps from topic/article name to path.
downlit.rdname
: name of current Rd file being documented (if any);
used to avoid self-links.
downlit.attached
: character vector of currently attached R packages.
downlit.local_packages
: named character vector providing relative
paths (value) to packages (name) that can be reached with relative links
from the target HTML document.
downlit.topic_path
and downlit.article_path
: paths to reference
topics and articles/vignettes relative to the "current" file.
cat(highlight("1 + 1")) cat(highlight("base::t(1:3)")) # Unparseable R code returns NA cat(highlight("base::t("))
cat(highlight("1 + 1")) cat(highlight("base::t(1:3)")) # Unparseable R code returns NA cat(highlight("base::t("))
Compare two recorded plots
is_low_change(p1, p2)
is_low_change(p1, p2)
p1 , p2
|
Plot results |
Logical value indicating whether p2
is a low-level update of p1
.