Skip to content

Commit

Permalink
fix: address refactor comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixcell authored and harttle committed Dec 16, 2020
1 parent b8e4b33 commit 6a0ad10
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/builtin/tags/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
const r = this.liquid.renderer
const html = childDefined !== undefined
? 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) {
blocks[this.block] = html
Expand Down
3 changes: 1 addition & 2 deletions src/builtin/tags/capture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types'
import { evalQuotedToken } from '../../render/expression'
import { Emitter } from '../../render/emitter'

export default {
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) {
Expand All @@ -20,7 +19,7 @@ export default {
},
render: function * (ctx: Context) {
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
}
} as TagImplOptions
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (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))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
Expand Down
6 changes: 3 additions & 3 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (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))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)

// render the remaining tokens immediately
ctx.setRegister('blockMode', BlockMode.STORE)
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
const templates = yield liquid._parseFile(filepath, ctx.opts, ctx.sync)
ctx.push(yield hash.render(ctx))
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()
emitter.write(partial)
}
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (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))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
Expand Down
7 changes: 7 additions & 0 deletions src/render/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { stringify, toValue } from '../util/underscore'

export class Emitter {
public html: any = '';
public break = false;
Expand All @@ -9,6 +11,11 @@ export class Emitter {
}

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.
// I.E:
// {{ my-port }} -> 42
Expand Down
5 changes: 4 additions & 1 deletion src/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Template } from '../template/template'
import { Emitter } from './emitter'

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) {
try {
const html = yield tpl.render(ctx, emitter)
Expand Down
7 changes: 1 addition & 6 deletions src/template/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Value } from './value'
import { FilterMap } from './filter/filter-map'
import { stringify, toValue } from '../util/underscore'
import { TemplateImpl } from '../template/template-impl'
import { Template } from '../template/template'
import { Context } from '../context/context'
Expand All @@ -16,10 +15,6 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
}
public * render (ctx: Context, emitter: Emitter) {
const val = yield this.value.value(ctx)
if (ctx.opts.keepOutputType) {
emitter.write(toValue(val))
} else {
emitter.write(stringify(toValue(val)))
}
emitter.write(val)
}
}

0 comments on commit 6a0ad10

Please sign in to comment.