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.ConsoleLogger;
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 
37 class Environment
38 {
39     private 
40     {
41         Request _request;
42         string input_path;
43         string output_path;
44 
45         alias TemplateLexer = Lexer!(TemplateConfig.init.exprOpBegin,
46                 TemplateConfig.init.exprOpEnd, TemplateConfig.init.stmtOpBegin, TemplateConfig.init.stmtOpEnd,
47                 TemplateConfig.init.cmntOpBegin, TemplateConfig.init.cmntOpEnd, TemplateConfig.init.stmtOpInline, TemplateConfig.init.cmntOpInline);
48         Parser!TemplateLexer _parser;
49 
50         string _routeGroup = DEFAULT_ROUTE_GROUP;
51         string _locale = "en-us";
52     }
53 
54     this()
55     {
56         auto tpl_path = config().view.path;
57         if (tpl_path.length == 0)
58             tpl_path = "./views/";
59         string p = buildPath(APP_PATH, buildNormalizedPath(tpl_path));
60         input_path = output_path = p ~ dirSeparator;
61     }
62 
63     this(string global_path)
64     {
65         input_path = output_path = buildNormalizedPath(global_path) ~ dirSeparator;
66     }
67 
68     this(string input_path, string output_path)
69     {
70         this.input_path = buildNormalizedPath(input_path) ~ dirSeparator;
71         this.output_path = buildNormalizedPath(output_path) ~ dirSeparator;
72     }
73 
74     Request request() {
75         return _request;
76     }
77 
78     void request(Request value) {
79         _request = value;
80     }
81 
82     void setRouteGroup(string rg)
83     {
84         _routeGroup = rg;
85     }
86 
87     void setLocale(string locale)
88     {
89         _locale = locale;
90     }
91 
92     void setTemplatePath(string path)
93     {
94         this.input_path = buildNormalizedPath(path) ~ dirSeparator;
95     }
96 
97     TemplateNode parse_template(string filename)
98     {
99         version (HUNT_FM_DEBUG) trace("parse template file: ", input_path ~ filename);
100         return _parser.parseTreeFromFile(input_path ~ filename);
101     }
102 
103     string render(TemplateNode tree,JSONValue data)
104     {
105         import hunt.framework.util.uninode.Serialization;
106 
107         auto render = new Render(tree);
108         render.setRouteGroup(_routeGroup);
109         render.setLocale(_locale);
110         render.request = _request;
111         return render.render(jsonToUniNode(data));
112     }
113 
114     string renderFile(string path,JSONValue data)
115     {
116          return render(parse_template(path),data);
117     }
118 
119     string render(string str,JSONValue data)
120     {
121         auto tree = _parser.parseTree(str,"",input_path);
122         auto render = new Render(tree);
123         render.setRouteGroup(_routeGroup);
124         render.setLocale(_locale);
125         render.request = _request;
126         return render.render(jsonToUniNode(data));
127     } 
128 }