| 1 |
#' All built-in patterns |
| 2 |
#' |
| 3 |
#' This object is a named list of all built-in patterns. |
| 4 |
#' @references Usage: \url{https://yihui.org/knitr/patterns/} |
| 5 |
#' @export |
| 6 |
#' @seealso \code{\link{knit_patterns}} |
| 7 |
#' @examples all_patterns$rnw; all_patterns$html |
| 8 |
#' |
| 9 |
#' str(all_patterns) |
| 10 |
all_patterns = list( |
| 11 |
`rnw` = list( |
| 12 |
chunk.begin = '^\\s*<<(.*)>>=.*$', chunk.end = '^\\s*@\\s*(%+.*|)$', |
| 13 |
inline.code = '\\\\Sexpr\\{([^}]+)\\}', inline.comment = '^\\s*%.*', |
| 14 |
ref.chunk = '^\\s*<<(.+)>>\\s*$', header.begin = '(^|\n)\\s*\\\\documentclass[^}]+\\}', |
| 15 |
document.begin = '\\s*\\\\begin\\{document\\}'), |
| 16 |
|
| 17 |
`brew` = list(inline.code = '<%[=]{0,1}\\s+([^%]+)\\s+[-]*%>'), |
| 18 |
|
| 19 |
`tex` = list( |
| 20 |
chunk.begin = '^\\s*%+\\s*begin.rcode\\s*(.*)', chunk.end = '^\\s*%+\\s*end.rcode', |
| 21 |
chunk.code = '^\\s*%+', ref.chunk = '^%+\\s*<<(.+)>>\\s*$', |
| 22 |
inline.comment = '^\\s*%.*', inline.code = '\\\\rinline\\{([^}]+)\\}', |
| 23 |
header.begin = '(^|\n)\\s*\\\\documentclass[^}]+\\}', |
| 24 |
document.begin = '\\s*\\\\begin\\{document\\}'), |
| 25 |
|
| 26 |
`html` = list( |
| 27 |
chunk.begin = '^\\s*<!--\\s*begin.rcode\\s*(.*)', |
| 28 |
chunk.end = '^\\s*end.rcode\\s*-->', ref.chunk = '^\\s*<<(.+)>>\\s*$', |
| 29 |
inline.code = '<!--\\s*rinline(.+?)-->', header.begin = '\\s*<head>'), |
| 30 |
|
| 31 |
`md` = list( |
| 32 |
chunk.begin = '^[\t >]*```+\\s*\\{([a-zA-Z0-9_]+( *[ ,].*)?)\\}\\s*$', |
| 33 |
chunk.end = '^[\t >]*```+\\s*$', |
| 34 |
ref.chunk = '^\\s*<<(.+)>>\\s*$', |
| 35 |
inline.code = '(?<!(^``))(?<!(\n``))`r[ #]([^`]+)\\s*`'), |
| 36 |
|
| 37 |
`rst` = list( |
| 38 |
chunk.begin = '^\\s*[.][.]\\s+\\{r(.*)\\}\\s*$', |
| 39 |
chunk.end = '^\\s*[.][.]\\s+[.][.]\\s*$', chunk.code = '^\\s*[.][.]', |
| 40 |
ref.chunk = '^\\.*\\s*<<(.+)>>\\s*$', inline.code = ':r:`([^`]+)`'), |
| 41 |
|
| 42 |
`asciidoc` = list( |
| 43 |
chunk.begin = '^//\\s*begin[.]rcode(.*)$', chunk.end = '^//\\s*end[.]rcode\\s*$', |
| 44 |
chunk.code = '^//+', ref.chunk = '^\\s*<<(.+)>>\\s*$', |
| 45 |
inline.code = '`r +([^`]+)\\s*`|[+]r +([^+]+)\\s*[+]', |
| 46 |
inline.comment = '^//.*'), |
| 47 |
|
| 48 |
`textile` = list( |
| 49 |
chunk.begin = '^###[.]\\s+begin[.]rcode(.*)$', |
| 50 |
chunk.end = '^###[.]\\s+end[.]rcode\\s*$', |
| 51 |
ref.chunk = '^\\s*<<(.+)>>\\s*$', |
| 52 |
inline.code = '@r +([^@]+)\\s*@', |
| 53 |
inline.comment = '^###[.].*') |
| 54 |
) |
| 55 |
|
| 56 |
.sep.label = '^(#|--)+\\s*(@knitr|----+)(.*?)-*\\s*$' # pattern for code chunks in an R script |
| 57 |
|
| 58 |
# initial pattern list |
| 59 |
.pat.init = list( |
| 60 |
chunk.begin = NULL, chunk.end = NULL, chunk.code = NULL, inline.code = NULL, |
| 61 |
global.options = NULL, input.doc = NULL, ref.chunk = NULL, |
| 62 |
header.begin = NULL, document.begin = NULL |
| 63 |
) |
| 64 |
|
| 65 |
#' Patterns to match and extract R code in a document |
| 66 |
#' |
| 67 |
#' Patterns are regular expressions and will be used in functions like |
| 68 |
#' \code{base::\link{grep}()} to extract R code and chunk options. The object |
| 69 |
#' \code{knit_patterns} controls the patterns currently used; see the references |
| 70 |
#' and examples for usage. All built-in patterns are available in the list |
| 71 |
#' \link{all_patterns}. |
| 72 |
#' |
| 73 |
#' @seealso \code{\link{all_patterns}} |
| 74 |
#' @references Usage: \url{https://yihui.org/knitr/objects/} |
| 75 |
#' |
| 76 |
#' Components in \code{knit_patterns}: \url{https://yihui.org/knitr/patterns/} |
| 77 |
#' @export |
| 78 |
#' @examples library(knitr) |
| 79 |
#' opat = knit_patterns$get() # old pattern list (to restore later) |
| 80 |
#' |
| 81 |
#' apats = all_patterns # a list of all built-in patterns |
| 82 |
#' str(apats) |
| 83 |
#' knit_patterns$set(apats[['rnw']]) # set pattern list from apats |
| 84 |
#' |
| 85 |
#' knit_patterns$get(c('chunk.begin', 'chunk.end', 'inline.code')) |
| 86 |
#' |
| 87 |
#' # a customized pattern list; has to empty the original patterns first! |
| 88 |
#' knit_patterns$restore() |
| 89 |
#' # we may want to use this in an HTML document |
| 90 |
#' knit_patterns$set(list(chunk.begin = '<!--helloR\\s+(.*)', chunk.end = '^byeR-->')) |
| 91 |
#' str(knit_patterns$get()) |
| 92 |
#' |
| 93 |
#' knit_patterns$set(opat) # put the old patterns back |
| 94 |
knit_patterns = new_defaults(.pat.init) |
| 95 |
|
| 96 |
# in LaTeX, may need to put this \newcommand{\rinline}[1]{R output} |
| 97 |
|
| 98 |
# convenience functions |
| 99 |
set_pattern = function(type) { |
| 100 |
knit_patterns$restore(all_patterns[[type]]) |
| 101 |
} |
| 102 |
|
| 103 |
#' Set regular expressions to read input documents |
| 104 |
#' |
| 105 |
#' These are convenience functions to set pre-defined pattern lists (the syntax |
| 106 |
#' to read input documents). The function names are built from corresponding |
| 107 |
#' file extensions, e.g. \code{pat_rnw()} can set the Sweave syntax to read Rnw |
| 108 |
#' documents. |
| 109 |
#' @rdname pat_fun |
| 110 |
#' @return The patterns object \code{\link{knit_patterns}} is modified as a side |
| 111 |
#' effect. |
| 112 |
#' @export pat_rnw pat_brew pat_tex pat_html pat_md pat_rst pat_asciidoc pat_textile |
| 113 |
#' @examples # see how knit_patterns is modified |
| 114 |
#' knit_patterns$get(); pat_rnw(); knit_patterns$get() |
| 115 |
#' |
| 116 |
#' knit_patterns$restore() # empty the list |
| 117 |
pat_rnw = function() set_pattern('rnw') |
| 118 |
#' @rdname pat_fun |
| 119 |
pat_brew = function() set_pattern('brew') |
| 120 |
#' @rdname pat_fun |
| 121 |
pat_tex = function() set_pattern('tex') |
| 122 |
#' @rdname pat_fun |
| 123 |
pat_html = function() set_pattern('html') |
| 124 |
#' @rdname pat_fun |
| 125 |
pat_md = function() set_pattern('md') |
| 126 |
#' @rdname pat_fun |
| 127 |
pat_rst = function() set_pattern('rst') |
| 128 |
#' @rdname pat_fun |
| 129 |
pat_asciidoc = function() set_pattern('asciidoc') |
| 130 |
#' @rdname pat_fun |
| 131 |
pat_textile = function() set_pattern('textile') |
| 132 |
|
| 133 |
|
| 134 |
# helper functions |
| 135 |
|
| 136 |
# is it a group pattern? |
| 137 |
group_pattern = function(pattern) { |
| 138 |
!is.null(pattern) && grepl('\\(.+\\)', pattern) |
| 139 |
} |
| 140 |
|
| 141 |
# automatically detect the chunk patterns |
| 142 |
detect_pattern = function(text, ext) { |
| 143 |
if (!missing(ext)) { |
| 144 |
ext = tolower(ext) |
| 145 |
if (ext %in% c('rnw', 'snw', 'stex')) return('rnw') |
| 146 |
if (ext == 'brew') return('brew') |
| 147 |
if (ext %in% c('htm', 'html', 'rhtm', 'rhtml')) return('html') |
| 148 |
if (ext %in% c('rmd', 'rmarkdown', 'markdown', 'md', 'qmd')) return('md') |
| 149 |
if (ext %in% c('rst', 'rrst')) return('rst') |
| 150 |
if (ext %in% c('asciidoc', 'rasciidoc', 'adoc', 'radoc')) return('asciidoc') |
| 151 |
} |
| 152 |
for (p in names(all_patterns)) { |
| 153 |
for (i in c('chunk.begin', 'inline.code')) { |
| 154 |
pat = all_patterns[[p]][[i]] |
| 155 |
if (length(pat) && any(grepl(pat, text, perl = TRUE))) return(p) |
| 156 |
} |
| 157 |
} |
| 158 |
# *.Rtex indicates the tex syntax in knitr, but Rnw syntax in traditional |
| 159 |
# Sweave, which should have been detected in the above loop |
| 160 |
if (!missing(ext) && ext == 'rtex') return('rnw') |
| 161 |
NULL |
| 162 |
} |