-
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
147 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
name: Release | ||
on: | ||
push: | ||
branches: | ||
- master | ||
on: workflow_dispatch | ||
jobs: | ||
release: | ||
name: Release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Emitter { | ||
write (html: any): void; | ||
end (): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { stringify, toValue } from '../util/underscore' | ||
|
||
export class KeepingTypeEmitter { | ||
public html: any = ''; | ||
|
||
public write (html: any) { | ||
html = toValue(html) | ||
// This will only preserve the type if the value is isolated. | ||
// I.E: | ||
// {{ my-port }} -> 42 | ||
// {{ my-host }}:{{ my-port }} -> 'host:42' | ||
if (typeof html !== 'string' && this.html === '') { | ||
this.html = html | ||
} else { | ||
this.html = stringify(this.html) + stringify(html) | ||
} | ||
} | ||
|
||
public end () { | ||
return this.html | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { stringify } from '../util/underscore' | ||
import { Emitter } from './emitter' | ||
|
||
export class SimpleEmitter implements Emitter { | ||
public html: any = ''; | ||
|
||
public write (html: any) { | ||
this.html += stringify(html) | ||
} | ||
|
||
public end () { | ||
return this.html | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { stringify } from '../util/underscore' | ||
|
||
export class StreamedEmitter { | ||
public html: any = ''; | ||
public stream = new (require('stream').PassThrough)() | ||
public write (html: any) { | ||
this.stream.write(stringify(html)) | ||
} | ||
public end () { | ||
this.stream.end() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters