Comparing version 1.2.5 to 1.2.6
@@ -15,45 +15,45 @@ 'use strict' | ||
class Compiler { | ||
/** | ||
* @param {Logger} logger | ||
* @return {Compiler} | ||
/** | ||
* @param {Logger} logger | ||
* @return {Compiler} | ||
*/ | ||
constructor(logger) { | ||
this.logger = logger | ||
this._ = {} | ||
} | ||
constructor(logger) { | ||
this.logger = logger | ||
this._ = {} | ||
} | ||
/** | ||
* @callback compileCallback | ||
* @param {String} filepath | ||
* @param {String} content | ||
* @return {Function} | ||
*/ | ||
/** | ||
* @description Register a compile funtion. | ||
* @param {String} ext Template extname starts with `.`. | ||
* @param {compileCallback} | ||
*/ | ||
register(ext, fn) { | ||
if (!isFunction(fn)) { | ||
throw new TypeError('fn must be a Function') | ||
} | ||
this._[ext] = {ext, fn} | ||
} | ||
/** | ||
* @callback compileCallback | ||
* @param {String} filepath | ||
* @param {String} content | ||
* @return {Function} | ||
*/ | ||
/** | ||
* @description Register a compile funtion. | ||
* @param {String} ext Template extname starts with `.`. | ||
* @param {compileCallback} | ||
*/ | ||
register(ext, fn) { | ||
if (!isFunction(fn)) { | ||
throw new TypeError('fn must be a Function') | ||
} | ||
this._[ext] = {ext, fn} | ||
} | ||
/** | ||
* @description Compile template into function. | ||
* @param {String} filepath | ||
* @param {String} content | ||
* @return {Function} | ||
*/ | ||
async compile(filepath, content) { | ||
* @description Compile template into function. | ||
* @param {String} filepath | ||
* @param {String} content | ||
* @return {Function} | ||
*/ | ||
async compile(filepath, content) { | ||
if (content == null) { | ||
content = await fse.readFile(filepath, 'utf8') | ||
} | ||
const ext = path.extname(filepath) | ||
const handler = this._[ext] | ||
return await handler['fn'](filepath, content) | ||
} | ||
const ext = path.extname(filepath) | ||
const handler = this._[ext] | ||
return await handler['fn'](filepath, content) | ||
} | ||
} | ||
module.exports = Compiler |
@@ -15,61 +15,58 @@ 'use strict' | ||
class Decorator { | ||
/** | ||
* @param {Logger} logger | ||
* @param {Compiler} compiler | ||
* @return {Decorator} | ||
/** | ||
* @param {Logger} logger | ||
* @param {Compiler} compiler | ||
* @return {Decorator} | ||
*/ | ||
constructor(logger, compiler) { | ||
this.logger = logger | ||
this.compiler = compiler | ||
this._ = {} | ||
} | ||
constructor(logger, compiler) { | ||
this.logger = logger | ||
this.compiler = compiler | ||
this._ = {} | ||
} | ||
/** | ||
* @callback decorateCallback | ||
* @param {Object} ctx | ||
* @return {String} | ||
*/ | ||
/** | ||
* @description Register a decorate function. | ||
* @param {String} layout | ||
* @param {decorateCallback|String} fn If string, | ||
* will call Compiler while decorating. | ||
*/ | ||
register(layout, fn) { | ||
if (!(isFunction(fn) || isString(fn))) { | ||
/** | ||
* @callback decorateCallback | ||
* @param {Object} ctx | ||
* @return {String} | ||
*/ | ||
/** | ||
* @description Register a decorate function. | ||
* @param {String} layout | ||
* @param {decorateCallback|String} fn If string, | ||
* will call Compiler while decorating. | ||
*/ | ||
register(layout, fn) { | ||
if (!(isFunction(fn) || isString(fn))) { | ||
throw new TypeError('fn must be a Function or filepath') | ||
} | ||
this._[layout] = {layout, fn} | ||
} | ||
this._[layout] = {layout, fn} | ||
} | ||
/** | ||
* @description Decorate input file with layout. | ||
* @param {File} file | ||
* @return {String} | ||
*/ | ||
async decorate(file, ctx) { | ||
const layout = this.getFileLayout(file) | ||
if (layout != null) { | ||
this.logger.debug(`Hikaru is decorating \`${ | ||
this.logger.cyan(path.join(file['docDir'], file['docPath'])) | ||
}\` with layout \`${ | ||
this.logger.blue(layout) | ||
}\`...`) | ||
const handler = this._[layout] | ||
/** | ||
* @description Decorate input file with layout. | ||
* @param {File} file | ||
* @return {String} | ||
*/ | ||
async decorate(file, ctx) { | ||
const layout = this.getFileLayout(file) | ||
if (layout != null) { | ||
this.logger.debug(`Hikaru is decorating \`${ | ||
this.logger.cyan(path.join(file['docDir'], file['docPath'])) | ||
}\` with layout \`${ | ||
this.logger.blue(layout) | ||
}\`...`) | ||
const handler = this._[layout] | ||
if (handler == null) { | ||
throw new Error(`Decorator for \`${layout}\` is not registered!`) | ||
} | ||
if (isString(handler['fn'])) { | ||
const fn = await this.compiler.compile(handler['fn']) | ||
return fn(Object.assign(new File(), file, ctx)) | ||
} | ||
return await handler['fn'](Object.assign(new File(), file, ctx)) | ||
} | ||
// this.logger.debug(`Hikaru is decorating \`${ | ||
// this.logger.cyan(path.join(file['docDir'], file['docPath'])) | ||
// }\`...`) | ||
return file['content'] | ||
} | ||
if (isString(handler['fn'])) { | ||
const fn = await this.compiler.compile(handler['fn']) | ||
return fn(Object.assign(new File(), file, ctx)) | ||
} | ||
return await handler['fn'](Object.assign(new File(), file, ctx)) | ||
} | ||
return file['content'] | ||
} | ||
/** | ||
/** | ||
* @description List registered layout. | ||
@@ -79,23 +76,23 @@ * @return {String[]} | ||
list() { | ||
return Object.keys(this._) | ||
} | ||
return Object.keys(this._) | ||
} | ||
/** | ||
* @private | ||
* @description Get simpified layout for file. | ||
* @param {File} file | ||
* @param {String[]} available If not in available, fallback to `page`. | ||
* @return {String} | ||
*/ | ||
getFileLayout(file) { | ||
if (file['layout'] == null) { | ||
return null | ||
} | ||
if (!this.list().includes(file['layout'])) { | ||
return 'page' | ||
} | ||
return file['layout'] | ||
} | ||
/** | ||
* @private | ||
* @description Get simpified layout for file. | ||
* @param {File} file | ||
* @param {String[]} available If not in available, fallback to `page`. | ||
* @return {String} | ||
*/ | ||
getFileLayout(file) { | ||
if (file['layout'] == null) { | ||
return null | ||
} | ||
if (!this.list().includes(file['layout'])) { | ||
return 'page' | ||
} | ||
return file['layout'] | ||
} | ||
} | ||
module.exports = Decorator |
@@ -454,3 +454,4 @@ 'use strict' | ||
) | ||
this.decorator.register(layout, filepath) | ||
const fn = await this.compiler.compile(filepath) | ||
this.decorator.register(layout, fn) | ||
} | ||
@@ -473,3 +474,3 @@ } | ||
return highlight(code, Object.assign({ | ||
'lang': lang != null? lang.toLowerCase() : null, | ||
'lang': lang != null ? lang.toLowerCase() : null, | ||
'hljs': true, | ||
@@ -476,0 +477,0 @@ 'gutter': true |
{ | ||
"name": "hikarujs", | ||
"version": "1.2.5", | ||
"version": "1.2.6", | ||
"description": "A static site generator that generates routes based on directories naturally.", | ||
@@ -10,3 +10,3 @@ "main": "hikaru/index.js", | ||
"scripts": { | ||
"build:doc": "jsdoc -c jsdoc.json -R README.md -u tutorials/", | ||
"build:doc": "jsdoc -c jsdoc.json -R README.md -u tutorials/ && echo 'hikaru.alynx.moe' > docs/CNAME", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -13,0 +13,0 @@ }, |
90171
2638