| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/highlight.R |
| 3 |
\name{hilight} |
| 4 |
\alias{hilight} |
| 5 |
\alias{hi_latex} |
| 6 |
\alias{hi_html} |
| 7 |
\title{Syntax highlight an R code fragment} |
| 8 |
\usage{ |
| 9 |
hilight( |
| 10 |
code, |
| 11 |
format = c("latex", "html"), |
| 12 |
markup, |
| 13 |
prompt = FALSE, |
| 14 |
fallback = FALSE |
| 15 |
) |
| 16 |
|
| 17 |
hi_latex(code, ...) |
| 18 |
|
| 19 |
hi_html(code, ...) |
| 20 |
} |
| 21 |
\arguments{ |
| 22 |
\item{code}{a character string (the R source code)} |
| 23 |
|
| 24 |
\item{format}{the output format} |
| 25 |
|
| 26 |
\item{markup}{a data frame of two columns containing the markup commands} |
| 27 |
|
| 28 |
\item{prompt}{whether to add prompts to the code} |
| 29 |
|
| 30 |
\item{fallback}{whether to use the fallback method, i.e. the regular |
| 31 |
expression based method; this method is not precise and only highlights a |
| 32 |
few types of symbols such as comments, strings and functions; |
| 33 |
\code{fallback} will be set to \code{TRUE} when the input \code{code} fails |
| 34 |
to be \code{\link{parse}d}} |
| 35 |
|
| 36 |
\item{...}{arguments to be passed to \code{hilight()}} |
| 37 |
} |
| 38 |
\value{ |
| 39 |
A character vector for the syntax highlighted code. |
| 40 |
} |
| 41 |
\description{ |
| 42 |
This function \code{\link{parse}}s the R code, fetches the tokens in it |
| 43 |
(\code{\link{getParseData}}), and attach syntax highlighting commands onto |
| 44 |
them. With proper style definitions for these commands (such as colors or |
| 45 |
font styles), the R code will be syntax highlighted in the LaTeX/HTML output. |
| 46 |
The two functions \code{hi_latex} and \code{hi_html} are wrappers of |
| 47 |
\code{hilight} for LaTeX and HTML output, respectively. |
| 48 |
} |
| 49 |
\details{ |
| 50 |
For the \code{markup} data frame, the first column is put before the R |
| 51 |
tokens, and the second column is behind; the row names of the data frame must |
| 52 |
be the R token names; a special row is named \code{DEFAULT}, which contains |
| 53 |
the markup for the standard tokens (i.e. those that do not need to be |
| 54 |
highlighted); if missing, the built-in data frames \code{highr:::cmd_latex} |
| 55 |
and \code{highr:::cmd_html} will be used. |
| 56 |
|
| 57 |
This function only binds markups onto R tokens, and the real syntax |
| 58 |
highlighting must be done with style definitions, which is out of the scope |
| 59 |
of this package. It was designed to be used as the syntax highlighting |
| 60 |
infrastructure of other packages such as \pkg{knitr}, where the colors and |
| 61 |
font styles are properly defined in the LaTeX preamble and HTML header. |
| 62 |
} |
| 63 |
\examples{ |
| 64 |
library(highr) |
| 65 |
hilight("x=1 # assignment") |
| 66 |
|
| 67 |
txt = c("a <- 1 # something", "c(y=\"world\", z=\"hello\")", "b=function(x=5) {", |
| 68 |
"for(i in 1:10) { |
| 69 |
if (i < x) print(i) else break}}", "z@child # S4 slot", |
| 70 |
"'special chars <>#$\%&_{}'") |
| 71 |
cat(hi_latex(txt), sep = "\n") |
| 72 |
cat(hi_html(txt), sep = "\n") |
| 73 |
|
| 74 |
# the markup data frames |
| 75 |
highr:::cmd_latex |
| 76 |
highr:::cmd_html |
| 77 |
} |
| 78 |
\seealso{ |
| 79 |
See the package vignettes \code{browseVignettes('highr')} for how |
| 80 |
this function works internally. |
| 81 |
} |
| 82 |
\author{ |
| 83 |
Yihui Xie and Yixuan Qiu |
| 84 |
} |