2020-06-16 00:11:54 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2016-2022 Yomichan Authors
|
2020-06-16 00:11:54 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
|
|
|
* Handlebars
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TemplateRenderer {
|
2021-07-18 15:20:03 +00:00
|
|
|
constructor() {
|
2020-06-16 00:11:54 +00:00
|
|
|
this._cache = new Map();
|
|
|
|
this._cacheMaxSize = 5;
|
2021-01-11 00:28:50 +00:00
|
|
|
this._dataTypes = new Map();
|
2021-07-18 15:20:03 +00:00
|
|
|
this._renderSetup = null;
|
|
|
|
this._renderCleanup = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerHelpers(helpers) {
|
|
|
|
Handlebars.partials = Handlebars.templates;
|
|
|
|
for (const [name, helper] of helpers) {
|
|
|
|
this._registerHelper(name, helper);
|
|
|
|
}
|
2020-06-16 00:11:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 23:09:21 +00:00
|
|
|
registerDataType(name, {modifier=null, composeData=null}) {
|
|
|
|
this._dataTypes.set(name, {modifier, composeData});
|
2021-01-11 00:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 15:20:03 +00:00
|
|
|
setRenderCallbacks(setup, cleanup) {
|
|
|
|
this._renderSetup = setup;
|
|
|
|
this._renderCleanup = cleanup;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:42:06 +00:00
|
|
|
render(template, data, type) {
|
|
|
|
const instance = this._getTemplateInstance(template);
|
2021-04-02 23:09:21 +00:00
|
|
|
data = this._getModifiedData(data, void 0, type);
|
2021-04-02 16:42:06 +00:00
|
|
|
return this._renderTemplate(instance, data);
|
|
|
|
}
|
|
|
|
|
2021-04-02 23:09:21 +00:00
|
|
|
renderMulti(items) {
|
|
|
|
const results = [];
|
|
|
|
for (const {template, templateItems} of items) {
|
|
|
|
const instance = this._getTemplateInstance(template);
|
|
|
|
for (const {type, commonData, datas} of templateItems) {
|
|
|
|
for (let data of datas) {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
data = this._getModifiedData(data, commonData, type);
|
|
|
|
result = this._renderTemplate(instance, data);
|
|
|
|
result = {result};
|
|
|
|
} catch (error) {
|
|
|
|
result = {error};
|
|
|
|
}
|
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:42:06 +00:00
|
|
|
getModifiedData(data, type) {
|
2021-04-02 23:09:21 +00:00
|
|
|
return this._getModifiedData(data, void 0, type);
|
2021-04-02 16:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_getTemplateInstance(template) {
|
2020-06-16 00:11:54 +00:00
|
|
|
const cache = this._cache;
|
|
|
|
let instance = cache.get(template);
|
|
|
|
if (typeof instance === 'undefined') {
|
|
|
|
this._updateCacheSize(this._cacheMaxSize - 1);
|
|
|
|
instance = Handlebars.compile(template);
|
|
|
|
cache.set(template, instance);
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:42:06 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderTemplate(instance, data) {
|
2021-07-18 15:20:03 +00:00
|
|
|
const renderSetup = this._renderSetup;
|
|
|
|
const renderCleanup = this._renderCleanup;
|
|
|
|
let result;
|
|
|
|
let additions1;
|
|
|
|
let additions2;
|
2020-07-31 20:17:58 +00:00
|
|
|
try {
|
2021-07-18 15:20:03 +00:00
|
|
|
additions1 = (typeof renderSetup === 'function' ? renderSetup(data) : null);
|
|
|
|
result = instance(data).trim();
|
2020-07-31 20:17:58 +00:00
|
|
|
} finally {
|
2021-07-18 15:20:03 +00:00
|
|
|
additions2 = (typeof renderCleanup === 'function' ? renderCleanup(data) : null);
|
2021-04-01 00:07:11 +00:00
|
|
|
}
|
2021-07-18 15:20:03 +00:00
|
|
|
return Object.assign({result}, additions1, additions2);
|
2021-04-01 00:07:11 +00:00
|
|
|
}
|
2021-01-11 00:28:50 +00:00
|
|
|
|
2021-04-02 23:09:21 +00:00
|
|
|
_getModifiedData(data, commonData, type) {
|
2021-04-01 00:07:11 +00:00
|
|
|
if (typeof type === 'string') {
|
|
|
|
const typeInfo = this._dataTypes.get(type);
|
|
|
|
if (typeof typeInfo !== 'undefined') {
|
2021-04-02 23:09:21 +00:00
|
|
|
if (typeof commonData !== 'undefined') {
|
|
|
|
const {composeData} = typeInfo;
|
|
|
|
if (typeof composeData === 'function') {
|
|
|
|
data = composeData(data, commonData);
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 16:42:06 +00:00
|
|
|
const {modifier} = typeInfo;
|
|
|
|
if (typeof modifier === 'function') {
|
|
|
|
data = modifier(data);
|
|
|
|
}
|
2020-09-04 21:57:05 +00:00
|
|
|
}
|
2020-07-31 20:17:58 +00:00
|
|
|
}
|
2021-04-01 00:07:11 +00:00
|
|
|
return data;
|
|
|
|
}
|
2020-06-16 00:11:54 +00:00
|
|
|
|
|
|
|
_updateCacheSize(maxSize) {
|
|
|
|
const cache = this._cache;
|
|
|
|
let removeCount = cache.size - maxSize;
|
|
|
|
if (removeCount <= 0) { return; }
|
|
|
|
|
|
|
|
for (const key of cache.keys()) {
|
|
|
|
cache.delete(key);
|
|
|
|
if (--removeCount <= 0) { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:02:35 +00:00
|
|
|
_registerHelper(name, helper) {
|
|
|
|
function wrapper(...args) {
|
|
|
|
return helper(this, ...args);
|
|
|
|
}
|
|
|
|
Handlebars.registerHelper(name, wrapper);
|
|
|
|
}
|
2020-06-16 00:11:54 +00:00
|
|
|
}
|