1 /**
2 * Copyright © DiamondMVC 2018
3 * License: MIT (https://github.com/DiamondMVC/emeralD/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module templates;
7 
8 import std.file : dirEntries, SpanMode, isDir, isFile, mkdirRecurse, write, readText, exists, append;
9 import std.path : baseName;
10 import std.stdio : File;
11 import std..string : format;
12 import std.array : split, replace;
13 import std.algorithm : map;
14 
15 import meta : thisExeDir;
16 
17 /// Collection of templates.
18 private string[string][string] _templates;
19 
20 /// Loads the template paths.
21 void loadTemplates()
22 {
23   import std.stdio : writeln;
24   writeln(thisExeDir);
25   foreach (string root; dirEntries(thisExeDir ~ "/templates", SpanMode.shallow))
26   {
27     if (root.isDir)
28     {
29       auto rootName = baseName(root);
30 
31       foreach (string templatePath; dirEntries(root, SpanMode.shallow))
32       {
33         if (templatePath.isFile)
34         {
35           auto templateName = baseName(templatePath);
36 
37           _templates[rootName][templateName] = templatePath;
38         }
39       }
40     }
41   }
42 }
43 
44 /// Loads all remote templates.
45 void loadRemoteTemplates()
46 {
47   auto templates = File(thisExeDir ~ "/remotetemplates.emd");
48 
49   foreach (line; templates.byLine.map!(l => l.replace("\r", "")))
50   {
51     if (!line || !line.length)
52     {
53       continue;
54     }
55 
56     auto data = line.split('|');
57 
58     if (data.length != 3)
59     {
60       continue;
61     }
62 
63     immutable root = cast(immutable)data[0];
64     immutable name = cast(immutable)data[1];
65     immutable url = cast(immutable)data[2];
66 
67     if (root !in _templates || name !in _templates[root])
68     {
69       import std.net.curl : get, HTTP;
70 
71       import std.stdio : writefln;
72       writefln("'%s'", url);
73 
74       auto templateResult = cast(string)get!HTTP(url);
75 
76       if (templateResult)
77       {
78         auto rootPath = thisExeDir ~ "/templates/" ~ root;
79 
80         if (!exists(rootPath))
81         {
82           mkdirRecurse(rootPath);
83         }
84 
85         auto path = rootPath ~ "/" ~ name;
86 
87         write(path, templateResult);
88 
89         _templates[root][name] = path;
90       }
91     }
92   }
93 }
94 
95 /**
96 * Reads a template.
97 * Params:
98 *   root = The root of the template.
99 *   name = The name of the template.
100 */
101 string readTemplate(string root, string name)
102 {
103   auto templates = _templates.get(root, null);
104 
105   if (!templates)
106   {
107     return null;
108   }
109 
110   auto path = templates.get(name, null);
111 
112   if (!path)
113   {
114     return null;
115   }
116 
117   return readText(path);
118 }
119 
120 /**
121 * Adds a remote template.
122 * Params:
123 *   root = The root of the template.
124 *   name = The name of the template.
125 *   url =  The url of the template.
126 */
127 void addRemoteTemplate(string root, string name, string url)
128 {
129   append(thisExeDir ~ "/remotetemplates.emd", "%s|%s|%s\r\n".format(root, name, url));
130 }