Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 1 | # These functions are imported from knitr/highr as `:::` is not recommended by |
| 2 | # CRAN |
| 3 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 4 | # escape special LaTeX characters |
| 5 | # @author Yihui Xie |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 6 | escape_latex <- function(x, newlines = FALSE, spaces = FALSE) { |
| 7 | x = gsub('\\\\', '\\\\textbackslash', x) |
| 8 | x = gsub('([#$%&_{}])', '\\\\\\1', x) |
| 9 | x = gsub('\\\\textbackslash', '\\\\textbackslash{}', x) |
| 10 | x = gsub('~', '\\\\textasciitilde{}', x) |
| 11 | x = gsub('\\^', '\\\\textasciicircum{}', x) |
| 12 | if (newlines) x = gsub('(?<!\n)\n(?!\n)', '\\\\\\\\', x, perl = TRUE) |
| 13 | if (spaces) x = gsub(' ', '\\\\ \\\\ ', x) |
| 14 | x |
| 15 | } |
| 16 | |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 17 | escape_latex2 <- function(x) { |
| 18 | x = gsub('\\\\', '\\\\\\\\textbackslash', x) |
| 19 | x = gsub('([#$%&_{}])', '\\\\\\\\\\1', x) |
| 20 | x = gsub('\\\\textbackslash', '\\\\\\\\textbackslash{}', x) |
| 21 | x = gsub('~', '\\\\\\\\textasciitilde{}', x) |
| 22 | x = gsub('\\^', '\\\\\\\\textasciicircum{}', x) |
| 23 | x |
| 24 | } |
| 25 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 26 | # escape special HTML characters |
| 27 | # @author Yihui Xie |
Hao Zhu | 5e4dd50 | 2018-04-05 12:01:58 -0400 | [diff] [blame] | 28 | # Added conversion |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 29 | escape_html <- function(x) { |
| 30 | x = gsub('&', '&', x) |
| 31 | x = gsub('<', '<', x) |
| 32 | x = gsub('>', '>', x) |
| 33 | x = gsub('"', '"', x) |
Hao Zhu | 5e4dd50 | 2018-04-05 12:01:58 -0400 | [diff] [blame] | 34 | x = gsub('\n', '<br />', x) |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 35 | x |
| 36 | } |