Inja 3.3.0
A Template Engine for Modern C++
Loading...
Searching...
No Matches
environment.hpp
1#ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
2#define INCLUDE_INJA_ENVIRONMENT_HPP_
3
4#include <fstream>
5#include <iostream>
6#include <memory>
7#include <sstream>
8#include <string>
9
10#include <nlohmann/json.hpp>
11
12#include "config.hpp"
13#include "function_storage.hpp"
14#include "parser.hpp"
15#include "renderer.hpp"
16#include "string_view.hpp"
17#include "template.hpp"
18#include "utils.hpp"
19
20namespace inja {
21
22using json = nlohmann::json;
23
28 std::string input_path;
29 std::string output_path;
30
31 LexerConfig lexer_config;
32 ParserConfig parser_config;
33 RenderConfig render_config;
34
35 FunctionStorage function_storage;
36 TemplateStorage template_storage;
37
38public:
39 Environment() : Environment("") {}
40
41 explicit Environment(const std::string &global_path) : input_path(global_path), output_path(global_path) {}
42
43 Environment(const std::string &input_path, const std::string &output_path)
44 : input_path(input_path), output_path(output_path) {}
45
47 void set_statement(const std::string &open, const std::string &close) {
48 lexer_config.statement_open = open;
49 lexer_config.statement_open_no_lstrip = open + "+";
50 lexer_config.statement_open_force_lstrip = open + "-";
51 lexer_config.statement_close = close;
52 lexer_config.statement_close_force_rstrip = "-" + close;
53 lexer_config.update_open_chars();
54 }
55
57 void set_line_statement(const std::string &open) {
58 lexer_config.line_statement = open;
59 lexer_config.update_open_chars();
60 }
61
63 void set_expression(const std::string &open, const std::string &close) {
64 lexer_config.expression_open = open;
65 lexer_config.expression_open_force_lstrip = open + "-";
66 lexer_config.expression_close = close;
67 lexer_config.expression_close_force_rstrip = "-" + close;
68 lexer_config.update_open_chars();
69 }
70
72 void set_comment(const std::string &open, const std::string &close) {
73 lexer_config.comment_open = open;
74 lexer_config.comment_open_force_lstrip = open + "-";
75 lexer_config.comment_close = close;
76 lexer_config.comment_close_force_rstrip = "-" + close;
77 lexer_config.update_open_chars();
78 }
79
81 void set_trim_blocks(bool trim_blocks) {
82 lexer_config.trim_blocks = trim_blocks;
83 }
84
86 void set_lstrip_blocks(bool lstrip_blocks) {
87 lexer_config.lstrip_blocks = lstrip_blocks;
88 }
89
91 void set_search_included_templates_in_files(bool search_in_files) {
92 parser_config.search_included_templates_in_files = search_in_files;
93 }
94
96 void set_throw_at_missing_includes(bool will_throw) {
97 render_config.throw_at_missing_includes = will_throw;
98 }
99
100 Template parse(nonstd::string_view input) {
101 Parser parser(parser_config, lexer_config, template_storage, function_storage);
102 return parser.parse(input);
103 }
104
105 Template parse_template(const std::string &filename) {
106 Parser parser(parser_config, lexer_config, template_storage, function_storage);
107 auto result = Template(parser.load_file(input_path + static_cast<std::string>(filename)));
108 parser.parse_into_template(result, input_path + static_cast<std::string>(filename));
109 return result;
110 }
111
112 Template parse_file(const std::string &filename) {
113 return parse_template(filename);
114 }
115
116 std::string render(nonstd::string_view input, const json &data) { return render(parse(input), data); }
117
118 std::string render(const Template &tmpl, const json &data) {
119 std::stringstream os;
120 render_to(os, tmpl, data);
121 return os.str();
122 }
123
124 std::string render_file(const std::string &filename, const json &data) {
125 return render(parse_template(filename), data);
126 }
127
128 std::string render_file_with_json_file(const std::string &filename, const std::string &filename_data) {
129 const json data = load_json(filename_data);
130 return render_file(filename, data);
131 }
132
133 void write(const std::string &filename, const json &data, const std::string &filename_out) {
134 std::ofstream file(output_path + filename_out);
135 file << render_file(filename, data);
136 file.close();
137 }
138
139 void write(const Template &temp, const json &data, const std::string &filename_out) {
140 std::ofstream file(output_path + filename_out);
141 file << render(temp, data);
142 file.close();
143 }
144
145 void write_with_json_file(const std::string &filename, const std::string &filename_data,
146 const std::string &filename_out) {
147 const json data = load_json(filename_data);
148 write(filename, data, filename_out);
149 }
150
151 void write_with_json_file(const Template &temp, const std::string &filename_data, const std::string &filename_out) {
152 const json data = load_json(filename_data);
153 write(temp, data, filename_out);
154 }
155
156 std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) {
157 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
158 return os;
159 }
160
161 std::string load_file(const std::string &filename) {
162 Parser parser(parser_config, lexer_config, template_storage, function_storage);
163 return parser.load_file(input_path + filename);
164 }
165
166 json load_json(const std::string &filename) {
167 std::ifstream file;
168 open_file_or_throw(input_path + filename, file);
169 json j;
170 file >> j;
171 return j;
172 }
173
177 void add_callback(const std::string &name, const CallbackFunction &callback) {
178 add_callback(name, -1, callback);
179 }
180
184 void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) {
185 add_void_callback(name, -1, callback);
186 }
187
191 void add_callback(const std::string &name, int num_args, const CallbackFunction &callback) {
192 function_storage.add_callback(name, num_args, callback);
193 }
194
198 void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) {
199 function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); });
200 }
201
206 void include_template(const std::string &name, const Template &tmpl) {
207 template_storage[name] = tmpl;
208 }
209};
210
214inline std::string render(nonstd::string_view input, const json &data) {
215 return Environment().render(input, data);
216}
217
221inline void render_to(std::ostream &os, nonstd::string_view input, const json &data) {
222 Environment env;
223 env.render_to(os, env.parse(input), data);
224}
225
226} // namespace inja
227
228#endif // INCLUDE_INJA_ENVIRONMENT_HPP_
Class for changing the configuration.
Definition: environment.hpp:27
void set_throw_at_missing_includes(bool will_throw)
Sets whether a missing include will throw an error.
Definition: environment.hpp:96
void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback)
Adds a void callback with given number or arguments.
Definition: environment.hpp:198
void set_statement(const std::string &open, const std::string &close)
Sets the opener and closer for template statements.
Definition: environment.hpp:47
void set_search_included_templates_in_files(bool search_in_files)
Sets the element notation syntax.
Definition: environment.hpp:91
void set_trim_blocks(bool trim_blocks)
Sets whether to remove the first newline after a block.
Definition: environment.hpp:81
void set_lstrip_blocks(bool lstrip_blocks)
Sets whether to strip the spaces and tabs from the start of a line to a block.
Definition: environment.hpp:86
void set_comment(const std::string &open, const std::string &close)
Sets the opener and closer for template comments.
Definition: environment.hpp:72
void set_line_statement(const std::string &open)
Sets the opener for template line statements.
Definition: environment.hpp:57
void set_expression(const std::string &open, const std::string &close)
Sets the opener and closer for template expressions.
Definition: environment.hpp:63
void add_callback(const std::string &name, const CallbackFunction &callback)
Adds a variadic callback.
Definition: environment.hpp:177
void include_template(const std::string &name, const Template &tmpl)
Definition: environment.hpp:206
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback)
Adds a variadic void callback.
Definition: environment.hpp:184
void add_callback(const std::string &name, int num_args, const CallbackFunction &callback)
Adds a callback with given number or arguments.
Definition: environment.hpp:191
Class for builtin functions and user-defined callbacks.
Definition: function_storage.hpp:19
Class for parsing an inja Template.
Definition: parser.hpp:27
Class for lexer configuration.
Definition: config.hpp:14
Class for parser configuration.
Definition: config.hpp:66
Class for render configuration.
Definition: config.hpp:73
The main inja Template.
Definition: template.hpp:18