diff options
Diffstat (limited to 'nuldoc-src/commands')
| -rw-r--r-- | nuldoc-src/commands/build.ts | 145 |
1 files changed, 42 insertions, 103 deletions
diff --git a/nuldoc-src/commands/build.ts b/nuldoc-src/commands/build.ts index 90aaab9..ef529e0 100644 --- a/nuldoc-src/commands/build.ts +++ b/nuldoc-src/commands/build.ts @@ -3,27 +3,32 @@ 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 { Document } from "../docbook/document.ts"; import { Page } from "../page.ts"; import { render } from "../render.ts"; -import convertPost from "../templates/post.ts"; -import convertPostList from "../templates/post_list.ts"; -import convertTag from "../templates/tag.ts"; -import generateAbout from "../templates/about.ts"; +import { generateAboutPage } from "../pages/about.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 generatePosts(config); - await generateTags(posts, config); - await generatePostList(posts, config); - await generateAboutPage(config); + const posts = await buildPostPages(config); + await buildPostListPage(posts, config); + await buildTagPages(posts, config); + await buildAboutPage(config); await copyStaticFiles(config); } -async function generatePosts(config: 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); - await outputPosts(posts, config); + for (const post of posts) { + await writePage(post, config); + } return posts; } @@ -39,70 +44,35 @@ async function collectPostFiles(sourceDir: string): Promise<string[]> { async function parsePosts( postFiles: string[], config: Config, -): Promise<Document[]> { +): Promise<PostPage[]> { const posts = []; for (const postFile of postFiles) { posts.push( - await convertPost(await parseDocBookFile(postFile, config), config), + await generatePostPage(await parseDocBookFile(postFile, config), config), ); } return posts; } -async function outputPosts(posts: Document[], config: Config) { - const cwd = Deno.cwd(); - const contentDir = join(cwd, config.locations.contentDir); - const destDir = join(cwd, config.locations.destDir); - for (const post of posts) { - const destFilePath = join( - post.sourceFilePath.replace(contentDir, destDir).replace(".xml", ""), - "index.html", - ); - await ensureDir(dirname(destFilePath)); - await writePage(docToPage(post, destFilePath)); - } -} - -async function generatePostList(posts: Document[], config: Config) { - const postList = await buildPostListDoc(posts, config); - await outputPostList(postList, config); -} - -async function buildPostListDoc( - posts: Document[], - config: Config, -): Promise<Document> { - return await convertPostList(posts, config); -} - -async function outputPostList(postList: Document, config: Config) { - const cwd = Deno.cwd(); - const destDir = join(cwd, config.locations.destDir); - const destFilePath = join(destDir, "posts", "index.html"); - await ensureDir(dirname(destFilePath)); - await writePage(docToPage(postList, destFilePath)); -} - -async function generateAboutPage(config: Config) { - const aboutDoc = await generateAbout(config); - await outputAboutPage(aboutDoc, config); +async function buildPostListPage(posts: PostPage[], config: Config) { + const postListPage = await generatePostListPage(posts, config); + await writePage(postListPage, config); } -async function outputAboutPage(about: Document, config: Config) { - const cwd = Deno.cwd(); - const destDir = join(cwd, config.locations.destDir); - const destFilePath = join(destDir, "about", "index.html"); - await ensureDir(dirname(destFilePath)); - await writePage(docToPage(about, destFilePath)); +async function buildAboutPage(config: Config) { + const aboutPage = await generateAboutPage(config); + await writePage(aboutPage, config); } -async function generateTags(posts: Document[], config: Config) { +async function buildTagPages(posts: PostPage[], config: Config) { const tagsAndPosts = collectTags(posts); - const tagDocs = await buildTagDocs(tagsAndPosts, config); - await outputTags(tagDocs, config); + for (const [tag, posts] of tagsAndPosts) { + const tagPage = await generateTagPage(tag, posts, config); + await writePage(tagPage, config); + } } -function collectTags(posts: Document[]): [string, Document[]][] { +function collectTags(posts: PostPage[]): [string, PostPage[]][] { const tagsAndPosts = new Map(); for (const post of posts) { for (const tag of post.tags) { @@ -113,13 +83,13 @@ function collectTags(posts: Document[]): [string, Document[]][] { } } - const result: [string, Document[]][] = []; + const result: [string, PostPage[]][] = []; for (const tag of Array.from(tagsAndPosts.keys()).sort()) { result.push([ tag, - tagsAndPosts.get(tag).sort((a: Document, b: Document) => { - const ta = a.getCreatedDate(); - const tb = b.getCreatedDate(); + 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; @@ -129,35 +99,6 @@ function collectTags(posts: Document[]): [string, Document[]][] { return result; } -async function buildTagDocs( - tagsAndPosts: [string, Document[]][], - config: Config, -): Promise<[string, Document][]> { - const docs: [string, Document][] = []; - for (const [tag, posts] of tagsAndPosts) { - docs.push([tag, await buildTagDoc(tag, posts, config)]); - } - return docs; -} - -async function buildTagDoc( - tag: string, - posts: Document[], - config: Config, -): Promise<Document> { - return await convertTag(tag, posts, config); -} - -async function outputTags(tagDocs: [string, Document][], config: Config) { - const cwd = Deno.cwd(); - const destDir = join(cwd, config.locations.destDir); - for (const [tag, tagDoc] of tagDocs) { - const destFilePath = join(destDir, "tags", tag, "index.html"); - await ensureDir(dirname(destFilePath)); - await writePage(docToPage(tagDoc, destFilePath)); - } -} - async function copyStaticFiles(config: Config) { const globPattern = joinGlobs([Deno.cwd(), config.locations.staticDir, "*"]); for await (const entry of expandGlob(globPattern)) { @@ -170,14 +111,12 @@ async function copyStaticFiles(config: Config) { } } -async function writePage(page: Page) { - await Deno.writeTextFile(page.destFilePath, render(page.root, page.renderer)); -} - -function docToPage(d: Document, p: string): Page { - return { - root: d.root, - renderer: "html", - destFilePath: p, - }; +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)); } |
