MCPcopy Create free account
hub / github.com/Justineo/github-hovercard / nestTokens

Function nestTokens

extensions/chrome/mustache.js:260–287  ·  view source on GitHub ↗

* Forms the given array of `tokens` into a nested tree structure where * tokens that represent a section have two additional items: 1) an array of * all tokens that appear in that section and 2) the index in the original * template that represents the end of that section.

(tokens)

Source from the content-addressed store, hash-verified

258 * template that represents the end of that section.
259 */
260 function nestTokens (tokens) {
261 var nestedTokens = [];
262 var collector = nestedTokens;
263 var sections = [];
264
265 var token, section;
266 for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
267 token = tokens[i];
268
269 switch (token[0]) {
270 case '#':
271 case '^':
272 collector.push(token);
273 sections.push(token);
274 collector = token[4] = [];
275 break;
276 case '/':
277 section = sections.pop();
278 section[5] = token[2];
279 collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
280 break;
281 default:
282 collector.push(token);
283 }
284 }
285
286 return nestedTokens;
287 }
288
289 /**
290 * A simple string scanner that is used by the template parser to find

Callers 1

parseTemplateFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected