Skip to content

Commit 689f5f6

Browse files
committed
Fix/improve docs import script
- Fix edge cases in handling URLs - Replace "hipsycl::sycl" with "sycl" namespace
1 parent dfb970f commit 689f5f6

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/import_api_docs.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
const fs = require("fs");
22
const path = require("path");
33

4-
// TODO: Change so we copy files from a source directory to a destination and only include what we actually need
5-
// TODO: Do we actually need the api_docs.js file? Or can we just write it inline?
6-
// TODO: Write a _category_.json file for each subdirectory
7-
84
if (process.argv[2] == null) {
95
console.error(
106
`Usage: node ${path.basename(process.argv[1])} <input directory>`
@@ -88,24 +84,39 @@ function parseTitle(html) {
8884
return title;
8985
}
9086

87+
/**
88+
* The URLs generated by leafy-green-doc are not directly compatible with our setup,
89+
* so we need to do some post-processing.
90+
*
91+
* FIXME: We should have options to do this in leafy-green-doc
92+
*/
9193
function fixUrls(html, allDirs) {
92-
// We have to remove links to `index.html` as well as all trailing `.html` to make it work with Docusaurus
93-
// FIXME: This should be done during generation instead
94-
html = html.replace(/index\.html/g, "");
95-
html = html.replace(/\.html/g, "");
96-
// We also have to replace all relative "../" links with "../../", since in Docusaurus each file becomes a subdirectory in the URL
97-
// FIXME: We should have an option to generate all URLs relative to some base URL
98-
9994
for (const subdir of allDirs) {
95+
// Replace all links to category index pages with the category directory
96+
html = html.replace(
97+
new RegExp(`${subdir}\/index\.html`, "g"),
98+
`${subdir}/`
99+
);
100+
// Replace all relative "../" links with absolute paths
100101
html = html.replace(
101-
new RegExp(`"\.\.\/${subdir}/`, "g"),
102-
`"${baseUrl}/${subdir}/`
102+
new RegExp(`\.\.\/${subdir}/`, "g"),
103+
`${baseUrl}/${subdir}/`
103104
);
104105
}
105106

107+
// Finally, replace all ".html" extensions with a slash
108+
html = html.replace(/\.html/g, "/");
109+
106110
return html;
107111
}
108112

113+
// Since AdaptiveCpp (formerly hipSYCL) aliases the sycl namespace to hipsycl::sycl,
114+
// we have to replace it in the generated HTML files.
115+
// TODO: We should have an option to do this in leafy-green-doc
116+
function replaceSyclNamespace(html) {
117+
return html.replace(/hipsycl::sycl/g, "sycl");
118+
}
119+
109120
function processSubdir(subdir, allDirs) {
110121
fs.mkdirSync(path.join(outputPath, subdir), { recursive: true });
111122
writeCategorySettings(subdir);
@@ -124,9 +135,11 @@ function processSubdir(subdir, allDirs) {
124135
const title = parseTitle(html);
125136
const withFixedUrls = fixUrls(html, allDirs);
126137

138+
const withSyclNamespace = replaceSyclNamespace(withFixedUrls);
139+
127140
fs.writeFileSync(
128141
path.join(outputPath, subdir, `${fileName}.html`),
129-
withFixedUrls
142+
withSyclNamespace
130143
);
131144

132145
writeMdx(subdir, fileName, title);

0 commit comments

Comments
 (0)