highlight: syntax highlighting based on the R parser

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/highlight.R

Description

The highlight function performs syntax highlighting based on the results of the parse and the investigation of a detective.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
highlight(file, output = stdout(), detective = simple_detective, renderer, 
  encoding = "unknown", 
  parse.output = parse(file, encoding = encoding, keep.source = TRUE), 
  styles = detective(parse.output), expr = NULL, final.newline = FALSE, 
  showPrompts = FALSE, prompt = getOption("prompt"), 
  continue = getOption("continue"), 
  initial.spaces = TRUE, 
  size = c("normalsize", "tiny", "scriptsize", 
  "footnotesize", "small", "large", "Large", "LARGE", "huge", 
  "Huge"), 
  show_line_numbers = FALSE,
  ...)

Arguments

file

code file to parse. This is only used if the parse.output is given

output

where to write the rendered text. If this is anything else than the default (standard output), the sink function is used to redirect the standard output to the output.

detective

the detective chooses the style to apply to each token, basing its investigation on the results of the parse

renderer

highlight delegates rendering the information to the renderer. This package includes html and latex renderers. See renderer_html and renderer_latex

encoding

encoding to assume for the file. the argument is directly passed to the parse.

parse.output

output from the parse. If this is given, the arguments file and encoding are not used

styles

result of the detective investigation. A character vector with as many elements as there are tokens in the parser output

expr

In case we want to render only one expression and not the full parse tree, this argument can be used to specify which expression to render. The default (NULL) means render all expressions. This feature is used by the sweave driver shipped with this package. See HighlightWeaveLatex

final.newline

logical. Indicates if a newline character is added after all tokens.

showPrompts

if TRUE, the highlighted text will show standard and continue prompt

prompt

standard prompt

continue

continue prompt

initial.spaces

should initial spaces be displayed or skipped.

size

font size. only respected by the latex renderer so far.

show_line_numbers

logical. When TRUE, line numbers are shown in the output.

...

additional arguments, currently ignored.

Value

The resulting formatted text is returned invisibly. It is also written to the output if the output is not NULL

Note

At the moment, the result is sent to the output by means of a call to sink. This is mainly due to the fact that the writing is performed by a C function and the R API does not allow packages to use connections in the C code.

Author(s)

Romain Francois <romain@r-enthusiasts.com>

See Also

renderer_html and renderer_latex are the two implementation of renderers currently available in this package. The xterm256 package defines an additional renderer that uses xterm escape sequences.

simple_detective is an example detective which does a very simple investigation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## Not run: 
	tf <- tempfile()
	dump( "jitter", file = tf )
	highlight( file = tf, detective = simple_detective, 
		renderer = renderer_html( document = TRUE ) )
	highlight( file = tf, detective = simple_detective, 
		renderer = renderer_latex( document = TRUE ) )
		
	# verbatim renderer that upper cases function names
	uppercase_formatter <- function( tokens, styles, ...){
		out <- tokens
		fcalls <- styles == "functioncall"
		out[ fcalls ] <- casefold( out[ fcalls ], upper = TRUE )
		out
	}
	uppercase_renderer <- renderer_verbatim( 
		formatter = uppercase_formatter )
	# to debug the formatter, use this : 
	# debug( uppercase_renderer$formatter )
	# because this will not work:
	# debug( uppercase_formatter )
	highlight( file = tf, detective = simple_detective, 
		renderer = uppercase_renderer )
	

## End(Not run)

highlight documentation built on May 2, 2019, 4:58 p.m.