-
-
Notifications
You must be signed in to change notification settings - Fork 878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lengthen fence characters when needed (fix #1621) #2047
Conversation
For some reason following output occurs in https://github.com/yihui/knitr-examples/blob/master/115-engine-sql.Rmd
I do not understand why, but the error disappears if I trim following lines I have to investigate more. |
It seems the error happens when
rmd = tempfile(fileext = ".Rmd")
c(
"```{r}", "x=1", "```",
"",
"```{r}", "x=1", "``` ",
""
) |> writeLines(rmd)
cat(readLines(knitr::knit(rmd, tempfile())), sep = "\n")
#> processing file: /tmp/RtmppEyr1p/file37db7e54a1b1.Rmd
#> output file: /tmp/RtmppEyr1p/file37db661c6a48
#>
#> ```r
#> x=1
#> ```
#>
#>
#> ````r
#> x=1
#> ```
#> ````
#>
#> ```
#> #> Error: attempt to use zero-length variable name
#> ``` Created on 2021-09-22 by the reprex package (v2.0.1) |
The previous post is wrong. rmd = tempfile(fileext = ".Rmd")
c(
"```{r}", "x = 1", "``` ", ""
) |> writeLines(rmd)
cat(readLines(knitr::knit(rmd, tempfile())), sep = "\n")
#> processing file: /tmp/RtmpdRpzv0/file6f376822dca7.Rmd
#> output file: /tmp/RtmpdRpzv0/file6f374be0aa19
#>
#> ````r
#> x = 1
#> ```
#> ````
#>
#> ```
#> #> Error: attempt to use zero-length variable name
#> ``` Created on 2021-09-23 by the reprex package (v2.0.1) |
Yes, CI is green! Now, I'm ready for the review!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is okay. I just wish to avoid the repetition of code in filter_chunk_end_general()
and filter_chunk_end_md()
if possible. Thank you!
R/parser.R
Outdated
filter_chunk_end = function(chunk.begin, chunk.end) { | ||
filter_chunk_end = function(chunk.begin, chunk.end, lines = NULL, patterns = NULL) { | ||
keys = c('chunk.begin', 'chunk.end') | ||
if (identical(patterns[keys], all_patterns[['md']][keys])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I hope to update chunk.end
and turn its elements to FALSE
if the matched lines are not really the end of code chunks (i.e., if they do not match the number of backticks in chunk.begin
).
After chunk.end
truly represents chunk endings, we can use the original filter_chunk_end()
instead of calling filter_chunk_end_md()
, because the latter contains duplicated code from filter_chunk_end()
, which I hope to avoid.
Could you try that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope my simplification is fine which employs an approach different from yours.
If I understand right, updating chunk.end
is the exactly what is done by filter_chunk_end*()
.
It means calling filter_chunk_end
after updating the chunk.end
will attempts unnecessary filtering.
This is why I chose the different approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! c302685 achieved exactly what I wanted, although it was a different method. Thank you!
Co-authored-by: Yihui Xie <xie@yihui.name>
…tht add -u R/parser.R
@yihui Thanks for the review! I updated the codes based on it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I'll merge this PR later today.
Fixes #1621.
Reprex
Created on 2021-09-21 by the reprex package (v2.0.1)