estilo
Version:
Create color schemes for Vim, NeoVim, Airline and Lightline
72 lines (71 loc) • 2.52 kB
JavaScript
import { EtaNameResolutionError } from "./err.js";
/* END TYPES */
function handleCache(template, options) {
const templateStore = options && options.async
? this.templatesAsync
: this.templatesSync;
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
const templatePath = options.filepath;
const cachedTemplate = templateStore.get(templatePath);
if (this.config.cache && cachedTemplate) {
return cachedTemplate;
}
else {
const templateString = this.readFile(templatePath);
const templateFn = this.compile(templateString, options);
if (this.config.cache)
templateStore.define(templatePath, templateFn);
return templateFn;
}
}
else {
const cachedTemplate = templateStore.get(template);
if (cachedTemplate) {
return cachedTemplate;
}
else {
throw new EtaNameResolutionError("Failed to get template '" + template + "'");
}
}
}
export function render(template, // template name or template function
data, meta) {
let templateFn;
const options = { ...meta, async: false };
if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
options.filepath = this.resolvePath(template, options);
}
templateFn = handleCache.call(this, template, options);
}
else {
templateFn = template;
}
const res = templateFn.call(this, data, options);
return res;
}
export function renderAsync(template, // template name or template function
data, meta) {
let templateFn;
const options = { ...meta, async: true };
if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
options.filepath = this.resolvePath(template, options);
}
templateFn = handleCache.call(this, template, options);
}
else {
templateFn = template;
}
const res = templateFn.call(this, data, options);
// Return a promise
return Promise.resolve(res);
}
export function renderString(template, data) {
const templateFn = this.compile(template, { async: false });
return render.call(this, templateFn, data);
}
export function renderStringAsync(template, data) {
const templateFn = this.compile(template, { async: true });
return renderAsync.call(this, templateFn, data);
}