| author | ymh <ymh.work@gmail.com> |
| Wed, 18 Dec 2024 15:24:41 +0100 | |
| changeset 1584 | 257c14dae52a |
| parent 1514 | 5869151a1f2f |
| permissions | -rwxr-xr-x |
| 940 | 1 |
/*! |
2 |
* mustache.js - Logic-less {{mustache}} templates with JavaScript |
|
3 |
* http://github.com/janl/mustache.js |
|
4 |
*/ |
|
| 598 | 5 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
6 |
/*global define: false Mustache: true*/ |
| 598 | 7 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
8 |
(function defineMustache (global, factory) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
9 |
if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') { |
| 940 | 10 |
factory(exports); // CommonJS |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
11 |
} else if (typeof define === 'function' && define.amd) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
12 |
define(['exports'], factory); // AMD |
| 940 | 13 |
} else { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
14 |
global.Mustache = {}; |
| 1514 | 15 |
factory(global.Mustache); // script, wsh, asp |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
16 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
17 |
}(this, function mustacheFactory (mustache) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
18 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
19 |
var objectToString = Object.prototype.toString; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
20 |
var isArray = Array.isArray || function isArrayPolyfill (object) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
21 |
return objectToString.call(object) === '[object Array]'; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
22 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
23 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
24 |
function isFunction (object) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
25 |
return typeof object === 'function'; |
| 940 | 26 |
} |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
27 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
28 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
29 |
* More correct typeof string handling array |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
30 |
* which normally returns typeof 'object' |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
31 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
32 |
function typeStr (obj) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
33 |
return isArray(obj) ? 'array' : typeof obj; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
34 |
} |
| 598 | 35 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
36 |
function escapeRegExp (string) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
37 |
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
38 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
39 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
40 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
41 |
* Null safe way of checking whether or not an object, |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
42 |
* including its prototype, has a given property |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
43 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
44 |
function hasProperty (obj, propName) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
45 |
return obj != null && typeof obj === 'object' && (propName in obj); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
46 |
} |
| 940 | 47 |
|
| 1514 | 48 |
/** |
49 |
* Safe way of detecting whether or not the given thing is a primitive and |
|
50 |
* whether it has the given property |
|
51 |
*/ |
|
52 |
function primitiveHasOwnProperty (primitive, propName) { |
|
53 |
return ( |
|
54 |
primitive != null |
|
55 |
&& typeof primitive !== 'object' |
|
56 |
&& primitive.hasOwnProperty |
|
57 |
&& primitive.hasOwnProperty(propName) |
|
58 |
); |
|
59 |
} |
|
60 |
||
| 940 | 61 |
// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577 |
62 |
// See https://github.com/janl/mustache.js/issues/189 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
63 |
var regExpTest = RegExp.prototype.test; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
64 |
function testRegExp (re, string) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
65 |
return regExpTest.call(re, string); |
| 598 | 66 |
} |
67 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
68 |
var nonSpaceRe = /\S/; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
69 |
function isWhitespace (string) { |
| 940 | 70 |
return !testRegExp(nonSpaceRe, string); |
| 598 | 71 |
} |
72 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
73 |
var entityMap = { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
74 |
'&': '&', |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
75 |
'<': '<', |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
76 |
'>': '>', |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
77 |
'"': '"', |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
78 |
"'": ''', |
| 1514 | 79 |
'/': '/', |
80 |
'`': '`', |
|
81 |
'=': '=' |
|
| 940 | 82 |
}; |
83 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
84 |
function escapeHtml (string) { |
| 1514 | 85 |
return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) { |
| 940 | 86 |
return entityMap[s]; |
| 598 | 87 |
}); |
88 |
} |
|
89 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
90 |
var whiteRe = /\s*/; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
91 |
var spaceRe = /\s+/; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
92 |
var equalsRe = /\s*=/; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
93 |
var curlyRe = /\s*\}/; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
94 |
var tagRe = /#|\^|\/|>|\{|&|=|!/; |
| 940 | 95 |
|
96 |
/** |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
97 |
* Breaks up the given `template` string into a tree of tokens. If the `tags` |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
98 |
* argument is given here it must be an array with two string values: the |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
99 |
* opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
100 |
* course, the default is to use mustaches (i.e. mustache.tags). |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
101 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
102 |
* A token is an array with at least 4 elements. The first element is the |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
103 |
* mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
104 |
* did not contain a symbol (i.e. {{myValue}}) this element is "name". For |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
105 |
* all text that appears outside a symbol this element is "text". |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
106 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
107 |
* The second element of a token is its "value". For mustache tags this is |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
108 |
* whatever else was inside the tag besides the opening symbol. For text tokens |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
109 |
* this is the text itself. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
110 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
111 |
* The third and fourth elements of the token are the start and end indices, |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
112 |
* respectively, of the token in the original template. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
113 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
114 |
* Tokens that are the root node of a subtree contain two more elements: 1) an |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
115 |
* array of tokens in the subtree and 2) the index in the original template at |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
116 |
* which the closing tag for that section begins. |
| 1514 | 117 |
* |
118 |
* Tokens for partials also contain two more elements: 1) a string value of |
|
119 |
* indendation prior to that tag and 2) the index of that tag on that line - |
|
120 |
* eg a value of 2 indicates the partial is the third tag on this line. |
|
| 940 | 121 |
*/ |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
122 |
function parseTemplate (template, tags) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
123 |
if (!template) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
124 |
return []; |
| 1514 | 125 |
var lineHasNonSpace = false; |
| 940 | 126 |
var sections = []; // Stack to hold section tokens |
127 |
var tokens = []; // Buffer to hold the tokens |
|
128 |
var spaces = []; // Indices of whitespace tokens on the current line |
|
129 |
var hasTag = false; // Is there a {{tag}} on the current line? |
|
130 |
var nonSpace = false; // Is there a non-space char on the current line? |
|
| 1514 | 131 |
var indentation = ''; // Tracks indentation for tags that use it |
132 |
var tagIndex = 0; // Stores a count of number of tags encountered on a line |
|
| 940 | 133 |
|
134 |
// Strips all whitespace tokens array for the current line |
|
135 |
// if there was a {{#tag}} on it and otherwise only space. |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
136 |
function stripSpace () { |
| 940 | 137 |
if (hasTag && !nonSpace) { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
138 |
while (spaces.length) |
| 940 | 139 |
delete tokens[spaces.pop()]; |
140 |
} else { |
|
141 |
spaces = []; |
|
| 598 | 142 |
} |
143 |
||
| 940 | 144 |
hasTag = false; |
145 |
nonSpace = false; |
|
146 |
} |
|
| 598 | 147 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
148 |
var openingTagRe, closingTagRe, closingCurlyRe; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
149 |
function compileTags (tagsToCompile) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
150 |
if (typeof tagsToCompile === 'string') |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
151 |
tagsToCompile = tagsToCompile.split(spaceRe, 2); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
152 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
153 |
if (!isArray(tagsToCompile) || tagsToCompile.length !== 2) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
154 |
throw new Error('Invalid tags: ' + tagsToCompile); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
155 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
156 |
openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*'); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
157 |
closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1])); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
158 |
closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1])); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
159 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
160 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
161 |
compileTags(tags || mustache.tags); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
162 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
163 |
var scanner = new Scanner(template); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
164 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
165 |
var start, type, value, chr, token, openSection; |
| 940 | 166 |
while (!scanner.eos()) { |
167 |
start = scanner.pos; |
|
| 598 | 168 |
|
| 940 | 169 |
// Match any text between tags. |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
170 |
value = scanner.scanUntil(openingTagRe); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
171 |
|
| 940 | 172 |
if (value) { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
173 |
for (var i = 0, valueLength = value.length; i < valueLength; ++i) { |
| 940 | 174 |
chr = value.charAt(i); |
| 598 | 175 |
|
| 940 | 176 |
if (isWhitespace(chr)) { |
177 |
spaces.push(tokens.length); |
|
| 1514 | 178 |
indentation += chr; |
| 598 | 179 |
} else { |
| 940 | 180 |
nonSpace = true; |
| 1514 | 181 |
lineHasNonSpace = true; |
182 |
indentation += ' '; |
|
| 598 | 183 |
} |
184 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
185 |
tokens.push([ 'text', chr, start, start + 1 ]); |
| 940 | 186 |
start += 1; |
| 598 | 187 |
|
| 940 | 188 |
// Check for whitespace on the current line. |
| 1514 | 189 |
if (chr === '\n') { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
190 |
stripSpace(); |
| 1514 | 191 |
indentation = ''; |
192 |
tagIndex = 0; |
|
193 |
lineHasNonSpace = false; |
|
194 |
} |
|
| 598 | 195 |
} |
196 |
} |
|
197 |
||
| 940 | 198 |
// Match the opening tag. |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
199 |
if (!scanner.scan(openingTagRe)) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
200 |
break; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
201 |
|
| 940 | 202 |
hasTag = true; |
| 598 | 203 |
|
| 940 | 204 |
// Get the tag type. |
205 |
type = scanner.scan(tagRe) || 'name'; |
|
206 |
scanner.scan(whiteRe); |
|
| 598 | 207 |
|
| 940 | 208 |
// Get the tag value. |
209 |
if (type === '=') { |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
210 |
value = scanner.scanUntil(equalsRe); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
211 |
scanner.scan(equalsRe); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
212 |
scanner.scanUntil(closingTagRe); |
| 940 | 213 |
} else if (type === '{') { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
214 |
value = scanner.scanUntil(closingCurlyRe); |
| 940 | 215 |
scanner.scan(curlyRe); |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
216 |
scanner.scanUntil(closingTagRe); |
| 940 | 217 |
type = '&'; |
| 598 | 218 |
} else { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
219 |
value = scanner.scanUntil(closingTagRe); |
| 598 | 220 |
} |
221 |
||
| 940 | 222 |
// Match the closing tag. |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
223 |
if (!scanner.scan(closingTagRe)) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
224 |
throw new Error('Unclosed tag at ' + scanner.pos); |
| 940 | 225 |
|
| 1514 | 226 |
if (type == '>') { |
227 |
token = [ type, value, start, scanner.pos, indentation, tagIndex, lineHasNonSpace ]; |
|
228 |
} else { |
|
229 |
token = [ type, value, start, scanner.pos ]; |
|
230 |
} |
|
231 |
tagIndex++; |
|
| 940 | 232 |
tokens.push(token); |
233 |
||
234 |
if (type === '#' || type === '^') { |
|
235 |
sections.push(token); |
|
236 |
} else if (type === '/') { |
|
237 |
// Check section nesting. |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
238 |
openSection = sections.pop(); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
239 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
240 |
if (!openSection) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
241 |
throw new Error('Unopened section "' + value + '" at ' + start); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
242 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
243 |
if (openSection[1] !== value) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
244 |
throw new Error('Unclosed section "' + openSection[1] + '" at ' + start); |
| 940 | 245 |
} else if (type === 'name' || type === '{' || type === '&') { |
246 |
nonSpace = true; |
|
247 |
} else if (type === '=') { |
|
248 |
// Set the tags for the next time around. |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
249 |
compileTags(value); |
| 598 | 250 |
} |
| 940 | 251 |
} |
| 598 | 252 |
|
| 1514 | 253 |
stripSpace(); |
254 |
||
| 940 | 255 |
// Make sure there are no open sections when we're done. |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
256 |
openSection = sections.pop(); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
257 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
258 |
if (openSection) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
259 |
throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
260 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
261 |
return nestTokens(squashTokens(tokens)); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
262 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
263 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
264 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
265 |
* Combines the values of consecutive text tokens in the given `tokens` array |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
266 |
* to a single token. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
267 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
268 |
function squashTokens (tokens) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
269 |
var squashedTokens = []; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
270 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
271 |
var token, lastToken; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
272 |
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
273 |
token = tokens[i]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
274 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
275 |
if (token) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
276 |
if (token[0] === 'text' && lastToken && lastToken[0] === 'text') { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
277 |
lastToken[1] += token[1]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
278 |
lastToken[3] = token[3]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
279 |
} else { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
280 |
squashedTokens.push(token); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
281 |
lastToken = token; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
282 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
283 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
284 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
285 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
286 |
return squashedTokens; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
287 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
288 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
289 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
290 |
* Forms the given array of `tokens` into a nested tree structure where |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
291 |
* tokens that represent a section have two additional items: 1) an array of |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
292 |
* all tokens that appear in that section and 2) the index in the original |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
293 |
* template that represents the end of that section. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
294 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
295 |
function nestTokens (tokens) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
296 |
var nestedTokens = []; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
297 |
var collector = nestedTokens; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
298 |
var sections = []; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
299 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
300 |
var token, section; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
301 |
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
302 |
token = tokens[i]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
303 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
304 |
switch (token[0]) { |
| 1514 | 305 |
case '#': |
306 |
case '^': |
|
307 |
collector.push(token); |
|
308 |
sections.push(token); |
|
309 |
collector = token[4] = []; |
|
310 |
break; |
|
311 |
case '/': |
|
312 |
section = sections.pop(); |
|
313 |
section[5] = token[2]; |
|
314 |
collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens; |
|
315 |
break; |
|
316 |
default: |
|
317 |
collector.push(token); |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
318 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
319 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
320 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
321 |
return nestedTokens; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
322 |
} |
| 940 | 323 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
324 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
325 |
* A simple string scanner that is used by the template parser to find |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
326 |
* tokens in template strings. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
327 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
328 |
function Scanner (string) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
329 |
this.string = string; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
330 |
this.tail = string; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
331 |
this.pos = 0; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
332 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
333 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
334 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
335 |
* Returns `true` if the tail is empty (end of string). |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
336 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
337 |
Scanner.prototype.eos = function eos () { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
338 |
return this.tail === ''; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
339 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
340 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
341 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
342 |
* Tries to match the given regular expression at the current position. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
343 |
* Returns the matched text if it can match, the empty string otherwise. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
344 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
345 |
Scanner.prototype.scan = function scan (re) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
346 |
var match = this.tail.match(re); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
347 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
348 |
if (!match || match.index !== 0) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
349 |
return ''; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
350 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
351 |
var string = match[0]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
352 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
353 |
this.tail = this.tail.substring(string.length); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
354 |
this.pos += string.length; |
| 940 | 355 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
356 |
return string; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
357 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
358 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
359 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
360 |
* Skips all text until the given regular expression can be matched. Returns |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
361 |
* the skipped string, which is the entire tail if no match can be made. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
362 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
363 |
Scanner.prototype.scanUntil = function scanUntil (re) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
364 |
var index = this.tail.search(re), match; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
365 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
366 |
switch (index) { |
| 1514 | 367 |
case -1: |
368 |
match = this.tail; |
|
369 |
this.tail = ''; |
|
370 |
break; |
|
371 |
case 0: |
|
372 |
match = ''; |
|
373 |
break; |
|
374 |
default: |
|
375 |
match = this.tail.substring(0, index); |
|
376 |
this.tail = this.tail.substring(index); |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
377 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
378 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
379 |
this.pos += match.length; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
380 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
381 |
return match; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
382 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
383 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
384 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
385 |
* Represents a rendering context by wrapping a view object and |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
386 |
* maintaining a reference to the parent context. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
387 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
388 |
function Context (view, parentContext) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
389 |
this.view = view; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
390 |
this.cache = { '.': this.view }; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
391 |
this.parent = parentContext; |
| 940 | 392 |
} |
| 598 | 393 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
394 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
395 |
* Creates a new context using the given view with this context |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
396 |
* as the parent. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
397 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
398 |
Context.prototype.push = function push (view) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
399 |
return new Context(view, this); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
400 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
401 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
402 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
403 |
* Returns the value of the given name in this context, traversing |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
404 |
* up the context hierarchy if the value is absent in this context's view. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
405 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
406 |
Context.prototype.lookup = function lookup (name) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
407 |
var cache = this.cache; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
408 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
409 |
var value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
410 |
if (cache.hasOwnProperty(name)) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
411 |
value = cache[name]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
412 |
} else { |
| 1514 | 413 |
var context = this, intermediateValue, names, index, lookupHit = false; |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
414 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
415 |
while (context) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
416 |
if (name.indexOf('.') > 0) { |
| 1514 | 417 |
intermediateValue = context.view; |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
418 |
names = name.split('.'); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
419 |
index = 0; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
420 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
421 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
422 |
* Using the dot notion path in `name`, we descend through the |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
423 |
* nested objects. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
424 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
425 |
* To be certain that the lookup has been successful, we have to |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
426 |
* check if the last object in the path actually has the property |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
427 |
* we are looking for. We store the result in `lookupHit`. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
428 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
429 |
* This is specially necessary for when the value has been set to |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
430 |
* `undefined` and we want to avoid looking up parent contexts. |
| 1514 | 431 |
* |
432 |
* In the case where dot notation is used, we consider the lookup |
|
433 |
* to be successful even if the last "object" in the path is |
|
434 |
* not actually an object but a primitive (e.g., a string, or an |
|
435 |
* integer), because it is sometimes useful to access a property |
|
436 |
* of an autoboxed primitive, such as the length of a string. |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
437 |
**/ |
| 1514 | 438 |
while (intermediateValue != null && index < names.length) { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
439 |
if (index === names.length - 1) |
| 1514 | 440 |
lookupHit = ( |
441 |
hasProperty(intermediateValue, names[index]) |
|
442 |
|| primitiveHasOwnProperty(intermediateValue, names[index]) |
|
443 |
); |
|
| 598 | 444 |
|
| 1514 | 445 |
intermediateValue = intermediateValue[names[index++]]; |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
446 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
447 |
} else { |
| 1514 | 448 |
intermediateValue = context.view[name]; |
449 |
||
450 |
/** |
|
451 |
* Only checking against `hasProperty`, which always returns `false` if |
|
452 |
* `context.view` is not an object. Deliberately omitting the check |
|
453 |
* against `primitiveHasOwnProperty` if dot notation is not used. |
|
454 |
* |
|
455 |
* Consider this example: |
|
456 |
* ``` |
|
457 |
* Mustache.render("The length of a football field is {{#length}}{{length}}{{/length}}.", {length: "100 yards"}) |
|
458 |
* ``` |
|
459 |
* |
|
460 |
* If we were to check also against `primitiveHasOwnProperty`, as we do |
|
461 |
* in the dot notation case, then render call would return: |
|
462 |
* |
|
463 |
* "The length of a football field is 9." |
|
464 |
* |
|
465 |
* rather than the expected: |
|
466 |
* |
|
467 |
* "The length of a football field is 100 yards." |
|
468 |
**/ |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
469 |
lookupHit = hasProperty(context.view, name); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
470 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
471 |
|
| 1514 | 472 |
if (lookupHit) { |
473 |
value = intermediateValue; |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
474 |
break; |
| 1514 | 475 |
} |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
476 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
477 |
context = context.parent; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
478 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
479 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
480 |
cache[name] = value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
481 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
482 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
483 |
if (isFunction(value)) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
484 |
value = value.call(this.view); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
485 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
486 |
return value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
487 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
488 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
489 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
490 |
* A Writer knows how to take a stream of tokens and render them to a |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
491 |
* string, given a context. It also maintains a cache of templates to |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
492 |
* avoid the need to parse the same template twice. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
493 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
494 |
function Writer () { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
495 |
this.cache = {}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
496 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
497 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
498 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
499 |
* Clears all cached templates in this writer. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
500 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
501 |
Writer.prototype.clearCache = function clearCache () { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
502 |
this.cache = {}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
503 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
504 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
505 |
/** |
| 1514 | 506 |
* Parses and caches the given `template` according to the given `tags` or |
507 |
* `mustache.tags` if `tags` is omitted, and returns the array of tokens |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
508 |
* that is generated from the parse. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
509 |
*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
510 |
Writer.prototype.parse = function parse (template, tags) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
511 |
var cache = this.cache; |
| 1514 | 512 |
var cacheKey = template + ':' + (tags || mustache.tags).join(':'); |
513 |
var tokens = cache[cacheKey]; |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
514 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
515 |
if (tokens == null) |
| 1514 | 516 |
tokens = cache[cacheKey] = parseTemplate(template, tags); |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
517 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
518 |
return tokens; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
519 |
}; |
| 940 | 520 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
521 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
522 |
* High-level method that is used to render the given `template` with |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
523 |
* the given `view`. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
524 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
525 |
* The optional `partials` argument may be an object that contains the |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
526 |
* names and templates of partials that are used in the template. It may |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
527 |
* also be a function that is used to load partial templates on the fly |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
528 |
* that takes a single argument: the name of the partial. |
| 1514 | 529 |
* |
530 |
* If the optional `tags` argument is given here it must be an array with two |
|
531 |
* string values: the opening and closing tags used in the template (e.g. |
|
532 |
* [ "<%", "%>" ]). The default is to mustache.tags. |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
533 |
*/ |
| 1514 | 534 |
Writer.prototype.render = function render (template, view, partials, tags) { |
535 |
var tokens = this.parse(template, tags); |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
536 |
var context = (view instanceof Context) ? view : new Context(view); |
| 1514 | 537 |
return this.renderTokens(tokens, context, partials, template, tags); |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
538 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
539 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
540 |
/** |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
541 |
* Low-level method that renders the given array of `tokens` using |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
542 |
* the given `context` and `partials`. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
543 |
* |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
544 |
* Note: The `originalTemplate` is only ever used to extract the portion |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
545 |
* of the original template that was contained in a higher-order section. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
546 |
* If the template doesn't use higher-order sections, this argument may |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
547 |
* be omitted. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
548 |
*/ |
| 1514 | 549 |
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
550 |
var buffer = ''; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
551 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
552 |
var token, symbol, value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
553 |
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
554 |
value = undefined; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
555 |
token = tokens[i]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
556 |
symbol = token[0]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
557 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
558 |
if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
559 |
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate); |
| 1514 | 560 |
else if (symbol === '>') value = this.renderPartial(token, context, partials, tags); |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
561 |
else if (symbol === '&') value = this.unescapedValue(token, context); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
562 |
else if (symbol === 'name') value = this.escapedValue(token, context); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
563 |
else if (symbol === 'text') value = this.rawValue(token); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
564 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
565 |
if (value !== undefined) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
566 |
buffer += value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
567 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
568 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
569 |
return buffer; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
570 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
571 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
572 |
Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
573 |
var self = this; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
574 |
var buffer = ''; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
575 |
var value = context.lookup(token[1]); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
576 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
577 |
// This function is used to render an arbitrary template |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
578 |
// in the current context by higher-order sections. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
579 |
function subRender (template) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
580 |
return self.render(template, context, partials); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
581 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
582 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
583 |
if (!value) return; |
| 940 | 584 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
585 |
if (isArray(value)) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
586 |
for (var j = 0, valueLength = value.length; j < valueLength; ++j) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
587 |
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
588 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
589 |
} else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
590 |
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
591 |
} else if (isFunction(value)) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
592 |
if (typeof originalTemplate !== 'string') |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
593 |
throw new Error('Cannot use higher-order sections without the original template'); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
594 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
595 |
// Extract the portion of the original template that the section contains. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
596 |
value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
597 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
598 |
if (value != null) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
599 |
buffer += value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
600 |
} else { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
601 |
buffer += this.renderTokens(token[4], context, partials, originalTemplate); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
602 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
603 |
return buffer; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
604 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
605 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
606 |
Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
607 |
var value = context.lookup(token[1]); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
608 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
609 |
// Use JavaScript's definition of falsy. Include empty arrays. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
610 |
// See https://github.com/janl/mustache.js/issues/186 |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
611 |
if (!value || (isArray(value) && value.length === 0)) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
612 |
return this.renderTokens(token[4], context, partials, originalTemplate); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
613 |
}; |
| 598 | 614 |
|
| 1514 | 615 |
Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) { |
616 |
var filteredIndentation = indentation.replace(/[^ \t]/g, ''); |
|
617 |
var partialByNl = partial.split('\n'); |
|
618 |
for (var i = 0; i < partialByNl.length; i++) { |
|
619 |
if (partialByNl[i].length && (i > 0 || !lineHasNonSpace)) { |
|
620 |
partialByNl[i] = filteredIndentation + partialByNl[i]; |
|
621 |
} |
|
622 |
} |
|
623 |
return partialByNl.join('\n'); |
|
624 |
}; |
|
625 |
||
626 |
Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) { |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
627 |
if (!partials) return; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
628 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
629 |
var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; |
| 1514 | 630 |
if (value != null) { |
631 |
var lineHasNonSpace = token[6]; |
|
632 |
var tagIndex = token[5]; |
|
633 |
var indentation = token[4]; |
|
634 |
var indentedValue = value; |
|
635 |
if (tagIndex == 0 && indentation) { |
|
636 |
indentedValue = this.indentPartial(value, indentation, lineHasNonSpace); |
|
637 |
} |
|
638 |
return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue); |
|
639 |
} |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
640 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
641 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
642 |
Writer.prototype.unescapedValue = function unescapedValue (token, context) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
643 |
var value = context.lookup(token[1]); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
644 |
if (value != null) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
645 |
return value; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
646 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
647 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
648 |
Writer.prototype.escapedValue = function escapedValue (token, context) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
649 |
var value = context.lookup(token[1]); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
650 |
if (value != null) |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
651 |
return mustache.escape(value); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
652 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
653 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
654 |
Writer.prototype.rawValue = function rawValue (token) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
655 |
return token[1]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
656 |
}; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
657 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
658 |
mustache.name = 'mustache.js'; |
| 1514 | 659 |
mustache.version = '3.1.0'; |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
660 |
mustache.tags = [ '{{', '}}' ]; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
661 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
662 |
// All high-level mustache.* functions use this writer. |
| 940 | 663 |
var defaultWriter = new Writer(); |
| 598 | 664 |
|
| 940 | 665 |
/** |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
666 |
* Clears all cached templates in the default writer. |
| 940 | 667 |
*/ |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
668 |
mustache.clearCache = function clearCache () { |
| 940 | 669 |
return defaultWriter.clearCache(); |
670 |
}; |
|
671 |
||
672 |
/** |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
673 |
* Parses and caches the given template in the default writer and returns the |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
674 |
* array of tokens it contains. Doing this ahead of time avoids the need to |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
675 |
* parse templates on the fly as they are rendered. |
| 940 | 676 |
*/ |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
677 |
mustache.parse = function parse (template, tags) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
678 |
return defaultWriter.parse(template, tags); |
| 940 | 679 |
}; |
| 598 | 680 |
|
| 940 | 681 |
/** |
682 |
* Renders the `template` with the given `view` and `partials` using the |
|
| 1514 | 683 |
* default writer. If the optional `tags` argument is given here it must be an |
684 |
* array with two string values: the opening and closing tags used in the |
|
685 |
* template (e.g. [ "<%", "%>" ]). The default is to mustache.tags. |
|
| 940 | 686 |
*/ |
| 1514 | 687 |
mustache.render = function render (template, view, partials, tags) { |
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
688 |
if (typeof template !== 'string') { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
689 |
throw new TypeError('Invalid template! Template should be a "string" ' + |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
690 |
'but "' + typeStr(template) + '" was given as the first ' + |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
691 |
'argument for mustache#render(template, view, partials)'); |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
692 |
} |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
693 |
|
| 1514 | 694 |
return defaultWriter.render(template, view, partials, tags); |
| 940 | 695 |
}; |
| 598 | 696 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
697 |
// This is here for backwards compatibility with 0.4.x., |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
698 |
/*eslint-disable */ // eslint wants camel cased function name |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
699 |
mustache.to_html = function to_html (template, view, partials, send) { |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
700 |
/*eslint-enable*/ |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
701 |
|
| 940 | 702 |
var result = mustache.render(template, view, partials); |
| 598 | 703 |
|
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
704 |
if (isFunction(send)) { |
| 940 | 705 |
send(result); |
706 |
} else { |
|
707 |
return result; |
|
| 598 | 708 |
} |
709 |
}; |
|
710 |
||
|
1304
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
711 |
// Export the escaping function so that the user may override it. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
712 |
// See https://github.com/janl/mustache.js/issues/244 |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
713 |
mustache.escape = escapeHtml; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
714 |
|
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
715 |
// Export these mainly for testing, but also for advanced usage. |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
716 |
mustache.Scanner = Scanner; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
717 |
mustache.Context = Context; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
718 |
mustache.Writer = Writer; |
|
10974bff4dae
upgrade metadataplayer + publish enmi 14and 15
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
940
diff
changeset
|
719 |
|
| 1514 | 720 |
return mustache; |
| 940 | 721 |
})); |