1 /** 2 Copyright: Copyright (c) 2013-2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.templates.templatemanager; 8 9 import std.algorithm : canFind; 10 import std.array : Appender; 11 import std.file : read, exists; 12 13 import anchovy.gui; 14 15 import anchovy.gui.templates.widgettemplate; 16 import anchovy.gui.templates.templateparser; 17 18 class TemplateManager 19 { 20 WidgetTemplate[string] templates; 21 Appender!string parsedFiles; 22 private TemplateParser parser; 23 24 this(TemplateParser parser) 25 { 26 assert(parser); 27 this.parser = parser; 28 } 29 30 void parseFile(string filename) 31 { 32 Appender!(string[]) filesToParse; 33 filesToParse ~= filename; 34 35 string nextFilename; 36 37 while (filesToParse.data.length > 0) 38 { 39 nextFilename = filesToParse.data[$-1]; 40 //writefln("Parsing %s", nextFilename); 41 filesToParse.shrinkTo(filesToParse.data.length - 1); 42 43 if (!parsedFiles.data.canFind(nextFilename)) 44 { 45 parsedFiles ~= nextFilename; 46 47 if (!exists(nextFilename)) 48 { 49 stderr.writefln("Template file %s not found", nextFilename); 50 continue; 51 } 52 53 string file = cast(string)read(nextFilename); 54 55 filesToParse ~= parseString(file, nextFilename); 56 } 57 } 58 } 59 60 string[] parseString(string str, string filename = null) 61 { 62 TemplateParserResult parserResult = parser.parse(str, filename); 63 64 foreach(templ; parserResult.parsedTemplates) 65 { 66 templates[templ.tree.properties["type"].get!string] = templ; 67 } 68 69 return parserResult.filesToParse; 70 } 71 72 /// Returns true if given type name exists. 73 bool typeExists(string type) 74 { 75 return !!(type in templates); 76 } 77 78 /// Returns null if not found. 79 WidgetTemplate getTemplate(string type) 80 { 81 return templates.get(type, null); 82 } 83 }