Skip to content
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

Merged
merged 12 commits into from
Sep 27, 2021
Merged

Conversation

atusy
Copy link
Collaborator

@atusy atusy commented Sep 21, 2021

Fixes #1621.

  • Update source
  • Add tests
  • Update NEWS
  • pass CI

Reprex

rmd = tempfile(fileext = ".Rmd")
c(
  "````{r}",
  "invisible('",
  "```",
  "')",
  "````"
) |> writeLines(rmd)

knitr::knit(rmd, tempfile(), quiet = TRUE) |>
  readLines() |>
  cat(sep = "\n")
#> ````r
#> invisible('
#> ```
#> ')
#> ````

Created on 2021-09-21 by the reprex package (v2.0.1)

@atusy
Copy link
Collaborator Author

atusy commented Sep 21, 2021

For some reason following output occurs in https://github.com/yihui/knitr-examples/blob/master/115-engine-sql.Rmd

````r
readrPackage
``` 
````

```
## Error: attempt to use zero-length variable name
```

I do not understand why, but the error disappears if I trim following lines
https://github.com/yihui/knitr-examples/blob/08cf670e3296b5febffb454bad7b5acf5f1f6295/115-engine-sql.Rmd#L71-L77

I have to investigate more.

@atusy
Copy link
Collaborator Author

atusy commented Sep 21, 2021

It seems the error happens when

  • duplicated chunks exist.
  • if there are any extra lines after the last duplicated chunk.
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)

@atusy
Copy link
Collaborator Author

atusy commented Sep 23, 2021

The previous post is wrong.
The problem happens if chunk is ending with backticks followed by space(s).

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)

@atusy
Copy link
Collaborator Author

atusy commented Sep 23, 2021

Yes, CI is green! Now, I'm ready for the review!!

@atusy atusy marked this pull request as ready for review September 23, 2021 15:15
Copy link
Owner

@yihui yihui left a 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/hooks-md.R Outdated Show resolved Hide resolved
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])) {
Copy link
Owner

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?

Copy link
Collaborator Author

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.

https://github.com/yihui/knitr/pull/2047/files#diff-92c9f6892b4000a9291d262dbbdb4519edfc0437c89e7eeb686d84c1ee13119eL402-R417

Copy link
Owner

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!

@yihui yihui added the next Issues/PRs considered for the next release label Sep 24, 2021
@atusy
Copy link
Collaborator Author

atusy commented Sep 25, 2021

@yihui Thanks for the review! I updated the codes based on it.

Copy link
Owner

@yihui yihui left a 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.

@yihui yihui merged commit 0eaadad into yihui:master Sep 27, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
next Issues/PRs considered for the next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

knit() cannot handle variable numbers of backticks
2 participants