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
|
import { dirname, join, joinGlobs } from "std/path/mod.ts";
import { ensureDir } from "std/fs/mod.ts";
import { expandGlob } from "std/fs/expand_glob.ts";
import { Config } from "../config.ts";
import { parseDocBookFile } from "../docbook/parse.ts";
import { Page } from "../page.ts";
import { render } from "../render.ts";
import { generateAboutPage } from "../pages/about.ts";
import { generateNotFoundPage } from "../pages/not_found.ts";
import {
generatePostPage,
getPostCreatedDate,
PostPage,
} from "../pages/post.ts";
import { generatePostListPage } from "../pages/post_list.ts";
import { generateTagPage } from "../pages/tag.ts";
export async function runBuildCommand(config: Config) {
const posts = await buildPostPages(config);
await buildPostListPage(posts, config);
await buildTagPages(posts, config);
await buildAboutPage(config);
await buildNotFoundPage(config);
await copyStaticFiles(config);
}
async function buildPostPages(config: Config) {
const sourceDir = join(Deno.cwd(), config.locations.contentDir, "posts");
const postFiles = await collectPostFiles(sourceDir);
const posts = await parsePosts(postFiles, config);
for (const post of posts) {
await writePage(post, config);
}
return posts;
}
async function collectPostFiles(sourceDir: string): Promise<string[]> {
const filePaths = [];
const globPattern = joinGlobs([sourceDir, "**", "*.xml"]);
for await (const entry of expandGlob(globPattern)) {
filePaths.push(entry.path);
}
return filePaths;
}
async function parsePosts(
postFiles: string[],
config: Config,
): Promise<PostPage[]> {
const posts = [];
for (const postFile of postFiles) {
posts.push(
await generatePostPage(await parseDocBookFile(postFile, config), config),
);
}
return posts;
}
async function buildPostListPage(posts: PostPage[], config: Config) {
const postListPage = await generatePostListPage(posts, config);
await writePage(postListPage, config);
}
async function buildAboutPage(config: Config) {
const aboutPage = await generateAboutPage(config);
await writePage(aboutPage, config);
}
async function buildNotFoundPage(config: Config) {
const notFoundPage = await generateNotFoundPage(config);
await writePage(notFoundPage, config);
}
async function buildTagPages(posts: PostPage[], config: Config) {
const tagsAndPosts = collectTags(posts);
for (const [tag, posts] of tagsAndPosts) {
const tagPage = await generateTagPage(tag, posts, config);
await writePage(tagPage, config);
}
}
function collectTags(posts: PostPage[]): [string, PostPage[]][] {
const tagsAndPosts = new Map();
for (const post of posts) {
for (const tag of post.tags) {
if (!tagsAndPosts.has(tag)) {
tagsAndPosts.set(tag, []);
}
tagsAndPosts.get(tag).push(post);
}
}
const result: [string, PostPage[]][] = [];
for (const tag of Array.from(tagsAndPosts.keys()).sort()) {
result.push([
tag,
tagsAndPosts.get(tag).sort((a: PostPage, b: PostPage) => {
const ta = getPostCreatedDate(a);
const tb = getPostCreatedDate(b);
if (ta > tb) return -1;
if (ta < tb) return 1;
return 0;
}),
]);
}
return result;
}
async function copyStaticFiles(config: Config) {
const globPattern = joinGlobs([Deno.cwd(), config.locations.staticDir, "*"]);
for await (const entry of expandGlob(globPattern)) {
const src = entry.path;
const dst = src.replace(
config.locations.staticDir,
config.locations.destDir,
);
await Deno.copyFile(src, dst);
}
}
async function writePage(page: Page, config: Config) {
const destFilePath = join(
Deno.cwd(),
config.locations.destDir,
page.destFilePath,
);
await ensureDir(dirname(destFilePath));
await Deno.writeTextFile(destFilePath, render(page.root, page.renderer));
}
|