Inja 3.5.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 <filesystem>
5#include <fstream>
6#include <iostream>
7#include <sstream>
8#include <string>
9#include <string_view>
10
11#include "json.hpp"
12#include "config.hpp"
13#include "function_storage.hpp"
14#include "parser.hpp"
15#include "renderer.hpp"
16#include "template.hpp"
17#include "throw.hpp"
18
19namespace inja {
20
25 FunctionStorage function_storage;
26 TemplateStorage template_storage;
27
28protected:
29 LexerConfig lexer_config;
30 ParserConfig parser_config;
31 RenderConfig render_config;
32
33 std::filesystem::path input_path;
34 std::filesystem::path output_path;
35
36public:
37 Environment(): Environment("") {}
38 explicit Environment(const std::filesystem::path& global_path): input_path(global_path), output_path(global_path) {}
39 Environment(const std::filesystem::path& input_path, const std::filesystem::path& output_path): input_path(input_path), output_path(output_path) {}
40
42 void set_statement(const std::string& open, const std::string& close) {
43 lexer_config.statement_open = open;
44 lexer_config.statement_open_no_lstrip = open + "+";
45 lexer_config.statement_open_force_lstrip = open + "-";
46 lexer_config.statement_close = close;
47 lexer_config.statement_close_force_rstrip = "-" + close;
48 lexer_config.update_open_chars();
49 }
50
52 void set_line_statement(const std::string& open) {
53 lexer_config.line_statement = open;
54 lexer_config.update_open_chars();
55 }
56
58 void set_expression(const std::string& open, const std::string& close) {
59 lexer_config.expression_open = open;
60 lexer_config.expression_open_force_lstrip = open + "-";
61 lexer_config.expression_close = close;
62 lexer_config.expression_close_force_rstrip = "-" + close;
63 lexer_config.update_open_chars();
64 }
65
67 void set_comment(const std::string& open, const std::string& close) {
68 lexer_config.comment_open = open;
69 lexer_config.comment_open_force_lstrip = open + "-";
70 lexer_config.comment_close = close;
71 lexer_config.comment_close_force_rstrip = "-" + close;
72 lexer_config.update_open_chars();
73 }
74
76 void set_trim_blocks(bool trim_blocks) {
77 lexer_config.trim_blocks = trim_blocks;
78 }
79
81 void set_lstrip_blocks(bool lstrip_blocks) {
82 lexer_config.lstrip_blocks = lstrip_blocks;
83 }
84
86 void set_search_included_templates_in_files(bool search_in_files) {
87 parser_config.search_included_templates_in_files = search_in_files;
88 }
89
91 void set_throw_at_missing_includes(bool will_throw) {
92 render_config.throw_at_missing_includes = will_throw;
93 }
94
96 void set_html_autoescape(bool will_escape) {
97 render_config.html_autoescape = will_escape;
98 }
99
100 Template parse(std::string_view input) {
101 Parser parser(parser_config, lexer_config, template_storage, function_storage);
102 return parser.parse(input, input_path);
103 }
104
105 Template parse_template(const std::filesystem::path& filename) {
106 Parser parser(parser_config, lexer_config, template_storage, function_storage);
107 auto result = Template(Parser::load_file(input_path / filename));
108 parser.parse_into_template(result, (input_path / filename).string());
109 return result;
110 }
111
112 Template parse_file(const std::filesystem::path& filename) {
113 return parse_template(filename);
114 }
115
116 std::string render(std::string_view input, const json& data) {
117 return render(parse(input), data);
118 }
119
120 std::string render(const Template& tmpl, const json& data) {
121 std::stringstream os;
122 render_to(os, tmpl, data);
123 return os.str();
124 }
125
126 std::string render_file(const std::filesystem::path& filename, const json& data) {
127 return render(parse_template(filename), data);
128 }
129
130 std::string render_file_with_json_file(const std::filesystem::path& filename, const std::string& filename_data) {
131 const json data = load_json(filename_data);
132 return render_file(filename, data);
133 }
134
135 void write(const std::filesystem::path& filename, const json& data, const std::string& filename_out) {
136 std::ofstream file(output_path / filename_out);
137 file << render_file(filename, data);
138 file.close();
139 }
140
141 void write(const Template& temp, const json& data, const std::string& filename_out) {
142 std::ofstream file(output_path / filename_out);
143 file << render(temp, data);
144 file.close();
145 }
146
147 void write_with_json_file(const std::filesystem::path& filename, const std::string& filename_data, const std::string& filename_out) {
148 const json data = load_json(filename_data);
149 write(filename, data, filename_out);
150 }
151
152 void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) {
153 const json data = load_json(filename_data);
154 write(temp, data, filename_out);
155 }
156
157 std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
158 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
159 return os;
160 }
161
162 std::ostream& render_to(std::ostream& os, const std::string_view input, const json& data) {
163 return render_to(os, parse(input), data);
164 }
165
166 std::string load_file(const std::string& filename) {
167 const Parser parser(parser_config, lexer_config, template_storage, function_storage);
168 return Parser::load_file(input_path / filename);
169 }
170
171 json load_json(const std::string& filename) {
172 std::ifstream file;
173 file.open(input_path / filename);
174 if (file.fail()) {
175 INJA_THROW(FileError("failed accessing file at '" + (input_path / filename).string() + "'"));
176 }
177
178 return json::parse(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
179 }
180
184 void add_callback(const std::string& name, const CallbackFunction& callback) {
185 add_callback(name, -1, callback);
186 }
187
191 void add_void_callback(const std::string& name, const VoidCallbackFunction& callback) {
192 add_void_callback(name, -1, callback);
193 }
194
198 void add_callback(const std::string& name, int num_args, const CallbackFunction& callback) {
199 function_storage.add_callback(name, num_args, callback);
200 }
201
205 void add_void_callback(const std::string& name, int num_args, const VoidCallbackFunction& callback) {
206 function_storage.add_callback(name, num_args, [callback](Arguments& args) {
207 callback(args);
208 return json();
209 });
210 }
211
216 void include_template(const std::string& name, const Template& tmpl) {
217 template_storage[name] = tmpl;
218 }
219
223 void set_include_callback(const std::function<Template(const std::filesystem::path&, const std::string&)>& callback) {
224 parser_config.include_callback = callback;
225 }
226};
227
231inline std::string render(std::string_view input, const json& data) {
232 return Environment().render(input, data);
233}
234
238inline void render_to(std::ostream& os, std::string_view input, const json& data) {
239 Environment env;
240 env.render_to(os, env.parse(input), data);
241}
242
243} // namespace inja
244
245#endif // INCLUDE_INJA_ENVIRONMENT_HPP_
Class for changing the configuration.
Definition environment.hpp:24
void set_throw_at_missing_includes(bool will_throw)
Sets whether a missing include will throw an error.
Definition environment.hpp:91
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:205
void set_statement(const std::string &open, const std::string &close)
Sets the opener and closer for template statements.
Definition environment.hpp:42
void set_search_included_templates_in_files(bool search_in_files)
Sets the element notation syntax.
Definition environment.hpp:86
void set_trim_blocks(bool trim_blocks)
Sets whether to remove the first newline after a block.
Definition environment.hpp:76
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:81
void set_comment(const std::string &open, const std::string &close)
Sets the opener and closer for template comments.
Definition environment.hpp:67
void set_line_statement(const std::string &open)
Sets the opener for template line statements.
Definition environment.hpp:52
void set_html_autoescape(bool will_escape)
Sets whether we'll automatically perform HTML escape.
Definition environment.hpp:96
void set_include_callback(const std::function< Template(const std::filesystem::path &, const std::string &)> &callback)
Sets a function that is called when an included file is not found.
Definition environment.hpp:223
void set_expression(const std::string &open, const std::string &close)
Sets the opener and closer for template expressions.
Definition environment.hpp:58
void add_callback(const std::string &name, const CallbackFunction &callback)
Adds a variadic callback.
Definition environment.hpp:184
void include_template(const std::string &name, const Template &tmpl)
Definition environment.hpp:216
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback)
Adds a variadic void callback.
Definition environment.hpp:191
void add_callback(const std::string &name, int num_args, const CallbackFunction &callback)
Adds a callback with given number or arguments.
Definition environment.hpp:198
Class for builtin functions and user-defined callbacks.
Definition function_storage.hpp:22
Class for parsing an inja Template.
Definition parser.hpp:29
Class for lexer configuration.
Definition config.hpp:15
Class for parser configuration.
Definition config.hpp:67
Class for render configuration.
Definition config.hpp:76
The main inja Template.
Definition template.hpp:16