mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
feat: passing liquid to FilterImpl, closes #277
This commit is contained in:
+2
-2
@@ -31,7 +31,7 @@ export class Liquid {
|
||||
this.parser = new Parser(this)
|
||||
this.renderer = new Render()
|
||||
this.fs = opts.fs || fs
|
||||
this.filters = new FilterMap(this.options.strictFilters)
|
||||
this.filters = new FilterMap(this.options.strictFilters, this)
|
||||
this.tags = new TagMap()
|
||||
|
||||
forOwn(builtinTags, (conf: TagImplOptions, name: string) => this.registerTag(snakeCase(name), conf))
|
||||
@@ -104,7 +104,7 @@ export class Liquid {
|
||||
}
|
||||
|
||||
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
|
||||
const value = new Value(str, this.filters)
|
||||
const value = new Value(str, this.filters, this)
|
||||
return value.value(ctx)
|
||||
}
|
||||
public async evalValue (str: string, ctx: Context): Promise<any> {
|
||||
|
||||
@@ -29,7 +29,7 @@ export default class Parser {
|
||||
return new Tag(token, remainTokens, this.liquid)
|
||||
}
|
||||
if (isOutputToken(token)) {
|
||||
return new Output(token as OutputToken, this.liquid.filters)
|
||||
return new Output(token as OutputToken, this.liquid.filters, this.liquid)
|
||||
}
|
||||
return new HTML(token)
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { Context } from '../../context/context'
|
||||
import { Liquid } from '../../liquid'
|
||||
|
||||
export interface FilterImpl {
|
||||
context: Context;
|
||||
liquid: Liquid;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ import { FilterImplOptions } from './filter-impl-options'
|
||||
import { Filter } from './filter'
|
||||
import { FilterArg } from '../../parser/filter-arg'
|
||||
import { assert } from '../../util/assert'
|
||||
import { Liquid } from '../../liquid'
|
||||
|
||||
export class FilterMap {
|
||||
private impls: {[key: string]: FilterImplOptions} = {}
|
||||
|
||||
constructor (private readonly strictFilters: boolean) {}
|
||||
constructor (
|
||||
private readonly strictFilters: boolean,
|
||||
private readonly liquid: Liquid
|
||||
) {}
|
||||
|
||||
get (name: string) {
|
||||
const impl = this.impls[name]
|
||||
@@ -19,6 +23,6 @@ export class FilterMap {
|
||||
}
|
||||
|
||||
create (name: string, args: FilterArg[]) {
|
||||
return new Filter(name, this.get(name), args)
|
||||
return new Filter(name, this.get(name), args, this.liquid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,16 +3,19 @@ import { Context } from '../../context/context'
|
||||
import { identify } from '../../util/underscore'
|
||||
import { FilterImplOptions } from './filter-impl-options'
|
||||
import { FilterArg, isKeyValuePair } from '../../parser/filter-arg'
|
||||
import { Liquid } from '../../liquid'
|
||||
|
||||
export class Filter {
|
||||
public name: string
|
||||
public args: FilterArg[]
|
||||
private impl: FilterImplOptions
|
||||
private liquid: Liquid
|
||||
|
||||
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[]) {
|
||||
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[], liquid: Liquid) {
|
||||
this.name = name
|
||||
this.impl = impl || identify
|
||||
this.args = args
|
||||
this.liquid = liquid
|
||||
}
|
||||
public * render (value: any, context: Context) {
|
||||
const argv: any[] = []
|
||||
@@ -20,6 +23,6 @@ export class Filter {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||
else argv.push(yield evalToken(arg, context))
|
||||
}
|
||||
return yield this.impl.apply({ context }, [value, ...argv])
|
||||
return yield this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ import { Template } from '../template/template'
|
||||
import { Context } from '../context/context'
|
||||
import { Emitter } from '../render/emitter'
|
||||
import { OutputToken } from '../tokens/output-token'
|
||||
import { Liquid } from '../liquid'
|
||||
|
||||
export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||
private value: Value
|
||||
public constructor (token: OutputToken, filters: FilterMap) {
|
||||
public constructor (token: OutputToken, filters: FilterMap, liquid: Liquid) {
|
||||
super(token)
|
||||
this.value = new Value(token.content, filters)
|
||||
this.value = new Value(token.content, filters, liquid)
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter) {
|
||||
const val = yield this.value.value(ctx)
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Filter } from './filter/filter'
|
||||
import { Context } from '../context/context'
|
||||
import { ValueToken } from '../tokens/value-token'
|
||||
import { assert } from '../util/assert'
|
||||
import { Liquid } from '../liquid'
|
||||
|
||||
export class Value {
|
||||
public readonly filters: Filter[] = []
|
||||
@@ -13,15 +14,15 @@ export class Value {
|
||||
/**
|
||||
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
||||
*/
|
||||
public constructor (str: string, private readonly filterMap: FilterMap) {
|
||||
public constructor (str: string, private readonly filterMap: FilterMap, liquid: Liquid) {
|
||||
const tokenizer = new Tokenizer(str)
|
||||
this.initial = tokenizer.readValue()
|
||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args, liquid))
|
||||
}
|
||||
public * value (ctx: Context) {
|
||||
assert(ctx, () => 'unable to evaluate: context not defined')
|
||||
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name == "default"
|
||||
|
||||
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default'
|
||||
|
||||
let val = yield evalToken(this.initial, ctx, lenient)
|
||||
for (const filter of this.filters) {
|
||||
val = yield filter.render(val, ctx)
|
||||
|
||||
Reference in New Issue
Block a user