1 #ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
2 #define INCLUDE_INJA_ENVIRONMENT_HPP_
12 #include "function_storage.hpp"
14 #include "renderer.hpp"
15 #include "template.hpp"
29 TemplateStorage template_storage;
32 std::string input_path;
33 std::string output_path;
38 explicit Environment(
const std::string& global_path): input_path(global_path), output_path(global_path) {}
40 Environment(
const std::string& input_path,
const std::string& output_path): input_path(input_path), output_path(output_path) {}
44 lexer_config.statement_open = open;
45 lexer_config.statement_open_no_lstrip = open +
"+";
46 lexer_config.statement_open_force_lstrip = open +
"-";
47 lexer_config.statement_close = close;
48 lexer_config.statement_close_force_rstrip =
"-" + close;
49 lexer_config.update_open_chars();
54 lexer_config.line_statement = open;
55 lexer_config.update_open_chars();
60 lexer_config.expression_open = open;
61 lexer_config.expression_open_force_lstrip = open +
"-";
62 lexer_config.expression_close = close;
63 lexer_config.expression_close_force_rstrip =
"-" + close;
64 lexer_config.update_open_chars();
68 void set_comment(
const std::string& open,
const std::string& close) {
69 lexer_config.comment_open = open;
70 lexer_config.comment_open_force_lstrip = open +
"-";
71 lexer_config.comment_close = close;
72 lexer_config.comment_close_force_rstrip =
"-" + close;
73 lexer_config.update_open_chars();
78 lexer_config.trim_blocks = trim_blocks;
83 lexer_config.lstrip_blocks = lstrip_blocks;
88 parser_config.search_included_templates_in_files = search_in_files;
93 render_config.throw_at_missing_includes = will_throw;
96 Template parse(std::string_view input) {
97 Parser parser(parser_config, lexer_config, template_storage, function_storage);
98 return parser.parse(input, input_path);
101 Template parse_template(
const std::string& filename) {
102 Parser parser(parser_config, lexer_config, template_storage, function_storage);
103 auto result = Template(Parser::load_file(input_path +
static_cast<std::string
>(filename)));
104 parser.parse_into_template(result, input_path +
static_cast<std::string
>(filename));
108 Template parse_file(
const std::string& filename) {
109 return parse_template(filename);
112 std::string render(std::string_view input,
const json& data) {
113 return render(parse(input), data);
116 std::string render(
const Template& tmpl,
const json& data) {
117 std::stringstream os;
118 render_to(os, tmpl, data);
122 std::string render_file(
const std::string& filename,
const json& data) {
123 return render(parse_template(filename), data);
126 std::string render_file_with_json_file(
const std::string& filename,
const std::string& filename_data) {
127 const json data = load_json(filename_data);
128 return render_file(filename, data);
131 void write(
const std::string& filename,
const json& data,
const std::string& filename_out) {
132 std::ofstream file(output_path + filename_out);
133 file << render_file(filename, data);
137 void write(
const Template& temp,
const json& data,
const std::string& filename_out) {
138 std::ofstream file(output_path + filename_out);
139 file << render(temp, data);
143 void write_with_json_file(
const std::string& filename,
const std::string& filename_data,
const std::string& filename_out) {
144 const json data = load_json(filename_data);
145 write(filename, data, filename_out);
148 void write_with_json_file(
const Template& temp,
const std::string& filename_data,
const std::string& filename_out) {
149 const json data = load_json(filename_data);
150 write(temp, data, filename_out);
153 std::ostream& render_to(std::ostream& os,
const Template& tmpl,
const json& data) {
154 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
158 std::string load_file(
const std::string& filename) {
159 Parser parser(parser_config, lexer_config, template_storage, function_storage);
160 return Parser::load_file(input_path + filename);
163 json load_json(
const std::string& filename) {
165 file.open(input_path + filename);
167 INJA_THROW(FileError(
"failed accessing file at '" + input_path + filename +
"'"));
170 return json::parse(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
176 void add_callback(
const std::string& name,
const CallbackFunction& callback) {
190 void add_callback(
const std::string& name,
int num_args,
const CallbackFunction& callback) {
191 function_storage.add_callback(name, num_args, callback);
197 void add_void_callback(
const std::string& name,
int num_args,
const VoidCallbackFunction& callback) {
198 function_storage.add_callback(name, num_args, [callback](Arguments& args) {
209 template_storage[name] = tmpl;
216 parser_config.include_callback = callback;
223 inline std::string render(std::string_view input,
const json& data) {
224 return Environment().render(input, data);
230 inline void render_to(std::ostream& os, std::string_view input,
const json& data) {
232 env.render_to(os, env.parse(input), data);
Class for changing the configuration.
Definition: environment.hpp:23
void set_throw_at_missing_includes(bool will_throw)
Sets whether a missing include will throw an error.
Definition: environment.hpp:92
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:197
void set_statement(const std::string &open, const std::string &close)
Sets the opener and closer for template statements.
Definition: environment.hpp:43
void set_search_included_templates_in_files(bool search_in_files)
Sets the element notation syntax.
Definition: environment.hpp:87
void set_trim_blocks(bool trim_blocks)
Sets whether to remove the first newline after a block.
Definition: environment.hpp:77
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:82
void set_comment(const std::string &open, const std::string &close)
Sets the opener and closer for template comments.
Definition: environment.hpp:68
void set_line_statement(const std::string &open)
Sets the opener for template line statements.
Definition: environment.hpp:53
void set_include_callback(const std::function< Template(const std::string &, const std::string &)> &callback)
Sets a function that is called when an included file is not found.
Definition: environment.hpp:215
void set_expression(const std::string &open, const std::string &close)
Sets the opener and closer for template expressions.
Definition: environment.hpp:59
void add_callback(const std::string &name, const CallbackFunction &callback)
Adds a variadic callback.
Definition: environment.hpp:176
void include_template(const std::string &name, const Template &tmpl)
Definition: environment.hpp:208
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback)
Adds a variadic void callback.
Definition: environment.hpp:183
void add_callback(const std::string &name, int num_args, const CallbackFunction &callback)
Adds a callback with given number or arguments.
Definition: environment.hpp:190
Class for builtin functions and user-defined callbacks.
Definition: function_storage.hpp:16
Class for parsing an inja Template.
Definition: parser.hpp:24
Class for lexer configuration.
Definition: config.hpp:14
Class for parser configuration.
Definition: config.hpp:66
Class for render configuration.
Definition: config.hpp:75
The main inja Template.
Definition: template.hpp:17