Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions site/generate_reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

library(tools)
library(xml2)
library(data.table)
Comment thread
jangorecki marked this conversation as resolved.
Resolved

# Create reference directory
ref_dir = "site/reference"
Expand Down Expand Up @@ -48,8 +49,23 @@ for (rd_file in rd_files) {
out_file = file.path(ref_dir, paste0(info$name, ".html"))

temp_html = tempfile(fileext = ".html")
Rd2HTML(rd_file, out = temp_html, package = "data.table", Links = topic_links, Links2 = character())
html_root = read_html(temp_html)
Rd2HTML(rd_file, out = temp_html, package = "data.table", Links = topic_links, Links2 = character(), Rhtml = TRUE)

# The <!--begin.rcode ... end.rcode--> blocks are unsafe to parse as HTML, as an --> inside R code would terminate the comment.
env = new.env()
html_text = readLines(temp_html)
begin.rcode = which(startsWith(html_text, "<!--begin.rcode"))

@MichaelChirico MichaelChirico Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I don't see this documented in ?Rd2HTML -- should we add a defensive check here to alert us if some internals change upstream that break our assumption on how to process the result?

The current stopifnot() might suffice... anything more we can assert?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These "rcode" blocks follow the knitr convention for *.Rhtml documents, see the source of the example R HTML vignette. The documentation is at https://yihui.org/knitr/patterns/, but the patterns themselves live in code:

> knitr::all_patterns$html
$chunk.begin
[1] "^\\s*<!--\\s*begin.rcode\\s*(.*)"

$chunk.end
[1] "^\\s*end.rcode\\s*-->"

$ref.chunk
[1] "^\\s*<<(.+)>>\\s*$"

$inline.code
[1] "<!--\\s*rinline(.+?)-->"

$header.begin
[1] "\\s*<head>"

I can adjust our script to use the exact patterns from knitr.

end.rcode = which(html_text == "end.rcode-->")
stopifnot(length(begin.rcode) == length(end.rcode), begin.rcode < end.rcode)
for (i in seq_along(begin.rcode)) {
text = html_text[begin.rcode[i]:end.rcode[i]]
text[1] = sub('^<!--begin[.]rcode(.*)', '```{r}\n#| \\1', text[1])
text[length(text)] = '```'
html_text[begin.rcode[i]] = litedown::fuse(text = text, envir = env)
html_text[(begin.rcode[i]+1):end.rcode[i]] = ""
}

html_root = read_html(paste(html_text, collapse = '\n'))

if (!is.null(html_meta$lang))
xml_attr(html_root, 'lang') = html_meta$lang
Expand Down
Loading