-
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
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import * as log from "../deps/deno.land/std/log/mod.ts";
import * as cli from "../deps/deno.land/std/cli/mod.ts";
import * as colors from "../deps/deno.land/std/fmt/colors.ts";
import { DenoFsReader } from "../filesystem_reader/deno_fs.ts";
import { DenoFsWriter } from "../filesystem_writer/deno_fs.ts";
import {
DefaultTreeBuilder,
fileExtensions,
ignoreDotfiles,
langDir,
removeExtFromMetadata,
} from "../tree_builder/default_tree_builder.ts";
import { ObsidianMarkdownParser } from "../content_parser/obsidian_markdown.ts";
import { JSONCanvasParser } from "../content_parser/json_canvas.ts";
import { oneof } from "../content_parser/oneof.ts";
import { DefaultThemeBuilder } from "../page_builder/default_theme/mod.ts";
export async function build() {
const outDir = new URL("./.dist", import.meta.url);
await Deno.permissions.request({
name: "write",
path: outDir,
});
const srcDir = new URL(".", import.meta.url);
await Deno.permissions.request({
name: "read",
path: srcDir,
});
await Deno.mkdir(outDir, { recursive: true });
const fileSystemReader = new DenoFsReader(srcDir);
const fileSystemWriter = new DenoFsWriter(outDir);
const treeBuilder = new DefaultTreeBuilder({
defaultLanguage: "en",
ignore: [ignoreDotfiles],
strategies: [
fileExtensions([".md", ".canvas"]),
removeExtFromMetadata(),
langDir({
en: "English",
ja: "日本語",
}, true),
],
resolveShortestPathWhenPossible: true,
});
const contentParser = oneof(
new JSONCanvasParser(),
new ObsidianMarkdownParser({ frontmatter: true }),
);
const pageBuilder = new DefaultThemeBuilder({
siteName: "Macana",
copyright: "© 2024 Shota FUJI. This document is licensed under CC BY 4.0",
faviconSvg: ["Assets", "logo.svg"],
faviconPng: ["Assets", "logo-64x64.png"],
siteLogo: ["Assets", "logo.svg"],
});
const documentTree = await treeBuilder.build({
fileSystemReader,
contentParser,
});
await pageBuilder.build({
documentTree,
fileSystemReader,
fileSystemWriter,
});
}
function prettyLogFormatter(
useColors: boolean,
verbose: boolean,
): log.FormatterFunction {
function id<T>(x: T): T {
return x;
}
function pipe<T>(a: (x: T) => T, b: (x: T) => T): (x: T) => T {
return (x) => b(a(x));
}
const ts = useColors ? pipe(colors.black, colors.dim) : id;
return (record) => {
let level: (str: string) => string = id;
let msg: (str: string) => string = id;
let payload: (str: string) => string = id;
if (useColors) {
switch (record.level) {
case log.LogLevels.NOTSET:
level = colors.dim;
msg = colors.dim;
payload = colors.dim;
break;
case log.LogLevels.DEBUG:
level = colors.gray;
msg = colors.gray;
payload = pipe(colors.dim, colors.black);
break;
case log.LogLevels.INFO:
level = colors.blue;
msg = pipe(colors.bold, colors.black);
payload = colors.black;
break;
case log.LogLevels.WARN:
level = colors.yellow;
msg = colors.black;
payload = colors.black;
break;
case log.LogLevels.ERROR:
level = colors.red;
msg = colors.black;
payload = colors.black;
break;
case log.LogLevels.CRITICAL:
level = pipe(colors.red, colors.bold);
msg = pipe(colors.bold, colors.red);
payload = colors.red;
break;
}
}
let ret = "\n" + level(record.levelName) + " " +
ts(record.datetime.toISOString()) + "\n" +
msg(record.msg);
if (verbose) {
for (const arg of record.args) {
const text = Deno.inspect(arg, {
colors: useColors,
compact: false,
});
for (const line of text.split("\n")) {
ret += "\n " + payload(line);
}
}
}
return ret;
};
}
if (import.meta.main) {
const args = cli.parseArgs(Deno.args, {
string: ["log"],
boolean: ["json", "verbose", "help"],
alias: {
"help": ["h"],
},
});
const useColors = Deno.stdout.isTerminal();
if (args.help) {
const title = useColors
? (s: string) => colors.underline(colors.bold(s))
: (s: string) => s;
console.log(`
docs/build.ts
Build Macana's documentation website using Macana.
${title("Usage")}:
deno run --allow-read=docs --allow-write=docs/.dist docs/build.ts [OPTIONS]
${title("Options")}:
-h, --help
Print this help text to stdout and exit.
--log=<debug|info|warn|error|critical>
Set the lowest log level to output.
By default, docs/build.ts logs INFO, WARN, ERROR, and CRITICAL level logs.
--json
Output logs as JSON Lines.
--verbose
Output additional information alongisde log messages.
This does not take effect when ${colors.bold("--json")} is set.
`.trim());
Deno.exit(0);
}
let level: log.LevelName = "INFO";
if (args.log) {
const upper = args.log.toUpperCase() as log.LevelName;
const found = log.LogLevelNames.includes(upper);
if (found) {
level = upper;
} else {
log.critical(
`"${args.log}" is not a valid log level.\n` +
` Available log levels are: ${log.LogLevelNames.join(", ")}`,
);
Deno.exit(1);
}
}
log.setup({
handlers: {
default: new log.ConsoleHandler(level, {
formatter: args.json
? log.jsonFormatter
: prettyLogFormatter(useColors, args.verbose),
useColors,
}),
},
loggers: {
macana: {
level: "DEBUG",
handlers: ["default"],
},
},
});
try {
const start = performance.now();
await build();
const duration = performance.now() - start;
log.info(`Complete docs build, elapsed ${duration}ms`, {
duration,
});
} catch (error) {
log.critical(`Build aborted due to an error: ${error}`, {
error,
});
Deno.exit(1);
}
}