edge.js
Advanced tools
Comparing version 3.1.2 to 3.2.0
import { Parser } from 'edge-parser'; | ||
import { Token } from 'edge-lexer'; | ||
import { Processor } from '../Processor'; | ||
import { CacheManager } from '../CacheManager'; | ||
@@ -12,5 +13,6 @@ import { LoaderContract, TagsContract, LoaderTemplate, CompilerContract } from '../Contracts'; | ||
private tags; | ||
private processor; | ||
private cache; | ||
cacheManager: CacheManager; | ||
constructor(loader: LoaderContract, tags: TagsContract, cache?: boolean); | ||
constructor(loader: LoaderContract, tags: TagsContract, processor: Processor, cache?: boolean); | ||
/** | ||
@@ -17,0 +19,0 @@ * Merges sections of base template and parent template tokens |
@@ -21,5 +21,6 @@ "use strict"; | ||
class Compiler { | ||
constructor(loader, tags, cache = true) { | ||
constructor(loader, tags, processor, cache = true) { | ||
this.loader = loader; | ||
this.tags = tags; | ||
this.processor = processor; | ||
this.cache = cache; | ||
@@ -133,3 +134,4 @@ this.cacheManager = new CacheManager_1.CacheManager(this.cache); | ||
const absPath = this.loader.makePath(templatePath); | ||
const { template } = this.loader.resolve(absPath); | ||
let { template } = this.loader.resolve(absPath); | ||
template = this.processor.executeRaw({ path: absPath, raw: template }); | ||
return this.templateContentToTokens(template, parser || new edge_parser_1.Parser(this.tags), absPath); | ||
@@ -154,3 +156,7 @@ } | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
const template = this.processor.executeCompiled({ | ||
path: absPath, | ||
compiled: cachedResponse.template, | ||
}); | ||
return { template }; | ||
} | ||
@@ -171,5 +177,5 @@ const parser = new edge_parser_1.Parser(this.tags); | ||
this.cacheManager.set(absPath, { template }); | ||
return { template }; | ||
return { template: this.processor.executeCompiled({ path: absPath, compiled: template }) }; | ||
} | ||
} | ||
exports.Compiler = Compiler; |
@@ -93,2 +93,10 @@ /** | ||
/** | ||
* Shape of the template contract | ||
*/ | ||
export interface TemplateContract { | ||
renderInline(templatePath: string, ...localVariables: string[]): Function; | ||
renderWithState(template: string, state: any, slots: any): string; | ||
render(template: string, state: any): string; | ||
} | ||
/** | ||
* Shape of the renderer that renders the edge templates | ||
@@ -101,2 +109,23 @@ */ | ||
/** | ||
* The processor is used to execute process functions for different | ||
* lifecycles | ||
*/ | ||
export interface ProcessorContract { | ||
/** | ||
* Define a processor handler | ||
*/ | ||
process(event: 'raw', handler: (data: { | ||
raw: string; | ||
path: string; | ||
}) => string | void): this; | ||
process(event: 'compiled', handler: (data: { | ||
compiled: string; | ||
path: string; | ||
}) => string | void): this; | ||
process(event: 'output', handler: (data: { | ||
output: string; | ||
template: TemplateContract; | ||
}) => string | void): this; | ||
} | ||
/** | ||
* Shape of options that can be passed to the | ||
@@ -112,3 +141,3 @@ * edge constructor | ||
*/ | ||
export interface EdgeContract { | ||
export interface EdgeContract extends ProcessorContract { | ||
loader: LoaderContract; | ||
@@ -115,0 +144,0 @@ compiler: CompilerContract; |
import { Compiler } from '../Compiler'; | ||
import { TagContract, EdgeOptions, EdgeContract, LoaderTemplate, EdgeRendererContract } from '../Contracts'; | ||
import { TagContract, EdgeOptions, EdgeContract, LoaderTemplate, TemplateContract, EdgeRendererContract } from '../Contracts'; | ||
/** | ||
@@ -20,2 +20,6 @@ * Exposes the API to render templates, register custom tags and globals | ||
/** | ||
* Reference to the registered processor handlers | ||
*/ | ||
private processor; | ||
/** | ||
* The loader to load templates. A loader can read and return | ||
@@ -110,2 +114,18 @@ * templates from anywhere. The default loader reads files | ||
/** | ||
* Define processor functions to modify the output of templates | ||
* at different stages | ||
*/ | ||
process(event: 'raw', handler: (data: { | ||
raw: string; | ||
path: string; | ||
}) => string | void): this; | ||
process(event: 'compiled', handler: (data: { | ||
compiled: string; | ||
path: string; | ||
}) => string | void): this; | ||
process(event: 'output', handler: (data: { | ||
output: string; | ||
template: TemplateContract; | ||
}) => string | void): this; | ||
/** | ||
* Share locals with the current view context. | ||
@@ -112,0 +132,0 @@ * |
@@ -35,2 +35,3 @@ "use strict"; | ||
const Compiler_1 = require("../Compiler"); | ||
const Processor_1 = require("../Processor"); | ||
const Renderer_1 = require("../Renderer"); | ||
@@ -53,2 +54,6 @@ /** | ||
/** | ||
* Reference to the registered processor handlers | ||
*/ | ||
this.processor = new Processor_1.Processor(); | ||
/** | ||
* The loader to load templates. A loader can read and return | ||
@@ -62,3 +67,3 @@ * templates from anywhere. The default loader reads files | ||
*/ | ||
this.compiler = new Compiler_1.Compiler(this.loader, this.tags, !!this.options.cache); | ||
this.compiler = new Compiler_1.Compiler(this.loader, this.tags, this.processor, !!this.options.cache); | ||
Object.keys(Tags).forEach((name) => this.registerTag(Tags[name])); | ||
@@ -168,4 +173,8 @@ } | ||
getRenderer() { | ||
return new Renderer_1.EdgeRenderer(this.compiler, this.GLOBALS); | ||
return new Renderer_1.EdgeRenderer(this.compiler, this.GLOBALS, this.processor); | ||
} | ||
process(event, handler) { | ||
this.processor.process(event, handler); | ||
return this; | ||
} | ||
/** | ||
@@ -172,0 +181,0 @@ * Share locals with the current view context. |
@@ -0,1 +1,2 @@ | ||
import { Processor } from '../Processor'; | ||
import { EdgeRendererContract, CompilerContract } from '../Contracts'; | ||
@@ -8,4 +9,5 @@ /** | ||
private globals; | ||
private processor; | ||
private locals; | ||
constructor(compiler: CompilerContract, globals: any); | ||
constructor(compiler: CompilerContract, globals: any, processor: Processor); | ||
/** | ||
@@ -12,0 +14,0 @@ * Share local variables with the template. They will overwrite the |
@@ -21,5 +21,6 @@ "use strict"; | ||
class EdgeRenderer { | ||
constructor(compiler, globals) { | ||
constructor(compiler, globals, processor) { | ||
this.compiler = compiler; | ||
this.globals = globals; | ||
this.processor = processor; | ||
this.locals = {}; | ||
@@ -39,3 +40,3 @@ } | ||
render(templatePath, state = {}) { | ||
const template = new Template_1.Template(this.compiler, this.globals, this.locals); | ||
const template = new Template_1.Template(this.compiler, this.globals, this.locals, this.processor); | ||
return template.render(templatePath, state); | ||
@@ -42,0 +43,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { CompilerContract } from '../Contracts'; | ||
import { Processor } from '../Processor'; | ||
import { CompilerContract, TemplateContract } from '../Contracts'; | ||
/** | ||
@@ -7,4 +8,5 @@ * The template is used to compile and run templates. Also the instance | ||
*/ | ||
export declare class Template { | ||
export declare class Template implements TemplateContract { | ||
private compiler; | ||
private processor; | ||
/** | ||
@@ -15,3 +17,3 @@ * The shared state is used to hold the globals and locals, | ||
private sharedState; | ||
constructor(compiler: CompilerContract, globals: any, locals: any); | ||
constructor(compiler: CompilerContract, globals: any, locals: any, processor: Processor); | ||
/** | ||
@@ -18,0 +20,0 @@ * Wraps template to a function |
@@ -23,4 +23,5 @@ "use strict"; | ||
class Template { | ||
constructor(compiler, globals, locals) { | ||
constructor(compiler, globals, locals, processor) { | ||
this.compiler = compiler; | ||
this.processor = processor; | ||
this.sharedState = lodash_merge_1.default({}, globals, locals); | ||
@@ -69,3 +70,4 @@ } | ||
const templateState = Object.assign({}, this.sharedState, state, { $slots: slots }); | ||
return this.wrapToFunction(compiledTemplate)(this, templateState, new Context_1.Context()); | ||
const context = new Context_1.Context(); | ||
return this.wrapToFunction(compiledTemplate)(this, templateState, context); | ||
} | ||
@@ -82,6 +84,7 @@ /** | ||
const templateState = Object.assign({}, this.sharedState, state); | ||
const fn = this.wrapToFunction(compiledTemplate); | ||
return this.trimTopBottomNewLines(fn(this, templateState, new Context_1.Context())); | ||
const context = new Context_1.Context(); | ||
const output = this.trimTopBottomNewLines(this.wrapToFunction(compiledTemplate)(this, templateState, context)); | ||
return this.processor.executeOutput({ output, template: this }); | ||
} | ||
} | ||
exports.Template = Template; |
{ | ||
"name": "edge.js", | ||
"version": "3.1.2", | ||
"version": "3.2.0", | ||
"description": "Template engine", | ||
@@ -38,5 +38,5 @@ "main": "build/index.js", | ||
"@adonisjs/mrm-preset": "^2.4.0", | ||
"@adonisjs/require-ts": "^1.0.0", | ||
"@adonisjs/require-ts": "^1.0.4", | ||
"@poppinss/dev-utils": "^1.0.10", | ||
"@types/node": "^14.11.1", | ||
"@types/node": "^14.11.2", | ||
"commitizen": "^4.2.1", | ||
@@ -47,4 +47,4 @@ "cz-conventional-changelog": "^3.3.0", | ||
"doctoc": "^1.4.0", | ||
"eslint": "^7.9.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint": "^7.10.0", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint-plugin-adonis": "^1.0.15", | ||
@@ -56,3 +56,3 @@ "eslint-plugin-prettier": "^3.1.4", | ||
"js-stringify": "^1.0.2", | ||
"mrm": "^2.5.0", | ||
"mrm": "^2.5.1", | ||
"np": "^6.5.0", | ||
@@ -79,4 +79,4 @@ "npm-audit-html": "^1.4.3", | ||
"edge-error": "^1.0.5", | ||
"edge-lexer": "^3.1.4", | ||
"edge-parser": "^5.1.4", | ||
"edge-lexer": "^3.1.5", | ||
"edge-parser": "^5.1.6", | ||
"he": "^1.2.0", | ||
@@ -83,0 +83,0 @@ "lodash.foreach": "^4.5.0", |
113128
62
3111
Updatededge-lexer@^3.1.5
Updatededge-parser@^5.1.6