import { parse } from 'https://deno.land/std/path/mod.ts';
import { ensureDir } from "https://deno.land/std/fs/mod.ts";
import { getCredentials } from './get-credentials.ts';
import { getContentLoc } from './get-content-loc.ts';
import { getRefPaths } from './get-ref-paths.ts';
import { fetchFromWebspace } from './fetch-from-webspace.ts';
import { fetchContentFromWebspace } from './fetch-content-from-webspace.ts';
import { getDoc } from './get-doc.ts';
import { mkNavs } from './mk-navs.ts';
import { docToString } from './doc-to-string.ts';
import { targetDir } from './target-dir.ts';
import { ensureDirOfPath } from './ensure-dir-of-path.ts';
import { domParser } from "./dom-parser.ts";
const root_target = targetDir('/index.html');
const ensure_root_dir_promise = ensureDirOfPath(root_target);
const credentials = await getCredentials();
const pubignore = await fetchFromWebspace('/pubignore.json', credentials)
.then(response => response.json());
Deno.writeTextFile('.pubignore', pubignore.join('\n'));
const root_doc = await getDoc('/index.html', credentials);
ensure_root_dir_promise.then(
_ => Deno.writeTextFile(root_target, docToString(root_doc))
);
const insertNav = mkNavs(root_doc);
const mathjax_as_text = await Deno.readTextFile('util/mathjax.html');
const mathjax_script_tags = domParser.parseFromString(
mathjax_as_text, 'text/html'
).querySelectorAll('script');
const unreferenced_promise = Deno.readTextFile('unreferenced.json')
.then(JSON.parse);
const process = async function(processing_started, to_process) {
const path = to_process.pop();
if (path) {
const target = targetDir(path);
const ensure_dir_promise = ensureDirOfPath(target);
processing_started.push(path);
if (path.endsWith('.html')) {
const doc = await getDoc(path, credentials);
const head = doc.querySelector('head');
const meta_viewport = doc.createElement('meta');
meta_viewport.setAttribute('name', 'viewport');
if (getContentLoc(path) === 'webspace') {
// Remove Redundant 'div'
doc.querySelector('div.titlepage')?.remove();
// Fix Character Encoding
doc.querySelector('meta').remove();
const meta = doc.createElement('meta');
meta.setAttribute('charset', 'utf-8');
head.appendChild(meta);
// Remove Attributes set by Docbook
const body = doc.querySelector('body');
['vlink', 'text', 'link', 'bgcolor', 'alink']
.forEach(attr => body.removeAttribute(attr));
// Add global stylesheet
const link_global_stylesheet = doc.createElement('link');
link_global_stylesheet.setAttribute('rel', 'stylesheet');
link_global_stylesheet.setAttribute('href', '/global.css');
head.appendChild(link_global_stylesheet);
// Add Docbook specific stylesheet
const link_docbook_stylesheet = doc.createElement('link');
link_docbook_stylesheet.setAttribute('rel', 'stylesheet');
link_docbook_stylesheet.setAttribute(
'href', '/notes/docbook-output.css');
head.appendChild(link_docbook_stylesheet);
// Swap Mathjax script tags
[...doc.querySelectorAll('script')]
.forEach(script_tag => script_tag.remove());
head.appendChild(mathjax_script_tags[0]);
head.appendChild(mathjax_script_tags[1]);
// Viewport
meta_viewport.setAttribute(
'content', 'width=743'
);
} else {
meta_viewport.setAttribute(
'content', 'width=device-width, initial-scale=1'
);
}
head.appendChild(meta_viewport);
const ref_paths = getRefPaths(
pubignore, processing_started.concat(to_process), path, doc
);
process(processing_started, to_process.concat(ref_paths));
doc.documentElement.setAttribute('lang', 'en');
insertNav(path, doc);
await ensure_dir_promise;
Deno.writeTextFile(target, docToString(doc));
} else {
process(processing_started, to_process);
if (getContentLoc(path) === 'src') {
await ensure_dir_promise;
Deno.copyFile('src' + path, target);
} else {
const content_buffer =
await fetchContentFromWebspace(path, credentials)
.then(response => response.blob())
.then(blob => blob.arrayBuffer());
await ensure_dir_promise;
Deno.writeFile(target, new Uint8Array(content_buffer));
}
}
}
};
const processing_started = ['/index.html'];
const unreferenced = await unreferenced_promise;
const to_process = getRefPaths(
pubignore, processing_started, '/index.html', root_doc
).concat(unreferenced);
process(processing_started, to_process);