@@ -28,84 +28,64 @@ using json = nlohmann::json;
28
28
* \brief Class for changing the configuration.
29
29
*/
30
30
class Environment {
31
- class Impl {
32
- public:
33
- std::string input_path;
34
- std::string output_path;
35
-
36
- LexerConfig lexer_config;
37
- ParserConfig parser_config;
38
-
39
- FunctionStorage callbacks;
40
- TemplateStorage included_templates;
41
- };
42
-
43
- std::unique_ptr<Impl> m_impl;
44
-
45
31
public:
46
32
Environment (): Environment(" " ) { }
47
33
48
- explicit Environment (const std::string& global_path): m_impl(stdinja::make_unique<Impl>()) {
49
- m_impl->input_path = global_path;
50
- m_impl->output_path = global_path;
51
- }
34
+ explicit Environment (const std::string& global_path): m_input_path(global_path), m_output_path(global_path) {}
52
35
53
- explicit Environment (const std::string& input_path, const std::string& output_path): m_impl(stdinja::make_unique<Impl>()) {
54
- m_impl->input_path = input_path;
55
- m_impl->output_path = output_path;
56
- }
36
+ Environment (const std::string& input_path, const std::string& output_path): m_input_path(input_path), m_output_path(output_path) {}
57
37
58
38
// / Sets the opener and closer for template statements
59
39
void set_statement (const std::string& open, const std::string& close) {
60
- m_impl-> lexer_config .statement_open = open ;
61
- m_impl-> lexer_config .statement_close = close ;
62
- m_impl-> lexer_config .update_open_chars ();
40
+ m_lexer_config .statement_open = open ;
41
+ m_lexer_config .statement_close = close ;
42
+ m_lexer_config .update_open_chars ();
63
43
}
64
44
65
45
// / Sets the opener for template line statements
66
46
void set_line_statement (const std::string& open) {
67
- m_impl-> lexer_config .line_statement = open ;
68
- m_impl-> lexer_config .update_open_chars ();
47
+ m_lexer_config .line_statement = open ;
48
+ m_lexer_config .update_open_chars ();
69
49
}
70
50
71
51
// / Sets the opener and closer for template expressions
72
52
void set_expression (const std::string& open, const std::string& close) {
73
- m_impl-> lexer_config .expression_open = open ;
74
- m_impl-> lexer_config .expression_close = close ;
75
- m_impl-> lexer_config .update_open_chars ();
53
+ m_lexer_config .expression_open = open ;
54
+ m_lexer_config .expression_close = close ;
55
+ m_lexer_config .update_open_chars ();
76
56
}
77
57
78
58
// / Sets the opener and closer for template comments
79
59
void set_comment (const std::string& open, const std::string& close) {
80
- m_impl-> lexer_config .comment_open = open ;
81
- m_impl-> lexer_config .comment_close = close ;
82
- m_impl-> lexer_config .update_open_chars ();
60
+ m_lexer_config .comment_open = open ;
61
+ m_lexer_config .comment_close = close ;
62
+ m_lexer_config .update_open_chars ();
83
63
}
84
64
85
65
// / Sets whether to remove the first newline after a block
86
66
void set_trim_blocks (bool trim_blocks) {
87
- m_impl-> lexer_config .trim_blocks = trim_blocks;
67
+ m_lexer_config .trim_blocks = trim_blocks;
88
68
}
89
69
90
70
// / Sets whether to strip the spaces and tabs from the start of a line to a block
91
71
void set_lstrip_blocks (bool lstrip_blocks) {
92
- m_impl-> lexer_config .lstrip_blocks = lstrip_blocks;
72
+ m_lexer_config .lstrip_blocks = lstrip_blocks;
93
73
}
94
74
95
75
// / Sets the element notation syntax
96
76
void set_element_notation (ElementNotation notation) {
97
- m_impl-> parser_config .notation = notation;
77
+ m_parser_config .notation = notation;
98
78
}
99
79
100
80
101
81
Template parse (nonstd::string_view input) {
102
- Parser parser (m_impl-> parser_config , m_impl-> lexer_config , m_impl-> included_templates );
82
+ Parser parser (m_parser_config, m_lexer_config, m_included_templates );
103
83
return parser.parse (input);
104
84
}
105
85
106
86
Template parse_template (const std::string& filename) {
107
- Parser parser (m_impl-> parser_config , m_impl-> lexer_config , m_impl-> included_templates );
108
- return parser.parse_template (m_impl-> input_path + static_cast <std::string>(filename));
87
+ Parser parser (m_parser_config, m_lexer_config, m_included_templates );
88
+ return parser.parse_template (m_input_path + static_cast <std::string>(filename));
109
89
}
110
90
111
91
std::string render (nonstd::string_view input, const json& data) {
@@ -128,13 +108,13 @@ class Environment {
128
108
}
129
109
130
110
void write (const std::string& filename, const json& data, const std::string& filename_out) {
131
- std::ofstream file (m_impl-> output_path + filename_out);
111
+ std::ofstream file (m_output_path + filename_out);
132
112
file << render_file (filename, data);
133
113
file.close ();
134
114
}
135
115
136
116
void write (const Template& temp, const json& data, const std::string& filename_out) {
137
- std::ofstream file (m_impl-> output_path + filename_out);
117
+ std::ofstream file (m_output_path + filename_out);
138
118
file << render (temp, data);
139
119
file.close ();
140
120
}
@@ -150,33 +130,43 @@ class Environment {
150
130
}
151
131
152
132
std::ostream& render_to (std::ostream& os, const Template& tmpl, const json& data) {
153
- Renderer (m_impl-> included_templates , m_impl-> callbacks ).render_to (os, tmpl, data);
133
+ Renderer (m_included_templates, m_callbacks ).render_to (os, tmpl, data);
154
134
return os;
155
135
}
156
136
157
137
std::string load_file (const std::string& filename) {
158
- Parser parser (m_impl-> parser_config , m_impl-> lexer_config , m_impl-> included_templates );
159
- return parser.load_file (m_impl-> input_path + filename);
138
+ Parser parser (m_parser_config, m_lexer_config, m_included_templates );
139
+ return parser.load_file (m_input_path + filename);
160
140
}
161
141
162
142
json load_json (const std::string& filename) {
163
- std::ifstream file = open_file_or_throw (m_impl-> input_path + filename);
143
+ std::ifstream file = open_file_or_throw (m_input_path + filename);
164
144
json j;
165
145
file >> j;
166
146
return j;
167
147
}
168
148
169
149
void add_callback (const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
170
- m_impl-> callbacks .add_callback (name, numArgs, callback);
150
+ m_callbacks .add_callback (name, numArgs, callback);
171
151
}
172
152
173
153
/* * Includes a template with a given name into the environment.
174
154
* Then, a template can be rendered in another template using the
175
155
* include "<name>" syntax.
176
156
*/
177
157
void include_template (const std::string& name, const Template& tmpl) {
178
- m_impl-> included_templates [name] = tmpl;
158
+ m_included_templates [name] = tmpl;
179
159
}
160
+
161
+ private:
162
+ std::string m_input_path;
163
+ std::string m_output_path;
164
+
165
+ LexerConfig m_lexer_config;
166
+ ParserConfig m_parser_config;
167
+
168
+ FunctionStorage m_callbacks;
169
+ TemplateStorage m_included_templates;
180
170
};
181
171
182
172
/* !
0 commit comments