This function parses the R code, fetches the tokens in it
(getParseData), and attach syntax highlighting commands onto
them. With proper style definitions for these commands (such as colors or
font styles), the R code will be syntax highlighted in the LaTeX/HTML output.
The two functions hi_latex and hi_html are wrappers of
hilight for LaTeX and HTML output, respectively.
hilight(
code,
format = c("latex", "html"),
markup,
prompt = FALSE,
fallback = FALSE
)
hi_latex(code, ...)
hi_html(code, ...)a character string (the R source code)
the output format
a data frame of two columns containing the markup commands
whether to add prompts to the code
whether to use the fallback method, i.e. the regular
expression based method; this method is not precise and only highlights a
few types of symbols such as comments, strings and functions;
fallback will be set to TRUE when the input code fails
to be parsed
arguments to be passed to hilight()
A character vector for the syntax highlighted code.
For the markup data frame, the first column is put before the R
tokens, and the second column is behind; the row names of the data frame must
be the R token names; a special row is named DEFAULT, which contains
the markup for the standard tokens (i.e. those that do not need to be
highlighted); if missing, the built-in data frames highr:::cmd_latex
and highr:::cmd_html will be used.
This function only binds markups onto R tokens, and the real syntax highlighting must be done with style definitions, which is out of the scope of this package. It was designed to be used as the syntax highlighting infrastructure of other packages such as knitr, where the colors and font styles are properly defined in the LaTeX preamble and HTML header.
See the package vignettes browseVignettes('highr') for how
this function works internally.
library(highr)
hilight("x=1 # assignment")
#> [1] "\\hldef{x}\\hlkwb{=}\\hlnum{1} \\hlcom{# assignment}"
txt = c("a <- 1 # something", "c(y=\"world\", z=\"hello\")", "b=function(x=5) {",
"for(i in 1:10) {
if (i < x) print(i) else break}}", "z@child # S4 slot",
"'special chars <>#$%&_{}'")
cat(hi_latex(txt), sep = "\n")
#> \hldef{a} \hlkwb{<-} \hlnum{1} \hlcom{# something}
#> \hlkwd{c}\hldef{(}\hlkwc{y}\hldef{=}\hlsng{"world"}\hldef{,} \hlkwc{z}\hldef{=}\hlsng{"hello"}\hldef{)}
#> \hldef{b}\hlkwb{=}\hlkwa{function}\hldef{(}\hlkwc{x}\hldef{=}\hlnum{5}\hldef{) \{}
#> \hlkwa{for}\hldef{(i} \hlkwa{in} \hlnum{1}\hlopt{:}\hlnum{10}\hldef{) \{}
#> \hlkwa{if} \hldef{(i} \hlopt{<} \hldef{x)} \hlkwd{print}\hldef{(i)} \hlkwa{else break}\hldef{\}\}}
#> \hldef{z}\hlopt{@}\hlkwc{child} \hlcom{# S4 slot}
#> \hlsng{'special chars <>#$%&_\{\}'}
cat(hi_html(txt), sep = "\n")
#> <span class="hl def">a</span> <span class="hl kwb"><-</span> <span class="hl num">1</span> <span class="hl com"># something</span>
#> <span class="hl kwd">c</span><span class="hl def">(</span><span class="hl kwc">y</span><span class="hl def">=</span><span class="hl sng">"world"</span><span class="hl def">,</span> <span class="hl kwc">z</span><span class="hl def">=</span><span class="hl sng">"hello"</span><span class="hl def">)</span>
#> <span class="hl def">b</span><span class="hl kwb">=</span><span class="hl kwa">function</span><span class="hl def">(</span><span class="hl kwc">x</span><span class="hl def">=</span><span class="hl num">5</span><span class="hl def">) {</span>
#> <span class="hl kwa">for</span><span class="hl def">(i</span> <span class="hl kwa">in</span> <span class="hl num">1</span><span class="hl opt">:</span><span class="hl num">10</span><span class="hl def">) {</span>
#> <span class="hl kwa">if</span> <span class="hl def">(i</span> <span class="hl opt"><</span> <span class="hl def">x)</span> <span class="hl kwd">print</span><span class="hl def">(i)</span> <span class="hl kwa">else break</span><span class="hl def">}}</span>
#> <span class="hl def">z</span><span class="hl opt">@</span><span class="hl kwc">child</span> <span class="hl com"># S4 slot</span>
#> <span class="hl sng">'special chars <>#$%&_{}'</span>
# the markup data frames
highr:::cmd_latex
#> cmd1 cmd2
#> COMMENT \\hlcom{ }
#> DEFAULT \\hldef{ }
#> FUNCTION \\hlkwa{ }
#> IF \\hlkwa{ }
#> ELSE \\hlkwa{ }
#> WHILE \\hlkwa{ }
#> FOR \\hlkwa{ }
#> IN \\hlkwa{ }
#> BREAK \\hlkwa{ }
#> REPEAT \\hlkwa{ }
#> NEXT \\hlkwa{ }
#> NULL_CONST \\hlkwa{ }
#> LEFT_ASSIGN \\hlkwb{ }
#> EQ_ASSIGN \\hlkwb{ }
#> RIGHT_ASSIGN \\hlkwb{ }
#> SYMBOL_FORMALS \\hlkwc{ }
#> SYMBOL_SUB \\hlkwc{ }
#> SLOT \\hlkwc{ }
#> SYMBOL_FUNCTION_CALL \\hlkwd{ }
#> NUM_CONST \\hlnum{ }
#> '+' \\hlopt{ }
#> '-' \\hlopt{ }
#> '*' \\hlopt{ }
#> '/' \\hlopt{ }
#> '^' \\hlopt{ }
#> '$' \\hlopt{ }
#> '@' \\hlopt{ }
#> ':' \\hlopt{ }
#> '?' \\hlopt{ }
#> '~' \\hlopt{ }
#> '!' \\hlopt{ }
#> SPECIAL \\hlopt{ }
#> GT \\hlopt{ }
#> GE \\hlopt{ }
#> LT \\hlopt{ }
#> LE \\hlopt{ }
#> EQ \\hlopt{ }
#> NE \\hlopt{ }
#> AND \\hlopt{ }
#> AND2 \\hlopt{ }
#> OR \\hlopt{ }
#> OR2 \\hlopt{ }
#> NS_GET \\hlopt{ }
#> NS_GET_INT \\hlopt{ }
#> STR_CONST \\hlsng{ }
highr:::cmd_html
#> cmd1 cmd2
#> COMMENT <span class="hl com"> </span>
#> DEFAULT <span class="hl def"> </span>
#> FUNCTION <span class="hl kwa"> </span>
#> IF <span class="hl kwa"> </span>
#> ELSE <span class="hl kwa"> </span>
#> WHILE <span class="hl kwa"> </span>
#> FOR <span class="hl kwa"> </span>
#> IN <span class="hl kwa"> </span>
#> BREAK <span class="hl kwa"> </span>
#> REPEAT <span class="hl kwa"> </span>
#> NEXT <span class="hl kwa"> </span>
#> NULL_CONST <span class="hl kwa"> </span>
#> LEFT_ASSIGN <span class="hl kwb"> </span>
#> EQ_ASSIGN <span class="hl kwb"> </span>
#> RIGHT_ASSIGN <span class="hl kwb"> </span>
#> SYMBOL_FORMALS <span class="hl kwc"> </span>
#> SYMBOL_SUB <span class="hl kwc"> </span>
#> SLOT <span class="hl kwc"> </span>
#> SYMBOL_FUNCTION_CALL <span class="hl kwd"> </span>
#> NUM_CONST <span class="hl num"> </span>
#> '+' <span class="hl opt"> </span>
#> '-' <span class="hl opt"> </span>
#> '*' <span class="hl opt"> </span>
#> '/' <span class="hl opt"> </span>
#> '^' <span class="hl opt"> </span>
#> '$' <span class="hl opt"> </span>
#> '@' <span class="hl opt"> </span>
#> ':' <span class="hl opt"> </span>
#> '?' <span class="hl opt"> </span>
#> '~' <span class="hl opt"> </span>
#> '!' <span class="hl opt"> </span>
#> SPECIAL <span class="hl opt"> </span>
#> GT <span class="hl opt"> </span>
#> GE <span class="hl opt"> </span>
#> LT <span class="hl opt"> </span>
#> LE <span class="hl opt"> </span>
#> EQ <span class="hl opt"> </span>
#> NE <span class="hl opt"> </span>
#> AND <span class="hl opt"> </span>
#> AND2 <span class="hl opt"> </span>
#> OR <span class="hl opt"> </span>
#> OR2 <span class="hl opt"> </span>
#> NS_GET <span class="hl opt"> </span>
#> NS_GET_INT <span class="hl opt"> </span>
#> STR_CONST <span class="hl sng"> </span>