mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
feat: add layouts, partials apart from root, #395
This commit is contained in:
+32
-2
@@ -1,5 +1,5 @@
|
||||
import { ParseError } from '../util/error'
|
||||
import { Liquid } from '../liquid'
|
||||
import { Liquid, Tokenizer } from '../liquid'
|
||||
import { ParseStream } from './parse-stream'
|
||||
import { isTagToken, isOutputToken } from '../util/type-guards'
|
||||
import { OutputToken } from '../tokens/output-token'
|
||||
@@ -8,14 +8,31 @@ import { Output } from '../template/output'
|
||||
import { HTML } from '../template/html'
|
||||
import { Template } from '../template/template'
|
||||
import { TopLevelToken } from '../tokens/toplevel-token'
|
||||
import { Cache } from '../cache/cache'
|
||||
import { Loader, LookupType } from '../fs/loader'
|
||||
import { FS } from '../fs/fs'
|
||||
|
||||
export default class Parser {
|
||||
public parseFile: (file: string, sync?: boolean, type?: LookupType) => Iterator<Template[]>
|
||||
|
||||
private liquid: Liquid
|
||||
private fs: FS
|
||||
private cache: Cache<Template[]> | undefined
|
||||
private loader: Loader
|
||||
|
||||
public constructor (liquid: Liquid) {
|
||||
this.liquid = liquid
|
||||
this.cache = this.liquid.options.cache
|
||||
this.fs = this.liquid.options.fs
|
||||
this.parseFile = this.cache ? this._parseFileCached : this._parseFile
|
||||
this.loader = new Loader(this.liquid.options)
|
||||
}
|
||||
public parse (tokens: TopLevelToken[]) {
|
||||
public parse (html: string, filepath?: string): Template[] {
|
||||
const tokenizer = new Tokenizer(html, this.liquid.options.operatorsTrie, filepath)
|
||||
const tokens = tokenizer.readTopLevelTokens(this.liquid.options)
|
||||
return this.parseTokens(tokens)
|
||||
}
|
||||
public parseTokens (tokens: TopLevelToken[]) {
|
||||
let token
|
||||
const templates: Template[] = []
|
||||
while ((token = tokens.shift())) {
|
||||
@@ -39,4 +56,17 @@ export default class Parser {
|
||||
public parseStream (tokens: TopLevelToken[]) {
|
||||
return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens))
|
||||
}
|
||||
private * _parseFileCached (file: string, sync?: boolean, type: LookupType = LookupType.Root) {
|
||||
const key = type + ':' + file
|
||||
let templates = yield this.cache!.read(key)
|
||||
if (templates) return templates
|
||||
|
||||
templates = yield this._parseFile(file, sync)
|
||||
this.cache!.write(key, templates)
|
||||
return templates
|
||||
}
|
||||
private * _parseFile (file: string, sync?: boolean, type: LookupType = LookupType.Root) {
|
||||
const filepath = yield this.loader.lookup(file, type, sync)
|
||||
return this.liquid.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user