~bfluhr/website

ref: 5fc0873a6abd1c066995fea827788c7a7ca2d694 website/util/build.ts -rw-r--r-- 4.4 KiB
5fc0873a — Benedikt Fluhr Not Setting 'initial-scale' on Docbook Output 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);