Codebase list mustache-d / 3add8ec
New upstream release. Debian Janitor 2 years ago
2 changed file(s) with 36 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
0 mustache-d (0.1.5-1) UNRELEASED; urgency=low
1
2 * New upstream release.
3
4 -- Debian Janitor <janitor@jelmer.uk> Thu, 19 Aug 2021 15:18:04 -0000
5
06 mustache-d (0.1.4-1) unstable; urgency=medium
17
28 * New upstream version: 0.1.4
145145 * //{{^repo}}No repos :({{/repo}}
146146 * // to
147147 * //No repos :(
148 * context["foo"] = "bar"; // not set to "repo"
148 * context["foo"] = "bar"; // not set to "repo"
149149 * -----
150150 */
151151 static final class Context
216216 return !list.length;
217217 }
218218 }
219
219
220220 /* Convenience function */
221221 @safe @property
222222 static Section nil() nothrow
234234
235235 public:
236236 @safe
237 this(in Context context = null) nothrow
237 this(const Context context = null) nothrow
238238 {
239239 parent = context;
240240 }
332332 * size = reserve size for avoiding reallocation
333333 *
334334 * Returns:
335 * new Context object that added to $(D_PARAM key) section list.
335 * new Context object that added to $(D_PARAM key) section list.
336336 */
337337 @trusted
338338 Context addSubContext(in String key, lazy size_t size = 1)
356356 *
357357 * Params:
358358 * key = key string to fetch
359 *
359 *
360360 * Returns:
361361 * a $(D_PARAM key) associated value. null if key does not exist.
362362 */
364364 String fetch(in String[] key, lazy Handler handler = null) const
365365 {
366366 assert(key.length > 0);
367
367
368368 if (key.length == 1) {
369369 auto result = key[0] in variables;
370370
382382 return *result;
383383 }
384384 }
385
385
386386 return handler is null ? null : handler()(keyToString(key));
387387 }
388388
390390 const(Section) fetchSection()(in String[] key) const /* nothrow */
391391 {
392392 assert(key.length > 0);
393
393
394394 // Ascend context tree to find the key's beginning
395395 auto currentSection = key[0] in sections;
396396 if (currentSection is null) {
399399
400400 return parent.fetchSection(key);
401401 }
402
402
403403 // Decend context tree to match the rest of the key
404404 size_t keyIndex = 0;
405405 while (currentSection) {
406406 // Matched the entire key?
407407 if (keyIndex == key.length-1)
408408 return currentSection.empty ? Section.nil : *currentSection;
409
409
410410 if (currentSection.type != SectionType.list)
411411 return Section.nil; // Can't decend any further
412
412
413413 // Find next part of key
414414 keyIndex++;
415415 foreach (c; currentSection.list)
429429 auto result = fetchSection(key);
430430 if (result.type == type)
431431 return result.empty ? null : mixin("result." ~ to!string(type));
432
432
433433 return null;
434434 }
435435
629629 render(name, context, sink);
630630 return sink.data;
631631 }
632
632
633633 /**
634634 * OutputRange version of $(D render).
635635 */
709709 static void encode(in String text, ref Sink sink)
710710 {
711711 size_t index;
712
712
713713 foreach (i, c; text) {
714714 String temp;
715715
943943 sTag = src[0..i];
944944 eTag = src[i + 1..$].stripLeft();
945945 }
946
946
947947 size_t getEnd(String src)
948948 {
949949 auto end = src.indexOf(eTag);
950950 if (end == -1)
951951 throw new MustacheException("Mustache tag is not closed");
952
952
953953 return end;
954954 }
955
955
956956 // State capturing for section
957957 struct Memo
958958 {
959959 String[] key;
960960 Node[] nodes;
961961 String source;
962
962
963963 bool opEquals()(auto ref const Memo m) inout
964964 {
965965 // Don't compare source because the internal
10461046 case '{':
10471047 src = src[1..$];
10481048 auto key = parseKey(src, "}", end);
1049
1049
10501050 end += 1;
10511051 if (end >= src.length || !src[end..$].startsWith(eTag))
10521052 throw new MustacheException("Unescaped tag is not closed");
1053
1053
10541054 result ~= Node(NodeType.var, key, true);
10551055 break;
10561056 case '&':
11361136 beforeEatWSIndex = index;
11371137 index = src.length - src[index..$].stripLeft().length;
11381138 }
1139
1139
11401140 void acceptKeySegment()
11411141 {
11421142 if (keySegmentStart >= beforeEatWSIndex)
11441144
11451145 key ~= src[keySegmentStart .. beforeEatWSIndex];
11461146 }
1147
1147
11481148 eatWhitespace();
11491149 keySegmentStart = index;
11501150
11631163 eatWhitespace();
11641164 }
11651165 }
1166
1166
11671167 end = index;
11681168 return key;
11691169 }
12641264 } catch (MustacheException e) { }
12651265 }
12661266 }
1267
1267
12681268 @trusted
12691269 static String keyToString(in String[] key)
12701270 {
12711271 if (key.length == 0)
12721272 return null;
1273
1273
12741274 if (key.length == 1)
12751275 return key[0];
1276
1276
12771277 Appender!String buf;
12781278 foreach (index, segment; key) {
12791279 if (index != 0)
12801280 buf.put('.');
1281
1281
12821282 buf.put(segment);
12831283 }
1284
1284
12851285 return buf.data;
12861286 }
1287
1287
12881288 /*
12891289 * Mustache's node types
12901290 */
13561356 string toString() const
13571357 {
13581358 string result;
1359
1359
13601360 final switch (type) {
13611361 case NodeType.text:
13621362 result = "[T : \"" ~ to!string(text) ~ "\"]";