diet.traits 0/120(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
/** Definitions to support customization of the Diet compilation process. */ module diet.traits; import diet.dom; /** Marks a struct as a Diet traits container. A traits struct can contain any of the following: $(UL $(LI `string translate(string)` - A function that takes a `string` and returns the translated version of that string. This is used for translating the text of nodes marked with `&` at compile time. Note that the input string may contain string interpolations.) $(LI `void filterX(string)` - Any number of compile-time filter functions, where "X" is a placeholder for the actual filter name. The first character will be converted to lower case, so that a function `filterCss` will be available as `:css` within the Diet template.) $(LI `SafeFilterCallback[string] filters` - A dictionary of runtime filter functions that will be used to for filter nodes that don't have an available compile-time filter or contain string interpolations.) $(LI `alias processors = AliasSeq!(...)` - A list of callables taking a `Document` to modify its contents) $(LI `HTMLOutputStyle htmlOutputStyle` - An enum to configure the output style of the generated HTML, e.g. compact or pretty) ) */ @property DietTraitsAttribute dietTraits() { return DietTraitsAttribute.init; } /// unittest { import diet.html : compileHTMLDietString; import std.array : appender, array; import std.string : toUpper; @dietTraits static struct CTX { static string translate(string text) { return text == "Hello, World!" ? "Hallo, Welt!" : text; } static string filterUppercase(I)(I input) { return input.toUpper(); } } auto dst = appender!string; dst.compileHTMLDietString!("p& Hello, World!", CTX); assert(dst.data == "<p>Hallo, Welt!</p>"); dst = appender!string; dst.compileHTMLDietString!(":uppercase testing", CTX); assert(dst.data == "TESTING"); } /** Translates a line of text based on the traits passed to the Diet parser. The input text may contain string interpolations of the form `#{...}` or `!{...}`, where the contents form an arbitrary D expression. The translation function is required to pass these through unmodified. */ string translate(TRAITS...)(string text) { import std.traits : hasUDA; foreach (T; TRAITS) { static assert(hasUDA!(T, DietTraitsAttribute)); static if (is(typeof(&T.translate))) text = T.translate(text); } return text; } /** Applies any transformations that are defined in the */ Document applyTraits(TRAITS...)(Document doc) { import diet.defs : enforcep; import std.algorithm.searching : startsWith; import std.array : split; void processNode(ref Node n, bool in_filter) { bool is_filter = n.name == Node.SpecialName.filter; // process children first for (size_t i = 0; i < n.contents.length;) { auto nc = n.contents[i]; if (nc.kind == NodeContent.Kind.node) { processNode(nc.node, is_filter || in_filter); if ((is_filter || in_filter) && nc.node.name == Node.SpecialName.text) { n.contents = n.contents[0 .. i] ~ nc.node.contents ~ n.contents[i+1 .. $]; i += nc.node.contents.length; } else i++; } else i++; } // then consolidate text for (size_t i = 1; i < n.contents.length;) { if (n.contents[i-1].kind == NodeContent.Kind.text && n.contents[i].kind == NodeContent.Kind.text) { n.contents[i-1].value ~= n.contents[i].value; n.contents = n.contents[0 .. i] ~ n.contents[i+1 .. $]; } else i++; } // finally process filters if (is_filter) { enforcep(n.isProceduralTextNode, "Only text is supported as filter contents.", n.loc); auto chain = n.getAttribute("filterChain").expectText().split(' '); n.attributes = null; n.attribs = NodeAttribs.none; if (n.isTextNode) { while (chain.length) { if (hasFilterCT!TRAITS(chain[$-1])) { n.contents[0].value = runFilterCT!TRAITS(n.contents[0].value, chain[$-1]); chain.length--; } else break; } } if (!chain.length) n.name = Node.SpecialName.text; else { n.name = Node.SpecialName.code; n.contents = [NodeContent.text(generateFilterChainMixin(chain, n.contents), n.loc)]; } } } foreach (ref n; doc.nodes) processNode(n, false); // apply DOM processors foreach (T; TRAITS) { static if (is(typeof(T.processors.length))) { foreach (p; T.processors) p(doc); } } return doc; } deprecated("Use SafeFilterCallback instead.") alias FilterCallback = void delegate(in char[] input, scope CharacterSink output); alias SafeFilterCallback = void delegate(in char[] input, scope CharacterSink output) @safe; alias CharacterSink = void delegate(in char[]) @safe; void filter(ALIASES...)(in char[] input, string filter, CharacterSink output) { import std.traits : hasUDA; foreach (A; ALIASES) static if (hasUDA!(A, DietTraitsAttribute)) { static if (is(typeof(A.filters))) if (auto pf = filter in A.filters) { (*pf)(input, output); return; } } // FIXME: output location information throw new Exception("Unknown filter: "~filter); } private string generateFilterChainMixin(string[] chain, NodeContent[] contents) { import std.format : format; import diet.defs : enforcep, dietOutputRangeName; import diet.internal.string : dstringEscape; string ret = `{ import std.array : appender; import std.format : formattedWrite; `; auto tloname = format("__f%s", chain.length); if (contents.length == 1 && contents[0].kind == NodeContent.Kind.text) { ret ~= q{enum %s = "%s";}.format(tloname, dstringEscape(contents[0].value)); } else { ret ~= q{auto %s_app = appender!(char[])();}.format(tloname); foreach (c; contents) { switch (c.kind) { default: assert(false, "Unexpected node content in filter."); case NodeContent.Kind.text: ret ~= q{%s_app.put("%s");}.format(tloname, dstringEscape(c.value)); break; case NodeContent.Kind.rawInterpolation: ret ~= q{%s_app.formattedWrite("%%s", %s);}.format(tloname, c.value); break; case NodeContent.Kind.interpolation: enforcep(false, "Non-raw interpolations are not supported within filter contents.", c.loc); break; } ret ~= "\n"; } ret ~= q{auto %s = %s_app.data;}.format(tloname, tloname); } foreach_reverse (i, f; chain) { ret ~= "\n"; string iname = format("__f%s", i+1); string oname; if (i > 0) { oname = format("__f%s_app", i); ret ~= q{auto %s = appender!(char[]);}.format(oname); } else oname = dietOutputRangeName; ret ~= q{%s.filter!ALIASES("%s", (in char[] s) @safe { %s.put(s); });}.format(iname, dstringEscape(f), oname); if (i > 0) ret ~= q{auto __f%s = %s.data;}.format(i, oname); } return ret ~ `}`; } unittest { import std.array : appender; import diet.html : compileHTMLDietString; @dietTraits static struct CTX { static string filterFoo(string str) { return "("~str~")"; } static SafeFilterCallback[string] filters; } CTX.filters["foo"] = (input, scope output) { output("(R"); output(input); output("R)"); }; CTX.filters["bar"] = (input, scope output) { output("(RB"); output(input); output("RB)"); }; auto dst = appender!string; dst.compileHTMLDietString!(":foo text", CTX); assert(dst.data == "(text)"); dst = appender!string; dst.compileHTMLDietString!(":foo text\n\tmore", CTX); assert(dst.data == "(text\nmore)"); dst = appender!string; dst.compileHTMLDietString!(":foo :foo text", CTX); assert(dst.data == "((text))"); dst = appender!string; dst.compileHTMLDietString!(":bar :foo text", CTX); assert(dst.data == "(RB(text)RB)"); dst = appender!string; dst.compileHTMLDietString!(":foo :bar text", CTX); assert(dst.data == "(R(RBtextRB)R)"); dst = appender!string; dst.compileHTMLDietString!(":foo text !{1}", CTX); assert(dst.data == "(Rtext 1R)"); } @safe unittest { import diet.html : compileHTMLDietString; static struct R { void put(char) @safe {} void put(in char[]) @safe {} void put(dchar) @safe {} } @dietTraits static struct CTX { static SafeFilterCallback[string] filters; } CTX.filters["foo"] = (input, scope output) { output(input); }; R r; r.compileHTMLDietString!(":foo bar", CTX); } private struct DietTraitsAttribute {} private bool hasFilterCT(TRAITS...)(string filter) { alias Filters = FiltersFromTraits!TRAITS; static if (Filters.length) { switch (filter) { default: break; foreach (i, F; Filters) { case FilterName!(Filters[i]): return true; } } } return false; } private string runFilterCT(TRAITS...)(string text, string filter) { alias Filters = FiltersFromTraits!TRAITS; static if (Filters.length) { switch (filter) { default: break; foreach (i, F; Filters) { case FilterName!(Filters[i]): return F(text); } } } return text; // FIXME: error out? } private template FiltersFromTraits(TRAITS...) { import std.meta : AliasSeq; template impl(size_t i) { static if (i < TRAITS.length) { // FIXME: merge lists avoiding duplicates alias impl = AliasSeq!(FiltersFromContext!(TRAITS[i]), impl!(i+1)); } else alias impl = AliasSeq!(); } alias FiltersFromTraits = impl!0; } /** Extracts all Diet traits structs from a set of aliases as passed to a render function. */ template DietTraits(ALIASES...) { import std.meta : AliasSeq; import std.traits : hasUDA; template impl(size_t i) { static if (i < ALIASES.length) { static if (is(ALIASES[i]) && hasUDA!(ALIASES[i], DietTraitsAttribute)) { alias impl = AliasSeq!(ALIASES[i], impl!(i+1)); } else alias impl = impl!(i+1); } else alias impl = AliasSeq!(); } alias DietTraits = impl!0; } private template FiltersFromContext(Context) { import std.meta : AliasSeq; import std.algorithm.searching : startsWith; alias members = AliasSeq!(__traits(allMembers, Context)); template impl(size_t i) { static if (i < members.length) { static if (members[i].startsWith("filter") && members[i].length > 6 && members[i] != "filters") alias impl = AliasSeq!(__traits(getMember, Context, members[i]), impl!(i+1)); else alias impl = impl!(i+1); } else alias impl = AliasSeq!(); } alias FiltersFromContext = impl!0; } private template FilterName(alias FilterFunction) { import std.algorithm.searching : startsWith; import std.ascii : toLower; enum ident = __traits(identifier, FilterFunction); static if (ident.startsWith("filter") && ident.length > 6) enum FilterName = ident[6].toLower ~ ident[7 .. $]; else static assert(false, "Filter function must start with \"filter\" and must have a non-zero length suffix: " ~ ident); }