1#ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
2#define INCLUDE_INJA_ENVIRONMENT_HPP_
10#include <nlohmann/json.hpp>
13#include "function_storage.hpp"
15#include "renderer.hpp"
16#include "string_view.hpp"
17#include "template.hpp"
22using json = nlohmann::json;
28 std::string input_path;
29 std::string output_path;
36 TemplateStorage template_storage;
41 explicit Environment(
const std::string &global_path) : input_path(global_path), output_path(global_path) {}
43 Environment(
const std::string &input_path,
const std::string &output_path)
44 : input_path(input_path), output_path(output_path) {}
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();
58 lexer_config.line_statement = open;
59 lexer_config.update_open_chars();
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();
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();
82 lexer_config.trim_blocks = trim_blocks;
87 lexer_config.lstrip_blocks = lstrip_blocks;
92 parser_config.search_included_templates_in_files = search_in_files;
97 render_config.throw_at_missing_includes = will_throw;
100 Template parse(nonstd::string_view input) {
101 Parser parser(parser_config, lexer_config, template_storage, function_storage);
102 return parser.parse(input);
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));
112 Template parse_file(
const std::string &filename) {
113 return parse_template(filename);
116 std::string render(nonstd::string_view input,
const json &data) {
return render(parse(input), data); }
118 std::string render(
const Template &tmpl,
const json &data) {
119 std::stringstream os;
120 render_to(os, tmpl, data);
124 std::string render_file(
const std::string &filename,
const json &data) {
125 return render(parse_template(filename), data);
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);
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);
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);
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);
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);
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);
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);
166 json load_json(
const std::string &filename) {
168 open_file_or_throw(input_path + filename, file);
177 void add_callback(
const std::string &name,
const CallbackFunction &callback) {
191 void add_callback(
const std::string &name,
int num_args,
const CallbackFunction &callback) {
192 function_storage.add_callback(name, num_args, callback);
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(); });
207 template_storage[name] = tmpl;
214inline std::string render(nonstd::string_view input,
const json &data) {
215 return Environment().render(input, data);
221inline void render_to(std::ostream &os, nonstd::string_view input,
const json &data) {
223 env.render_to(os, env.parse(input), data);
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