Cleanup EJS rendering code

This commit is contained in:
Shadowfacts 2019-01-04 21:59:52 -05:00
parent 167e60d852
commit 6bb3514f6c
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
8 changed files with 28 additions and 51 deletions

View File

@ -19,9 +19,7 @@ async function generate(theme: string) {
const filename = `site/css/${theme}.scss`; const filename = `site/css/${theme}.scss`;
let sass = (await fs.readFile(filename)).toString(); let sass = (await fs.readFile(filename)).toString();
sass = ejs.render(sass, {}, { sass = util.render(sass, {}, filename);
filename: filename
});
const result = await renderSass(sass); const result = await renderSass(sass);
util.write(`css/${theme}.css`, result.css); util.write(`css/${theme}.css`, result.css);

View File

@ -1,4 +1,3 @@
import ejs from "ejs";
import * as metadata from "../metadata"; import * as metadata from "../metadata";
import layout from "../layout"; import layout from "../layout";
import * as util from "../util"; import * as util from "../util";
@ -6,11 +5,9 @@ import * as util from "../util";
export default async function missing() { export default async function missing() {
const page = await metadata.get("site/404.html.ejs"); const page = await metadata.get("site/404.html.ejs");
page.text = ejs.render(page.text, { page.text = util.render(page.text, {
metadata: page.metadata metadata: page.metadata
}, { }, "site/404.html.ejs");
filename: "site/404.html.ejs"
});
page.text = await layout(page.text, page.metadata, page.metadata.layout!); page.text = await layout(page.text, page.metadata, page.metadata.layout!);

View File

@ -1,6 +1,4 @@
import path from "path"; import path from "path";
import ejs from "ejs";
import formatDate from "date-fns/format";
import * as util from "../util"; import * as util from "../util";
import * as metadata from "../metadata"; import * as metadata from "../metadata";
import { Page, PostMetadata } from "../metadata"; import { Page, PostMetadata } from "../metadata";
@ -29,15 +27,11 @@ export default async function generatePaginted(posts: Page[], basePath: string,
total: chunks.length, total: chunks.length,
prevLink: pageNum == 1 ? "" : pageNum == 2 ? basePath : path.join(basePath, (pageNum - 1).toString()), prevLink: pageNum == 1 ? "" : pageNum == 2 ? basePath : path.join(basePath, (pageNum - 1).toString()),
nextLink: pageNum == chunks.length ? "" : path.join(basePath, (pageNum + 1).toString()) nextLink: pageNum == chunks.length ? "" : path.join(basePath, (pageNum + 1).toString())
}, }
formatDate,
fancyLink: util.fancyLink
}; };
if (extraData) data = {...data, ...extraData}; if (extraData) data = {...data, ...extraData};
let renderedTemplate = ejs.render(page.text, data, { let renderedTemplate = util.render(page.text, data, templatePath);
filename: templatePath
});
renderedTemplate = await layout(renderedTemplate, page.metadata, page.metadata.layout!); renderedTemplate = await layout(renderedTemplate, page.metadata, page.metadata.layout!);

View File

@ -1,4 +1,3 @@
import ejs from "ejs";
import * as metadata from "../metadata"; import * as metadata from "../metadata";
import { Page } from "../metadata"; import { Page } from "../metadata";
import layout from "../layout"; import layout from "../layout";
@ -7,12 +6,10 @@ import * as util from "../util";
async function generateRedirect(oldPermalink: string, newPermalink: string) { async function generateRedirect(oldPermalink: string, newPermalink: string) {
const page = await metadata.get("site/redirect.html.ejs"); const page = await metadata.get("site/redirect.html.ejs");
page.text = ejs.render(page.text, { page.text = util.render(page.text, {
metadata: page.metadata, metadata: page.metadata,
newPermalink newPermalink
}, { }, "site/redirect.html.ejs");
filename: "site/redirect.html.ejs"
});
page.text = await layout(page.text, page.metadata, page.metadata.layout!); page.text = await layout(page.text, page.metadata, page.metadata.layout!);

View File

@ -1,5 +1,4 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import ejs from "ejs";
import path from "path"; import path from "path";
import { Page, PostMetadata } from "../metadata"; import { Page, PostMetadata } from "../metadata";
import * as util from "../util"; import * as util from "../util";
@ -15,14 +14,12 @@ async function generateFeed(posts: Page[], permalink: string, category?: string)
const dest = path.join(permalink, "feed.xml"); const dest = path.join(permalink, "feed.xml");
let text = (await fs.readFile("site/feed.xml.ejs")).toString(); let text = (await fs.readFile("site/feed.xml.ejs")).toString();
text = ejs.render(text, { text = util.render(text, {
posts, posts,
category, category,
permalink, permalink,
feedPath: dest feedPath: dest
}, { }, "site/feed.xml.ejs");
filename: "site/feed.xml.ejs"
});
util.write(dest, text); util.write(dest, text);
} }

View File

@ -1,14 +1,12 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
import dateFns from "date-fns"; import dateFns from "date-fns";
import formatDate from "date-fns/format";
import slugify from "@sindresorhus/slugify"; import slugify from "@sindresorhus/slugify";
import { Page, PostMetadata } from "../metadata"; import { Page, PostMetadata } from "../metadata";
import * as metadata from "../metadata"; import * as metadata from "../metadata";
import * as markdown from "../markdown"; import * as markdown from "../markdown";
import layout from "../layout"; import layout from "../layout";
import * as util from "../util"; import * as util from "../util";
import ejs from "ejs";
async function generateTutorials(group: string): Promise<Page[]> { async function generateTutorials(group: string): Promise<Page[]> {
const tutorials: Page[] = []; const tutorials: Page[] = [];
@ -70,14 +68,10 @@ async function generateTutorialsAndIndex(group: string, title: string): Promise<
(<TutorialIndexMetadata>page.metadata).group = group; (<TutorialIndexMetadata>page.metadata).group = group;
(<TutorialIndexMetadata>page.metadata).lastUpdated = <Date>(<PostMetadata>tutorials[tutorials.length - 1].metadata).date (<TutorialIndexMetadata>page.metadata).lastUpdated = <Date>(<PostMetadata>tutorials[tutorials.length - 1].metadata).date
page.text = ejs.render(page.text, { page.text = util.render(page.text, {
tutorials, tutorials,
metadata: page.metadata, metadata: page.metadata
formatDate, }, "site/tutorial-series.html.ejs");
fancyLink: util.fancyLink
}, {
filename: "site/tutorial-series.html.ejs"
});
page.text = await layout(page.text, page.metadata, page.metadata.layout!); page.text = await layout(page.text, page.metadata, page.metadata.layout!);
@ -94,13 +88,9 @@ async function generateTutorialsAndIndex(group: string, title: string): Promise<
async function generateIndex(allSeries: TutorialSeries[]) { async function generateIndex(allSeries: TutorialSeries[]) {
const page = await metadata.get("site/tutorials.html.ejs"); const page = await metadata.get("site/tutorials.html.ejs");
page.text = ejs.render(page.text, { page.text = util.render(page.text, {
allSeries, allSeries
formatDate, }, "site/tutorials.html.ejs");
fancyLink: util.fancyLink
}, {
filename: "site/tutorials.html.ejs"
});
page.text = await layout(page.text, page.metadata, page.metadata.layout!); page.text = await layout(page.text, page.metadata, page.metadata.layout!);

View File

@ -1,6 +1,4 @@
import path from "path"; import path from "path";
import ejs from "ejs";
import formatDate from "date-fns/format";
import * as metadata from "./metadata"; import * as metadata from "./metadata";
import { Metadata } from "./metadata"; import { Metadata } from "./metadata";
import * as util from "./util"; import * as util from "./util";
@ -8,14 +6,10 @@ import * as util from "./util";
export default async function layout(text: string, pageMetadata: Metadata, layoutPath: string): Promise<string> { export default async function layout(text: string, pageMetadata: Metadata, layoutPath: string): Promise<string> {
const layoutFile = path.join("site/layouts", layoutPath); const layoutFile = path.join("site/layouts", layoutPath);
let layoutPage = await metadata.get(layoutFile); let layoutPage = await metadata.get(layoutFile);
text = ejs.render(layoutPage.text, { text = util.render(layoutPage.text, {
content: text, content: text,
metadata: pageMetadata, metadata: pageMetadata
formatDate, }, layoutFile);
fancyLink: util.fancyLink
}, {
filename: layoutFile,
});
if (layoutPage.metadata.layout) { if (layoutPage.metadata.layout) {
return await layout(text, pageMetadata, layoutPage.metadata.layout); return await layout(text, pageMetadata, layoutPage.metadata.layout);
} else { } else {

View File

@ -1,6 +1,8 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
import hljs from "highlight.js"; import hljs from "highlight.js";
import ejs from "ejs";
import formatDate from "date-fns/format";
export async function write(filePath: string, data: any) { export async function write(filePath: string, data: any) {
const dest = path.join("out", filePath); const dest = path.join("out", filePath);
@ -41,4 +43,12 @@ export function fancyLink(text: string, href: string, meta?: string): string {
${text} ${text}
<span aria-hidden="true">}</span> <span aria-hidden="true">}</span>
</a>`; </a>`;
}
export function render(template: string, data: any, filename?: string): string {
data.fancyLink = fancyLink;
data.formatDate = formatDate;
return ejs.render(template, data, {
filename
});
} }