This commit is contained in:
harttle
2018-08-21 01:18:13 +08:00
parent bc9d8f92af
commit 4d4a2d04ec
25 changed files with 485 additions and 395 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
import * as lexical from './lexical.js'
import {evalValue} from './syntax.js'
import assert from './util/assert.js'
import {assign} from './util/underscore.js'
import {assign, create} from './util/underscore.js'
const valueRE = new RegExp(`${lexical.value.source}`, 'g')
@@ -50,7 +50,7 @@ export default function (options) {
}
function construct (str) {
const instance = Object.create(_filterInstance)
const instance = create(_filterInstance)
return instance.parse(str)
}
+5 -7
View File
@@ -7,7 +7,6 @@ import * as tokenizer from './tokenizer.js'
import {statFileAsync, readFileAsync} from './util/fs.js'
import path from 'path'
import {valid as isValidUrl, extname, resolve} from './util/url.js'
import * as lexical from './lexical.js'
import Render from './render.js'
import Tag from './tag.js'
import Filter from './filter.js'
@@ -151,7 +150,7 @@ export default function Liquid (options) {
}, options)
options.root = normalizeStringArray(options.root)
const engine = Object.create(_engine)
const engine = _.create(_engine)
engine.init(Tag(), Filter(options), options)
return engine
}
@@ -165,9 +164,8 @@ Liquid.Types = {
TokenizationError,
RenderBreakError,
AssertionError,
AssignScope: Object.create(null),
CaptureScope: Object.create(null),
IncrementScope: Object.create(null),
DecrementScope: Object.create(null)
AssignScope: {},
CaptureScope: {},
IncrementScope: {},
DecrementScope: {}
}
Liquid.lexical = lexical
+2 -1
View File
@@ -1,4 +1,5 @@
import * as lexical from './lexical.js'
import {create} from './util/underscore.js'
import {ParseError} from './util/error.js'
import assert from './util/assert.js'
@@ -92,7 +93,7 @@ export default function (Tag, Filter) {
}
function parseStream (tokens) {
const s = Object.create(stream)
const s = create(stream)
return s.init(tokens)
}
+2 -2
View File
@@ -1,6 +1,6 @@
import {evalExp} from './syntax.js'
import {RenderBreakError, RenderError} from './util/error.js'
import {stringify} from './util/underscore.js'
import {stringify, create} from './util/underscore.js'
import assert from './util/assert.js'
const render = {
@@ -57,6 +57,6 @@ const render = {
}
export default function () {
const instance = Object.create(render)
const instance = create(render)
return instance
}
+2 -2
View File
@@ -4,7 +4,7 @@ import assert from './util/assert.js'
const Scope = {
getAll: function () {
return this.contexts.reduce((ctx, val) => Object.assign(ctx, val), Object.create(null))
return this.contexts.reduce((ctx, val) => _.assign(ctx, val), _.create(null))
},
get: function (path) {
const paths = this.propertyAccessSeq(path)
@@ -162,7 +162,7 @@ export function factory (ctx, opts) {
blocks: {},
root: []
}
const scope = Object.create(Scope)
const scope = _.create(Scope)
scope.opts = _.assign(defaultOptions, opts)
scope.contexts = [ctx || {}]
return scope
+3 -2
View File
@@ -1,4 +1,5 @@
import {hashCapture} from './lexical.js'
import {create} from './util/underscore.js'
import {evalValue} from './syntax.js'
import assert from './util/assert.js'
@@ -33,7 +34,7 @@ export default function () {
const tagImpl = tagImpls[this.name]
assert(tagImpl, `tag ${this.name} not found`)
this.tagImpl = Object.create(tagImpl)
this.tagImpl = create(tagImpl)
if (this.tagImpl.parse) {
this.tagImpl.parse(token, tokens)
}
@@ -45,7 +46,7 @@ export default function () {
}
function construct (token, tokens) {
const instance = Object.create(_tagInstance)
const instance = create(_tagInstance)
instance.parse(token, tokens)
return instance
}
+4 -3
View File
@@ -1,8 +1,9 @@
import assert from '../util/assert.js'
import {identifier} from '../lexical.js'
import {create} from '../util/underscore.js'
export default function (liquid, Liquid) {
const rIdentifier = Liquid.lexical.identifier
const re = new RegExp(`(${rIdentifier.source})\\s*=(.*)`)
const re = new RegExp(`(${identifier.source})\\s*=(.*)`)
const {AssignScope} = Liquid.Types
liquid.registerTag('assign', {
@@ -13,7 +14,7 @@ export default function (liquid, Liquid) {
this.value = match[2]
},
render: function (scope) {
const ctx = Object.create(AssignScope)
const ctx = create(AssignScope)
ctx[this.key] = liquid.evalValue(this.value, scope)
scope.push(ctx)
return Promise.resolve('')
+4 -3
View File
@@ -1,8 +1,9 @@
import assert from '../util/assert.js'
import {create} from '../util/underscore.js'
import {identifier} from '../lexical.js'
export default function (liquid, Liquid) {
const rIdentifier = Liquid.lexical.identifier
const re = new RegExp(`(${rIdentifier.source})`)
const re = new RegExp(`(${identifier.source})`)
const {CaptureScope} = Liquid.Types
liquid.registerTag('capture', {
@@ -23,7 +24,7 @@ export default function (liquid, Liquid) {
},
render: async function (scope, hash) {
const html = await liquid.renderer.renderTemplates(this.templates, scope)
const ctx = Object.create(CaptureScope)
const ctx = create(CaptureScope)
ctx[this.variable] = html
scope.push(ctx)
}
+1 -1
View File
@@ -1,7 +1,7 @@
import assert from '../util/assert.js'
import {value as rValue} from '../lexical.js'
export default function (liquid, Liquid) {
const rValue = Liquid.lexical.value
const groupRE = new RegExp(`^(?:(${rValue.source})\\s*:\\s*)?(.*)$`)
const candidatesRE = new RegExp(rValue.source, 'g')
+6 -5
View File
@@ -1,12 +1,13 @@
import {create} from '../util/underscore.js'
import assert from '../util/assert.js'
import {identifier} from '../lexical.js'
export default function (liquid, Liquid) {
const lexical = Liquid.lexical
const {CaptureScope, AssignScope, DecrementScope} = Liquid.Types
liquid.registerTag('decrement', {
parse: function (token) {
const match = token.args.match(lexical.identifier)
const match = token.args.match(identifier)
assert(match, `illegal identifier ${token.args}`)
this.variable = match[0]
},
@@ -14,12 +15,12 @@ export default function (liquid, Liquid) {
let context = scope.findContextFor(
this.variable,
ctx => {
return Object.getPrototypeOf(ctx) !== CaptureScope &&
Object.getPrototypeOf(ctx) !== AssignScope
const proto = Object.getPrototypeOf(ctx)
return proto !== CaptureScope && proto !== AssignScope
}
)
if (!context) {
context = Object.create(DecrementScope)
context = create(DecrementScope)
scope.unshift(context)
}
if (typeof context[this.variable] !== 'number') {
+5 -5
View File
@@ -1,15 +1,15 @@
import {mapSeries} from '../util/promise.js'
import {isString, isObject} from '../util/underscore.js'
import assert from '../util/assert.js'
import {identifier, value, hash} from '../lexical.js'
export default function (liquid, Liquid) {
const RenderBreakError = Liquid.Types.RenderBreakError
const lexical = Liquid.lexical
const re = new RegExp(`^(${lexical.identifier.source})\\s+in\\s+` +
`(${lexical.value.source})` +
`(?:\\s+${lexical.hash.source})*` +
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
`(?:\\s+${hash.source})*` +
`(?:\\s+(reversed))?` +
`(?:\\s+${lexical.hash.source})*$`)
`(?:\\s+${hash.source})*$`)
liquid.registerTag('for', {parse, render})
+4 -4
View File
@@ -1,10 +1,10 @@
import assert from '../util/assert.js'
import {value, quotedLine} from '../lexical.js'
const staticFileRE = /[^\s,]+/
export default function (liquid, Liquid) {
const lexical = Liquid.lexical
const withRE = new RegExp(`with\\s+(${lexical.value.source})`)
const withRE = new RegExp(`with\\s+(${value.source})`)
liquid.registerTag('include', {
parse: function (token) {
@@ -13,7 +13,7 @@ export default function (liquid, Liquid) {
this.staticValue = match[0]
}
match = lexical.value.exec(token.args)
match = value.exec(token.args)
if (match) {
this.value = match[0]
}
@@ -26,7 +26,7 @@ export default function (liquid, Liquid) {
render: async function (scope, hash) {
let filepath
if (scope.opts.dynamicPartials) {
if (lexical.quotedLine.exec(this.value)) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = await liquid.parseAndRender(template, scope.getAll(), scope.opts)
} else {
+6 -5
View File
@@ -1,12 +1,13 @@
import assert from '../util/assert.js'
import {create} from '../util/underscore.js'
import {identifier} from '../lexical.js'
export default function (liquid, Liquid) {
const lexical = Liquid.lexical
const {CaptureScope, AssignScope, IncrementScope} = Liquid.Types
liquid.registerTag('increment', {
parse: function (token) {
const match = token.args.match(lexical.identifier)
const match = token.args.match(identifier)
assert(match, `illegal identifier ${token.args}`)
this.variable = match[0]
},
@@ -14,12 +15,12 @@ export default function (liquid, Liquid) {
let context = scope.findContextFor(
this.variable,
ctx => {
return Object.getPrototypeOf(ctx) !== CaptureScope &&
Object.getPrototypeOf(ctx) !== AssignScope
const proto = Object.getPrototypeOf(ctx)
return proto !== CaptureScope && proto !== AssignScope
}
)
if (!context) {
context = Object.create(IncrementScope)
context = create(IncrementScope)
scope.unshift(context)
}
if (typeof context[this.variable] !== 'number') {
+1 -1
View File
@@ -1,4 +1,5 @@
import assert from '../util/assert.js'
import {value as rValue} from '../lexical.js'
/*
* blockMode:
@@ -7,7 +8,6 @@ import assert from '../util/assert.js'
*/
export default function (liquid, Liquid) {
const rValue = Liquid.lexical.value
const staticFileRE = /\S+/
liquid.registerTag('layout', {
+4 -4
View File
@@ -1,11 +1,11 @@
import {mapSeries} from '../util/promise.js'
import assert from '../util/assert.js'
import {identifier, value, hash} from '../lexical.js'
export default function (liquid, Liquid) {
const lexical = Liquid.lexical
const re = new RegExp(`^(${lexical.identifier.source})\\s+in\\s+` +
`(${lexical.value.source})` +
`(?:\\s+${lexical.hash.source})*$`)
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
`(?:\\s+${hash.source})*$`)
liquid.registerTag('tablerow', {
+5 -5
View File
@@ -24,7 +24,7 @@ function initLiquidError (err, token) {
export function TokenizationError (message, token) {
initLiquidError.call(this, {message: message}, token)
}
TokenizationError.prototype = Object.create(Error.prototype)
TokenizationError.prototype = _.create(Error.prototype)
TokenizationError.prototype.constructor = TokenizationError
export function ParseError (e, token) {
@@ -33,7 +33,7 @@ export function ParseError (e, token) {
initLiquidError.call(this, e, token)
}
ParseError.prototype = Object.create(Error.prototype)
ParseError.prototype = _.create(Error.prototype)
ParseError.prototype.constructor = ParseError
export function RenderError (e, tpl) {
@@ -46,21 +46,21 @@ export function RenderError (e, tpl) {
initLiquidError.call(this, e, tpl.token)
}
RenderError.prototype = Object.create(Error.prototype)
RenderError.prototype = _.create(Error.prototype)
RenderError.prototype.constructor = RenderError
export function RenderBreakError (message) {
initError.call(this)
this.message = message + ''
}
RenderBreakError.prototype = Object.create(Error.prototype)
RenderBreakError.prototype = _.create(Error.prototype)
RenderBreakError.prototype.constructor = RenderBreakError
export function AssertionError (message) {
initError.call(this)
this.message = message + ''
}
AssertionError.prototype = Object.create(Error.prototype)
AssertionError.prototype = _.create(Error.prototype)
AssertionError.prototype.constructor = AssertionError
function mkContext (input, line) {
+6 -5
View File
@@ -2,14 +2,11 @@ const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
]
const monthNamesShort = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec'
]
const dayNames = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
]
const dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const monthNamesShort = monthNames.map(abbr)
const dayNamesShort = dayNames.map(abbr)
const suffixes = {
1: 'st',
2: 'nd',
@@ -17,6 +14,10 @@ const suffixes = {
'default': 'th'
}
function abbr (str) {
return str.slice(0, 3)
}
// prototype extensions
const _date = {
daysInMonth: function (d) {
+5 -1
View File
@@ -35,6 +35,10 @@ export function stringify (value) {
})
}
export function create (proto) {
return Object.create(proto)
}
export function isNil (value) {
return value === null || value === undefined
}
@@ -45,7 +49,7 @@ export function isArray (value) {
}
export function isError (value) {
const signature = Object.prototype.toString.call(value)
const signature = toStr.call(value)
// [object XXXError]
return signature.substr(-6, 5) === 'Error' ||
(typeof value.message === 'string' && typeof value.name === 'string')