11const fs = require ( "fs" ) ;
22const 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-
84if ( 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+ */
9193function 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 ( / i n d e x \. h t m l / g, "" ) ;
95- html = html . replace ( / \. h t m l / 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 ( / \. h t m l / 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 ( / h i p s y c l : : s y c l / g, "sycl" ) ;
118+ }
119+
109120function 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