feat: support filters in if/unless/case, see #287

This commit is contained in:
harttle
2021-02-12 22:22:41 +08:00
parent 4e82da689e
commit 2f059f6a25
18 changed files with 164 additions and 176 deletions
+4 -4
View File
@@ -17,12 +17,12 @@ export class Filter {
this.args = args
this.liquid = liquid
}
public * render (value: any, context: Context) {
public render (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args as FilterArg[]) {
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
else argv.push(yield evalToken(arg, context))
if (isKeyValuePair(arg)) argv.push([arg[0], evalToken(arg[1], context)])
else argv.push(evalToken(arg, context))
}
return yield this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
return this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
}
}
+3 -4
View File
@@ -1,5 +1,4 @@
import { Value } from './value'
import { FilterMap } from './filter/filter-map'
import { TemplateImpl } from '../template/template-impl'
import { Template } from '../template/template'
import { Context } from '../context/context'
@@ -9,12 +8,12 @@ import { Liquid } from '../liquid'
export class Output extends TemplateImpl<OutputToken> implements Template {
private value: Value
public constructor (token: OutputToken, filters: FilterMap, liquid: Liquid) {
public constructor (token: OutputToken, liquid: Liquid) {
super(token)
this.value = new Value(token.content, filters, liquid)
this.value = new Value(token.content, liquid)
}
public * render (ctx: Context, emitter: Emitter) {
const val = yield this.value.value(ctx)
const val = yield this.value.value(ctx, false)
emitter.write(val)
}
}
+8 -10
View File
@@ -1,27 +1,25 @@
import { evalToken } from '../render/expression'
import { Expression } from '../render/expression'
import { Tokenizer } from '../parser/tokenizer'
import { FilterMap } from '../template/filter/filter-map'
import { Filter } from './filter/filter'
import { Context } from '../context/context'
import { ValueToken } from '../tokens/value-token'
import { Liquid } from '../liquid'
export class Value {
public readonly filters: Filter[] = []
public readonly initial?: ValueToken
public readonly initial: Expression
/**
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
*/
public constructor (str: string, private readonly filterMap: FilterMap, liquid: Liquid) {
public constructor (str: string, liquid: Liquid) {
const tokenizer = new Tokenizer(str, liquid.options.operatorsTrie)
this.initial = tokenizer.readValue()
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args, liquid))
this.initial = tokenizer.readExpression()
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, liquid.filters.get(name), args, liquid))
}
public * value (ctx: Context) {
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default'
public * value (ctx: Context, lenient: boolean) {
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
let val = yield this.initial.evaluate(ctx, lenient)
let val = yield evalToken(this.initial, ctx, lenient)
for (const filter of this.filters) {
val = yield filter.render(val, ctx)
}