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

highlight Rd code blocks according to their language #1700

Closed
wants to merge 6 commits into from

Conversation

maelle
Copy link
Collaborator

@maelle maelle commented May 24, 2021

Fix #1690
Fix #1692

It feels a bit clunky for now, but the result looks much better. 😅


non_r_blocks <- xml2::xml_find_all(
html,
"//div[contains(@class, 'sourceCode') and not(contains(@class, 'sourceCode r'))]"
Copy link
Collaborator Author

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(
Copy link
Collaborator Author

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(
Copy link
Collaborator Author

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)) {
Copy link
Collaborator Author

@maelle maelle May 24, 2021

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?

Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

Copy link
Collaborator Author

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. 🤔

@maelle maelle marked this pull request as ready for review May 24, 2021 11:16
@maelle maelle requested a review from hadley May 24, 2021 11:17
@hadley
Copy link
Member

hadley commented Jun 2, 2021

Why is this pkgdown's responsibility and not downlit's?

@maelle
Copy link
Collaborator Author

maelle commented Jun 3, 2021

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?

@hadley
Copy link
Member

hadley commented Jun 15, 2021

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.

@maelle
Copy link
Collaborator Author

maelle commented Jun 18, 2021

This is an excellent idea! I opened a new PR that feels much less clunky!

@maelle maelle closed this Jun 18, 2021
@hadley hadley deleted the highlight-later branch October 19, 2021 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants