1 module mood.templates; 2 3 import std.array: byPair; 4 5 /** 6 * Generate templates. 7 * 8 * Used to generate more than a hundred templates used to more easily generate html in webpages. 9 * 10 * Params: 11 * tags = Array of tags that will be used to generate templates. 12 * Returns: Source code of all of the generated templates that will be mixin'd. 13 */ 14 private string generateTemplates(string[] tags)() 15 { 16 string code; 17 static foreach(tag; tags) 18 { 19 // default 20 code ~= `string ` ~ tag ~ `(string content, string attributes = "") { return "<` ~ tag ~ ` " ~ attributes ~ ">" ~ content ~ "</` ~ tag ~ `>"; }` ~ "\n"; 21 code ~= `string ` ~ tag ~ `(string content, string[string] attributes) 22 { 23 string code = "<` ~ tag ~ `"; 24 foreach(key, val; attributes.byPair) 25 { 26 if (val == "") 27 code ~= " " ~ key; 28 else 29 { 30 code ~= " " ~ key ~ "=" ~ val; 31 } 32 } 33 code ~= ">" ~ content ~ "</` ~ tag ~ `>"; 34 return code; 35 }` ~ "\n"; 36 } 37 return code; 38 } 39 40 mixin(generateTemplates!([ 41 "a", 42 "abbr", 43 "acronym", 44 "address", 45 "applet", 46 "area", 47 "article", 48 "aside", 49 "audio", 50 "b", 51 "base", 52 "basefont", 53 "Specifies", 54 "bdi", 55 "bdo", 56 "big", 57 "blockquote", 58 "body", 59 "br", 60 "button", 61 "canvas", 62 "caption", 63 "center", 64 "cite", 65 "code", 66 "col", 67 "colgroup", 68 "data", 69 "datalist", 70 "dd", 71 "del", 72 "details", 73 "dfn", 74 "dialog", 75 "dir", 76 "div", 77 "dl", 78 "dt", 79 "em", 80 "embed", 81 "fieldset", 82 "figcaption", 83 "figure", 84 "font", 85 "footer", 86 "form", 87 "frame", 88 "frameset", 89 "h1", 90 "head", 91 "header", 92 "hr", 93 "html", 94 "i", 95 "iframe", 96 "img", 97 "input", 98 "ins", 99 "kbd", 100 "label", 101 "legend", 102 "li", 103 "link", 104 "map", 105 "mark", 106 "meta", 107 "meter", 108 "nav", 109 "noframes", 110 "noscript", 111 "ol", 112 "optgroup", 113 "option", 114 "output", 115 "p", 116 "param", 117 "picture", 118 "pre", 119 "progress", 120 "q", 121 "rp", 122 "rt", 123 "ruby", 124 "s", 125 "samp", 126 "script", 127 "section", 128 "select", 129 "small", 130 "source", 131 "span", 132 "strike", 133 "strong", 134 "style", 135 "sub", 136 "summary", 137 "sup", 138 "svg", 139 "table", 140 "tbody", 141 "td", 142 "textarea", 143 "tfoot", 144 "th", 145 "thead", 146 "time", 147 "title", 148 "tr", 149 "track", 150 "tt", 151 "u", 152 "ul", 153 "var", 154 "video", 155 "wbr" 156 ])());