UNPKG

meblog

Version:

A simple blog engine for personal blogging

71 lines (70 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const pug_1 = tslib_1.__importDefault(require("pug")); const i18n_1 = tslib_1.__importDefault(require("i18n")); const ConfigHolder_1 = tslib_1.__importDefault(require("../ConfigHolder")); const FileUtils_1 = tslib_1.__importDefault(require("../util/FileUtils")); class PageTemplate extends ConfigHolder_1.default { dataSource; template; constructor(dataSource, template) { super(dataSource.config); this.dataSource = dataSource; this.template = template; } render() { const output = this.template.clone(); output.path = output.path.replace(output.extname, '.html'); output.contents = this.compile(output); return [output]; } get templateName() { return PageTemplate.getTemplateName(this.template); } static getTemplateName(file) { return FileUtils_1.default.basenameWithoutExt(file.path); } compile(file, data) { const options = { pretty: this.config.devMode, filename: file.path, }; const pugFn = pug_1.default.compile(String(file.contents), options); const locale = file.locale; const templateData = { ...this.config, ...data, allPosts: this.dataSource.getAllPosts(), posts: this.dataSource.getPosts(locale), tags: this.dataSource.getTags(), templateName: this.templateName, formatDateTime: this.formatDateTime.bind(this), formatDate: this.formatDate.bind(this), rootUrl: this.rootUrl.bind(this), url: this.url.bind(this), postUrl: this.postUrl.bind(this), postRootUrl: this.postRootUrl.bind(this), tagUrl: this.tagUrl.bind(this), tagRootUrl: this.tagRootUrl.bind(this), locale }; Object.assign(templateData, this.getI18nUtils()); const compiled = pugFn(templateData); return Buffer.from(compiled); } i18nFnToPickup = [ '__', '__n', '__l', '__h', '__mf', ]; getI18nUtils() { return this.i18nFnToPickup.reduce((obj, name) => { obj[name] = i18n_1.default[name].bind(i18n_1.default); return obj; }, {}); } } exports.default = PageTemplate;