mirror of
https://github.com/pantor/inja.git
synced 2026-11-21 23:12:41 +00:00
Undo PIMPL pattern in Environment (#127)
* resync single_include * Undo PIMPL pattern in Environment * Environment now supports copy construction and assignment * Add test for copying Environment Closes #126
This commit is contained in:
1 parent
1d419d1940
commit
8e6a8428fa
3 files changed
+232
-197
No files matched your search
@@ -28,84 +28,64 @@ using json = nlohmann::json;
|
|||||||
* \brief Class for changing the configuration.
|
* \brief Class for changing the configuration.
|
||||||
*/
|
*/
|
||||||
class Environment {
|
class Environment {
|
||||||
class Impl {
|
|
||||||
public:
|
|
||||||
std::string input_path;
|
|
||||||
std::string output_path;
|
|
||||||
|
|
||||||
LexerConfig lexer_config;
|
|
||||||
ParserConfig parser_config;
|
|
||||||
|
|
||||||
FunctionStorage callbacks;
|
|
||||||
TemplateStorage included_templates;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unique_ptr<Impl> m_impl;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Environment(): Environment("") { }
|
Environment(): Environment("") { }
|
||||||
|
|
||||||
explicit Environment(const std::string& global_path): m_impl(stdinja::make_unique<Impl>()) {
|
explicit Environment(const std::string& global_path): m_input_path(global_path), m_output_path(global_path) {}
|
||||||
m_impl->input_path = global_path;
|
|
||||||
m_impl->output_path = global_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit Environment(const std::string& input_path, const std::string& output_path): m_impl(stdinja::make_unique<Impl>()) {
|
Environment(const std::string& input_path, const std::string& output_path): m_input_path(input_path), m_output_path(output_path) {}
|
||||||
m_impl->input_path = input_path;
|
|
||||||
m_impl->output_path = output_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the opener and closer for template statements
|
/// Sets the opener and closer for template statements
|
||||||
void set_statement(const std::string& open, const std::string& close) {
|
void set_statement(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.statement_open = open;
|
m_lexer_config.statement_open = open;
|
||||||
m_impl->lexer_config.statement_close = close;
|
m_lexer_config.statement_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener for template line statements
|
/// Sets the opener for template line statements
|
||||||
void set_line_statement(const std::string& open) {
|
void set_line_statement(const std::string& open) {
|
||||||
m_impl->lexer_config.line_statement = open;
|
m_lexer_config.line_statement = open;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener and closer for template expressions
|
/// Sets the opener and closer for template expressions
|
||||||
void set_expression(const std::string& open, const std::string& close) {
|
void set_expression(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.expression_open = open;
|
m_lexer_config.expression_open = open;
|
||||||
m_impl->lexer_config.expression_close = close;
|
m_lexer_config.expression_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener and closer for template comments
|
/// Sets the opener and closer for template comments
|
||||||
void set_comment(const std::string& open, const std::string& close) {
|
void set_comment(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.comment_open = open;
|
m_lexer_config.comment_open = open;
|
||||||
m_impl->lexer_config.comment_close = close;
|
m_lexer_config.comment_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets whether to remove the first newline after a block
|
/// Sets whether to remove the first newline after a block
|
||||||
void set_trim_blocks(bool trim_blocks) {
|
void set_trim_blocks(bool trim_blocks) {
|
||||||
m_impl->lexer_config.trim_blocks = trim_blocks;
|
m_lexer_config.trim_blocks = trim_blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets whether to strip the spaces and tabs from the start of a line to a block
|
/// Sets whether to strip the spaces and tabs from the start of a line to a block
|
||||||
void set_lstrip_blocks(bool lstrip_blocks) {
|
void set_lstrip_blocks(bool lstrip_blocks) {
|
||||||
m_impl->lexer_config.lstrip_blocks = lstrip_blocks;
|
m_lexer_config.lstrip_blocks = lstrip_blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the element notation syntax
|
/// Sets the element notation syntax
|
||||||
void set_element_notation(ElementNotation notation) {
|
void set_element_notation(ElementNotation notation) {
|
||||||
m_impl->parser_config.notation = notation;
|
m_parser_config.notation = notation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Template parse(nonstd::string_view input) {
|
Template parse(nonstd::string_view input) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.parse(input);
|
return parser.parse(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
Template parse_template(const std::string& filename) {
|
Template parse_template(const std::string& filename) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.parse_template(m_impl->input_path + static_cast<std::string>(filename));
|
return parser.parse_template(m_input_path + static_cast<std::string>(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string render(nonstd::string_view input, const json& data) {
|
std::string render(nonstd::string_view input, const json& data) {
|
||||||
@@ -128,13 +108,13 @@ class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void write(const std::string& filename, const json& data, const std::string& filename_out) {
|
void write(const std::string& filename, const json& data, const std::string& filename_out) {
|
||||||
std::ofstream file(m_impl->output_path + filename_out);
|
std::ofstream file(m_output_path + filename_out);
|
||||||
file << render_file(filename, data);
|
file << render_file(filename, data);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const Template& temp, const json& data, const std::string& filename_out) {
|
void write(const Template& temp, const json& data, const std::string& filename_out) {
|
||||||
std::ofstream file(m_impl->output_path + filename_out);
|
std::ofstream file(m_output_path + filename_out);
|
||||||
file << render(temp, data);
|
file << render(temp, data);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
@@ -150,24 +130,24 @@ class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
|
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
|
||||||
Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data);
|
Renderer(m_included_templates, m_callbacks).render_to(os, tmpl, data);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string load_file(const std::string& filename) {
|
std::string load_file(const std::string& filename) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.load_file(m_impl->input_path + filename);
|
return parser.load_file(m_input_path + filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
json load_json(const std::string& filename) {
|
json load_json(const std::string& filename) {
|
||||||
std::ifstream file = open_file_or_throw(m_impl->input_path + filename);
|
std::ifstream file = open_file_or_throw(m_input_path + filename);
|
||||||
json j;
|
json j;
|
||||||
file >> j;
|
file >> j;
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_callback(const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
|
void add_callback(const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
|
||||||
m_impl->callbacks.add_callback(name, numArgs, callback);
|
m_callbacks.add_callback(name, numArgs, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Includes a template with a given name into the environment.
|
/** Includes a template with a given name into the environment.
|
||||||
@@ -175,8 +155,18 @@ class Environment {
|
|||||||
* include "<name>" syntax.
|
* include "<name>" syntax.
|
||||||
*/
|
*/
|
||||||
void include_template(const std::string& name, const Template& tmpl) {
|
void include_template(const std::string& name, const Template& tmpl) {
|
||||||
m_impl->included_templates[name] = tmpl;
|
m_included_templates[name] = tmpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_input_path;
|
||||||
|
std::string m_output_path;
|
||||||
|
|
||||||
|
LexerConfig m_lexer_config;
|
||||||
|
ParserConfig m_parser_config;
|
||||||
|
|
||||||
|
FunctionStorage m_callbacks;
|
||||||
|
TemplateStorage m_included_templates;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
+165
-150
@@ -1,5 +1,7 @@
|
|||||||
#ifndef PANTOR_INJA_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_INJA_HPP_
|
||||||
|
#define INCLUDE_INJA_INJA_HPP_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -12,8 +14,10 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
// #include "environment.hpp"
|
// #include "environment.hpp"
|
||||||
#ifndef PANTOR_INJA_ENVIRONMENT_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_ENVIRONMENT_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||||
|
#define INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -23,8 +27,10 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
// #include "config.hpp"
|
// #include "config.hpp"
|
||||||
#ifndef PANTOR_INJA_CONFIG_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_CONFIG_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_CONFIG_HPP_
|
||||||
|
#define INCLUDE_INJA_CONFIG_HPP_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -1400,19 +1406,23 @@ struct ParserConfig {
|
|||||||
ElementNotation notation {ElementNotation::Dot};
|
ElementNotation notation {ElementNotation::Dot};
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_CONFIG_HPP
|
#endif // INCLUDE_INJA_CONFIG_HPP_
|
||||||
|
|
||||||
// #include "function_storage.hpp"
|
// #include "function_storage.hpp"
|
||||||
#ifndef PANTOR_INJA_FUNCTION_STORAGE_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_FUNCTION_STORAGE_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||||
|
#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// #include "bytecode.hpp"
|
// #include "bytecode.hpp"
|
||||||
#ifndef PANTOR_INJA_BYTECODE_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_BYTECODE_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_BYTECODE_HPP_
|
||||||
|
#define INCLUDE_INJA_BYTECODE_HPP_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -1543,7 +1553,7 @@ struct Bytecode {
|
|||||||
|
|
||||||
} // namespace inja
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_BYTECODE_HPP
|
#endif // INCLUDE_INJA_BYTECODE_HPP_
|
||||||
|
|
||||||
// #include "string_view.hpp"
|
// #include "string_view.hpp"
|
||||||
|
|
||||||
@@ -1551,7 +1561,7 @@ struct Bytecode {
|
|||||||
|
|
||||||
namespace inja {
|
namespace inja {
|
||||||
|
|
||||||
using namespace nlohmann;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
using Arguments = std::vector<const json*>;
|
using Arguments = std::vector<const json*>;
|
||||||
using CallbackFunction = std::function<json(Arguments& args)>;
|
using CallbackFunction = std::function<json(Arguments& args)>;
|
||||||
@@ -1616,11 +1626,13 @@ class FunctionStorage {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // PANTOR_INJA_FUNCTION_STORAGE_HPP
|
#endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||||
|
|
||||||
// #include "parser.hpp"
|
// #include "parser.hpp"
|
||||||
#ifndef PANTOR_INJA_PARSER_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_PARSER_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_PARSER_HPP_
|
||||||
|
#define INCLUDE_INJA_PARSER_HPP_
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -1634,8 +1646,10 @@ class FunctionStorage {
|
|||||||
// #include "function_storage.hpp"
|
// #include "function_storage.hpp"
|
||||||
|
|
||||||
// #include "lexer.hpp"
|
// #include "lexer.hpp"
|
||||||
#ifndef PANTOR_INJA_LEXER_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_LEXER_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_LEXER_HPP_
|
||||||
|
#define INCLUDE_INJA_LEXER_HPP_
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
@@ -1643,8 +1657,10 @@ class FunctionStorage {
|
|||||||
// #include "config.hpp"
|
// #include "config.hpp"
|
||||||
|
|
||||||
// #include "token.hpp"
|
// #include "token.hpp"
|
||||||
#ifndef PANTOR_INJA_TOKEN_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_TOKEN_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_TOKEN_HPP_
|
||||||
|
#define INCLUDE_INJA_TOKEN_HPP_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -1710,11 +1726,13 @@ struct Token {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // PANTOR_INJA_TOKEN_HPP
|
#endif // INCLUDE_INJA_TOKEN_HPP_
|
||||||
|
|
||||||
// #include "utils.hpp"
|
// #include "utils.hpp"
|
||||||
#ifndef PANTOR_INJA_UTILS_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_UTILS_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_UTILS_HPP_
|
||||||
|
#define INCLUDE_INJA_UTILS_HPP_
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -1761,11 +1779,11 @@ namespace string_view {
|
|||||||
inline bool starts_with(nonstd::string_view view, nonstd::string_view prefix) {
|
inline bool starts_with(nonstd::string_view view, nonstd::string_view prefix) {
|
||||||
return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0);
|
return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0);
|
||||||
}
|
}
|
||||||
} // namespace string
|
} // namespace string_view
|
||||||
|
|
||||||
} // namespace inja
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_UTILS_HPP
|
#endif // INCLUDE_INJA_UTILS_HPP_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1835,7 +1853,7 @@ class Lexer {
|
|||||||
inja::string_view::starts_with(open_str, m_config.line_statement)) {
|
inja::string_view::starts_with(open_str, m_config.line_statement)) {
|
||||||
m_state = State::LineStart;
|
m_state = State::LineStart;
|
||||||
} else {
|
} else {
|
||||||
m_pos += 1; // wasn't actually an opening sequence
|
m_pos += 1; // wasn't actually an opening sequence
|
||||||
goto again;
|
goto again;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2063,11 +2081,13 @@ class Lexer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // PANTOR_INJA_LEXER_HPP
|
#endif // INCLUDE_INJA_LEXER_HPP_
|
||||||
|
|
||||||
// #include "template.hpp"
|
// #include "template.hpp"
|
||||||
#ifndef PANTOR_INJA_TEMPLATE_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_TEMPLATE_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_TEMPLATE_HPP_
|
||||||
|
#define INCLUDE_INJA_TEMPLATE_HPP_
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -2089,9 +2109,9 @@ struct Template {
|
|||||||
|
|
||||||
using TemplateStorage = std::map<std::string, Template>;
|
using TemplateStorage = std::map<std::string, Template>;
|
||||||
|
|
||||||
}
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_TEMPLATE_HPP
|
#endif // INCLUDE_INJA_TEMPLATE_HPP_
|
||||||
|
|
||||||
// #include "token.hpp"
|
// #include "token.hpp"
|
||||||
|
|
||||||
@@ -2644,11 +2664,13 @@ class Parser {
|
|||||||
|
|
||||||
} // namespace inja
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_PARSER_HPP
|
#endif // INCLUDE_INJA_PARSER_HPP_
|
||||||
|
|
||||||
// #include "polyfill.hpp"
|
// #include "polyfill.hpp"
|
||||||
#ifndef PANTOR_INJA_POLYFILL_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_POLYFILL_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_POLYFILL_HPP_
|
||||||
|
#define INCLUDE_INJA_POLYFILL_HPP_
|
||||||
|
|
||||||
|
|
||||||
#if __cplusplus < 201402L
|
#if __cplusplus < 201402L
|
||||||
@@ -2660,48 +2682,51 @@ class Parser {
|
|||||||
|
|
||||||
|
|
||||||
namespace stdinja {
|
namespace stdinja {
|
||||||
template<class T> struct _Unique_if {
|
|
||||||
typedef std::unique_ptr<T> _Single_object;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class T> struct _Unique_if<T[]> {
|
template<class T> struct _Unique_if {
|
||||||
typedef std::unique_ptr<T[]> _Unknown_bound;
|
typedef std::unique_ptr<T> _Single_object;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T, size_t N> struct _Unique_if<T[N]> {
|
template<class T> struct _Unique_if<T[]> {
|
||||||
typedef void _Known_bound;
|
typedef std::unique_ptr<T[]> _Unknown_bound;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T, class... Args>
|
template<class T, size_t N> struct _Unique_if<T[N]> {
|
||||||
typename _Unique_if<T>::_Single_object
|
typedef void _Known_bound;
|
||||||
make_unique(Args&&... args) {
|
};
|
||||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
template<class T, class... Args>
|
||||||
typename _Unique_if<T>::_Unknown_bound
|
typename _Unique_if<T>::_Single_object
|
||||||
make_unique(size_t n) {
|
make_unique(Args&&... args) {
|
||||||
typedef typename std::remove_extent<T>::type U;
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||||
return std::unique_ptr<T>(new U[n]());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T, class... Args>
|
|
||||||
typename _Unique_if<T>::_Known_bound
|
|
||||||
make_unique(Args&&...) = delete;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
typename _Unique_if<T>::_Unknown_bound
|
||||||
|
make_unique(size_t n) {
|
||||||
|
typedef typename std::remove_extent<T>::type U;
|
||||||
|
return std::unique_ptr<T>(new U[n]());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class... Args>
|
||||||
|
typename _Unique_if<T>::_Known_bound
|
||||||
|
make_unique(Args&&...) = delete;
|
||||||
|
|
||||||
|
} // namespace stdinja
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
namespace stdinja = std;
|
namespace stdinja = std;
|
||||||
|
|
||||||
#endif // memory */
|
#endif // memory */
|
||||||
|
|
||||||
|
#endif // INCLUDE_INJA_POLYFILL_HPP_
|
||||||
#endif // PANTOR_INJA_POLYFILL_HPP
|
|
||||||
|
|
||||||
// #include "renderer.hpp"
|
// #include "renderer.hpp"
|
||||||
#ifndef PANTOR_INJA_RENDERER_HPP
|
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||||
#define PANTOR_INJA_RENDERER_HPP
|
|
||||||
|
#ifndef INCLUDE_INJA_RENDERER_HPP_
|
||||||
|
#define INCLUDE_INJA_RENDERER_HPP_
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
@@ -2843,22 +2868,21 @@ class Renderer {
|
|||||||
enum class Type { Map, Array };
|
enum class Type { Map, Array };
|
||||||
|
|
||||||
Type loop_type;
|
Type loop_type;
|
||||||
nonstd::string_view key_name; // variable name for keys
|
nonstd::string_view key_name; // variable name for keys
|
||||||
nonstd::string_view value_name; // variable name for values
|
nonstd::string_view value_name; // variable name for values
|
||||||
json data; // data with loop info added
|
json data; // data with loop info added
|
||||||
|
|
||||||
json values; // values to iterate over
|
json values; // values to iterate over
|
||||||
|
|
||||||
// loop over list
|
// loop over list
|
||||||
size_t index; // current list index
|
size_t index; // current list index
|
||||||
size_t size; // length of list
|
size_t size; // length of list
|
||||||
|
|
||||||
// loop over map
|
// loop over map
|
||||||
using KeyValue = std::pair<nonstd::string_view, json*>;
|
using KeyValue = std::pair<nonstd::string_view, json*>;
|
||||||
using MapValues = std::vector<KeyValue>;
|
using MapValues = std::vector<KeyValue>;
|
||||||
MapValues map_values; // values to iterate over
|
MapValues map_values; // values to iterate over
|
||||||
MapValues::iterator map_it; // iterator over values
|
MapValues::iterator map_it; // iterator over values
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<LoopLevel> m_loop_stack;
|
std::vector<LoopLevel> m_loop_stack;
|
||||||
@@ -3213,7 +3237,8 @@ class Renderer {
|
|||||||
for (auto it = level.values.begin(), end = level.values.end(); it != end; ++it) {
|
for (auto it = level.values.begin(), end = level.values.end(); it != end; ++it) {
|
||||||
level.map_values.emplace_back(it.key(), &it.value());
|
level.map_values.emplace_back(it.key(), &it.value());
|
||||||
}
|
}
|
||||||
std::sort(level.map_values.begin(), level.map_values.end(), [](const LoopLevel::KeyValue& a, const LoopLevel::KeyValue& b) { return a.first < b.first; });
|
auto sort_lambda = [](const LoopLevel::KeyValue& a, const LoopLevel::KeyValue& b) { return a.first < b.first; };
|
||||||
|
std::sort(level.map_values.begin(), level.map_values.end(), sort_lambda);
|
||||||
level.map_it = level.map_values.begin();
|
level.map_it = level.map_values.begin();
|
||||||
} else {
|
} else {
|
||||||
if (!level.values.is_array()) {
|
if (!level.values.is_array()) {
|
||||||
@@ -3281,7 +3306,7 @@ class Renderer {
|
|||||||
|
|
||||||
} // namespace inja
|
} // namespace inja
|
||||||
|
|
||||||
#endif // PANTOR_INJA_RENDERER_HPP
|
#endif // INCLUDE_INJA_RENDERER_HPP_
|
||||||
|
|
||||||
// #include "string_view.hpp"
|
// #include "string_view.hpp"
|
||||||
|
|
||||||
@@ -3293,91 +3318,71 @@ class Renderer {
|
|||||||
|
|
||||||
namespace inja {
|
namespace inja {
|
||||||
|
|
||||||
using namespace nlohmann;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Class for changing the configuration.
|
* \brief Class for changing the configuration.
|
||||||
*/
|
*/
|
||||||
class Environment {
|
class Environment {
|
||||||
class Impl {
|
|
||||||
public:
|
|
||||||
std::string input_path;
|
|
||||||
std::string output_path;
|
|
||||||
|
|
||||||
LexerConfig lexer_config;
|
|
||||||
ParserConfig parser_config;
|
|
||||||
|
|
||||||
FunctionStorage callbacks;
|
|
||||||
TemplateStorage included_templates;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unique_ptr<Impl> m_impl;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Environment(): Environment("") { }
|
Environment(): Environment("") { }
|
||||||
|
|
||||||
explicit Environment(const std::string& global_path): m_impl(stdinja::make_unique<Impl>()) {
|
explicit Environment(const std::string& global_path): m_input_path(global_path), m_output_path(global_path) {}
|
||||||
m_impl->input_path = global_path;
|
|
||||||
m_impl->output_path = global_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit Environment(const std::string& input_path, const std::string& output_path): m_impl(stdinja::make_unique<Impl>()) {
|
Environment(const std::string& input_path, const std::string& output_path): m_input_path(input_path), m_output_path(output_path) {}
|
||||||
m_impl->input_path = input_path;
|
|
||||||
m_impl->output_path = output_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the opener and closer for template statements
|
/// Sets the opener and closer for template statements
|
||||||
void set_statement(const std::string& open, const std::string& close) {
|
void set_statement(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.statement_open = open;
|
m_lexer_config.statement_open = open;
|
||||||
m_impl->lexer_config.statement_close = close;
|
m_lexer_config.statement_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener for template line statements
|
/// Sets the opener for template line statements
|
||||||
void set_line_statement(const std::string& open) {
|
void set_line_statement(const std::string& open) {
|
||||||
m_impl->lexer_config.line_statement = open;
|
m_lexer_config.line_statement = open;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener and closer for template expressions
|
/// Sets the opener and closer for template expressions
|
||||||
void set_expression(const std::string& open, const std::string& close) {
|
void set_expression(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.expression_open = open;
|
m_lexer_config.expression_open = open;
|
||||||
m_impl->lexer_config.expression_close = close;
|
m_lexer_config.expression_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the opener and closer for template comments
|
/// Sets the opener and closer for template comments
|
||||||
void set_comment(const std::string& open, const std::string& close) {
|
void set_comment(const std::string& open, const std::string& close) {
|
||||||
m_impl->lexer_config.comment_open = open;
|
m_lexer_config.comment_open = open;
|
||||||
m_impl->lexer_config.comment_close = close;
|
m_lexer_config.comment_close = close;
|
||||||
m_impl->lexer_config.update_open_chars();
|
m_lexer_config.update_open_chars();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets whether to remove the first newline after a block
|
/// Sets whether to remove the first newline after a block
|
||||||
void set_trim_blocks(bool trim_blocks) {
|
void set_trim_blocks(bool trim_blocks) {
|
||||||
m_impl->lexer_config.trim_blocks = trim_blocks;
|
m_lexer_config.trim_blocks = trim_blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets whether to strip the spaces and tabs from the start of a line to a block
|
/// Sets whether to strip the spaces and tabs from the start of a line to a block
|
||||||
void set_lstrip_blocks(bool lstrip_blocks) {
|
void set_lstrip_blocks(bool lstrip_blocks) {
|
||||||
m_impl->lexer_config.lstrip_blocks = lstrip_blocks;
|
m_lexer_config.lstrip_blocks = lstrip_blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the element notation syntax
|
/// Sets the element notation syntax
|
||||||
void set_element_notation(ElementNotation notation) {
|
void set_element_notation(ElementNotation notation) {
|
||||||
m_impl->parser_config.notation = notation;
|
m_parser_config.notation = notation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Template parse(nonstd::string_view input) {
|
Template parse(nonstd::string_view input) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.parse(input);
|
return parser.parse(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
Template parse_template(const std::string& filename) {
|
Template parse_template(const std::string& filename) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.parse_template(m_impl->input_path + static_cast<std::string>(filename));
|
return parser.parse_template(m_input_path + static_cast<std::string>(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string render(nonstd::string_view input, const json& data) {
|
std::string render(nonstd::string_view input, const json& data) {
|
||||||
return render(parse(input), data);
|
return render(parse(input), data);
|
||||||
@@ -3390,55 +3395,55 @@ class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string render_file(const std::string& filename, const json& data) {
|
std::string render_file(const std::string& filename, const json& data) {
|
||||||
return render(parse_template(filename), data);
|
return render(parse_template(filename), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string render_file_with_json_file(const std::string& filename, const std::string& filename_data) {
|
std::string render_file_with_json_file(const std::string& filename, const std::string& filename_data) {
|
||||||
const json data = load_json(filename_data);
|
const json data = load_json(filename_data);
|
||||||
return render_file(filename, data);
|
return render_file(filename, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const std::string& filename, const json& data, const std::string& filename_out) {
|
void write(const std::string& filename, const json& data, const std::string& filename_out) {
|
||||||
std::ofstream file(m_impl->output_path + filename_out);
|
std::ofstream file(m_output_path + filename_out);
|
||||||
file << render_file(filename, data);
|
file << render_file(filename, data);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const Template& temp, const json& data, const std::string& filename_out) {
|
void write(const Template& temp, const json& data, const std::string& filename_out) {
|
||||||
std::ofstream file(m_impl->output_path + filename_out);
|
std::ofstream file(m_output_path + filename_out);
|
||||||
file << render(temp, data);
|
file << render(temp, data);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) {
|
void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) {
|
||||||
const json data = load_json(filename_data);
|
const json data = load_json(filename_data);
|
||||||
write(filename, data, filename_out);
|
write(filename, data, filename_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) {
|
void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) {
|
||||||
const json data = load_json(filename_data);
|
const json data = load_json(filename_data);
|
||||||
write(temp, data, filename_out);
|
write(temp, data, filename_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
|
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
|
||||||
Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data);
|
Renderer(m_included_templates, m_callbacks).render_to(os, tmpl, data);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string load_file(const std::string& filename) {
|
std::string load_file(const std::string& filename) {
|
||||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
Parser parser(m_parser_config, m_lexer_config, m_included_templates);
|
||||||
return parser.load_file(m_impl->input_path + filename);
|
return parser.load_file(m_input_path + filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
json load_json(const std::string& filename) {
|
json load_json(const std::string& filename) {
|
||||||
std::ifstream file = open_file_or_throw(m_impl->input_path + filename);
|
std::ifstream file = open_file_or_throw(m_input_path + filename);
|
||||||
json j;
|
json j;
|
||||||
file >> j;
|
file >> j;
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_callback(const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
|
void add_callback(const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
|
||||||
m_impl->callbacks.add_callback(name, numArgs, callback);
|
m_callbacks.add_callback(name, numArgs, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Includes a template with a given name into the environment.
|
/** Includes a template with a given name into the environment.
|
||||||
@@ -3446,8 +3451,18 @@ class Environment {
|
|||||||
* include "<name>" syntax.
|
* include "<name>" syntax.
|
||||||
*/
|
*/
|
||||||
void include_template(const std::string& name, const Template& tmpl) {
|
void include_template(const std::string& name, const Template& tmpl) {
|
||||||
m_impl->included_templates[name] = tmpl;
|
m_included_templates[name] = tmpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_input_path;
|
||||||
|
std::string m_output_path;
|
||||||
|
|
||||||
|
LexerConfig m_lexer_config;
|
||||||
|
ParserConfig m_parser_config;
|
||||||
|
|
||||||
|
FunctionStorage m_callbacks;
|
||||||
|
TemplateStorage m_included_templates;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -3467,7 +3482,7 @@ inline void render_to(std::ostream& os, nonstd::string_view input, const json& d
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // PANTOR_INJA_ENVIRONMENT_HPP
|
#endif // INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||||
|
|
||||||
// #include "string_view.hpp"
|
// #include "string_view.hpp"
|
||||||
|
|
||||||
@@ -3479,4 +3494,4 @@ inline void render_to(std::ostream& os, nonstd::string_view input, const json& d
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // PANTOR_INJA_HPP
|
#endif // INCLUDE_INJA_INJA_HPP_
|
||||||
@@ -2,3 +2,33 @@
|
|||||||
|
|
||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
#include "catch/catch.hpp"
|
#include "catch/catch.hpp"
|
||||||
|
#include "inja/inja.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("copy-environment") {
|
||||||
|
inja::Environment env;
|
||||||
|
env.add_callback("double", 1, [](inja::Arguments& args) {
|
||||||
|
int number = args.at(0)->get<int>();
|
||||||
|
return 2 * number;
|
||||||
|
});
|
||||||
|
|
||||||
|
inja::Template t1 = env.parse("{{ double(2) }}");
|
||||||
|
env.include_template("tpl", t1);
|
||||||
|
std::string test_tpl = "{% include \"tpl\" %}";
|
||||||
|
|
||||||
|
REQUIRE(env.render(test_tpl, json()) == "4");
|
||||||
|
|
||||||
|
inja::Environment copy(env);
|
||||||
|
CHECK(copy.render(test_tpl, json()) == "4");
|
||||||
|
|
||||||
|
// overwrite template in source env
|
||||||
|
inja::Template t2 = env.parse("{{ double(4) }}");
|
||||||
|
env.include_template("tpl", t2);
|
||||||
|
REQUIRE(env.render(test_tpl, json()) == "8");
|
||||||
|
|
||||||
|
// template is unchanged in copy
|
||||||
|
CHECK(copy.render(test_tpl, json()) == "4");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user