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

Chunk code is not highlighted when comment = NA #1296

Closed
Eluvias opened this issue Apr 11, 2020 · 15 comments
Closed

Chunk code is not highlighted when comment = NA #1296

Eluvias opened this issue Apr 11, 2020 · 15 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@Eluvias
Copy link

Eluvias commented Apr 11, 2020

Changing the default value of the comment setting will remove chunk's highlighting.

reprex

temp <- tempfile()
usethis::create_package(temp, rstudio = FALSE, open = FALSE)
usethis::proj_activate(temp)
usethis::use_article("test")
writeLines("---
title: test
output:
  rmarkdown::html_vignette:
vignette: >
 %\\VignetteIndexEntry{test}
 %\\VignetteEngine{knitr::rmarkdown}
 %\\VignetteEncoding{UTF-8}
---
 ```{r setup}

knitr::opts_chunk$set(
  collapse = TRUE,
  cache = FALSE,
  comment = NA
)
 ```
```{r OK}

x <- data.frame(a = 1:4, b = 5:8)

y <- list(NULL)
```

```{r NOTOK}

x <- data.frame(a = 1:4, b = 5:8)

y <- list(NULL)

x
```
 ", 
con = "vignettes/test.Rmd")
pkgdown::build_site()

outputs

image

image

comment = "#" works:

image

@jayhesselberth

This comment has been minimized.

@Eluvias

This comment has been minimized.

@jayhesselberth

This comment has been minimized.

@Eluvias

This comment has been minimized.

@Eluvias

This comment has been minimized.

@Eluvias
Copy link
Author

Eluvias commented Apr 13, 2020

@jayhesselberth here are the differences:

v1.5.1

image

v1.4.1

image

knitr

image

@jayhesselberth
Copy link
Collaborator

Sorry, I see the issue now, I was focused on the comments themselves.

@jayhesselberth jayhesselberth changed the title vignette chunk setting: 'comment' Chunk is not highlighted when comment = NA Apr 13, 2020
@jayhesselberth jayhesselberth added the bug an unexpected problem or unintended behavior label Apr 13, 2020
@jayhesselberth
Copy link
Collaborator

Might be related to changes in f49ffa5

@jayhesselberth jayhesselberth changed the title Chunk is not highlighted when comment = NA Chunk code is not highlighted when comment = NA Apr 13, 2020
@wkmor1
Copy link
Contributor

wkmor1 commented Apr 22, 2020

Here is another minimal example:

package.skeleton("x")
dir.create("x/vignettes")
cat("```r\n#\n```", file = "x/vignettes/x.Rmd")
pkgdown::build_article("x", "x")
html <- xml2::read_html("x/docs/articles/x.html")
src <- xml2::xml_find_first(html, ".//div[contains(@class, 'sourceCode')]")
as.character(src)

under 1.4.1 this would return

[1] "<div class=\"sourceCode\" id=\"cb1\"><pre class=\"sourceCode r\"><code class=\"sourceCode r\"><a class=\"sourceLine\" id=\"cb1-1\" data-line-number=\"1\"><span class=\"co\">#</span></a></code></pre></div>"

whereas as of f49ffa5 it returns

[1] "<div class=\"sourceCode\" id=\"cb1\"><pre class=\"r\">#</pre></div>"

This function

pkgdown/R/highlight.r

Lines 4 to 28 in aea6864

highlight_text <- function(text) {
stopifnot(is.character(text), length(text) == 1)
text <- gsub("\r", "", text)
expr <- tryCatch(
parse(text = text, keep.source = TRUE),
error = function(e) NULL
)
# Failed to parse, or yielded empty expression
if (length(expr) == 0) {
return(escape_html(text))
}
packages <- extract_package_attach(expr)
register_attached_packages(packages)
out <- highlight::highlight(
parse.output = expr,
renderer = pkgdown_renderer(),
detective = pkgdown_detective,
output = NULL
)
paste0(out, collapse = "")
}
is stripping the markup that the syntax highlighting css would target when parsing the R code produces an empty expression.

Note that the behaviour is not just triggered if the comments are changed with opt_chunks$set but will happen to any code block that consists of comments only (such as in the reprex above) for example for all output code chunks when collapse = FALSE

jayhesselberth pushed a commit that referenced this issue Apr 23, 2020
Preserves highlight markup in code consisting entirely
of code comments (lines beginning with #'s).

Part of #1296
@jayhesselberth
Copy link
Collaborator

Highlighting of empty expressions is fixed.

I don't think we should encourage use of comment = NA or collapse = TRUE as it leads to a syntax error when copy / pasted.

@Eluvias
Copy link
Author

Eluvias commented Apr 23, 2020

I still see the problem.
Can you please confirm that is fixed by running the reprex at the very top?

pkgdown 1.5.1.9000 2020-04-23 [1] Github (r-lib/pkgdown@3b43ddb)

image

@wkmor1
Copy link
Contributor

wkmor1 commented Apr 24, 2020

@Eluvias Unfortunately #1307 wasn't really supposed to fix these cases. Only cases that produced empty expressions (e.g., comments only) rather than syntax errors. In any case it looks like #1307 is causing problems elsewhere so wont be making it in at this stage anyway.

wkmor1 added a commit to wkmor1/pkgdown that referenced this issue Apr 24, 2020
Improved version of r-lib#1307 accounting for problems caused by empty expressions consisting of empty
strings or whitespace only.

Prevents highlight markup from being stripped from code that consists entirely of code comments (only
lines beginning with #'s).

A partial fix for r-lib#1296, but would still not syntax
highlight code chunks that resulted in syntax
errors (e.g., when knitr chunk options collapse =
TRUE and comment = NA are set)
wkmor1 added a commit to wkmor1/pkgdown that referenced this issue Apr 24, 2020
Improved version of r-lib#1307 accounting for problems caused by empty expressions consisting of empty
strings or whitespace only.

Prevents highlight markup from being stripped from code that consists entirely of code comments (only
lines beginning with #'s).

A partial fix for r-lib#1296, but would still not syntax
highlight code chunks that resulted in syntax
errors (e.g., when knitr chunk options collapse =
TRUE and comment = NA are set)
@hadley
Copy link
Member

hadley commented May 12, 2020

This is technically a change in behaviour, but I don't think it's a bug. We now use R itself to do the syntax highlighting, so we can only syntax highlight valid R code.

@hadley hadley closed this as completed May 12, 2020
@wkmor1
Copy link
Contributor

wkmor1 commented May 13, 2020

@hadley, Agreed. But code that is just # commented lines is valid (right?) and this change has stopped highlighting such comment only blocks. I feel like I might have muddied the waters a bit conflating what are possibly two separate issues, albeit arising from the same change. I can open an new issue if needed or just leave you with #1310 to decide?

@hadley
Copy link
Member

hadley commented May 13, 2020

@wkmor1 I'd recommend opening a new issue with a minimal reprex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants