highr/0000755000175100001440000000000012714131132011364 5ustar hornikusershighr/inst/0000755000175100001440000000000012714020243012341 5ustar hornikusershighr/inst/doc/0000755000175100001440000000000012714020243013106 5ustar hornikusershighr/inst/doc/highr-internals.Rmd0000644000175100001440000000630212714020243016651 0ustar hornikusers # Internals of the `highr` package The **highr** package is based on the function `getParseData()`, which was introduced in R 3.0.0. This function gives detailed information of the symbols in a code fragment. A simple example: ```{r} p = parse(text = " xx = 1 + 1 # a comment", keep.source = TRUE) (d = getParseData(p)) ``` The first step is to filter out the rows that we do not need: ```{r} (d = d[d$terminal, ]) ``` There is a column `token` in the data frame, and we will wrap this column with markup commands, e.g. `\hlnum{1}` for the numeric constant `1`. We defined the markup commands in `cmd_latex` and `cmd_html`: ```{r} head(highr:::cmd_latex) tail(highr:::cmd_html) ``` These command data frames are connected to the tokens in the R code via their row names: ```{r} d$token rownames(highr:::cmd_latex) ``` Now we know how to wrap up the R tokens. The next big question is how to restore the white spaces in the source code, since they were not directly available in the parsed data, but the parsed data contains column numbers, and we can derive the positions of white spaces from them. For example, `col2 = 5` for the first row, and `col1 = 7` for the next row, and that indicates there must be one space after the token in the first row, otherwise the next row will start at the position `6` instead of `7`. A small trick is used to fill in the gaps of white spaces: ```{r} (z = d[, c('col1', 'col2')]) # take out the column positions (z = t(z)) # transpose the matrix (z = c(z)) # turn it into a vector (z = c(0, head(z, -1))) # append 0 in the beginning, and remove the last element (z = matrix(z, ncol = 2, byrow = TRUE)) ``` Now the two columns indicate the starting and ending positions of spaces, and we can easily figure out how many white spaces are needed for each row: ```{r} (s = z[, 2] - z[, 1] - 1) (s = mapply(highr:::spaces, s)) paste(s, d$text, sep = '') ``` So we have successfully restored the white spaces in the source code. Let's paste all pieces together (suppose we highlight for LaTeX): ```{r} m = highr:::cmd_latex[d$token, ] cbind(d, m) # use standard markup if tokens do not exist in the table m[is.na(m[, 1]), ] = highr:::cmd_latex['STANDARD', ] paste(s, m[, 1], d$text, m[, 2], sep = '', collapse = '') ``` So far so simple. That is one line of code, after all. A next challenge comes when there are multiple lines, and a token spans across multiple lines: ```{r} d = getParseData(parse(text = "x = \"a character\nstring\" #hi", keep.source = TRUE)) (d = d[d$terminal, ]) ``` Take a look at the third row. It says that the character string starts from line 1, and ends on line 2. In this case, we just pretend as if everything on line 1 were on line 2. Then for each line, we append the missing spaces and apply markup commands to text symbols. ```{r} d$line1[d$line1 == 1] = 2 d ``` Do not worry about the column `line2`. It does not matter. Only `line1` is needed to indicate the line number here. Why do we need to highlight line by line instead of applying highlighting commands to all text symbols (a.k.a vectorization)? Well, the margin of this paper is too small to write down the answer. highr/inst/doc/highr-custom.R0000644000175100001440000000071012714020243015640 0ustar hornikusers## ------------------------------------------------------------------------ library(highr) highr:::cmd_latex ## ------------------------------------------------------------------------ m = highr:::cmd_latex m[, 1] = sub('\\hl', '\\my', m[, 1], fixed = TRUE) head(m) ## ------------------------------------------------------------------------ hilight("x = 1+1 # a comment") # default markup hilight("x = 1+1 # a comment", markup = m) # custom markup highr/inst/doc/highr-internals.html0000644000175100001440000003632012714020243017076 0ustar hornikusers Internals of the <code>highr</code> package

Internals of the highr package

The highr package is based on the function getParseData(), which was introduced in R 3.0.0. This function gives detailed information of the symbols in a code fragment. A simple example:

p = parse(text = "   xx = 1 + 1  # a comment", keep.source = TRUE)
(d = getParseData(p))
##    line1 col1 line2 col2 id parent     token terminal        text
## 1      1    4     1    5  1      3    SYMBOL     TRUE          xx
## 3      1    4     1    5  3      0      expr    FALSE            
## 2      1    7     1    7  2      0 EQ_ASSIGN     TRUE           =
## 11     1    9     1   13 11      0      expr    FALSE            
## 4      1    9     1    9  4      5 NUM_CONST     TRUE           1
## 5      1    9     1    9  5     11      expr    FALSE            
## 6      1   11     1   11  6     11       '+'     TRUE           +
## 7      1   13     1   13  7      8 NUM_CONST     TRUE           1
## 8      1   13     1   13  8     11      expr    FALSE            
## 9      1   16     1   26  9    -11   COMMENT     TRUE # a comment

The first step is to filter out the rows that we do not need:

(d = d[d$terminal, ])
##   line1 col1 line2 col2 id parent     token terminal        text
## 1     1    4     1    5  1      3    SYMBOL     TRUE          xx
## 2     1    7     1    7  2      0 EQ_ASSIGN     TRUE           =
## 4     1    9     1    9  4      5 NUM_CONST     TRUE           1
## 6     1   11     1   11  6     11       '+'     TRUE           +
## 7     1   13     1   13  7      8 NUM_CONST     TRUE           1
## 9     1   16     1   26  9    -11   COMMENT     TRUE # a comment

There is a column token in the data frame, and we will wrap this column with markup commands, e.g. \hlnum{1} for the numeric constant 1. We defined the markup commands in cmd_latex and cmd_html:

head(highr:::cmd_latex)
##              cmd1 cmd2
## COMMENT  \\hlcom{    }
## FUNCTION \\hlkwa{    }
## IF       \\hlkwa{    }
## ELSE     \\hlkwa{    }
## WHILE    \\hlkwa{    }
## FOR      \\hlkwa{    }
tail(highr:::cmd_html)
##                             cmd1    cmd2
## 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>
## STANDARD   <span class="hl std"> </span>
## STR_CONST  <span class="hl str"> </span>

These command data frames are connected to the tokens in the R code via their row names:

d$token
## [1] "SYMBOL"    "EQ_ASSIGN" "NUM_CONST" "'+'"       "NUM_CONST" "COMMENT"
rownames(highr:::cmd_latex)
##  [1] "COMMENT"              "FUNCTION"             "IF"                  
##  [4] "ELSE"                 "WHILE"                "FOR"                 
##  [7] "IN"                   "BREAK"                "REPEAT"              
## [10] "NEXT"                 "NULL_CONST"           "LEFT_ASSIGN"         
## [13] "EQ_ASSIGN"            "RIGHT_ASSIGN"         "SYMBOL_FORMALS"      
## [16] "SYMBOL_SUB"           "SLOT"                 "SYMBOL_FUNCTION_CALL"
## [19] "NUM_CONST"            "'+'"                  "'-'"                 
## [22] "'*'"                  "'/'"                  "'^'"                 
## [25] "'$'"                  "'@'"                  "':'"                 
## [28] "'?'"                  "'~'"                  "'!'"                 
## [31] "SPECIAL"              "GT"                   "GE"                  
## [34] "LT"                   "LE"                   "EQ"                  
## [37] "NE"                   "AND"                  "AND2"                
## [40] "OR"                   "OR2"                  "NS_GET"              
## [43] "NS_GET_INT"           "STANDARD"             "STR_CONST"

Now we know how to wrap up the R tokens. The next big question is how to restore the white spaces in the source code, since they were not directly available in the parsed data, but the parsed data contains column numbers, and we can derive the positions of white spaces from them. For example, col2 = 5 for the first row, and col1 = 7 for the next row, and that indicates there must be one space after the token in the first row, otherwise the next row will start at the position 6 instead of 7.

A small trick is used to fill in the gaps of white spaces:

(z = d[, c("col1", "col2")])  # take out the column positions
##   col1 col2
## 1    4    5
## 2    7    7
## 4    9    9
## 6   11   11
## 7   13   13
## 9   16   26
(z = t(z))  # transpose the matrix
##      1 2 4  6  7  9
## col1 4 7 9 11 13 16
## col2 5 7 9 11 13 26
(z = c(z))  # turn it into a vector
##  [1]  4  5  7  7  9  9 11 11 13 13 16 26
(z = c(0, head(z, -1)))  # append 0 in the beginning, and remove the last element
##  [1]  0  4  5  7  7  9  9 11 11 13 13 16
(z = matrix(z, ncol = 2, byrow = TRUE))
##      [,1] [,2]
## [1,]    0    4
## [2,]    5    7
## [3,]    7    9
## [4,]    9   11
## [5,]   11   13
## [6,]   13   16

Now the two columns indicate the starting and ending positions of spaces, and we can easily figure out how many white spaces are needed for each row:

(s = z[, 2] - z[, 1] - 1)
## [1] 3 1 1 1 1 2
(s = mapply(highr:::spaces, s))
## [1] "   " " "   " "   " "   " "   "  "
paste(s, d$text, sep = "")
## [1] "   xx"         " ="            " 1"            " +"           
## [5] " 1"            "  # a comment"

So we have successfully restored the white spaces in the source code. Let's paste all pieces together (suppose we highlight for LaTeX):

m = highr:::cmd_latex[d$token, ]
cbind(d, m)
##   line1 col1 line2 col2 id parent     token terminal        text     cmd1
## 1     1    4     1    5  1      3    SYMBOL     TRUE          xx     <NA>
## 2     1    7     1    7  2      0 EQ_ASSIGN     TRUE           = \\hlkwb{
## 4     1    9     1    9  4      5 NUM_CONST     TRUE           1 \\hlnum{
## 6     1   11     1   11  6     11       '+'     TRUE           + \\hlopt{
## 7     1   13     1   13  7      8 NUM_CONST     TRUE           1 \\hlnum{
## 9     1   16     1   26  9    -11   COMMENT     TRUE # a comment \\hlcom{
##   cmd2
## 1 <NA>
## 2    }
## 4    }
## 6    }
## 7    }
## 9    }
# use standard markup if tokens do not exist in the table
m[is.na(m[, 1]), ] = highr:::cmd_latex["STANDARD", ]
paste(s, m[, 1], d$text, m[, 2], sep = "", collapse = "")
## [1] "   \\hlstd{xx} \\hlkwb{=} \\hlnum{1} \\hlopt{+} \\hlnum{1}  \\hlcom{# a comment}"

So far so simple. That is one line of code, after all. A next challenge comes when there are multiple lines, and a token spans across multiple lines:

d = getParseData(parse(text = "x = \"a character\nstring\" #hi", keep.source = TRUE))
(d = d[d$terminal, ])
##   line1 col1 line2 col2 id parent     token terminal                  text
## 1     1    1     1    1  1      3    SYMBOL     TRUE                     x
## 2     1    3     1    3  2      0 EQ_ASSIGN     TRUE                     =
## 4     1    5     2    7  4      7 STR_CONST     TRUE "a character\nstring"
## 5     2    9     2   11  5     -7   COMMENT     TRUE                   #hi

Take a look at the third row. It says that the character string starts from line 1, and ends on line 2. In this case, we just pretend as if everything on line 1 were on line 2. Then for each line, we append the missing spaces and apply markup commands to text symbols.

d$line1[d$line1 == 1] = 2
d
##   line1 col1 line2 col2 id parent     token terminal                  text
## 1     2    1     1    1  1      3    SYMBOL     TRUE                     x
## 2     2    3     1    3  2      0 EQ_ASSIGN     TRUE                     =
## 4     2    5     2    7  4      7 STR_CONST     TRUE "a character\nstring"
## 5     2    9     2   11  5     -7   COMMENT     TRUE                   #hi

Do not worry about the column line2. It does not matter. Only line1 is needed to indicate the line number here.

Why do we need to highlight line by line instead of applying highlighting commands to all text symbols (a.k.a vectorization)? Well, the margin of this paper is too small to write down the answer.

highr/inst/doc/highr-custom.html0000644000175100001440000001740612714020243016415 0ustar hornikusers Customization of the <code>highr</code> package

Customization of the highr package

If you are not satisfied with the default syntax highlighting commands in the highr package, you can just use your own tags/commands. In this vignette, we show a brief example.

The default highlighting commands are stored in two internal data frames cmd_latex and cmd_html:

library(highr)
highr:::cmd_latex
##                          cmd1 cmd2
## COMMENT              \\hlcom{    }
## 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{    }
## STANDARD             \\hlstd{    }
## STR_CONST            \\hlstr{    }

This data frame is passed to the markup argument in hilight(), so we are free to pass a modified version there. Suppose I want to use the command \my<*> instead of \hl<*>:

m = highr:::cmd_latex
m[, 1] = sub("\\hl", "\\my", m[, 1], fixed = TRUE)
head(m)
##              cmd1 cmd2
## COMMENT  \\mycom{    }
## FUNCTION \\mykwa{    }
## IF       \\mykwa{    }
## ELSE     \\mykwa{    }
## WHILE    \\mykwa{    }
## FOR      \\mykwa{    }

Then

hilight("x = 1+1  # a comment")  # default markup
## [1] "\\hlstd{x} \\hlkwb{=} \\hlnum{1}\\hlopt{+}\\hlnum{1}  \\hlcom{# a comment}"
hilight("x = 1+1  # a comment", markup = m)  # custom markup
## [1] "\\mystd{x} \\mykwb{=} \\mynum{1}\\myopt{+}\\mynum{1}  \\mycom{# a comment}"

This allows one to use arbitrary commands around the text symbols in the R code. See https://github.com/yihui/highr/blob/master/R/highlight.R for how cmd_latex and cmd_html were generated in highr.

highr/inst/doc/highr-internals.R0000644000175100001440000000304612714020243016332 0ustar hornikusers## ------------------------------------------------------------------------ p = parse(text = " xx = 1 + 1 # a comment", keep.source = TRUE) (d = getParseData(p)) ## ------------------------------------------------------------------------ (d = d[d$terminal, ]) ## ------------------------------------------------------------------------ head(highr:::cmd_latex) tail(highr:::cmd_html) ## ------------------------------------------------------------------------ d$token rownames(highr:::cmd_latex) ## ------------------------------------------------------------------------ (z = d[, c('col1', 'col2')]) # take out the column positions (z = t(z)) # transpose the matrix (z = c(z)) # turn it into a vector (z = c(0, head(z, -1))) # append 0 in the beginning, and remove the last element (z = matrix(z, ncol = 2, byrow = TRUE)) ## ------------------------------------------------------------------------ (s = z[, 2] - z[, 1] - 1) (s = mapply(highr:::spaces, s)) paste(s, d$text, sep = '') ## ------------------------------------------------------------------------ m = highr:::cmd_latex[d$token, ] cbind(d, m) # use standard markup if tokens do not exist in the table m[is.na(m[, 1]), ] = highr:::cmd_latex['STANDARD', ] paste(s, m[, 1], d$text, m[, 2], sep = '', collapse = '') ## ------------------------------------------------------------------------ d = getParseData(parse(text = "x = \"a character\nstring\" #hi", keep.source = TRUE)) (d = d[d$terminal, ]) ## ------------------------------------------------------------------------ d$line1[d$line1 == 1] = 2 d highr/inst/doc/highr-custom.Rmd0000644000175100001440000000207612714020243016170 0ustar hornikusers # Customization of the `highr` package If you are not satisfied with the default syntax highlighting commands in the **highr** package, you can just use your own tags/commands. In this vignette, we show a brief example. The default highlighting commands are stored in two internal data frames `cmd_latex` and `cmd_html`: ```{r} library(highr) highr:::cmd_latex ``` This data frame is passed to the `markup` argument in `hilight()`, so we are free to pass a modified version there. Suppose I want to use the command `\my<*>` instead of `\hl<*>`: ```{r} m = highr:::cmd_latex m[, 1] = sub('\\hl', '\\my', m[, 1], fixed = TRUE) head(m) ``` Then ```{r} hilight("x = 1+1 # a comment") # default markup hilight("x = 1+1 # a comment", markup = m) # custom markup ``` This allows one to use arbitrary commands around the text symbols in the R code. See for how `cmd_latex` and `cmd_html` were generated in **highr**. highr/tests/0000755000175100001440000000000012513364442012537 5ustar hornikusershighr/tests/testit/0000755000175100001440000000000012513364447014060 5ustar hornikusershighr/tests/testit/test-utils.R0000644000175100001440000000216612235531161016313 0ustar hornikuserslibrary(testit) assert( 'spaces(n) gives n spaces', spaces(-1) == '', spaces(0) == '', spaces(1) == ' ', spaces(5) == ' ' ) assert( 'escape_latex() sanitizes backslashes and {}', escape_latex('\\') == '\\textbackslash{}', escape_latex('\\{}') == '\\textbackslash{}\\{\\}', escape_latex('{\\}') == '\\{\\textbackslash{}\\}', escape_latex('~!@#$%^&*()') == '~!@#$%^&*()' ) assert( 'escape_html() escapes HTML chars', escape_html('&"<>') == '&"<>', escape_html('~!@#$%^&*()') == '~!@#$%^&*()' ) assert( 'try_parse() tells if a code fragment is complete or not', try_parse('1+1'), !try_parse('1+1+'), try_parse('if(TRUE)1'), !try_parse(c('if(T){','F')), try_parse(c('if(T){','F}')) ) assert( 'group_src() puts lines of the same expression into a list element', identical(group_src('1+1'), list('1+1')), identical(group_src(c('1+1+', '1')), list(c('1+1+', '1'))), identical(group_src(c('1+1+', '1', 'TRUE')), list(c('1+1+', '1'), 'TRUE')) ) assert( 'group_src() should signal an error for incomplete code', has_error(group_src('1+1+')), has_error(group_src(c('1+1', '1+1+'))) ) highr/tests/testit/test-hilight.R0000644000175100001440000000434112712271147016605 0ustar hornikuserslibrary(testit) assert( 'hi_latex() works without prompts', hi_latex('1+1') == '\\hlnum{1}\\hlopt{+}\\hlnum{1}', hi_latex(' 1 + 1') == ' \\hlnum{1} \\hlopt{+} \\hlnum{1}', identical(hi_latex(c(' if (TRUE ){', 'foo && bar}')), c( ' \\hlkwa{if} \\hlstd{(}\\hlnum{TRUE} \\hlstd{)\\{}', '\\hlstd{foo} \\hlopt{&&} \\hlstd{bar\\}}' )) ) assert( 'hi_latex() works with prompts', hi_latex('1+1', prompt=TRUE) == '\\hlstd{> }\\hlnum{1}\\hlopt{+}\\hlnum{1}', identical(hi_latex(c(' if (TRUE ){', 'foo && bar}'), prompt = TRUE), paste( '\\hlstd{> } \\hlkwa{if} \\hlstd{(}\\hlnum{TRUE} \\hlstd{)\\{}', '\\hlstd{+ }\\hlstd{foo} \\hlopt{&&} \\hlstd{bar\\}}', sep = '\n' )) ) assert( 'hi_latex() preserves blank lines', identical(hi_latex(c('1+1','','foo(x=3) # comm')), c( '\\hlnum{1}\\hlopt{+}\\hlnum{1}\n', '\\hlkwd{foo}\\hlstd{(}\\hlkwc{x}\\hlstd{=}\\hlnum{3}\\hlstd{)} \\hlcom{# comm}' )) ) assert( 'the fallback method recognizes comments, functions and strings', identical(hi_latex('1+1 # a comment', fallback = TRUE), '1+1 \\hlcom{# a comment}'), identical(hi_latex('paste("STRING", \'string\')', fallback = TRUE), '\\hlkwd{paste}(\\hlstr{"STRING"}, \\hlstr{\'string\'})') ) assert( 'the fallback mode is used when the code does not parse', has_warning(res <- hi_latex('1+1+ # comment')), identical(res, '1+1+ \\hlcom{# comment}') ) assert( 'hilight() works even if code only contains comments', identical(hi_latex('# only comments'), '\\hlcom{# only comments}') ) assert( 'the right arrow -> is preserved', identical(hi_latex('1 ->x # foo'), '\\hlnum{1} \\hlkwb{->}\\hlstd{x} \\hlcom{# foo}') ) assert( 'blank lines before/after code are preserved', hi_latex(c('', '', '1')) %==% c('\n', '\\hlnum{1}'), hi_latex(c('', '', '1', '')) %==% c('\n', '\\hlnum{1}', '') ) # define one's own markup data frame my_cmd = cmd_html my_cmd['NUM_CONST', 1] = '' assert( 'custom markup also works', hi_html('1+ 1') == '1+ 1', hi_html('1+ 1', markup = my_cmd) == '1+ 1' ) rm(my_cmd) highr/tests/test-all.R0000644000175100001440000000004312513232032014371 0ustar hornikuserslibrary(testit) test_pkg('highr') highr/NAMESPACE0000644000175100001440000000012012714020145012575 0ustar hornikusersexport(hi_andre) export(hi_html) export(hi_latex) export(hilight) import(utils) highr/NEWS0000644000175100001440000000425512712270576012107 0ustar hornikusers CHANGES IN highr VERSION 0.6 NEW FEATURES o `hilight()` can deal with multibyte characters that cannot represented in the system native encoding now (on Windows) BUG FIXES o blank lines before/after code are preserved in the hilight() output now (thanks, Terry Therneau) CHANGES IN highr VERSION 0.5 MAJOR CHANGES o the minimal required R version is 3.0.2 now CHANGES IN highr VERSION 0.4 BUG FIXES o hi_andre() may fail to find highlight under OS X (thanks, Christopher Gandrud, #2) MINOR CHANGES o :: and ::: are recognized as operators, and they will be syntax highlighted in the same way as other operators like +, -, *, and /, etc (thanks, Qiang Li, #3) CHANGES IN highr VERSION 0.3 BUG FIXES o blank lines were not preserved in previous versions; now they can be correctly preserved o hilight() works when the code is only a comment; fixed the bug reported at http://stackoverflow.com/q/18548020/559676 which was actually due to the bug of utils::getParseData() in R 3.0.1 o Linux Mint has a built-in version of highlight under /usr/local/bin/ which is not Andre Simon's highlight, and this can hang highr (https://groups.google.com/forum/#!topic/knitr/nicYgzqhwX8) MINOR CHANGES o the package vignettes were built with the knitr::docco_classic engine; see vignette(package = 'highr') CHANGES IN highr VERSION 0.2.1 MINOR CHANGES o fixed a test that failed under R 2.15.x due to the keep.source argument in parse(), which was not available in R until R 3.0.0 CHANGES IN highr VERSION 0.2 NEW FEATURES o added a new argument 'fallback' in hilight(); for R < 3.0.0, we can use hilight(..., fallback = TRUE), which is a rough syntax highlighting method based on regular expressions (hence less accurate and comphrehensive than the getParseData() approach); as a result, highr no longer has to depend on R 3.0.0, although it is recommended to use R 3.0.0 CHANGES IN highr VERSION 0.1 NEW FEATURES o the first version of highr: a hilight() function based on utils::getParseData() to do syntax highlighting for R code; hi_andrew() as a wrapper for Andre Simon's Highlight package highr/R/0000755000175100001440000000000012601126412011565 5ustar hornikusershighr/R/utils.R0000644000175100001440000000310112601126412013043 0ustar hornikusers# generate spaces of width n spaces = function(n = 1, char = ' ') { if (n <= 0) return('') if (n == 1) return(char) paste(rep(char, n), collapse = '') } # group source lines into complete expressions (using a brutal-force method) group_src = function(code) { if ((n <- length(code)) < 1) return(list(code)) i = i1 = i2 = 1 x = list() while (i2 <= n) { piece = code[i1:i2] if (try_parse(piece)) { x[[i]] = piece; i = i + 1 i1 = i2 + 1 # start from the next line } i2 = i2 + 1 } if (i1 <= n) parse(text = piece) # must be an error there x } # whether a code expression can be parsed try_parse = function(code, silent = TRUE) { !inherits( try(parse(text = code, keep.source = FALSE), silent = silent), 'try-error' ) } # TODO: eventually remove the hack for R <= 3.2.2 parse_source = if (getRversion() > '3.2.2') function(lines) { parse(text = lines, keep.source = TRUE) } else function(lines) { # adapted from evaluate src = srcfilecopy('', lines = '') if (length(grep('\n', lines))) lines = unlist(strsplit( sub('$', '\n', as.character(lines)), '\n' )) src$lines = lines parse(text = lines, srcfile = src) } # borrowed from knitr # escape backslashes and {} for the alltt package escape_latex = function(x) { x = gsub('\\\\', '\\\\textbackslash', x) x = gsub('([{}])', '\\\\\\1', x) gsub('\\\\textbackslash', '\\\\textbackslash{}', x) } # escape special HTML chars escape_html = function(x) { x = gsub('&', '&', x) x = gsub('<', '<', x) x = gsub('>', '>', x) x = gsub('"', '"', x) x } highr/R/fallback.R0000644000175100001440000000456212235531173013465 0ustar hornikusershi.keywords = paste('(\\W)(', paste(c( 'if', 'else', 'repeat', 'while', 'function', 'for', 'in', 'next', 'break', 'repeat', 'LETTERS', 'letters', 'month.abb', 'month.name', 'pi', 'TRUE', 'FALSE', 'NULL', 'Inf', 'NaN', 'NA', 'NA_integer_', 'NA_real_', 'NA_complex_', 'NA_character_' ), collapse = '|'), ')(\\W)', sep = '') # only highlight function names, strings and comments hi_naive_latex = function(x, str, com, kwa, kwd) { i = grepl('^\\s*#', x) # whole lines of comments x[i] = sprintf(paste(com[1], '%s', com[2], sep = ''), x[i]) str = escape_bs(str); com = escape_bs(com); kwd = escape_bs(kwd) # comments: what if # inside quotes? if (any(idx <- grepl('#', x) & !grepl('"', x) & !i)) x[idx] = gsub('(#.*)', sprintf('%s\\1%s', com[1], com[2]), x[idx]) i = which(!i) # not comments # function names x[i] = gsub('([[:alnum:]_\\.]+)(\\s*)\\(', sprintf('%s\\1%s\\2(', kwd[1], kwd[2]), x[i]) # character strings x[i] = gsub('"([^"]*)"', sprintf('%s"\\1"%s', str[1], str[2]), x[i]) x[i] = gsub("'([^']*)'", sprintf("%s'\\1'%s", str[1], str[2]), x[i]) x } hi_naive_html = function(x, str, com, kwa, kwd) { # character strings x = gsub('"(.*?)"', sprintf('%s"\\1"%s', str[1], str[2]), x) x = gsub("'([^']*)'", sprintf("%s'\\1'%s", str[1], str[2]), x) # function names x = gsub('([[:alnum:]_\\.]+)(\\s*)\\(', sprintf('%s\\1%s\\2(', kwd[1], kwd[2]), x) if (any(idx <- grepl('#', x) & !grepl('"', x))) x[idx] = gsub('(#.*)', sprintf('%s\\1%s', com[1], com[2]), x[idx]) gsub(hi.keywords, sprintf('\\1%s\\2%s\\3', kwa[1], kwa[2]), x) } hi_naive = function(code, format = c('latex', 'html'), markup, escape_fun = identity, prompt = NULL) { format = match.arg(format) code = escape_fun(code) if (length(prompt) == 2) { # borrowed from knitr:::line_prompt code = paste( prompt[1], gsub('(?<=\n)(?=.|\n)', prompt[2], code, perl = TRUE), sep = '' ) } hi_fun = if (format == 'latex') hi_naive_latex else hi_naive_html hi_fun(split_lines(code), str = markup['STR_CONST', ], com = markup['COMMENT', ], kwa = markup['IF', ], kwd = markup['SYMBOL_FUNCTION_CALL', ]) } # split a character vector by \n split_lines = function(x) { if (!any(grepl('\n', x))) return(x) x[x == ''] = '\n' unlist(strsplit(x, '\n')) } # escape backslashes escape_bs = function(x) gsub('\\\\', '\\\\\\\\', x) highr/R/highlight.R0000644000175100001440000002125312712265151013671 0ustar hornikusers# some special symbols (keywords a) .keywords = c('FUNCTION', 'IF', 'ELSE', 'WHILE', 'FOR', 'IN', 'BREAK', 'REPEAT', 'NEXT', 'NULL_CONST') .operators = c( sprintf("'%s'", c('+', '-', '*', '/', '^', '$', '@', ':', '?', '~', '!')), 'SPECIAL', 'GT', 'GE', 'LT', 'LE', 'EQ', 'NE', 'AND', 'AND2', 'OR', 'OR2', 'NS_GET', 'NS_GET_INT' ) .cmd.list = sort(c( NUM_CONST = 'num', # numbers SYMBOL_FUNCTION_CALL = 'kwd', # function calls STR_CONST = 'str', # character strings COMMENT = 'com', # comment SYMBOL_FORMALS = 'kwc', # function(formals) SYMBOL_SUB = 'kwc', # FUN(args) SLOT = 'kwc', # S4 slot LEFT_ASSIGN = 'kwb', # assignment EQ_ASSIGN = 'kwb', RIGHT_ASSIGN = 'kwb', setNames(rep('opt', length(.operators)), .operators), setNames(rep('kwa', length(.keywords)), .keywords), STANDARD = 'std' # everything else )) cmd_latex = data.frame( cmd1 = paste('\\hl', .cmd.list, '{', sep = ''), cmd2 = '}', stringsAsFactors = FALSE, row.names = names(.cmd.list) ) cmd_html = data.frame( cmd1 = paste('', sep = ''), cmd2 = '', stringsAsFactors = FALSE, row.names = names(.cmd.list) ) # merge code and markups; use standard markups on unknown tokens merge_cmd = function(pdata, cmd) { res = cmd[pdata$token, ] idx = is.na(res[, 1]) res[idx, 1] = cmd['STANDARD', 1] res[idx, 2] = cmd['STANDARD', 2] res[is.na(res)] = '' # if STANDARD is undefined in the markup data frame res } #' Syntax highlight an R code fragment #' #' This function \code{\link{parse}}s the R code, fetches the tokens in it #' (\code{\link{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 \code{hi_latex} and \code{hi_html} are wrappers of #' \code{hilight} for LaTeX and HTML output, respectively. #' #' For the \code{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 \code{STANDARD}, 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 \code{highr:::cmd_latex} #' and \code{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 \pkg{knitr}, where the colors and #' font styles are properly defined in the LaTeX preamble and HTML header. #' @param code a character string (the R source code) #' @param format the output format #' @param markup a data frame of two columns containing the markup commands #' @param prompt whether to add prompts to the code #' @param fallback 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; #' \code{fallback} will be set to \code{TRUE} when the input \code{code} fails #' to be \code{\link{parse}d} #' @param ... arguments to be passed to \code{hilight()} #' @author Yihui Xie <\url{http://yihui.name}> and Yixuan Qiu #' <\url{http://yixuan.cos.name}> #' @seealso See the package vignettes \code{browseVignettes('highr')} for how #' this function works internally. #' @return A character vector for the syntax highlighted code. #' @examples library(highr) #' hilight("x=1 # 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') #' cat(hi_html(txt), sep = '\n') #' #' # the markup data frames #' highr:::cmd_latex; highr:::cmd_html #' @import utils #' @export hilight = function(code, format = c('latex', 'html'), markup, prompt = FALSE, fallback = FALSE) { if (length(code) == 0) return(code) format = match.arg(format) if (missing(markup) || is.null(markup)) markup = if (format == 'latex') cmd_latex else cmd_html escape_fun = if (format == 'latex') escape_latex else escape_html if (!fallback && !try_parse(code, silent = FALSE)) { # the code is not valid, so you must use the fallback mode warning('the syntax of the source code is invalid; the fallback mode is used') fallback = TRUE } if (!prompt) return( (if (fallback) hi_naive else hilight_one)(code, format, markup, escape_fun) ) p1 = escape_fun(getOption('prompt')); p2 = escape_fun(getOption('continue')) std = unlist(markup['STANDARD', ]) if (!any(is.na(std))) { p1 = paste0(std[1], p1, std[2]); p2 = paste0(std[1], p2, std[2]) } code = group_src(code) sapply(mapply(hilight_one, code, MoreArgs = list(format, markup, escape_fun), SIMPLIFY = FALSE, USE.NAMES = FALSE), function(x) paste0(rep(c(p1, p2), c(1L, length(x) - 1L)), x, collapse = '\n')) } # highlight one expression hilight_one = function(code, format, markup, escape_fun) { # the data frames do not need factors in this function; need to keep source op = options(stringsAsFactors = FALSE, keep.source = TRUE); on.exit(options(op)) p = parse_source(code) z = utils::getParseData(p) if (NROW(z) == 0L || !any(z$terminal)) return(code) z = z[z$terminal, ] # record how empty lines before/after the code one = paste(code, collapse = '\n') r1 = '^(\\s*)\n.*'; r2 = '^.*?\n(\\s*)$' s1 = if (grepl(r1, one)) gsub(r1, '\\1', one) s2 = if (grepl(r2, one)) gsub(r2, '\\1', one) res = cbind(z[, c('line1', 'col1', 'line2', 'col2', 'text')], merge_cmd(z, markup)) # escape special LaTeX/HTML chars res$text = escape_fun(res$text) # record how many blank lines after each token blanks = c(pmax(res$line1[-1] - res$line2[-nrow(res)] - 1, 0), 0) # add line breaks to the 8th column res = cbind(res, mapply(spaces, blanks, '\n')) # e.g. a string spans across multiple lines; now need to replace line1 with # line2 so that we know the starting and ending positions of spaces; e.g. turn # line/col numbers 1 5 2 6 into 2 5 2 6 for (i in which(res$line1 != res$line2)) { res$line1[res$line1 == res$line1[i]] = res$line2[i] } out = lapply(split(res, res$line1), function(d) { # merge adjacent tokens of the same type so that the output is cleaner empty = matrix(FALSE, nrow = nrow(d), ncol = 2) for (i in seq_len(nrow(d) - 1)) { if (all(d[i, 6:7] == d[i + 1, 6:7])) empty[i + 1, 1] = empty[i, 2] = TRUE } d[, 6:7][empty] = '' col = as.matrix(d[, c('col1', 'col2')]) # add 0 and remove col[n, 2] to get start/end positions of spaces col = matrix(head(c(0, t(col)), -1), ncol = 2, byrow = TRUE) paste(mapply(spaces, col[, 2] - col[, 1] - 1), d[, 6], d[, 'text'], d[, 7], d[, 8], sep = '', collapse = '') }) c(s1, unlist(out, use.names = FALSE), s2) } #' @export #' @rdname hilight hi_latex = function(code, ...) hilight(code, 'latex', ...) #' @export #' @rdname hilight hi_html = function(code, ...) hilight(code, 'html', ...) #' A wrapper to Andre Simon's Highlight #' #' This function calls Highlight to syntax highlight a code fragment. #' @param code a character string of the source code #' @param language the input language (c, cpp, python, r, ...); see #' \code{system('highlight -p')} #' @param format the output format (html, latex, ...) #' @references Andre Simon's Highlight package \url{http://www.andre-simon.de}. #' @return A character string for the syntax highlighted code. #' @export #' @examples \dontrun{hi_andre('1+1', language='R') #' hi_andre('void main() {\nreturn(0)\n}', language='c', format='latex')} hi_andre = function(code, language, format = 'html') { h = Sys.which('highlight') os = Sys.info()[['sysname']] # highlight on Linux Mint can be something else # on OS10 with highlight installed using Homebrew it's often in /usr/local/bin if (!nzchar(h) || (h == '/usr/local/bin/highlight' && os != 'Darwin' && !file.exists(h <- '/usr/bin/highlight'))) stop('please first install highlight from http://www.andre-simon.de') f = basename(tempfile('code', '.')) writeLines(code, f); on.exit(unlink(f)) cmd = sprintf('%s -f -S %s -O %s %s', shQuote(h), correct_lang(language), format, f) system(cmd, intern = TRUE) } # to help knitr engines decide the highlight language correct_lang = function(x) { switch(x, Rcpp = 'cpp', tikz = 'latex', Rscript = 'R', fortran = 'f', stan = 'R', x) } highr/vignettes/0000755000175100001440000000000012714020243013374 5ustar hornikusershighr/vignettes/highr-internals.Rmd0000644000175100001440000000630212235531173017146 0ustar hornikusers # Internals of the `highr` package The **highr** package is based on the function `getParseData()`, which was introduced in R 3.0.0. This function gives detailed information of the symbols in a code fragment. A simple example: ```{r} p = parse(text = " xx = 1 + 1 # a comment", keep.source = TRUE) (d = getParseData(p)) ``` The first step is to filter out the rows that we do not need: ```{r} (d = d[d$terminal, ]) ``` There is a column `token` in the data frame, and we will wrap this column with markup commands, e.g. `\hlnum{1}` for the numeric constant `1`. We defined the markup commands in `cmd_latex` and `cmd_html`: ```{r} head(highr:::cmd_latex) tail(highr:::cmd_html) ``` These command data frames are connected to the tokens in the R code via their row names: ```{r} d$token rownames(highr:::cmd_latex) ``` Now we know how to wrap up the R tokens. The next big question is how to restore the white spaces in the source code, since they were not directly available in the parsed data, but the parsed data contains column numbers, and we can derive the positions of white spaces from them. For example, `col2 = 5` for the first row, and `col1 = 7` for the next row, and that indicates there must be one space after the token in the first row, otherwise the next row will start at the position `6` instead of `7`. A small trick is used to fill in the gaps of white spaces: ```{r} (z = d[, c('col1', 'col2')]) # take out the column positions (z = t(z)) # transpose the matrix (z = c(z)) # turn it into a vector (z = c(0, head(z, -1))) # append 0 in the beginning, and remove the last element (z = matrix(z, ncol = 2, byrow = TRUE)) ``` Now the two columns indicate the starting and ending positions of spaces, and we can easily figure out how many white spaces are needed for each row: ```{r} (s = z[, 2] - z[, 1] - 1) (s = mapply(highr:::spaces, s)) paste(s, d$text, sep = '') ``` So we have successfully restored the white spaces in the source code. Let's paste all pieces together (suppose we highlight for LaTeX): ```{r} m = highr:::cmd_latex[d$token, ] cbind(d, m) # use standard markup if tokens do not exist in the table m[is.na(m[, 1]), ] = highr:::cmd_latex['STANDARD', ] paste(s, m[, 1], d$text, m[, 2], sep = '', collapse = '') ``` So far so simple. That is one line of code, after all. A next challenge comes when there are multiple lines, and a token spans across multiple lines: ```{r} d = getParseData(parse(text = "x = \"a character\nstring\" #hi", keep.source = TRUE)) (d = d[d$terminal, ]) ``` Take a look at the third row. It says that the character string starts from line 1, and ends on line 2. In this case, we just pretend as if everything on line 1 were on line 2. Then for each line, we append the missing spaces and apply markup commands to text symbols. ```{r} d$line1[d$line1 == 1] = 2 d ``` Do not worry about the column `line2`. It does not matter. Only `line1` is needed to indicate the line number here. Why do we need to highlight line by line instead of applying highlighting commands to all text symbols (a.k.a vectorization)? Well, the margin of this paper is too small to write down the answer. highr/vignettes/highr-custom.Rmd0000644000175100001440000000207612235531173016465 0ustar hornikusers # Customization of the `highr` package If you are not satisfied with the default syntax highlighting commands in the **highr** package, you can just use your own tags/commands. In this vignette, we show a brief example. The default highlighting commands are stored in two internal data frames `cmd_latex` and `cmd_html`: ```{r} library(highr) highr:::cmd_latex ``` This data frame is passed to the `markup` argument in `hilight()`, so we are free to pass a modified version there. Suppose I want to use the command `\my<*>` instead of `\hl<*>`: ```{r} m = highr:::cmd_latex m[, 1] = sub('\\hl', '\\my', m[, 1], fixed = TRUE) head(m) ``` Then ```{r} hilight("x = 1+1 # a comment") # default markup hilight("x = 1+1 # a comment", markup = m) # custom markup ``` This allows one to use arbitrary commands around the text symbols in the R code. See for how `cmd_latex` and `cmd_html` were generated in **highr**. highr/README.md0000644000175100001440000000224012422120727012645 0ustar hornikusers# highr [![Build Status](https://travis-ci.org/yihui/highr.svg)](https://travis-ci.org/yihui/highr) This is an infrastructure R package for syntax highlighting. It supports LaTeX and HTML output. Not surprisingly, it works best with R code. It attaches markups onto source code, e.g., it turns ```r a <- 1 # something ``` into LaTeX code ```latex \hlstd{a} \hlkwb{<-} \hlnum{1} \hlcom{\# something} ``` or HTML code ```html a <- 1 # something ``` via ```r library(highr) hi_latex("a <- 1 # something") hi_html("a <- 1 # something") # or hilight(code, format = "latex"/"html") ``` This package also has a wrapper function, `hi_andre()`, for Andre Simon's [Highlight](http://www.andre-simon.de) package. There are a few package Markdown vignettes in this package: ```r browseVignettes(package = "highr") ``` To install the development version here, use ```r install.packages('highr', repos = 'http://rforge.net', type = 'source') ``` This package is licensed under GPL, and is supposed to serve other packages such as [**knitr**](http://yihui.name/knitr). highr/MD50000644000175100001440000000215112714131132011673 0ustar hornikusersd00470ea569e94e927aa57254802dfd1 *DESCRIPTION 68274cca900e43411344b6790cc88137 *NAMESPACE 21ff6d881b76d6ccd90a0d1b3f1636de *NEWS edd4691064bc2829e778a19f3f7aea36 *R/fallback.R 2dd925be00117660b75264d5f5cc4864 *R/highlight.R 29e00274eddf176cfea8a247262f4f86 *R/utils.R 6db7f4556e35df982db7222498f97d5d *README.md 0660dfbbdf9cf3395ad363bc8842595b *build/vignette.rds be7537135afa7009923a53cdd13981e2 *inst/doc/highr-custom.R 8747f3ac8d45c3e813454387aea7631d *inst/doc/highr-custom.Rmd 86526b4e3c6b7b1d295bd2fb099d61c6 *inst/doc/highr-custom.html c5455e9b244aa670fda42f48fb3d2b61 *inst/doc/highr-internals.R 8e4a682819a65715392e54cb8fdfeea5 *inst/doc/highr-internals.Rmd 2e561a91cc91ae6f32613b49d419b555 *inst/doc/highr-internals.html 07ab81e53d0e7c48b9038a67c81e40ed *man/hi_andre.Rd 4aee28ca5a3069114542c893ab4aa899 *man/hilight.Rd 632f45dfbdf8402b0c542ce6e1995816 *tests/test-all.R 2df506588edab93aa06032a9dff49d44 *tests/testit/test-hilight.R 6febfe9ff05c510dfeb854a0e67e11ea *tests/testit/test-utils.R 8747f3ac8d45c3e813454387aea7631d *vignettes/highr-custom.Rmd 8e4a682819a65715392e54cb8fdfeea5 *vignettes/highr-internals.Rmd highr/build/0000755000175100001440000000000012714020243012463 5ustar hornikusershighr/build/vignette.rds0000644000175100001440000000037012714020243015022 0ustar hornikusers‹uOË Â0Œ}øEѳïö'”‚xñà5´ÑÛD’HÑ“_nMb"è!›ÝÙÙÙCàß çËÔŸÈЖo¤ñôTžãSÎé• ZÆ»23øäc"#°àºåRçKMÂw(0%=F"G‘æE˜žá ™ÉÙÚÊüžruÇŽ¥\”…iL¿=éžKº÷¸’kÔ‰šö¦ƒÆÿÖ2‚!%â¦Ù6`àžî±øþv•˜´e-uVè‚HÆMÙÝ [E™¬ÝE=F«Ø.(gêº~~;J È­# ö3(`|d’/«ç ¯–™highr/DESCRIPTION0000644000175100001440000000173512714131132013100 0ustar hornikusersPackage: highr Type: Package Title: Syntax Highlighting for R Source Code Version: 0.6 Date: 2016-05-09 Authors@R: c(person("Christopher", "Gandrud", role = "ctb"), person("Qiang", "Li", role = "ctb"), person("Yixuan", "Qiu", role = "aut"), person("Yihui", "Xie", email = "xie@yihui.name", role = c("aut", "cre"))) Maintainer: Yihui Xie Description: Provides syntax highlighting for R source code. Currently it supports LaTeX and HTML output. Source code of other languages is supported via Andre Simon's highlight package (http://www.andre-simon.de). Depends: R (>= 3.0.2) Suggests: knitr, testit License: GPL URL: https://github.com/yihui/highr BugReports: https://github.com/yihui/highr/issues VignetteBuilder: knitr RoxygenNote: 5.0.1 NeedsCompilation: no Packaged: 2016-05-09 05:31:15 UTC; yihui Author: Christopher Gandrud [ctb], Qiang Li [ctb], Yixuan Qiu [aut], Yihui Xie [aut, cre] Repository: CRAN Date/Publication: 2016-05-09 17:52:58 highr/man/0000755000175100001440000000000012513364442012150 5ustar hornikusershighr/man/hilight.Rd0000644000175100001440000000530412714020145014061 0ustar hornikusers% Please edit documentation in R/highlight.R \name{hilight} \alias{hi_html} \alias{hi_latex} \alias{hilight} \title{Syntax highlight an R code fragment} \usage{ hilight(code, format = c("latex", "html"), markup, prompt = FALSE, fallback = FALSE) hi_latex(code, ...) hi_html(code, ...) } \arguments{ \item{code}{a character string (the R source code)} \item{format}{the output format} \item{markup}{a data frame of two columns containing the markup commands} \item{prompt}{whether to add prompts to the code} \item{fallback}{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; \code{fallback} will be set to \code{TRUE} when the input \code{code} fails to be \code{\link{parse}d}} \item{...}{arguments to be passed to \code{hilight()}} } \value{ A character vector for the syntax highlighted code. } \description{ This function \code{\link{parse}}s the R code, fetches the tokens in it (\code{\link{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 \code{hi_latex} and \code{hi_html} are wrappers of \code{hilight} for LaTeX and HTML output, respectively. } \details{ For the \code{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 \code{STANDARD}, 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 \code{highr:::cmd_latex} and \code{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 \pkg{knitr}, where the colors and font styles are properly defined in the LaTeX preamble and HTML header. } \examples{ library(highr) hilight("x=1 # 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") cat(hi_html(txt), sep = "\\n") # the markup data frames highr:::cmd_latex highr:::cmd_html } \author{ Yihui Xie <\url{http://yihui.name}> and Yixuan Qiu <\url{http://yixuan.cos.name}> } \seealso{ See the package vignettes \code{browseVignettes('highr')} for how this function works internally. } highr/man/hi_andre.Rd0000644000175100001440000000135412714020145014203 0ustar hornikusers% Please edit documentation in R/highlight.R \name{hi_andre} \alias{hi_andre} \title{A wrapper to Andre Simon's Highlight} \usage{ hi_andre(code, language, format = "html") } \arguments{ \item{code}{a character string of the source code} \item{language}{the input language (c, cpp, python, r, ...); see \code{system('highlight -p')}} \item{format}{the output format (html, latex, ...)} } \value{ A character string for the syntax highlighted code. } \description{ This function calls Highlight to syntax highlight a code fragment. } \examples{ \dontrun{ hi_andre("1+1", language = "R") hi_andre("void main() {\\nreturn(0)\\n}", language = "c", format = "latex") } } \references{ Andre Simon's Highlight package \url{http://www.andre-simon.de}. }