mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: with & for in render tag, closes #195
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { FilterImplOptions } from './filter-impl-options'
|
||||
import { Filter, FilterArgs } from './filter'
|
||||
import { Filter } from './filter'
|
||||
import { FilterArg } from '../../parser/filter-arg'
|
||||
import { assert } from '../../util/assert'
|
||||
|
||||
export class FilterMap {
|
||||
@@ -17,7 +18,7 @@ export class FilterMap {
|
||||
this.impls[name] = impl
|
||||
}
|
||||
|
||||
create (name: string, args: FilterArgs) {
|
||||
create (name: string, args: FilterArg[]) {
|
||||
return new Filter(name, this.get(name), args)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
import { Expression } from '../../render/expression'
|
||||
import { Context } from '../../context/context'
|
||||
import { isArray, identify } from '../../util/underscore'
|
||||
import { identify } from '../../util/underscore'
|
||||
import { FilterImplOptions } from './filter-impl-options'
|
||||
|
||||
type KeyValuePair = [string?, string?]
|
||||
type FilterArg = string|KeyValuePair
|
||||
export type FilterArgs = FilterArg[]
|
||||
import { FilterArg, isKeyValuePair } from '../../parser/filter-arg'
|
||||
|
||||
export class Filter {
|
||||
public name: string
|
||||
public args: FilterArgs
|
||||
public args: FilterArg[]
|
||||
private impl: FilterImplOptions
|
||||
|
||||
public constructor (name: string, impl: FilterImplOptions, args: FilterArgs) {
|
||||
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[]) {
|
||||
this.name = name
|
||||
this.impl = impl || identify
|
||||
this.args = args
|
||||
@@ -26,7 +23,3 @@ export class Filter {
|
||||
return this.impl.apply({ context }, [value, ...argv])
|
||||
}
|
||||
}
|
||||
|
||||
function isKeyValuePair (arr: FilterArg): arr is KeyValuePair {
|
||||
return isArray(arr)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ export class HTML extends TemplateImpl<HTMLToken> implements Template {
|
||||
private str: string
|
||||
public constructor (token: HTMLToken) {
|
||||
super(token)
|
||||
this.str = token.value
|
||||
this.str = token.content
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter): IterableIterator<void> {
|
||||
emitter.write(this.str)
|
||||
|
||||
@@ -11,7 +11,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||
private value: Value
|
||||
public constructor (token: OutputToken, filters: FilterMap) {
|
||||
super(token)
|
||||
this.value = new Value(token.value, filters)
|
||||
this.value = new Value(token.content, filters)
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter) {
|
||||
const val = yield this.value.value(ctx)
|
||||
|
||||
+13
-16
@@ -1,31 +1,28 @@
|
||||
import { hashCapture } from '../../parser/lexical'
|
||||
import { Expression } from '../../render/expression'
|
||||
import { Context } from '../../context/context'
|
||||
import { Tokenizer } from '../../parser/tokenizer'
|
||||
|
||||
/**
|
||||
* Key-Value Pairs Representing Tag Arguments
|
||||
* Example:
|
||||
* For the markup `{% include 'head.html' foo='bar' %}`,
|
||||
* For the markup `, foo:'bar', coo:2 reversed %}`,
|
||||
* hash['foo'] === 'bar'
|
||||
* hash['coo'] === 2
|
||||
* hash['reversed'] === undefined
|
||||
*/
|
||||
export class Hash {
|
||||
[key: string]: any
|
||||
private static parse (markup: string) {
|
||||
const instance = new Hash()
|
||||
let match
|
||||
hashCapture.lastIndex = 0
|
||||
while ((match = hashCapture.exec(markup))) {
|
||||
const k = match[1]
|
||||
const v = match[2]
|
||||
instance[k] = v
|
||||
constructor (markup: string) {
|
||||
const tokenizer = new Tokenizer(markup)
|
||||
for (const [name, value] of tokenizer.readHashes()) {
|
||||
this[name] = value
|
||||
}
|
||||
return instance
|
||||
}
|
||||
public static * create (markup: string, ctx: Context) {
|
||||
const instance = Hash.parse(markup)
|
||||
for (const key of Object.keys(instance)) {
|
||||
instance[key] = yield new Expression(instance[key]).evaluate(ctx)
|
||||
* render (ctx: Context) {
|
||||
const hash = {}
|
||||
for (const key of Object.keys(this)) {
|
||||
hash[key] = yield new Expression(this[key]).evaluate(ctx)
|
||||
}
|
||||
return instance
|
||||
return hash
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,5 @@ import { Emitter } from '../../render/emitter'
|
||||
|
||||
export interface TagImplOptions {
|
||||
parse?: (this: TagImpl, token: TagToken, remainingTokens: Token[]) => void;
|
||||
render: (this: TagImpl, ctx: Context, hash: Hash, emitter: Emitter) => any;
|
||||
render: (this: TagImpl, ctx: Context, emitter: Emitter, hash: Hash) => any;
|
||||
}
|
||||
|
||||
@@ -22,8 +22,8 @@ export class Tag extends TemplateImpl<TagToken> implements Template {
|
||||
}
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter) {
|
||||
const hash = yield Hash.create(this.token.args, ctx)
|
||||
const hash = yield new Hash(this.token.args).render(ctx)
|
||||
const impl = this.impl
|
||||
if (isFunction(impl.render)) return yield impl.render(ctx, hash, emitter)
|
||||
if (isFunction(impl.render)) return yield impl.render(ctx, emitter, hash)
|
||||
}
|
||||
}
|
||||
|
||||
+6
-62
@@ -1,6 +1,7 @@
|
||||
import { Expression } from '../render/expression'
|
||||
import { Tokenizer } from '../parser/tokenizer'
|
||||
import { FilterMap } from '../template/filter/filter-map'
|
||||
import { FilterArgs, Filter } from './filter/filter'
|
||||
import { Filter } from './filter/filter'
|
||||
import { Context } from '../context/context'
|
||||
|
||||
export class Value {
|
||||
@@ -8,43 +9,12 @@ export class Value {
|
||||
public readonly initial: string
|
||||
|
||||
/**
|
||||
* @param str value string, like: "i have a dream | truncate: 3
|
||||
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
||||
*/
|
||||
public constructor (str: string, private readonly filterMap: FilterMap) {
|
||||
const tokens = Value.tokenize(str)
|
||||
this.initial = tokens[0]
|
||||
this.parseFilters(tokens, 1)
|
||||
}
|
||||
private parseFilters (tokens: string[], begin: number) {
|
||||
let i = begin
|
||||
while (i < tokens.length) {
|
||||
if (tokens[i] !== '|') {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
const j = ++i
|
||||
while (i < tokens.length && tokens[i] !== '|') i++
|
||||
this.parseFilter(tokens, j, i)
|
||||
}
|
||||
}
|
||||
private parseFilter (tokens: string[], begin: number, end: number) {
|
||||
const name = tokens[begin]
|
||||
const args: FilterArgs = []
|
||||
let argName, argValue
|
||||
for (let i = begin + 1; i < end + 1; i++) {
|
||||
if (i === end || tokens[i] === ',') {
|
||||
if (argName || argValue) {
|
||||
args.push(argName ? [argName, argValue] : argValue as string)
|
||||
}
|
||||
argValue = argName = undefined
|
||||
} else if (tokens[i] === ':') {
|
||||
argName = argValue
|
||||
argValue = undefined
|
||||
} else if (argValue === undefined) {
|
||||
argValue = tokens[i]
|
||||
}
|
||||
}
|
||||
this.filters.push(new Filter(name, this.filterMap.get(name), args))
|
||||
const tokenizer = new Tokenizer(str)
|
||||
this.initial = tokenizer.readValue()
|
||||
this.filters = tokenizer.readFilterTokens().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
||||
}
|
||||
public * value (ctx: Context) {
|
||||
let val = yield new Expression(this.initial).evaluate(ctx)
|
||||
@@ -53,30 +23,4 @@ export class Value {
|
||||
}
|
||||
return val
|
||||
}
|
||||
public static tokenize (str: string): ('|' | ',' | ':' | string)[] {
|
||||
const tokens = []
|
||||
let i = 0
|
||||
while (i < str.length) {
|
||||
const ch = str[i]
|
||||
if (ch === '"' || ch === "'") {
|
||||
const j = i
|
||||
for (i += 2; i < str.length && str[i - 1] !== ch; ++i);
|
||||
tokens.push(str.slice(j, i))
|
||||
} else if (/\s/.test(ch)) {
|
||||
i++
|
||||
} else if (/[|,:]/.test(ch)) {
|
||||
tokens.push(str[i++])
|
||||
} else {
|
||||
const j = i++
|
||||
let ch
|
||||
for (; i < str.length && !/[|,:\s]/.test(ch = str[i]); ++i) {
|
||||
if (ch === '"' || ch === "'") {
|
||||
for (i += 2; i < str.length && str[i - 1] !== ch; ++i);
|
||||
}
|
||||
}
|
||||
tokens.push(str.slice(j, i))
|
||||
}
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user