Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new features & code modernization #19

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d50c466
first iteration of code modernization
Apr 20, 2021
98e048c
ringo's generator implementation appearently breaks the prototype cha…
Apr 20, 2021
1caa746
use custom generator implemention, the former doesn't work in ringo v3.x
Apr 20, 2021
8d5e29b
output the test case name if assertion fails
Apr 20, 2021
5b32508
next step in code modernization, tests pass
Apr 20, 2021
26fa09c
replaced forEach callbacks with arrow functions (saves re-scoping)
Apr 21, 2021
2bde25b
fixed some regressions, replaced "in" checks with hasOwnProperty
Apr 21, 2021
fd7f780
code modernization
Apr 21, 2021
9f86f4b
fixed check
Apr 21, 2021
9b429a8
pass the environment to loader.loadTemplateSource
Apr 21, 2021
af7b230
use destructuring assignment
Apr 21, 2021
2b5231f
used arrow functions in various places
Apr 26, 2021
ad0ac94
fixed error template
Apr 27, 2021
d872ce6
minor jsdoc fixes
Apr 27, 2021
107a0af
use arrow functions where possible, fixed some minor warnings
Apr 27, 2021
f94fe9e
use native string.startsWith instead of strings module version
Apr 30, 2021
0fe25e7
slightly simplified retrieving block content in createToken
Apr 30, 2021
dc50416
removed noop functions from parser
May 1, 2021
95604b6
simplified loadTemplateSource
May 1, 2021
dbce4a8
modified safeJoin to use java.nio.Path(s)
May 1, 2021
ad9e0b9
simplified filter expression regex and tuned parsing
May 1, 2021
004c02d
simplified FilterExpression.prototype.resolve()
May 3, 2021
528b021
simplified regex used in splitContents
May 3, 2021
26272d5
removed unused split function
May 3, 2021
8dd214f
allow relative paths in include and extends nodes
May 5, 2021
15858b9
filesystem origin now contains the absolute file path
May 6, 2021
f2c9a25
cache include templates in the toplevel render context
May 6, 2021
ef7fcb2
prevent circular and self extends
May 6, 2021
25d4b09
re-added parser noop-functions - these are implemented by DebugParser
May 6, 2021
77f505a
fixed regression introduced in 25d4b0993ac97d27828cb2f436b7533bef403493
May 10, 2021
a144828
fixed token content replacements
May 10, 2021
83abbbf
enabled some more tests, these now pass too
May 10, 2021
9c9be43
simplified check if in verbatim node
May 10, 2021
090ebd2
Sets the default locale before running tests
botic Aug 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.iml
17 changes: 8 additions & 9 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ exports.TOKEN_MAPPING = {
TOKEN_TEXT: 'Text',
TOKEN_VAR: 'Var',
TOKEN_BLOCK: 'Block',
TOKEN_COMMENT: 'Comment',
}
TOKEN_COMMENT: 'Comment'
};

// template syntax constants
exports.FILTER_SEPARATOR = '|';
Expand All @@ -23,18 +23,17 @@ exports.COMMENT_TAG_END = '#}';
exports.SINGLE_BRACE_START = '{';
exports.SINGLE_BRACE_END = '}';

exports.ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.';
exports.ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.';

// match a variable or block tag and capture the entire tag, including start/end
// delimiters

var rEsc = exports.regexEscape = function(str) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
const rEsc = exports.regexEscape = (str) => {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
};

exports.tagRe = new RegExp('(' + rEsc(exports.BLOCK_TAG_START) + '.*?' + rEsc(exports.BLOCK_TAG_END) + '|' +
rEsc(exports.VARIABLE_TAG_START) + '.*?' + rEsc(exports.VARIABLE_TAG_END) + '|' +
rEsc(exports.COMMENT_TAG_START) + '.*?' + rEsc(exports.COMMENT_TAG_END) + ')', 'g');
rEsc(exports.VARIABLE_TAG_START) + '.*?' + rEsc(exports.VARIABLE_TAG_END) + '|' +
rEsc(exports.COMMENT_TAG_START) + '.*?' + rEsc(exports.COMMENT_TAG_END) + ')', 'g');

exports.TEMPLATE_STRING_IF_INVALID = '';
121 changes: 66 additions & 55 deletions lib/context.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,95 @@
var $o = require('ringo/utils/objects');
var {escape} = require('./utils/html');
var {isSafe, isMarkedForEscaping} = require('./utils');
const html = require('./utils/html');
const {isSafe, isMarkedForEscaping} = require('./utils');

var Context = exports.Context = function Context(dict, autoescape) {
this.autoescape = autoescape === false ? false : true;
this.dicts = dict ? [dict] : [{}]
this.renderContext = new RenderContext();
return this;
}
const Context = exports.Context = function(dict, autoescape) {
Object.defineProperties(this, {
autoescape: {value: autoescape !== false, writable: true, enumerable: true},
dicts: {value: dict ? [dict] : [{}], enumerable: true},
renderContext: {value: new RenderContext(), enumerable: true},
environment: {value: null, writable: true, enumerable: true}
});
return this;
};

Context.prototype.push = function() {
var d = {};
this.dicts.push(d);
return d;
}
const dict = {};
this.dicts.push(dict);
return dict;
};

Context.prototype.pop = function() {
if (this.dicts.length == 1) {
throw new Error('pop() more often called then push()');
}
return this.dicts.pop();
}
if (this.dicts.length === 1) {
throw new Error('pop() more often called then push()');
}
return this.dicts.pop();
};

/**
Get a variable's value, starting at the current context and going upward
*/
Get a variable's value, starting at the current context and going upward
*/
Context.prototype.get = function(key, otherwise) {
for (var i = this.dicts.length-1; i >= 0; i--) {
if (key in this.dicts[i]) {
return this.dicts[i][key];
}
}
return otherwise;
}
for (let i = this.dicts.length - 1; i >= 0; i--) {
let dict = this.dicts[i];
if (dict.hasOwnProperty(key)) {
return dict[key];
}
}
return otherwise;
};

Context.prototype.set = function(key, value) {
this.dicts[this.dicts.length-1][key] = value;
}
this.dicts[this.dicts.length - 1][key] = value;
};

Context.prototype.has = function(key) {
return this.get(key) !== undefined;
}
return this.get(key) !== undefined;
};

Context.prototype.update = function(obj) {
this.dicts.push(obj);
}
Context.prototype.update = function(dict) {
this.dicts.push(dict);
};

Context.prototype.new = function(dict) {
return new Context(dict, this.autoescape);
}
return new Context(dict, this.autoescape);
};

/**
* Converts any value to a string to become part of a rendered template. This
* means escaping, if required, and conversion to a unicode object. If value
* is a string, it is expected to have already been translated.
*/
*/
Context.prototype.renderValue = function(value) {
// @@ TODO
//value = template_localtime(value, use_tz=context.use_tz)
//value = localize(value, use_l10n=context.use_l10n)
if ((this.autoescape === true && isSafe(value) === false) || isMarkedForEscaping(value)) {
return escape(value);
}
return value;
}
// @@ TODO
//value = template_localtime(value, use_tz=context.use_tz)
//value = localize(value, use_l10n=context.use_l10n)
if ((this.autoescape === true && isSafe(value) === false)
|| isMarkedForEscaping(value)) {
return html.escape(value);
}
return value;
};

/**
*
*/
var RenderContext = exports.RenderContext = function RenderContext(dict) {
this.dicts = dict ? [dict] : [{}];
return this;
}
const RenderContext = exports.RenderContext = function(dict) {
Object.defineProperties(this, {
dicts: {
value: dict ? [dict] : [{}],
enumerable: true
}
});
return this;
};

RenderContext.prototype.push = Context.prototype.push;
RenderContext.prototype.pop = Context.prototype.pop;
RenderContext.prototype.set = Context.prototype.set;
RenderContext.prototype.has = Context.prototype.has;
RenderContext.prototype.get = function(key, otherwise) {
var d = this.dicts[this.dicts.length-1];
if (key in d) {
return d[key];
}
return otherwise;
}
const dict = this.dicts[this.dicts.length - 1];
if (dict.hasOwnProperty(key)) {
return dict[key];
}
return otherwise;
};
Loading