Skip to content

Commit

Permalink
closes #963: alternative syntax to engine='lang': just write ```{lang}
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Apr 12, 2015
1 parent 730b574 commit d038fc0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 15 additions & 1 deletion R/parser.R
Expand Up @@ -56,6 +56,21 @@ dep_list = new_defaults()

# separate params and R code in code chunks
parse_block = function(code, header, params.src) {
params = params.src
engine = 'r'
# consider the syntax ```{engine, opt=val} for chunk headers
if (out_format('markdown')) {
engine = sub('^([a-zA-Z]+).*$', '\\1', params)
params = sub('^([a-zA-Z]+)', '', params)
}
params = gsub('^\\s*,*|,*\\s*$', '', params) # rm empty options
# turn ```{engine} into ```{r, engine="engine"}
if (tolower(engine) != 'r') {
params = sprintf('%s, engine="%s"', params, engine)
params = gsub('^\\s*,\\s*', '', params)
}

params.src = params
params = parse_params(params.src)
if (nzchar(spaces <- gsub('^(\\s*).*', '\\1', header))) {
params$indent = spaces
Expand Down Expand Up @@ -95,7 +110,6 @@ unnamed_chunk = function(prefix = NULL, i = chunk_counter()) {
# parse params from chunk header
parse_params = function(params) {

params = gsub('^\\s*,*|,*\\s*$', '', params) # rm empty options
if (params == '') return(list(label = unnamed_chunk()))

res = withCallingHandlers(
Expand Down
3 changes: 2 additions & 1 deletion R/pattern.R
Expand Up @@ -29,7 +29,8 @@ all_patterns = list(
inline.code = '<!--\\s*rinline(.+?)-->', header.begin = '\\s*<head>'),

`md` = list(
chunk.begin = '^\\s*```+\\s*\\{[.]?r(.*)\\}\\s*$', chunk.end = '^\\s*```+\\s*$',
chunk.begin = '^\\s*```+\\s*\\{[.]?([a-zA-Z]+.*)\\}\\s*$',
chunk.end = '^\\s*```+\\s*$',
ref.chunk = '^\\s*<<(.+)>>\\s*$', inline.code = '`r +([^`]+)\\s*`'),

`rst` = list(
Expand Down

0 comments on commit d038fc0

Please sign in to comment.