fix: address refactor comments

This commit is contained in:
Alex de Wergifosse
2020-12-16 10:20:10 +08:00
committed by Jun Yang
parent b8e4b3337b
commit 6a0ad102a8
8 changed files with 19 additions and 15 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ export default {
const r = this.liquid.renderer const r = this.liquid.renderer
const html = childDefined !== undefined const html = childDefined !== undefined
? childDefined ? childDefined
: yield r.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType)) : yield r.renderTemplates(this.tpls, ctx)
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) { if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html blocks[this.block] = html
+1 -2
View File
@@ -1,6 +1,5 @@
import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types' import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types'
import { evalQuotedToken } from '../../render/expression' import { evalQuotedToken } from '../../render/expression'
import { Emitter } from '../../render/emitter'
export default { export default {
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) { parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) {
@@ -20,7 +19,7 @@ export default {
}, },
render: function * (ctx: Context) { render: function * (ctx: Context) {
const r = this.liquid.renderer const r = this.liquid.renderer
const html = yield r.renderTemplates(this.templates, ctx, new Emitter(ctx.opts.keepOutputType)) const html = yield r.renderTemplates(this.templates, ctx)
ctx.bottom()[this.variable] = html ctx.bottom()[this.variable] = html
} }
} as TagImplOptions } as TagImplOptions
+1 -1
View File
@@ -26,7 +26,7 @@ export default {
const { renderer } = liquid const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file) ? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
: yield evalToken(file, ctx)) : yield evalToken(file, ctx))
: file.getText() : file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
+3 -3
View File
@@ -16,7 +16,7 @@ export default {
const { renderer } = liquid const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file) ? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
: evalToken(this.file, ctx)) : evalToken(this.file, ctx))
: file.getText() : file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
@@ -24,12 +24,12 @@ export default {
// render the remaining tokens immediately // render the remaining tokens immediately
ctx.setRegister('blockMode', BlockMode.STORE) ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks') const blocks = ctx.getRegister('blocks')
const html = yield renderer.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType)) const html = yield renderer.renderTemplates(this.tpls, ctx)
if (blocks[''] === undefined) blocks[''] = html if (blocks[''] === undefined) blocks[''] = html
const templates = yield liquid._parseFile(filepath, ctx.opts, ctx.sync) const templates = yield liquid._parseFile(filepath, ctx.opts, ctx.sync)
ctx.push(yield hash.render(ctx)) ctx.push(yield hash.render(ctx))
ctx.setRegister('blockMode', BlockMode.OUTPUT) ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = yield renderer.renderTemplates(templates, ctx, new Emitter(ctx.opts.keepOutputType)) const partial = yield renderer.renderTemplates(templates, ctx)
ctx.pop() ctx.pop()
emitter.write(partial) emitter.write(partial)
} }
+1 -1
View File
@@ -44,7 +44,7 @@ export default {
const { renderer } = liquid const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file) ? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
: evalToken(file, ctx)) : evalToken(file, ctx))
: file.getText() : file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
+7
View File
@@ -1,3 +1,5 @@
import { stringify, toValue } from '../util/underscore'
export class Emitter { export class Emitter {
public html: any = ''; public html: any = '';
public break = false; public break = false;
@@ -9,6 +11,11 @@ export class Emitter {
} }
public write (html: any) { public write (html: any) {
if (this.keepOutputType === true) {
html = toValue(html)
} else {
html = stringify(toValue(html))
}
// This will only preserve the type if the value is isolated. // This will only preserve the type if the value is isolated.
// I.E: // I.E:
// {{ my-port }} -> 42 // {{ my-port }} -> 42
+4 -1
View File
@@ -4,7 +4,10 @@ import { Template } from '../template/template'
import { Emitter } from './emitter' import { Emitter } from './emitter'
export class Render { export class Render {
public * renderTemplates (templates: Template[], ctx: Context, emitter: Emitter): IterableIterator<any> { public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator<any> {
if (!emitter) {
emitter = new Emitter(ctx.opts.keepOutputType)
}
for (const tpl of templates) { for (const tpl of templates) {
try { try {
const html = yield tpl.render(ctx, emitter) const html = yield tpl.render(ctx, emitter)
+1 -6
View File
@@ -1,6 +1,5 @@
import { Value } from './value' import { Value } from './value'
import { FilterMap } from './filter/filter-map' import { FilterMap } from './filter/filter-map'
import { stringify, toValue } from '../util/underscore'
import { TemplateImpl } from '../template/template-impl' import { TemplateImpl } from '../template/template-impl'
import { Template } from '../template/template' import { Template } from '../template/template'
import { Context } from '../context/context' import { Context } from '../context/context'
@@ -16,10 +15,6 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
} }
public * render (ctx: Context, emitter: Emitter) { public * render (ctx: Context, emitter: Emitter) {
const val = yield this.value.value(ctx) const val = yield this.value.value(ctx)
if (ctx.opts.keepOutputType) { emitter.write(val)
emitter.write(toValue(val))
} else {
emitter.write(stringify(toValue(val)))
}
} }
} }