Skip to content

Commit

Permalink
fixes #1180: only allow white spaces or \n before \documentclass{}
Browse files Browse the repository at this point in the history
[^%]* may match a lot of things, including \documentclass{}
  • Loading branch information
yihui committed Mar 14, 2016
1 parent b62f307 commit 7d0f080
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

- the `cex` parameter was not correctly restored in the case of `opts_knit$set(global.par = TRUE)` (http://stackoverflow.com/q/35606445/559676)

- for Rnw documents, when there are two instances of `\documentclass{}`, **knitr** might mistakenly treats the second instance as the the actual command to declare the document class (thanks, #1180, @ekstroem)

## MAJOR CHANGES

- the default value of the package option `eval.after` is changed from `NULL` to `fig.cap`, i.e. the figure caption will always be evaluated after a code chunk is evaluated (thanks, @JoshOBrien, #1165)
Expand Down
4 changes: 2 additions & 2 deletions R/pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ all_patterns = list(
`rnw` = list(
chunk.begin = '^\\s*<<(.*)>>=.*$', chunk.end = '^\\s*@\\s*(%+.*|)$',
inline.code = '\\\\Sexpr\\{([^}]+)\\}', inline.comment = '^\\s*%.*',
ref.chunk = '^\\s*<<(.+)>>\\s*$', header.begin = '(^|\n)[^%]*\\s*\\\\documentclass[^}]+\\}',
ref.chunk = '^\\s*<<(.+)>>\\s*$', header.begin = '(^|\n)\\s*\\\\documentclass[^}]+\\}',
document.begin = '\\s*\\\\begin\\{document\\}'),

`brew` = list(inline.code = '<%[=]{0,1}\\s+([^%]+)\\s+[-]*%>'),
Expand All @@ -20,7 +20,7 @@ all_patterns = list(
chunk.begin = '^\\s*%+\\s*begin.rcode\\s*(.*)', chunk.end = '^\\s*%+\\s*end.rcode',
chunk.code = '^%+', ref.chunk = '^%+\\s*<<(.+)>>\\s*$',
inline.comment = '^\\s*%.*', inline.code = '\\\\rinline\\{([^}]+)\\}',
header.begin = '(^|\n)[^%]*\\s*\\\\documentclass[^}]+\\}',
header.begin = '(^|\n)\\s*\\\\documentclass[^}]+\\}',
document.begin = '\\s*\\\\begin\\{document\\}'),

`html` = list(
Expand Down
14 changes: 14 additions & 0 deletions tests/testit/test-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ assert(
'insert_header_latex() finds the correct \\documentclass{}',
identical(res[1], '%\\documentclass{article}')
)

txt = '\\documentclass{article}
\\begin{document}
\\begin{verbatim}
\\documentclass{article}
\\end{verbatim}
\\Sexpr{pi}
\\end{document}'
res = strsplit(knit(text = txt, quiet = TRUE), '\n')[[1]]

assert(
'insert_header_latex() finds the correct \\documentclass{}',
identical(res[length(res) - 3], '\\documentclass{article}')
)

0 comments on commit 7d0f080

Please sign in to comment.