aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-03-18 15:54:53 +0900
committernsfisis <nsfisis@gmail.com>2023-03-18 16:17:21 +0900
commit2428ea512cdbac4b86189c654814c5eeca54a704 (patch)
tree70fb4eab0d0b87155112f4370853dbde387fd021
parent2b50e1778b164e641c03c2e77176b6f47ca1e278 (diff)
downloadblog.nsfisis.dev-2428ea512cdbac4b86189c654814c5eeca54a704.tar.gz
blog.nsfisis.dev-2428ea512cdbac4b86189c654814c5eeca54a704.tar.zst
blog.nsfisis.dev-2428ea512cdbac4b86189c654814c5eeca54a704.zip
refactor: add Page type to represent single web page
-rw-r--r--nuldoc-src/commands/build.ts23
-rw-r--r--nuldoc-src/page.ts8
-rw-r--r--nuldoc-src/render.ts13
-rw-r--r--nuldoc-src/renderers/html.ts (renamed from nuldoc-src/html.ts)19
4 files changed, 46 insertions, 17 deletions
diff --git a/nuldoc-src/commands/build.ts b/nuldoc-src/commands/build.ts
index 8765802..90aaab9 100644
--- a/nuldoc-src/commands/build.ts
+++ b/nuldoc-src/commands/build.ts
@@ -3,8 +3,9 @@ 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 { writeHtmlFile } from "../html.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";
@@ -58,7 +59,7 @@ async function outputPosts(posts: Document[], config: Config) {
"index.html",
);
await ensureDir(dirname(destFilePath));
- await writeHtmlFile(post, destFilePath);
+ await writePage(docToPage(post, destFilePath));
}
}
@@ -79,7 +80,7 @@ async function outputPostList(postList: Document, config: Config) {
const destDir = join(cwd, config.locations.destDir);
const destFilePath = join(destDir, "posts", "index.html");
await ensureDir(dirname(destFilePath));
- await writeHtmlFile(postList, destFilePath);
+ await writePage(docToPage(postList, destFilePath));
}
async function generateAboutPage(config: Config) {
@@ -92,7 +93,7 @@ async function outputAboutPage(about: Document, config: Config) {
const destDir = join(cwd, config.locations.destDir);
const destFilePath = join(destDir, "about", "index.html");
await ensureDir(dirname(destFilePath));
- await writeHtmlFile(about, destFilePath);
+ await writePage(docToPage(about, destFilePath));
}
async function generateTags(posts: Document[], config: Config) {
@@ -153,7 +154,7 @@ async function outputTags(tagDocs: [string, Document][], config: Config) {
for (const [tag, tagDoc] of tagDocs) {
const destFilePath = join(destDir, "tags", tag, "index.html");
await ensureDir(dirname(destFilePath));
- await writeHtmlFile(tagDoc, destFilePath);
+ await writePage(docToPage(tagDoc, destFilePath));
}
}
@@ -168,3 +169,15 @@ async function copyStaticFiles(config: Config) {
await Deno.copyFile(src, dst);
}
}
+
+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,
+ };
+}
diff --git a/nuldoc-src/page.ts b/nuldoc-src/page.ts
new file mode 100644
index 0000000..970265e
--- /dev/null
+++ b/nuldoc-src/page.ts
@@ -0,0 +1,8 @@
+import { Element } from "./dom.ts";
+import { RendererType } from "./render.ts";
+
+export type Page = {
+ root: Element;
+ renderer: RendererType;
+ destFilePath: string;
+};
diff --git a/nuldoc-src/render.ts b/nuldoc-src/render.ts
new file mode 100644
index 0000000..feb72a4
--- /dev/null
+++ b/nuldoc-src/render.ts
@@ -0,0 +1,13 @@
+import { Node } from "./dom.ts";
+import { renderHtml } from "./renderers/html.ts";
+
+// export type RendererType = "html" | "xml";
+export type RendererType = "html";
+
+export function render(root: Node, renderer: RendererType): string {
+ if (renderer === "html") {
+ return renderHtml(root);
+ } else {
+ return renderHtml(root);
+ }
+}
diff --git a/nuldoc-src/html.ts b/nuldoc-src/renderers/html.ts
index b94877a..3c49174 100644
--- a/nuldoc-src/html.ts
+++ b/nuldoc-src/renderers/html.ts
@@ -1,9 +1,11 @@
-import { Document } from "./docbook/document.ts";
-import { Element, forEachChild, Node, Text } from "./dom.ts";
-import { DocBookError } from "./errors.ts";
+import { Element, forEachChild, Node, Text } from "../dom.ts";
+import { DocBookError } from "../errors.ts";
-export async function writeHtmlFile(dom: Document, filePath: string) {
- await Deno.writeTextFile(filePath, toHtmlText(dom));
+export function renderHtml(root: Node): string {
+ return `<!DOCTYPE html>\n` + nodeToHtmlText(root, {
+ indentLevel: -1,
+ isInPre: false,
+ });
}
type Context = {
@@ -106,13 +108,6 @@ function isBlockNode(n: Node): boolean {
return !isInlineNode(n);
}
-function toHtmlText(dom: Document): string {
- return `<!DOCTYPE html>\n` + nodeToHtmlText(dom.root, {
- indentLevel: -1,
- isInPre: false,
- });
-}
-
function nodeToHtmlText(n: Node, ctx: Context): string {
if (n.kind === "text") {
if (n.raw) {