diff --git a/site/generate_reference.R b/site/generate_reference.R
index 8a070a741..5b90c41fa 100644
--- a/site/generate_reference.R
+++ b/site/generate_reference.R
@@ -2,6 +2,7 @@
# Generate individual reference pages from .Rd files
library(tools)
+library(xml2)
# Create reference directory
ref_dir = "site/reference"
@@ -34,57 +35,82 @@ get_rd_info = function(rd_file) {
# Generate HTML for each .Rd file
cat("Generating individual reference pages...\n")
-all_info = list()
+
+all_info = lapply(setNames(nm = rd_files), get_rd_info)
+topic_links = character()
+for (topic in all_info) topic_links[topic$aliases] = paste0('reference/', topic$name, '.html')
+
+html_meta = xfun::yaml_load(readLines('site/_litedown.yml'))$output$html$meta
for (rd_file in rd_files) {
- info = get_rd_info(rd_file)
- all_info[[length(all_info) + 1]] <- info
+ info = all_info[[rd_file]]
out_file = file.path(ref_dir, paste0(info$name, ".html"))
temp_html = tempfile(fileext = ".html")
- Rd2HTML(rd_file, out = temp_html, package = "data.table")
-
- html_content = readLines(temp_html, warn = FALSE)
- navbar_html = readLines("site/_navbar.html", warn = FALSE)
- navbar_html = gsub('href="index.html"', 'href="../index.html"', navbar_html)
- navbar_html = gsub('href="news.html"', 'href="../news.html"', navbar_html)
- navbar_html = gsub('href="manual.html"', 'href="../manual.html"', navbar_html)
- navbar_html = gsub('href="articles/', 'href="../articles/', navbar_html)
- navbar_html = gsub('href="assets/', 'href="../assets/', navbar_html)
-
- wrapped_html = c(
- '',
- '',
- '
',
- '',
- '',
- '',
- sprintf('%s — %s • data.table', paste(info$aliases, collapse = ", "), info$title),
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- navbar_html,
- '',
- html_content,
- '
',
- '',
- '',
- ''
- )
-
- writeLines(wrapped_html, out_file)
+ Rd2HTML(rd_file, out = temp_html, package = "data.table", Links = topic_links, Links2 = character())
+ html_root = read_html(temp_html)
+
+ if (!is.null(html_meta$lang))
+ xml_attr(html_root, 'lang') = html_meta$lang
+
+ html_title = xml_find_first(html_root, '/html/head/title')
+ xml_text(html_title) <- sprintf('%s — %s • data.table', paste(info$aliases, collapse = ", "), info$title)
+
+ html_head = xml_find_first(html_root, '//head')
+
+ if (!is.null(add_head <- html_meta$header_includes)) {
+ add_head = xml_find_first(read_html(paste0('', add_head, '')), '//head')
+ for (tag in xml_children(add_head))
+ xml_add_child(html_head, tag)
+ }
+
+ for (css in html_meta$css2) {
+ link = xml_add_child(html_head, "link")
+ xml_set_attrs(link, list(
+ rel = "stylesheet",
+ href = css
+ ))
+ }
+
+ for (js in html_meta$js2) {
+ script = xml_add_child(html_head, "script")
+ xml_set_attr(script, 'src', js)
+ }
+
+ body = xml_find_first(html_root, '//body')
+ new_body = xml_add_child(html_root, 'body')
+ if (!is.null(include <- html_meta$include_before)) {
+ include = read_html(file.path('site', include))
+ include = xml_find_first(include, '//body')
+ for (node in xml_children(include))
+ xml_add_child(new_body, node)
+ }
+
+ new_body_div = xml_add_child(new_body, 'div')
+ xml_set_attr(new_body_div, 'class', 'body')
+ for (node in xml_children(body))
+ xml_add_child(new_body_div, node)
+
+ if (!is.null(include <- html_meta$include_after)) {
+ include = read_html(include)
+ include = xml_find_first(include, '//body')
+ for (node in xml_children(include))
+ xml_add_child(new_body, node)
+ }
+
+ xml_remove(body)
+
+ for (link in xml_find_all(html_root, '//a|//link')) {
+ href = xml_attr(link, 'href')
+ if (!is.na(href))
+ if (startsWith(href, '../../')) # \link[package]{topic}
+ xml_attr(link, 'href') <- NULL
+ else if (!grepl('^https?://', href))
+ xml_attr(link, 'href') <- paste0('../', href)
+ }
+
+ write_html(html_root, out_file)
unlink(temp_html)
cat(sprintf(" Generated %s\n", out_file))