1 /*
2  * Hunt - A high-level D Programming Language Web framework that encourages rapid development and clean, pragmatic design.
3  *
4  * Copyright (C) 2015-2019, HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.framework.view.Environment;
13 
14 import hunt.framework.Simplify;
15 import hunt.framework.view.Template;
16 import hunt.framework.view.Render;
17 import hunt.framework.view.Lexer;
18 import hunt.framework.view.Parser;
19 import hunt.framework.view.Uninode;
20 import hunt.framework.view.ast;
21 import hunt.framework.util.uninode.Serialization;
22 
23 import hunt.framework.http.Request;
24 import hunt.framework.Init;
25 
26 import hunt.logging;
27 
28 import std.meta;
29 import std.traits;
30 import std.string;
31 import std.json;
32 import std.file;
33 import std.path;
34 import std.stdio;
35 
36 private {
37    alias TemplateLexer = Lexer!(TemplateConfig.init.exprOpBegin,
38            TemplateConfig.init.exprOpEnd, TemplateConfig.init.stmtOpBegin, TemplateConfig.init.stmtOpEnd,
39            TemplateConfig.init.cmntOpBegin, TemplateConfig.init.cmntOpEnd, TemplateConfig.init.stmtOpInline, TemplateConfig.init.cmntOpInline);
40 }
41 
42 class Environment
43 {
44     private 
45     {
46         Request _request;
47         string input_path;
48         string output_path;
49 
50      
51         Parser!TemplateLexer _parser;
52 
53         string _routeGroup = DEFAULT_ROUTE_GROUP;
54         string _locale = "en-us";
55     }
56 
57     this()
58     {
59         auto tpl_path = config().view.path;
60         if (tpl_path.length == 0)
61             tpl_path = "./views/";
62         string p = buildPath(APP_PATH, buildNormalizedPath(tpl_path));
63         input_path = output_path = p ~ dirSeparator;
64     }
65 
66     this(string global_path)
67     {
68         input_path = output_path = buildNormalizedPath(global_path) ~ dirSeparator;
69     }
70 
71     this(string input_path, string output_path)
72     {
73         this.input_path = buildNormalizedPath(input_path) ~ dirSeparator;
74         this.output_path = buildNormalizedPath(output_path) ~ dirSeparator;
75     }
76 
77     Request request() {
78         return _request;
79     }
80 
81     void request(Request value) {
82         _request = value;
83     }
84 
85     void setRouteGroup(string rg)
86     {
87         _routeGroup = rg;
88     }
89 
90     void setLocale(string locale)
91     {
92         _locale = locale;
93     }
94 
95     void setTemplatePath(string path)
96     {
97         this.input_path = buildNormalizedPath(path) ~ dirSeparator;
98     }
99 
100     TemplateNode parse_template(string filename)
101     {
102         version (HUNT_FM_DEBUG) trace("parse template file: ", input_path ~ filename);
103         return _parser.parseTreeFromFile(input_path ~ filename);
104     }
105 
106     string render(TemplateNode tree,JSONValue data)
107     {
108         import hunt.framework.util.uninode.Serialization;
109 
110         auto render = new Render(tree);
111         render.setRouteGroup(_routeGroup);
112         render.setLocale(_locale);
113         render.request = _request;
114         return render.render(jsonToUniNode(data));
115     }
116 
117     string renderFile(string path,JSONValue data)
118     {
119          return render(parse_template(path),data);
120     }
121 
122     string render(string str,JSONValue data)
123     {
124         auto tree = _parser.parseTree(str,"",input_path);
125         auto render = new Render(tree);
126         render.setRouteGroup(_routeGroup);
127         render.setLocale(_locale);
128         render.request = _request;
129         return render.render(jsonToUniNode(data));
130     } 
131 }