mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
+14
-28
@@ -1,29 +1,15 @@
|
||||
import { stringify, toValue } from '../util/underscore'
|
||||
|
||||
export class Emitter {
|
||||
public html: any = '';
|
||||
public break = false;
|
||||
public continue = false;
|
||||
private keepOutputType? = false;
|
||||
|
||||
constructor (keepOutputType: boolean|undefined) {
|
||||
this.keepOutputType = keepOutputType
|
||||
}
|
||||
|
||||
public write (html: any) {
|
||||
if (this.keepOutputType === true) {
|
||||
html = toValue(html)
|
||||
} else {
|
||||
html = stringify(html)
|
||||
}
|
||||
// This will only preserve the type if the value is isolated.
|
||||
// I.E:
|
||||
// {{ my-port }} -> 42
|
||||
// {{ my-host }}:{{ my-port }} -> 'host:42'
|
||||
if (this.keepOutputType === true && typeof html !== 'string' && this.html === '') {
|
||||
this.html = html
|
||||
} else {
|
||||
this.html = stringify(this.html) + stringify(html)
|
||||
}
|
||||
}
|
||||
export interface Emitter {
|
||||
/**
|
||||
* Write a html value into emitter
|
||||
* @param html string, Drop or other primitive value
|
||||
*/
|
||||
write (html: any): void;
|
||||
/**
|
||||
* Notify the emitter render has ended
|
||||
*/
|
||||
end (): void;
|
||||
/**
|
||||
* Collect rendered string value immediately
|
||||
*/
|
||||
collect (): string;
|
||||
}
|
||||
|
||||
+15
-4
@@ -1,23 +1,34 @@
|
||||
import { RenderError } from '../util/error'
|
||||
import { Context } from '../context/context'
|
||||
import { Template } from '../template/template'
|
||||
import { Emitter } from './emitter'
|
||||
import { Emitter } from '../emitters/emitter'
|
||||
import { SimpleEmitter } from '../emitters/simple-emitter'
|
||||
import { StreamedEmitter } from '../emitters/streamed-emitter'
|
||||
import { toThenable } from '../util/async'
|
||||
import { KeepingTypeEmitter } from '../emitters/keeping-type-emitter'
|
||||
|
||||
export class Render {
|
||||
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
|
||||
const emitter = new StreamedEmitter()
|
||||
toThenable(this.renderTemplates(templates, ctx, emitter))
|
||||
return emitter.stream
|
||||
}
|
||||
public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator<any> {
|
||||
if (!emitter) {
|
||||
emitter = new Emitter(ctx.opts.keepOutputType)
|
||||
emitter = ctx.opts.keepOutputType ? new KeepingTypeEmitter() : new SimpleEmitter()
|
||||
}
|
||||
for (const tpl of templates) {
|
||||
try {
|
||||
// if tpl.render supports emitter, it'll return empty `html`
|
||||
const html = yield tpl.render(ctx, emitter)
|
||||
// if not, it'll return an `html`, write to the emitter for it
|
||||
html && emitter.write(html)
|
||||
if (emitter.break || emitter.continue) break
|
||||
if (emitter['break'] || emitter['continue']) break
|
||||
} catch (e) {
|
||||
const err = RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
return emitter.html
|
||||
return emitter.end()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user