fix: break/continue omitting output before them, #123

BREAKING CHANGE: remove default export, now should be used like import
{Liquid} from 'liquidjs'
This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 854e12ba53
commit ae45c4622e
99 changed files with 373 additions and 407 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
const re = new RegExp(`(${identifier.source})\\s*=([^]*)`)
+6 -6
View File
@@ -1,10 +1,10 @@
import BlockMode from '../../context/block-mode'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import ITemplate from '../../template/itemplate'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { TagToken } from '../../parser/tag-token'
import { Token } from '../../parser/token'
import { ITemplate } from '../../template/itemplate'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { ParseStream } from '../../parser/parse-stream'
export default {
parse: function (token: TagToken, remainTokens: Token[]) {
+3 -6
View File
@@ -1,9 +1,6 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { ITemplate, Context, ITagImplOptions, TagToken, Token } from '../../types'
const re = new RegExp(`(${identifier.source})`)
@@ -17,7 +14,7 @@ export default {
const stream = this.liquid.parser.parseStream(remainTokens)
stream.on('tag:endcapture', () => stream.stop())
.on('template', (tpl) => this.templates.push(tpl))
.on('template', (tpl: ITemplate) => this.templates.push(tpl))
.on('end', () => {
throw new Error(`tag ${tagToken.raw} not closed`)
})
+5 -9
View File
@@ -1,10 +1,5 @@
import { Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { evalExp } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -30,15 +25,16 @@ export default {
stream.start()
},
render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = await evalExp(branch.val, ctx)
const cond = await evalExp(this.cond, ctx)
if (val === cond) {
return this.liquid.renderer.renderTemplates(branch.templates, ctx)
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
}
}
return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
+3 -3
View File
@@ -1,6 +1,6 @@
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Token } from '../../parser/token'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
+4 -4
View File
@@ -1,9 +1,9 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
const groupRE = new RegExp(`^(?:(${rValue.source})\\s*:\\s*)?(.*)$`)
const candidatesRE = new RegExp(rValue.source, 'g')
+4 -4
View File
@@ -1,8 +1,8 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { isNumber } from '../../util/underscore'
export default {
+7 -16
View File
@@ -1,15 +1,10 @@
import { Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { isString, isObject, isArray } from '../../util/underscore'
import { parseExp } from '../../render/syntax'
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier, value, hash } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { ForloopDrop } from '../../drop/forloop-drop'
import { Hash } from '../../template/tag/hash'
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
@@ -41,7 +36,7 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await parseExp(this.collection, ctx)
if (!isArray(collection)) {
@@ -63,20 +58,16 @@ export default {
const context = { forloop: new ForloopDrop(collection.length) }
ctx.push(context)
let html = ''
for (const item of collection) {
context[this.variable] = item
try {
html += await this.liquid.renderer.renderTemplates(this.templates, ctx)
await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
} catch (e) {
if (e.name === 'RenderBreakError') {
html += e.resolvedHTML
if (e.message === 'break') break
} else throw e
if (e.name !== 'RenderBreakError') throw e
if (e.message === 'break') break
}
context.forloop.next()
}
ctx.pop()
return html
}
} as ITagImplOptions
+5 -10
View File
@@ -1,10 +1,4 @@
import { evalExp, isTruthy } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { Hash, Emitter, evalExp, isTruthy, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -33,13 +27,14 @@ export default {
stream.start()
},
render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (const branch of this.branches) {
const cond = await evalExp(branch.cond, ctx)
if (isTruthy(cond)) {
return this.liquid.renderer.renderTemplates(branch.templates, ctx)
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
}
}
return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
+4 -8
View File
@@ -1,11 +1,8 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { Hash, Emitter, TagToken, Context, ITagImplOptions } from '../../types'
import { value, quotedLine } from '../../parser/lexical'
import { evalValue, parseValue } from '../../render/syntax'
import BlockMode from '../../context/block-mode'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITagImplOptions from '../../template/tag/itag-impl-options'
const staticFileRE = /[^\s,]+/
const withRE = new RegExp(`with\\s+(${value.source})`)
@@ -21,7 +18,7 @@ export default {
match = withRE.exec(token.args)
if (match) this.with = match[1]
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
@@ -45,10 +42,9 @@ export default {
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
const html = await this.liquid.renderer.renderTemplates(templates, ctx)
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
return html
}
} as ITagImplOptions
+2 -2
View File
@@ -1,7 +1,7 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import { isNumber } from '../../util/underscore'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
export default {
parse: function (token) {
+1 -1
View File
@@ -15,7 +15,7 @@ import tablerow from './tablerow'
import unless from './unless'
import Break from './break'
import Continue from './continue'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
const tags: { [key: string]: ITagImplOptions } = {
assign, 'for': For, capture, 'case': Case, comment, include, decrement, increment, cycle, 'if': If, layout, block, raw, tablerow, unless, 'break': Break, 'continue': Continue
+3 -6
View File
@@ -1,12 +1,9 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import { TagToken, Token, Context, ITagImplOptions } from '../../types'
import BlockMode from '../../context/block-mode'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { Hash } from '../../template/tag/hash'
const staticFileRE = /\S+/
+1 -3
View File
@@ -1,6 +1,4 @@
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken, Token, ITagImplOptions } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
+9 -18
View File
@@ -1,13 +1,6 @@
import assert from '../../util/assert'
import { evalExp } from '../../render/syntax'
import { assert } from '../../util/assert'
import { evalExp, Emitter, Hash, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { identifier, value, hash } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import ITemplate from '../../template/itemplate'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { TablerowloopDrop } from '../../drop/tablerowloop-drop'
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
@@ -35,7 +28,7 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await evalExp(this.collection, ctx) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit
@@ -47,19 +40,17 @@ export default {
const scope = { tablerowloop }
ctx.push(scope)
let html = ''
for (let idx = 0; idx < collection.length; idx++, tablerowloop.next()) {
scope[this.variable] = collection[idx]
if (tablerowloop.col0() === 0) {
if (tablerowloop.row() !== 1) html += '</tr>'
html += `<tr class="row${tablerowloop.row()}">`
if (tablerowloop.row() !== 1) emitter.write('</tr>')
emitter.write(`<tr class="row${tablerowloop.row()}">`)
}
html += `<td class="col${tablerowloop.col()}">`
html += await this.liquid.renderer.renderTemplates(this.templates, ctx)
html += '</td>'
emitter.write(`<td class="col${tablerowloop.col()}">`)
await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
emitter.write('</td>')
}
if (collection.length) html += '</tr>'
if (collection.length) emitter.write('</tr>')
ctx.pop()
return html
}
} as ITagImplOptions
+5 -10
View File
@@ -1,9 +1,4 @@
import { evalExp, isFalsy } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { Emitter, evalExp, isFalsy, ParseStream, Context, ITagImplOptions, Token, Hash , TagToken } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -25,10 +20,10 @@ export default {
stream.start()
},
render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const cond = await evalExp(this.cond, ctx)
return isFalsy(cond)
? this.liquid.renderer.renderTemplates(this.templates, ctx)
: this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
isFalsy(cond)
? await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
: await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
+6 -6
View File
@@ -1,11 +1,11 @@
import * as _ from '../util/underscore'
import { Drop } from '../drop/drop'
import { __assign } from 'tslib'
import assert from '../util/assert'
import { assert } from '../util/assert'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
import { Scope } from './scope'
export default class Context {
export class Context {
private scopes: Scope[] = [{}]
private registers = {}
public environments: Scope
@@ -24,8 +24,8 @@ export default class Context {
return [this.environments, ...this.scopes]
.reduce((ctx, val) => __assign(ctx, val), {})
}
public async get (path: string) {
const paths = await this.parseProp(path)
public get (path: string) {
const paths = this.parseProp(path)
let ctx = this.findScope(paths[0]) || this.environments
for (const path of paths) {
ctx = readProperty(ctx, path)
@@ -62,7 +62,7 @@ export default class Context {
* accessSeq("foo['b]r']") // ['foo', 'b]r']
* accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar'
*/
private async parseProp (str: string) {
private parseProp (str: string) {
str = String(str)
const seq: string[] = []
let name = ''
@@ -85,7 +85,7 @@ export default class Context {
assert(j !== -1, `unbalanced []: ${str}`)
name = str.slice(i + 1, j)
if (!/^[+-]?\d+$/.test(name)) { // foo[bar] vs. foo[1]
name = String(await this.get(name))
name = String(this.get(name))
}
push()
i = j + 1
+13 -19
View File
@@ -1,23 +1,22 @@
import Context from './context/context'
import * as Types from './types'
import { Context } from './context/context'
import fs from './fs/node'
import * as _ from './util/underscore'
import ITemplate from './template/itemplate'
import Tokenizer from './parser/tokenizer'
import Render from './render/render'
import Tag from './template/tag/tag'
import { ITemplate } from './template/itemplate'
import { Tokenizer } from './parser/tokenizer'
import { Render } from './render/render'
import { Tag } from './template/tag/tag'
import { Filter } from './template/filter/filter'
import Parser from './parser/parser'
import ITagImplOptions from './template/tag/itag-impl-options'
import Value from './template/value'
import { isTruthy, isFalsy, evalExp, evalValue } from './render/syntax'
import { ITagImplOptions } from './template/tag/itag-impl-options'
import { Value } from './template/value'
import builtinTags from './builtin/tags'
import builtinFilters from './builtin/filters'
import { LiquidOptions, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImplOptions } from './template/filter/filter-impl-options'
import IFS from './fs/ifs'
export * from './types'
export default class Liquid {
export class Liquid {
public options: NormalizedFullOptions
public renderer: Render
public parser: Parser
@@ -39,10 +38,10 @@ export default class Liquid {
const tokens = this.tokenizer.tokenize(html, filepath)
return this.parser.parse(tokens)
}
public render (tpl: ITemplate[], ctx?: object, opts?: LiquidOptions) {
public render (tpl: ITemplate[], scope?: object, opts?: LiquidOptions) {
const options = { ...this.options, ...normalize(opts) }
const scope = new Context(ctx, options)
return this.renderer.renderTemplates(tpl, scope)
const ctx = new Context(scope, options)
return this.renderer.renderTemplates(tpl, ctx)
}
public async parseAndRender (html: string, ctx?: object, opts?: LiquidOptions) {
const tpl = await this.parse(html)
@@ -92,10 +91,5 @@ export default class Liquid {
self.renderFile(filePath, ctx, opts).then(html => cb(null, html), cb)
}
}
public static default = Liquid
public static isTruthy = isTruthy
public static isFalsy = isFalsy
public static evalExp = evalExp
public static evalValue = evalValue
public static Types = Types
public static default = Liquid // compatible to import { Liquid } from 'liquidjs'
}
+2 -2
View File
@@ -1,7 +1,7 @@
import Token from './token'
import { Token } from './token'
import { last } from '../util/underscore'
export default class DelimitedToken extends Token {
export class DelimitedToken extends Token {
public constructor (
raw: string,
value: string,
+2 -2
View File
@@ -1,6 +1,6 @@
import Token from './token'
import { Token } from './token'
export default class HTMLToken extends Token {
export class HTMLToken extends Token {
public constructor (str: string, input: string, line: number, col: number, file?: string) {
super(str, input, line, col, file)
this.type = 'html'
+3 -3
View File
@@ -1,8 +1,8 @@
import DelimitedToken from './delimited-token'
import Token from './token'
import { DelimitedToken } from './delimited-token'
import { Token } from './token'
import { NormalizedFullOptions } from '../liquid-options'
export default class OutputToken extends DelimitedToken {
export class OutputToken extends DelimitedToken {
public constructor (
raw: string,
value: string,
+4 -4
View File
@@ -1,10 +1,10 @@
import Token from '../parser/token'
import ITemplate from '../template/itemplate'
import TagToken from './tag-token'
import { Token } from '../parser/token'
import { ITemplate } from '../template/itemplate'
import { TagToken } from './tag-token'
type ParseToken = ((token: Token, remainTokens: Token[]) => ITemplate)
export default class ParseStream {
export class ParseStream {
private tokens: Token[]
private handlers: {[key: string]: (arg: any) => void} = {}
private stopRequested: boolean = false
+9 -9
View File
@@ -1,13 +1,13 @@
import { ParseError } from '../util/error'
import Liquid from '../liquid'
import ParseStream from './parse-stream'
import Token from './token'
import TagToken from './tag-token'
import OutputToken from './output-token'
import Tag from '../template/tag/tag'
import Output from '../template/output'
import HTML from '../template/html'
import ITemplate from '../template/itemplate'
import { Liquid } from '../liquid'
import { ParseStream } from './parse-stream'
import { Token } from './token'
import { TagToken } from './tag-token'
import { OutputToken } from './output-token'
import { Tag } from '../template/tag/tag'
import { Output } from '../template/output'
import { HTML } from '../template/html'
import { ITemplate } from '../template/itemplate'
export default class Parser {
private liquid: Liquid
+3 -3
View File
@@ -1,10 +1,10 @@
import DelimitedToken from './delimited-token'
import Token from './token'
import { DelimitedToken } from './delimited-token'
import { Token } from './token'
import { TokenizationError } from '../util/error'
import * as lexical from './lexical'
import { NormalizedFullOptions } from '../liquid-options'
export default class TagToken extends DelimitedToken {
export class TagToken extends DelimitedToken {
public name: string
public args: string
public constructor (
+1 -1
View File
@@ -1,4 +1,4 @@
export default class Token {
export class Token {
public trimLeft: boolean = false
public trimRight: boolean = false
public type: string = 'notset'
+6 -6
View File
@@ -1,14 +1,14 @@
import whiteSpaceCtrl from './whitespace-ctrl'
import HTMLToken from './html-token'
import TagToken from './tag-token'
import Token from './token'
import OutputToken from './output-token'
import { whiteSpaceCtrl } from './whitespace-ctrl'
import { HTMLToken } from './html-token'
import { TagToken } from './tag-token'
import { Token } from './token'
import { OutputToken } from './output-token'
import { TokenizationError } from '../util/error'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
enum ParseState { HTML, OUTPUT, TAG }
export default class Tokenizer {
export class Tokenizer {
private options: NormalizedFullOptions
public constructor (options?: NormalizedFullOptions) {
this.options = applyDefault(options)
+4 -4
View File
@@ -1,9 +1,9 @@
import Token from '../parser/token'
import TagToken from '../parser/tag-token'
import HTMLToken from '../parser/html-token'
import { Token } from '../parser/token'
import { TagToken } from '../parser/tag-token'
import { HTMLToken } from '../parser/html-token'
import { NormalizedFullOptions } from '../liquid-options'
export default function whiteSpaceCtrl (tokens: Token[], options: NormalizedFullOptions) {
export function whiteSpaceCtrl (tokens: Token[], options: NormalizedFullOptions) {
options = { greedy: true, ...options }
let inRaw = false
+6 -13
View File
@@ -1,22 +1,15 @@
import { RenderError } from '../util/error'
import assert from '../util/assert'
import Context from '../context/context'
import ITemplate from '../template/itemplate'
import { Context } from '../context/context'
import { ITemplate } from '../template/itemplate'
import { Emitter } from './emitter'
export default class Render {
public async renderTemplates (templates: ITemplate[], ctx: Context) {
assert(ctx, 'unable to evalTemplates: context undefined')
const emitter = new Emitter()
export class Render {
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()) {
for (const tpl of templates) {
try {
emitter.write(await tpl.render(ctx, emitter))
await tpl.render(ctx, emitter)
} catch (e) {
if (e.name === 'RenderBreakError') {
e.resolvedHTML = emitter.html
throw e
}
if (e.name === 'RenderBreakError') throw e
throw e.name === 'RenderError' ? e : new RenderError(e, tpl)
}
}
+12 -12
View File
@@ -1,6 +1,6 @@
import * as lexical from '../parser/lexical'
import assert from '../util/assert'
import Context from '../context/context'
import { assert } from '../util/assert'
import { Context } from '../context/context'
import { range, last, isFunction, toValue } from '../util/underscore'
import { isComparable } from '../drop/icomparable'
import { NullDrop } from '../drop/null-drop'
@@ -45,7 +45,7 @@ const binaryOperators: {[key: string]: (lhs: any, rhs: any) => boolean} = {
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
}
export async function parseExp (exp: string, ctx: Context): Promise<any> {
export function parseExp (exp: string, ctx: Context): any {
assert(ctx, 'unable to parseExp: scope undefined')
const operatorREs = lexical.operators
let match
@@ -53,27 +53,27 @@ export async function parseExp (exp: string, ctx: Context): Promise<any> {
const operatorRE = operatorREs[i]
const expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`)
if ((match = exp.match(expRE))) {
const l = await parseExp(match[1], ctx)
const l = parseExp(match[1], ctx)
const op = binaryOperators[match[2].trim()]
const r = await parseExp(match[3], ctx)
const r = parseExp(match[3], ctx)
return op(l, r)
}
}
if ((match = exp.match(lexical.rangeLine))) {
const low = await evalValue(match[1], ctx)
const high = await evalValue(match[2], ctx)
const low = evalValue(match[1], ctx)
const high = evalValue(match[2], ctx)
return range(+low, +high + 1)
}
return parseValue(exp, ctx)
}
export async function evalExp (str: string, ctx: Context): Promise<any> {
return toValue(await parseExp(str, ctx))
export function evalExp (str: string, ctx: Context): any {
return toValue(parseExp(str, ctx))
}
export async function parseValue (str: string | undefined, ctx: Context): Promise<any> {
export function parseValue (str: string | undefined, ctx: Context): any {
if (!str) return null
str = str.trim()
@@ -87,8 +87,8 @@ export async function parseValue (str: string | undefined, ctx: Context): Promis
return ctx.get(str)
}
export async function evalValue (str: string | undefined, ctx: Context) {
return toValue(await parseValue(str, ctx))
export function evalValue (str: string | undefined, ctx: Context) {
return toValue(parseValue(str, ctx))
}
export function isTruthy (val: any): boolean {
+1 -1
View File
@@ -1,4 +1,4 @@
import Context from '../../context/context'
import { Context } from '../../context/context'
export interface FilterImpl {
context: Context;
+4 -4
View File
@@ -1,5 +1,5 @@
import { parseValue } from '../../render/syntax'
import Context from '../../context/context'
import { Context } from '../../context/context'
import { isArray } from '../../util/underscore'
import { FilterImplOptions } from './filter-impl-options'
@@ -21,11 +21,11 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
public async render (value: any, context: Context) {
public render (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args) {
if (isKeyValuePair(arg)) argv.push([arg[0], await parseValue(arg[1], context)])
else argv.push(await parseValue(arg, context))
if (isKeyValuePair(arg)) argv.push([arg[0], parseValue(arg[1], context)])
else argv.push(parseValue(arg, context))
}
return this.impl.apply({ context }, [value, ...argv])
}
+8 -6
View File
@@ -1,14 +1,16 @@
import Template from '../template/template'
import ITemplate from '../template/itemplate'
import HTMLToken from '../parser/html-token'
import { Template } from '../template/template'
import { ITemplate } from '../template/itemplate'
import { HTMLToken } from '../parser/html-token'
import { Context } from '../context/context'
import { Emitter } from '../render/emitter'
export default class extends Template<HTMLToken> implements ITemplate {
export class HTML extends Template<HTMLToken> implements ITemplate {
private str: string
public constructor (token: HTMLToken) {
super(token)
this.str = token.value
}
public async render (): Promise<string> {
return this.str
public render (ctx: Context, emitter: Emitter) {
emitter.write(this.str)
}
}
+4 -4
View File
@@ -1,8 +1,8 @@
import Context from '../context/context'
import Token from '../parser/token'
import { Context } from '../context/context'
import { Token } from '../parser/token'
import { Emitter } from '../render/emitter'
export default interface ITemplate {
export interface ITemplate {
token: Token;
render(ctx: Context, emitter: Emitter): Promise<string>;
render(ctx: Context, emitter: Emitter): Promise<void> | void;
}
+9 -8
View File
@@ -1,18 +1,19 @@
import Value from './value'
import { Value } from './value'
import { stringify, toValue } from '../util/underscore'
import Template from '../template/template'
import ITemplate from '../template/itemplate'
import Context from '../context/context'
import OutputToken from '../parser/output-token'
import { Template } from '../template/template'
import { ITemplate } from '../template/itemplate'
import { Context } from '../context/context'
import { Emitter } from '../render/emitter'
import { OutputToken } from '../parser/output-token'
export default class Output extends Template<OutputToken> implements ITemplate {
export class Output extends Template<OutputToken> implements ITemplate {
private value: Value
public constructor (token: OutputToken, strictFilters: boolean) {
super(token)
this.value = new Value(token.value, strictFilters)
}
public async render (ctx: Context): Promise<string> {
public async render (ctx: Context, emitter: Emitter) {
const val = await this.value.value(ctx)
return stringify(toValue(val))
emitter.write(stringify(toValue(val)))
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { hashCapture } from '../../parser/lexical'
import { parseValue } from '../../render/syntax'
import Context from '../../context/context'
import { Context } from '../../context/context'
/**
* Key-Value Pairs Representing Tag Arguments
@@ -8,7 +8,7 @@ import Context from '../../context/context'
* For the markup `{% include 'head.html' foo='bar' %}`,
* hash['foo'] === 'bar'
*/
export default class Hash {
export class Hash {
[key: string]: any
public static async create (markup: string, ctx: Context) {
const instance = new Hash()
+8 -7
View File
@@ -1,10 +1,11 @@
import Context from '../../context/context'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Hash from '../../template/tag/hash'
import ITagImpl from './itag-impl'
import { Context } from '../../context/context'
import { TagToken } from '../../parser/tag-token'
import { Token } from '../../parser/token'
import { ITagImpl } from './itag-impl'
import { Hash } from '../../template/tag/hash'
import { Emitter } from '../../render/emitter'
export default interface ITagImplOptions {
export interface ITagImplOptions {
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Token[]) => void;
render?: (this: ITagImpl, ctx: Context, hash: Hash) => any | Promise<any>;
render: (this: ITagImpl, ctx: Context, hash: Hash, emitter: Emitter) => void;
}
+3 -3
View File
@@ -1,7 +1,7 @@
import Liquid from '../../liquid'
import ITagImplOptions from './itag-impl-options'
import { Liquid } from '../../liquid'
import { ITagImplOptions } from './itag-impl-options'
export default interface ITagImpl extends ITagImplOptions {
export interface ITagImpl extends ITagImplOptions {
liquid: Liquid;
[key: string]: any;
}
+9 -13
View File
@@ -1,16 +1,11 @@
import { stringify, isFunction } from '../../util/underscore'
import assert from '../../util/assert'
import Context from '../../context/context'
import ITagImpl from './itag-impl'
import ITagImplOptions from './itag-impl-options'
import Liquid from '../../liquid'
import Hash from './hash'
import Template from '../../template/template'
import ITemplate from '../../template/itemplate'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import { assert } from '../../util/assert'
import { Liquid } from '../../liquid'
import { Template } from '../../template/template'
import { Emitter, Hash, Context, ITagImplOptions, TagToken, ITemplate, Token } from '../../types'
import { ITagImpl } from './itag-impl'
export default class Tag extends Template<TagToken> implements ITemplate {
export class Tag extends Template<TagToken> implements ITemplate {
public name: string
private impl: ITagImpl
private static impls: { [key: string]: ITagImplOptions } = {}
@@ -28,10 +23,11 @@ export default class Tag extends Template<TagToken> implements ITemplate {
this.impl.parse(token, tokens)
}
}
public async render (ctx: Context) {
public async render (ctx: Context, emitter: Emitter) {
const hash = await Hash.create(this.token.args, ctx)
const impl = this.impl
return isFunction(impl.render) ? stringify(await impl.render(ctx, hash)) : ''
const html = isFunction(impl.render) ? stringify(await impl.render(ctx, hash, emitter)) : ''
html && emitter.write(html)
}
public static register (name: string, tag: ITagImplOptions) {
Tag.impls[name] = tag
+1 -1
View File
@@ -1,4 +1,4 @@
export default abstract class Template<T> {
export abstract class Template<T> {
public token: T;
public constructor (token: T) {
this.token = token
+5 -5
View File
@@ -1,8 +1,8 @@
import { parseExp } from '../render/syntax'
import { FilterArgs, Filter } from './filter/filter'
import Context from '../context/context'
import { Context } from '../context/context'
export default class Value {
export class Value {
private strictFilters: boolean
private initial: string
private filters: Filter[] = []
@@ -47,10 +47,10 @@ export default class Value {
}
this.filters.push(new Filter(name, args, this.strictFilters))
}
public async value (ctx: Context) {
let val = await parseExp(this.initial, ctx)
public value (ctx: Context) {
let val = parseExp(this.initial, ctx)
for (const filter of this.filters) {
val = await filter.render(val, ctx)
val = filter.render(val, ctx)
}
return val
}
+9
View File
@@ -1,2 +1,11 @@
export { ParseError, TokenizationError, RenderBreakError, AssertionError } from './util/error'
export { Drop } from './drop/drop'
export { Emitter } from './render/emitter'
export { isTruthy, isFalsy, evalExp, evalValue } from './render/syntax'
export { TagToken } from './parser/tag-token'
export { Context } from './context/context'
export { ITemplate } from './template/itemplate'
export { ITagImplOptions } from './template/tag/itag-impl-options'
export { ParseStream } from './parser/parse-stream'
export { Token } from './parser/token'
export { Hash } from './template/tag/hash'
+1 -1
View File
@@ -1,6 +1,6 @@
import { AssertionError } from './error'
export default function<T> (predicate: T | null | undefined, message?: string) {
export function assert <T> (predicate: T | null | undefined, message?: string) {
if (!predicate) {
message = message || `expect ${predicate} to be true`
throw new AssertionError(message)
+2 -2
View File
@@ -1,6 +1,6 @@
import * as _ from './underscore'
import Token from '../parser/token'
import ITemplate from '../template/itemplate'
import { Token } from '../parser/token'
import { ITemplate } from '../template/itemplate'
abstract class LiquidError extends Error {
private token: Token