1 /** 2 * Copyright © DiamondMVC 2019 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.views.viewgenerator; 7 8 import diamond.core.apptype; 9 10 static if (isWebServer || !isWeb) 11 { 12 /// Mixin template for the view generator. 13 mixin template GenerateViews() 14 { 15 import diamond.core.webconfig; 16 import diamond.views.viewparser; 17 18 struct ViewResult 19 { 20 string name; 21 string source; 22 } 23 24 /** 25 * Generates the strings of the view classes to use with mixin. 26 * Returns: 27 * An array consisting of the generated classes of the views. 28 * The first element of the array is the routable data. 29 */ 30 private ViewResult[] generateViews() 31 { 32 ViewResult[] viewGenerations = []; 33 34 string routableViewsMixin = "private static __gshared string[string] _routableViews; 35 @property string[string] routableViews() 36 { 37 if (_routableViews) return _routableViews; 38 "; 39 40 mixin LoadViewData; 41 42 foreach (viewName,viewContent; getViewData) 43 { 44 import diamond.templates.parser : parseTemplate; 45 46 auto parts = parseTemplate(viewContent); 47 48 string route; 49 viewGenerations ~= ViewResult(viewName, parseViewParts(parts, viewName, route)); 50 51 if (route && route.length) 52 { 53 import std..string : format; 54 routableViewsMixin ~= "_routableViews[\"%s\"] = \"%s\";".format(route, viewName); 55 } 56 } 57 58 routableViewsMixin ~= "return _routableViews; 59 }"; 60 61 return [ViewResult("__routes", routableViewsMixin)] ~ viewGenerations; 62 } 63 64 /// The result of the generated views. 65 enum generateViewsResult = generateViews(); 66 } 67 }