-
Notifications
You must be signed in to change notification settings - Fork 334
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
highlight Rd code blocks according to their language #1700
Conversation
|
||
non_r_blocks <- xml2::xml_find_all( | ||
html, | ||
"//div[contains(@class, 'sourceCode') and not(contains(@class, 'sourceCode r'))]" |
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.
the XPath query is different because we need to keep the div until we extract the language.
highlight_other_block <- function(block) { | ||
lang <- sub("sourceCode ", "", xml2::xml_attr(block, "class")) | ||
code <- xml2::xml_text(xml2::xml_find_first(block, "pre/code")) | ||
highlighted <- markdown_text( |
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.
a new use case for markdown_text()
😁 In that function pkg
is needed for finding external links but in code we do not need to try and identify them I think (and passing pkg
all the way to here would warrant more refactoring).
pkg = NULL | ||
) | ||
code_node <- xml2::xml_find_first(block, "pre/code") | ||
xml2::xml_text(code_node) <- paste( |
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.
passing stuff via the text is clunky because it gets escaped. But as the Rd transformation creates HTML as text, I couldn't find a better idea, and unescaped HTML at the very end of the code I added.
xml2::xml_text(block), | ||
classes = downlit::classes_pandoc() | ||
) | ||
if (!is.na(out)) { |
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.
The case where out
is NA probably only happens when the code is not valid R code (e.g. a YAML block with no language information) but I suppose it could be some sort of pseudo-code, so hard to warn?
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.
Or there could be a warning, and the workaround for non-valid pseudo-code would be to use
```pseudor
bla
```
@@ -74,4 +74,5 @@ Roxygen: list(markdown = TRUE) | |||
RoxygenNote: 7.1.1.9001 | |||
SystemRequirements: pandoc | |||
Remotes: | |||
r-lib/downlit | |||
r-lib/downlit, | |||
r-lib/roxygen2 |
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.
Depends on r-lib/roxygen2#1229
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.
Maybe I should make it more general. 🤔
Why is this pkgdown's responsibility and not downlit's? |
You mean pkgdown could pass the code, and code language information, to downlit? And downlit would use Pandoc via rmarkdown for language other than R/undefined? |
I think this would be cleaner if the Rd parsing code simply left preformatted code blocks alone, and then we did this syntax highlighting when we tweak the generated html. I don't think we want xml munging code in the Rd parsing code. |
This is an excellent idea! I opened a new PR that feels much less clunky! |
Fix #1690
Fix #1692
It feels a bit clunky for now, but the result looks much better. 😅