refactor: Tag class support in registerTag()

This commit is contained in:
Jun Yang
2022-11-27 14:04:01 +08:00
parent 1f6ce7c822
commit 92992689cd
145 changed files with 694 additions and 768 deletions
+11
View File
@@ -0,0 +1,11 @@
import type { Context } from '../context'
import type { Liquid } from '../liquid'
export interface FilterImpl {
context: Context;
liquid: Liquid;
}
export interface FilterImplOptions {
(this: FilterImpl, value: any, ...args: any[]): any;
}
@@ -1,9 +1,9 @@
import { _evalToken } from '../../render/expression'
import { Context } from '../../context/context'
import { identify } from '../../util/underscore'
import { _evalToken } from '../render'
import { Context } from '../context'
import { identify } from '../util/underscore'
import { FilterImplOptions } from './filter-impl-options'
import { FilterArg, isKeyValuePair } from '../../parser/filter-arg'
import { Liquid } from '../../liquid'
import { FilterArg, isKeyValuePair } from '../parser/filter-arg'
import { Liquid } from '../liquid'
export class Filter {
public name: string
@@ -11,7 +11,7 @@ export class Filter {
private impl: FilterImplOptions
private liquid: Liquid
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[], liquid: Liquid) {
public constructor (name: string, impl: FilterImplOptions | undefined, args: FilterArg[], liquid: Liquid) {
this.name = name
this.impl = impl || identify
this.args = args
@@ -1,5 +0,0 @@
import { FilterImpl } from './filter-impl'
export interface FilterImplOptions {
(this: FilterImpl, value: any, ...args: any[]): any;
}
-7
View File
@@ -1,7 +0,0 @@
import { Context } from '../../context/context'
import { Liquid } from '../../liquid'
export interface FilterImpl {
context: Context;
liquid: Liquid;
}
-28
View File
@@ -1,28 +0,0 @@
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,
private readonly liquid: Liquid
) {}
get (name: string) {
const impl = this.impls[name]
assert(impl || !this.strictFilters, () => `undefined filter: ${name}`)
return impl
}
set (name: string, impl: FilterImplOptions) {
this.impls[name] = impl
}
create (name: string, args: FilterArg[]) {
return new Filter(name, this.get(name), args, this.liquid)
}
}
@@ -1,10 +1,9 @@
import { _evalToken } from '../../render/expression'
import { Context } from '../../context/context'
import { Tokenizer } from '../../parser/tokenizer'
import { _evalToken } from '../render/expression'
import { Context } from '../context/context'
import { Tokenizer } from '../parser/tokenizer'
import { Token } from '../tokens/token'
export interface HashValue {
[key: string]: any;
}
type HashValueTokens = Record<string, Token | undefined>
/**
* Key-Value Pairs Representing Tag Arguments
@@ -15,14 +14,14 @@ export interface HashValue {
* hash['reversed'] === undefined
*/
export class Hash {
hash: HashValue = {}
hash: HashValueTokens = {}
constructor (markup: string, jekyllStyle?: boolean) {
const tokenizer = new Tokenizer(markup, {})
for (const hash of tokenizer.readHashes(jekyllStyle)) {
this.hash[hash.name.content] = hash.value
}
}
* render (ctx: Context): Generator<unknown, HashValue, unknown> {
* render (ctx: Context): Generator<unknown, Record<string, any>, unknown> {
const hash = {}
for (const key of Object.keys(this.hash)) {
hash[key] = this.hash[key] === undefined ? true : yield _evalToken(this.hash[key], ctx)
+4 -5
View File
@@ -1,8 +1,7 @@
import { TemplateImpl } from '../template/template-impl'
import { Template } from '../template/template'
import { HTMLToken } from '../tokens/html-token'
import { Context } from '../context/context'
import { Emitter } from '../emitters/emitter'
import { TemplateImpl, Template } from '../template'
import { HTMLToken } from '../tokens'
import { Context } from '../context'
import { Emitter } from '../emitters'
export class HTML extends TemplateImpl<HTMLToken> implements Template {
private str: string
+10
View File
@@ -0,0 +1,10 @@
export * from './template'
export * from './template-impl'
export * from './tag'
export * from './tag-options-adapter'
export * from './filter'
export * from './filter-impl-options'
export * from './hash'
export * from './value'
export * from './output'
export * from './html'
+2 -3
View File
@@ -1,11 +1,10 @@
import { Value } from './value'
import { TemplateImpl } from '../template/template-impl'
import { Template } from '../template/template'
import { Template, TemplateImpl } from '../template'
import { Context } from '../context/context'
import { Emitter } from '../emitters/emitter'
import { OutputToken } from '../tokens/output-token'
import { Liquid } from '../liquid'
import { Filter } from './filter/filter'
import { Filter } from './filter'
export class Output extends TemplateImpl<OutputToken> implements Template {
private value: Value
+27
View File
@@ -0,0 +1,27 @@
import { isFunction } from '../util'
import { Hash } from './hash'
import { Tag, TagClass, TagRenderReturn } from './tag'
import { TagToken, TopLevelToken } from '../tokens'
import { Emitter } from '../emitters'
import { Context } from '../context'
import type { Liquid } from '../liquid'
export interface TagImplOptions {
parse?: (this: Tag, token: TagToken, remainingTokens: TopLevelToken[]) => void;
render: (this: Tag, ctx: Context, emitter: Emitter, hash: Record<string, any>) => TagRenderReturn;
}
export function createTagClass (options: TagImplOptions): TagClass {
return class extends Tag {
constructor (token: TagToken, tokens: TopLevelToken[], liquid: Liquid) {
super(token, tokens, liquid)
if (isFunction(options.parse)) {
options.parse.call(this, token, tokens)
}
}
* render (ctx: Context, emitter: Emitter): TagRenderReturn {
const hash = (yield new Hash(this.token.args).render(ctx)) as Record<string, any>
return yield options.render.call(this, ctx, emitter, hash)
}
}
}
+24
View File
@@ -0,0 +1,24 @@
import { TemplateImpl } from './template-impl'
import type { Emitter } from '../emitters/emitter'
import type { Context } from '../context/context'
import type { TopLevelToken, TagToken } from '../tokens'
import type { Template } from './template'
import type { Liquid } from '../liquid'
export type TagRenderReturn = Generator<unknown, unknown, unknown> | Promise<unknown> | unknown
export abstract class Tag extends TemplateImpl<TagToken> implements Template {
public name: string
protected liquid: Liquid
public constructor (token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) {
super(token)
this.name = token.name
this.liquid = liquid
}
public abstract render (ctx: Context, emitter: Emitter): TagRenderReturn;
}
export interface TagClass {
new(token: TagToken, tokens: TopLevelToken[], liquid: Liquid): Tag
}
-11
View File
@@ -1,11 +0,0 @@
import { Context } from '../../context/context'
import { TagToken } from '../../tokens/tag-token'
import { TopLevelToken } from '../../tokens/toplevel-token'
import { TagImpl } from './tag-impl'
import { HashValue } from '../../template/tag/hash'
import { Emitter } from '../../emitters/emitter'
export interface TagImplOptions {
parse?: (this: TagImpl, token: TagToken, remainingTokens: TopLevelToken[]) => void;
render: (this: TagImpl, ctx: Context, emitter: Emitter, hash: HashValue) => void | string | Promise<void | string> | Generator<unknown, void | string, unknown>;
}
-7
View File
@@ -1,7 +0,0 @@
import { Liquid } from '../../liquid'
import { TagImplOptions } from './tag-impl-options'
export interface TagImpl extends TagImplOptions {
liquid: Liquid;
[key: string]: any;
}
-16
View File
@@ -1,16 +0,0 @@
import { TagImplOptions } from './tag-impl-options'
import { assert } from '../../util/assert'
export class TagMap {
private impls: {[key: string]: TagImplOptions} = {}
get (name: string) {
const impl = this.impls[name]
assert(impl, () => `tag "${name}" not found`)
return impl
}
set (name: string, impl: TagImplOptions) {
this.impls[name] = impl
}
}
-29
View File
@@ -1,29 +0,0 @@
import { isFunction } from '../../util/underscore'
import { Liquid } from '../../liquid'
import { TemplateImpl } from '../../template/template-impl'
import { Emitter, Hash, Context, TagToken, Template, TopLevelToken } from '../../types'
import { TagImpl } from './tag-impl'
import { HashValue } from './hash'
export class Tag extends TemplateImpl<TagToken> implements Template {
public name: string
private impl: TagImpl
public constructor (token: TagToken, tokens: TopLevelToken[], liquid: Liquid) {
super(token)
this.name = token.name
const impl = liquid.tags.get(token.name)
this.impl = Object.create(impl)
this.impl.liquid = liquid
if (this.impl.parse) {
this.impl.parse(token, tokens)
}
}
public * render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, HashValue | unknown> {
const hash = (yield new Hash(this.token.args).render(ctx)) as HashValue
const impl = this.impl
if (isFunction(impl.render)) return yield impl.render(ctx, emitter, hash)
}
}
+12 -6
View File
@@ -1,8 +1,9 @@
import { Expression } from '../render/expression'
import { Tokenizer } from '../parser/tokenizer'
import { Filter } from './filter/filter'
import { Context } from '../context/context'
import { Liquid } from '../liquid'
import { Filter } from './filter'
import { Expression } from '../render'
import { Tokenizer } from '../parser'
import { assert } from '../util'
import type { Liquid } from '../liquid'
import type { Context } from '../context'
export class Value {
public readonly filters: Filter[] = []
@@ -14,7 +15,7 @@ export class Value {
public constructor (str: string, liquid: Liquid) {
const tokenizer = new Tokenizer(str, liquid.options.operators)
this.initial = tokenizer.readExpression()
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, liquid.filters.get(name), args, liquid))
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.getFilter(liquid, name), args, liquid))
}
public * value (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
@@ -25,4 +26,9 @@ export class Value {
}
return val
}
private getFilter (liquid: Liquid, name: string) {
const impl = liquid.filters[name]
assert(impl || !liquid.options.strictFilters, () => `undefined filter: ${name}`)
return impl
}
}