+class PublishSystemFilesWebpackPlugin {
+ apply(compiler) {
+ compiler.hooks.done.tap(
+ 'Publish System Files Webpack Plugin',
+ (stats) => {
+ if (['production'].includes(stats.compilation.options.mode)) {
+ writeTOC();
+ }
+ }
+ );
+ }
+}
+
+function copySystemFile(filename) {
+ fs.copyFileSync(
+ path.join(__dirname, 'system-files', filename),
+ path.join(__dirname, 'ide', 'contents', filename)
+ );
+}
+
+function writePreamble(stream, iframe) {
+ stream.write('<?php require $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/common.php\'; start_session(); ?>');
+ stream.write('<!doctype html>');
+ stream.write('<html>');
+ stream.write('<?php include $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/head.php\'; ?>');
+ stream.write('<body>');
+ stream.write('<?php include $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/limbo.php\'; ?>');
+ stream.write('<div id="wrapper">');
+ stream.write('<?php include $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/header.php\'; ?>');
+ stream.write('<?php include $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/navigation-bar.php\'; ?>');
+ if (iframe) {
+ stream.write('<section id="main_content" class="iframe">');
+ } else {
+ stream.write('<section id="main_content">');
+ }
+}
+
+function writePostamble(stream) {
+ stream.write('</section>');
+ stream.write('<?php include $_SERVER[\'DOCUMENT_ROOT\'] . \'/common/footer.php\'; ?>');
+ stream.write('</div>');
+ stream.write('</body>');
+ stream.write('</html>');
+}
+
+function writeTOC() {
+ fs.mkdirSync(path.join(__dirname, 'ide', 'contents'));
+ copySystemFile('common.css');
+ copySystemFile('common.js');
+ const stream = fs.createWriteStream(path.join(__dirname, 'ide', 'contents', 'toc.php'));
+ writePreamble(stream);
+ stream.write('<ul>');
+ addTOCEntry(stream, 'USER-MANUAL');
+ addTOCEntry(stream, 'TUTORIAL');
+ addTOCEntry(stream, 'REFERENCE-MANUAL');
+ addTOCEntry(stream, 'IMPLEMENTATION-NOTES');
+ addTOCEntry(stream, 'BIBLIOGRAPHY');
+ addTOCEntry(stream, 'LICENSE');
+ stream.write('</ul>');
+ writePostamble(stream);
+ stream.end();
+}
+
+function addTOCEntry(tocStream, filename) {
+ let contents = fs.readFileSync(path.join(__dirname, 'system-files', filename), {encoding: 'utf8'});
+ contents = contents.replaceAll('___cssURL___', 'common.css');
+ contents = contents.replaceAll('___jsURL___', 'common.js');
+ contents = contents.replaceAll('___windowId___', -1);
+ fs.writeFileSync(path.join(__dirname, 'ide', 'contents', filename + '.html'), contents);
+ const stream = fs.createWriteStream(path.join(__dirname, 'ide', 'contents', filename + '.php'));
+ writePreamble(stream, true);
+ stream.write(`<iframe src="${filename + '.html'}"></iframe>`);
+ writePostamble(stream);
+ stream.end();
+ tocStream.write(`<li><a href="${filename + '.php'}">${filename}</a></li>`);
+}
+