-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
liquid.ts
129 lines (119 loc) · 5.71 KB
/
liquid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Context } from './context/context'
import { forOwn, snakeCase } from './util/underscore'
import { Template } from './template/template'
import { LookupType } from './fs/loader'
import { Render } from './render/render'
import Parser from './parser/parser'
import { TagImplOptions } from './template/tag/tag-impl-options'
import { Value } from './template/value'
import builtinTags from './builtin/tags'
import * as builtinFilters from './builtin/filters'
import { TagMap } from './template/tag/tag-map'
import { FilterMap } from './template/filter/filter-map'
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, normalize, RenderOptions } from './liquid-options'
import { FilterImplOptions } from './template/filter/filter-impl-options'
import { toPromise, toValueSync } from './util/async'
export * from './util/error'
export * from './types'
export const version = '[VI]{version}[/VI]'
export class Liquid {
public readonly options: NormalizedFullOptions
public readonly renderer: Render
public readonly parser: Parser
public readonly filters: FilterMap
public readonly tags: TagMap
public constructor (opts: LiquidOptions = {}) {
this.options = normalize(opts)
this.parser = new Parser(this)
this.renderer = new Render()
this.filters = new FilterMap(this.options.strictFilters, this)
this.tags = new TagMap()
forOwn(builtinTags, (conf: TagImplOptions, name: string) => this.registerTag(snakeCase(name), conf))
forOwn(builtinFilters, (handler: FilterImplOptions, name: string) => this.registerFilter(snakeCase(name), handler))
}
public parse (html: string, filepath?: string): Template[] {
return this.parser.parse(html, filepath)
}
public _render (tpl: Template[], scope: object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
const ctx = new Context(scope, this.options, renderOptions)
return this.renderer.renderTemplates(tpl, ctx)
}
public async render (tpl: Template[], scope?: object, renderOptions?: RenderOptions): Promise<any> {
return toPromise(this._render(tpl, scope, { ...renderOptions, sync: false }))
}
public renderSync (tpl: Template[], scope?: object, renderOptions?: RenderOptions): any {
return toValueSync(this._render(tpl, scope, { ...renderOptions, sync: true }))
}
public renderToNodeStream (tpl: Template[], scope?: object, renderOptions: RenderOptions = {}): NodeJS.ReadableStream {
const ctx = new Context(scope, this.options, renderOptions)
return this.renderer.renderTemplatesToNodeStream(tpl, ctx)
}
public _parseAndRender (html: string, scope: object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
const tpl = this.parse(html)
return this._render(tpl, scope, renderOptions)
}
public async parseAndRender (html: string, scope?: object, renderOptions?: RenderOptions): Promise<any> {
return toPromise(this._parseAndRender(html, scope, { ...renderOptions, sync: false }))
}
public parseAndRenderSync (html: string, scope?: object, renderOptions?: RenderOptions): any {
return toValueSync(this._parseAndRender(html, scope, { ...renderOptions, sync: true }))
}
public _parsePartialFile (file: string, sync?: boolean, currentFile?: string) {
return this.parser.parseFile(file, sync, LookupType.Partials, currentFile)
}
public _parseLayoutFile (file: string, sync?: boolean, currentFile?: string) {
return this.parser.parseFile(file, sync, LookupType.Layouts, currentFile)
}
public async parseFile (file: string): Promise<Template[]> {
return toPromise<Template[]>(this.parser.parseFile(file, false))
}
public parseFileSync (file: string): Template[] {
return toValueSync<Template[]>(this.parser.parseFile(file, true))
}
public async renderFile (file: string, ctx?: object, renderOptions?: RenderOptions) {
const templates = await this.parseFile(file)
return this.render(templates, ctx, renderOptions)
}
public renderFileSync (file: string, ctx?: object, renderOptions?: RenderOptions) {
const templates = this.parseFileSync(file)
return this.renderSync(templates, ctx, renderOptions)
}
public async renderFileToNodeStream (file: string, scope?: object, renderOptions?: RenderOptions) {
const templates = await this.parseFile(file)
return this.renderToNodeStream(templates, scope, renderOptions)
}
public _evalValue (str: string, scopeOrContext?: object | Context): IterableIterator<any> {
const value = new Value(str, this)
const ctx = scopeOrContext instanceof Context ? scopeOrContext : new Context(scopeOrContext, this.options)
return value.value(ctx, false)
}
public async evalValue (str: string, scopeOrContext?: object | Context): Promise<any> {
return toPromise(this._evalValue(str, scopeOrContext))
}
public evalValueSync (str: string, scopeOrContext?: object | Context): any {
return toValueSync(this._evalValue(str, scopeOrContext))
}
public registerFilter (name: string, filter: FilterImplOptions) {
this.filters.set(name, filter)
}
public registerTag (name: string, tag: TagImplOptions) {
this.tags.set(name, tag)
}
public plugin (plugin: (this: Liquid, L: typeof Liquid) => void) {
return plugin.call(this, Liquid)
}
public express () {
const self = this // eslint-disable-line
let firstCall = true
return function (this: any, filePath: string, ctx: object, callback: (err: Error | null, rendered: string) => void) {
if (firstCall) {
firstCall = false
const dirs = normalizeDirectoryList(this.root)
self.options.root.unshift(...dirs)
self.options.layouts.unshift(...dirs)
self.options.partials.unshift(...dirs)
}
self.renderFile(filePath, ctx).then(html => callback(null, html) as any, callback as any)
}
}
}