templates, split parser and renderer, c++ class parser

This commit is contained in:
pantor
2017-11-21 21:19:52 +01:00
parent 27140612b3
commit 2c96b3bdf5
7 changed files with 370 additions and 398 deletions

View File

@@ -57,13 +57,17 @@ Environment env = Environment();
std::string result = env.render("Hello {{ name }}!", data);
// Or directly read a template file
result = env.render_template("./template.txt", data);
Template temp = env.parse_template("./template.txt");
std::string result = temp.render(data); // "Hello World!"
// And read a json file for data
data["name"] = "Inja";
std::string result = temp.render(data); // "Hello Inja!"
// Or read a json file for data directly from the environment
result = env.render_template("./template.txt", "./data.json");
// Or write a rendered template file
env.write("./template.txt", data, "./result.txt")
temp.write(data, "./result.txt")
env.write("./template.txt", "./data.json", "./result.txt")
```

View File

@@ -42,12 +42,17 @@ inline std::string dot_to_json_pointer_notation(std::string dot) {
}
enum class ElementNotation {
Pointer,
Dot
};
/*!
@brief inja regex class, saves string pattern in addition to std::regex
*/
class Regex: public std::regex {
std::string pattern_;
@@ -59,6 +64,7 @@ public:
};
class Match: public std::smatch {
size_t offset_ = 0;
int group_offset_ = 0;
@@ -84,6 +90,7 @@ public:
};
class MatchClosed {
public:
Match open_match, close_match;
@@ -186,12 +193,13 @@ inline Match match(const std::string& input, std::vector<Regex> regexes) {
struct Parsed {
enum class Type {
Main,
String,
Comment,
Expression,
Loop,
Condition,
ConditionBranch,
Comment,
Expression
ConditionBranch
};
enum class Delimiter {
@@ -207,7 +215,7 @@ struct Parsed {
Include
};
enum class ConditionOperators {
enum class Function {
Not,
And,
Or,
@@ -217,10 +225,7 @@ struct Parsed {
Less,
GreaterEqual,
LessEqual,
Different
};
enum class Function {
Different,
Upper,
Lower,
Range,
@@ -228,7 +233,56 @@ struct Parsed {
Round,
DivisibleBy,
Odd,
Even
Even,
ReadJson
};
struct Element {
Parsed::Type type;
std::string inner;
std::vector<std::shared_ptr<Element>> children;
explicit Element(Parsed::Type type): Element(type, "") { }
explicit Element(Parsed::Type type, std::string inner): type(type), inner(inner), children({}) { }
};
struct ElementString: public Element {
std::string text;
explicit ElementString(std::string text): Element(Parsed::Type::String), text(text) { }
};
struct ElementComment: public Element {
std::string text;
explicit ElementComment(std::string text): Element(Parsed::Type::Comment), text(text) { }
};
struct ElementExpression: public Element {
Function function;
std::vector<ElementExpression> args;
std::string command;
explicit ElementExpression(): ElementExpression(Parsed::Function::ReadJson) { }
explicit ElementExpression(Parsed::Function function): Element(Parsed::Type::Expression), function(function), args({}), command("") { }
};
struct ElementLoop: public Element {
std::string item;
ElementExpression list;
explicit ElementLoop(std::string item, ElementExpression list, std::string inner): Element(Parsed::Type::Loop, inner), item(item), list(list) { }
};
struct ElementConditionContainer: public Element {
explicit ElementConditionContainer(): Element(Parsed::Type::Condition) { }
};
struct ElementConditionBranch: public Element {
std::string condition_type;
ElementExpression condition;
explicit ElementConditionBranch(std::string inner, std::string condition_type, ElementExpression condition): Element(Parsed::Type::ConditionBranch, inner), condition_type(condition_type), condition(condition) { }
};
};
@@ -236,143 +290,136 @@ struct Parsed {
class Template {
public:
const json parsed_template;
const Parsed::Element parsed_template;
ElementNotation elementNotation;
explicit Template(const json parsed_template): parsed_template(parsed_template) { }
explicit Template(const json parsed_template, ElementNotation elementNotation): parsed_template(parsed_template), elementNotation(elementNotation) { }
explicit Template(const Parsed::Element parsed_template): Template(parsed_template, ElementNotation::Pointer) { }
explicit Template(const Parsed::Element parsed_template, ElementNotation elementNotation): parsed_template(parsed_template), elementNotation(elementNotation) { }
template<typename T = json>
T eval_variable(json element, json data) {
T eval_variable(Parsed::ElementExpression element, json data) {
const json var = eval_variable(element, data, true);
if (std::is_same<T, json>::value) { return var; }
return var.get<T>();
}
json eval_variable(json element, json data, bool throw_error) {
if (element.find("function") != element.end()) {
switch ( static_cast<Parsed::Function>(element["function"]) ) {
case Parsed::Function::Upper: {
std::string str = eval_variable<std::string>(element["arg1"], data);
std::transform(str.begin(), str.end(), str.begin(), toupper);
return str;
}
case Parsed::Function::Lower: {
std::string str = eval_variable<std::string>(element["arg1"], data);
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
}
case Parsed::Function::Range: {
const int number = eval_variable<int>(element["arg1"], data);
std::vector<int> result(number);
std::iota(std::begin(result), std::end(result), 0);
return result;
}
case Parsed::Function::Length: {
const std::vector<json> list = eval_variable<std::vector<json>>(element["arg1"], data);
return list.size();
}
case Parsed::Function::Round: {
const double number = eval_variable<double>(element["arg1"], data);
const int precision = eval_variable<int>(element["arg2"], data);
return std::round(number * std::pow(10.0, precision)) / std::pow(10.0, precision);
}
case Parsed::Function::DivisibleBy: {
const int number = eval_variable<int>(element["arg1"], data);
const int divisor = eval_variable<int>(element["arg2"], data);
return (number % divisor == 0);
}
case Parsed::Function::Odd: {
const int number = eval_variable<int>(element["arg1"], data);
return (number % 2 != 0);
}
case Parsed::Function::Even: {
const int number = eval_variable<int>(element["arg1"], data);
return (number % 2 == 0);
json eval_variable(Parsed::ElementExpression element, json data, bool throw_error) {
switch (element.function) {
case Parsed::Function::Upper: {
std::string str = eval_variable<std::string>(element.args[0], data);
std::transform(str.begin(), str.end(), str.begin(), toupper);
return str;
}
case Parsed::Function::Lower: {
std::string str = eval_variable<std::string>(element.args[0], data);
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
}
case Parsed::Function::Range: {
const int number = eval_variable<int>(element.args[0], data);
std::vector<int> result(number);
std::iota(std::begin(result), std::end(result), 0);
return result;
}
case Parsed::Function::Length: {
const std::vector<json> list = eval_variable<std::vector<json>>(element.args[0], data);
return list.size();
}
case Parsed::Function::Round: {
const double number = eval_variable<double>(element.args[0], data);
const int precision = eval_variable<int>(element.args[1], data);
return std::round(number * std::pow(10.0, precision)) / std::pow(10.0, precision);
}
case Parsed::Function::DivisibleBy: {
const int number = eval_variable<int>(element.args[0], data);
const int divisor = eval_variable<int>(element.args[1], data);
return (number % divisor == 0);
}
case Parsed::Function::Odd: {
const int number = eval_variable<int>(element.args[0], data);
return (number % 2 != 0);
}
case Parsed::Function::Even: {
const int number = eval_variable<int>(element.args[0], data);
return (number % 2 == 0);
}
case Parsed::Function::Not: {
return not eval_condition(element.args[0], data);
}
case Parsed::Function::And: {
return (eval_condition(element.args[0], data) and eval_condition(element.args[1], data));
}
case Parsed::Function::Or: {
return (eval_condition(element.args[0], data) or eval_condition(element.args[1], data));
}
case Parsed::Function::In: {
const json item = eval_variable(element.args[0], data);
const json list = eval_variable(element.args[1], data);
return (std::find(list.begin(), list.end(), item) != list.end());
}
case Parsed::Function::Equal: {
return eval_variable(element.args[0], data) == eval_variable(element.args[1], data);
}
case Parsed::Function::Greater: {
return eval_variable(element.args[0], data) > eval_variable(element.args[1], data);
}
case Parsed::Function::Less: {
return eval_variable(element.args[0], data) < eval_variable(element.args[1], data);
}
case Parsed::Function::GreaterEqual: {
return eval_variable(element.args[0], data) >= eval_variable(element.args[1], data);
}
case Parsed::Function::LessEqual: {
return eval_variable(element.args[0], data) <= eval_variable(element.args[1], data);
}
case Parsed::Function::Different: {
return eval_variable(element.args[0], data) != eval_variable(element.args[1], data);
}
case Parsed::Function::ReadJson: {
// Json Raw Data
if ( json::accept(element.command) ) { return json::parse(element.command); }
std::string input = element.command;
switch (elementNotation) {
case ElementNotation::Pointer: {
if (input[0] != '/') { input.insert(0, "/"); }
break;
}
case ElementNotation::Dot: {
input = dot_to_json_pointer_notation(input);
break;
}
}
const json::json_pointer ptr(input);
const json result = data[ptr];
if (throw_error && result.is_null()) { throw std::runtime_error("Did not found json element: " + element.command); }
return result;
}
}
const std::string input = element["command"];
// Json Raw Data
if ( json::accept(input) ) { return json::parse(input); }
std::string input_copy = input;
switch (elementNotation) {
case ElementNotation::Pointer: {
if (input_copy[0] != '/') { input_copy.insert(0, "/"); }
break;
}
case ElementNotation::Dot: {
input_copy = dot_to_json_pointer_notation(input_copy);
break;
}
}
json::json_pointer ptr(input_copy);
json result = data[ptr];
if (throw_error && result.is_null()) { throw std::runtime_error("JSON pointer found no element."); }
return result;
}
bool eval_condition(json element, json data) {
if (element.find("condition") != element.end()) {
switch ( static_cast<Parsed::ConditionOperators>(element["condition"]) ) {
case Parsed::ConditionOperators::Not: {
return not eval_condition(element["arg1"], data);
}
case Parsed::ConditionOperators::And: {
return (eval_condition(element["arg1"], data) and eval_condition(element["arg2"], data));
}
case Parsed::ConditionOperators::Or: {
return (eval_condition(element["arg1"], data) or eval_condition(element["arg2"], data));
}
case Parsed::ConditionOperators::In: {
const json item = eval_variable(element["arg1"], data);
const json list = eval_variable(element["arg2"], data);
return (std::find(list.begin(), list.end(), item) != list.end());
}
case Parsed::ConditionOperators::Equal: {
return eval_variable(element["arg1"], data) == eval_variable(element["arg2"], data);
}
case Parsed::ConditionOperators::Greater: {
return eval_variable(element["arg1"], data) > eval_variable(element["arg2"], data);
}
case Parsed::ConditionOperators::Less: {
return eval_variable(element["arg1"], data) < eval_variable(element["arg2"], data);
}
case Parsed::ConditionOperators::GreaterEqual: {
return eval_variable(element["arg1"], data) >= eval_variable(element["arg2"], data);
}
case Parsed::ConditionOperators::LessEqual: {
return eval_variable(element["arg1"], data) <= eval_variable(element["arg2"], data);
}
case Parsed::ConditionOperators::Different: {
return eval_variable(element["arg1"], data) != eval_variable(element["arg2"], data);
}
}
}
bool eval_condition(Parsed::ElementExpression element, json data) {
const json var = eval_variable(element, data, false);
if (var.empty()) { return false; }
else if (var.is_boolean()) { return var; }
else if (var.is_number()) { return (var != 0); }
else if (var.is_string()) { return not var.empty(); }
return true;
return var;
}
std::string render(json data) {
std::string result = "";
for (auto element: parsed_template) {
switch ( static_cast<Parsed::Type>(element["type"]) ) {
for (auto element: parsed_template.children) {
switch (element->type) {
case Parsed::Type::String: {
result += element["text"].get<std::string>();
auto elementString = std::static_pointer_cast<Parsed::ElementString>(element);
result += elementString->text;
break;
}
case Parsed::Type::Expression: {
json variable = eval_variable(element, data);
auto elementExpression = std::static_pointer_cast<Parsed::ElementExpression>(element);
json variable = eval_variable(*elementExpression, data);
if (variable.is_string()) {
result += variable.get<std::string>();
} else {
@@ -383,9 +430,10 @@ public:
break;
}
case Parsed::Type::Loop: {
const std::string item_name = element["item"].get<std::string>();
auto elementLoop = std::static_pointer_cast<Parsed::ElementLoop>(element);
const std::string item_name = elementLoop->item;
std::vector<json> list = eval_variable<std::vector<json>>(element["list"], data);
const std::vector<json> list = eval_variable<std::vector<json>>(elementLoop->list, data);
for (int i = 0; i < list.size(); i++) {
json data_loop = data;
data_loop[item_name] = list[i];
@@ -393,14 +441,16 @@ public:
data_loop["index1"] = i + 1;
data_loop["is_first"] = (i == 0);
data_loop["is_last"] = (i == list.size() - 1);
result += Template(element["children"], elementNotation).render(data_loop);
result += Template(*elementLoop, elementNotation).render(data_loop);
}
break;
}
case Parsed::Type::Condition: {
for (auto branch: element["children"]) {
if (eval_condition(branch["condition"], data) || branch["condition_type"] == "else") {
result += Template(branch["children"], elementNotation).render(data);
auto elementCondition = std::static_pointer_cast<Parsed::ElementConditionContainer>(element);
for (auto branch: elementCondition->children) {
auto elementBranch = std::static_pointer_cast<Parsed::ElementConditionBranch>(branch);
if (eval_condition(elementBranch->condition, data) || elementBranch->condition_type == "else") {
result += Template(*elementBranch, elementNotation).render(data);
break;
}
}
@@ -439,20 +489,18 @@ public:
const Regex regex_condition_else{"else"};
const Regex regex_condition_close{"endif"};
const std::map<Parsed::ConditionOperators, Regex> regex_map_condition_operators = {
{Parsed::ConditionOperators::Not, Regex{"not (.+)"}},
{Parsed::ConditionOperators::And, Regex{"(.+) and (.+)"}},
{Parsed::ConditionOperators::Or, Regex{"(.+) or (.+)"}},
{Parsed::ConditionOperators::In, Regex{"(.+) in (.+)"}},
{Parsed::ConditionOperators::Equal, Regex{"(.+) == (.+)"}},
{Parsed::ConditionOperators::Greater, Regex{"(.+) > (.+)"}},
{Parsed::ConditionOperators::Less, Regex{"(.+) < (.+)"}},
{Parsed::ConditionOperators::GreaterEqual, Regex{"(.+) >= (.+)"}},
{Parsed::ConditionOperators::LessEqual, Regex{"(.+) <= (.+)"}},
{Parsed::ConditionOperators::Different, Regex{"(.+) != (.+)"}}
};
const std::map<Parsed::Function, Regex> regex_map_functions = {
{Parsed::Function::Not, Regex{"not (.+)"}},
{Parsed::Function::And, Regex{"(.+) and (.+)"}},
{Parsed::Function::Or, Regex{"(.+) or (.+)"}},
{Parsed::Function::In, Regex{"(.+) in (.+)"}},
{Parsed::Function::Equal, Regex{"(.+) == (.+)"}},
{Parsed::Function::Greater, Regex{"(.+) > (.+)"}},
{Parsed::Function::Less, Regex{"(.+) < (.+)"}},
{Parsed::Function::GreaterEqual, Regex{"(.+) >= (.+)"}},
{Parsed::Function::LessEqual, Regex{"(.+) <= (.+)"}},
{Parsed::Function::Different, Regex{"(.+) != (.+)"}},
{Parsed::Function::Upper, Regex{"upper\\(\\s*(.*?)\\s*\\)"}},
{Parsed::Function::Lower, Regex{"lower\\(\\s*(.*?)\\s*\\)"}},
{Parsed::Function::Range, Regex{"range\\(\\s*(.*?)\\s*\\)"}},
@@ -460,33 +508,24 @@ public:
{Parsed::Function::Round, Regex{"round\\(\\s*(.*?)\\s*,\\s*(.*?)\\s*\\)"}},
{Parsed::Function::DivisibleBy, Regex{"divisibleBy\\(\\s*(.*?)\\s*,\\s*(.*?)\\s*\\)"}},
{Parsed::Function::Odd, Regex{"odd\\(\\s*(.*?)\\s*\\)"}},
{Parsed::Function::Even, Regex{"even\\(\\s*(.*?)\\s*\\)"}}
{Parsed::Function::Even, Regex{"even\\(\\s*(.*?)\\s*\\)"}},
{Parsed::Function::ReadJson, Regex{"\\s*(.*?)\\s*"}}
};
Parser() { }
json element(Parsed::Type type, json element_data) {
element_data["type"] = type;
return element_data;
}
json element_function(Parsed::Function func, int number_args, Match match) {
json result = element(Parsed::Type::Expression, {{"function", func}});
for (int i = 1; i < number_args + 1; i++) {
result["arg" + std::to_string(i)] = parse_expression(match.str(i));
Parsed::ElementExpression element_function(Parsed::Function func, int number_args, Match match) {
std::vector<Parsed::ElementExpression> args = {};
for (int i = 0; i < number_args; i++) {
args.push_back( parse_expression(match.str(i + 1)) );
}
Parsed::ElementExpression result = Parsed::ElementExpression(func);
result.args = args;
return result;
}
json element_condition(Parsed::ConditionOperators op, int number_args, Match match) {
json result = element(Parsed::Type::Expression, {{"condition", op}});
for (int i = 1; i < number_args + 1; i++) {
result["arg" + std::to_string(i)] = parse_expression(match.str(i));
}
return result;
}
json parse_expression(const std::string& input) {
Parsed::ElementExpression parse_expression(const std::string& input) {
Match match_function = match(input, get_values(regex_map_functions));
switch ( static_cast<Parsed::Function>(match_function.regex_number()) ) {
case Parsed::Function::Upper: {
@@ -513,52 +552,46 @@ public:
case Parsed::Function::Even: {
return element_function(Parsed::Function::Even, 1, match_function);
}
}
return element(Parsed::Type::Expression, {{"command", input}});
}
json parse_condition(const std::string& input) {
Match match_condition = match(input, get_values(regex_map_condition_operators));
switch ( static_cast<Parsed::ConditionOperators>(match_condition.regex_number()) ) {
case Parsed::ConditionOperators::Not: {
return element_condition(Parsed::ConditionOperators::Not, 1, match_condition);
case Parsed::Function::Not: {
return element_function(Parsed::Function::Not, 1, match_function);
}
case Parsed::ConditionOperators::And: {
return element_condition(Parsed::ConditionOperators::And, 2, match_condition);
case Parsed::Function::And: {
return element_function(Parsed::Function::And, 2, match_function);
}
case Parsed::ConditionOperators::Or: {
return element_condition(Parsed::ConditionOperators::Or, 2, match_condition);
case Parsed::Function::Or: {
return element_function(Parsed::Function::Or, 2, match_function);
}
case Parsed::ConditionOperators::In: {
return element_condition(Parsed::ConditionOperators::In, 2, match_condition);
case Parsed::Function::In: {
return element_function(Parsed::Function::In, 2, match_function);
}
case Parsed::ConditionOperators::Equal: {
return element_condition(Parsed::ConditionOperators::Equal, 2, match_condition);
case Parsed::Function::Equal: {
return element_function(Parsed::Function::Equal, 2, match_function);
}
case Parsed::ConditionOperators::Greater: {
return element_condition(Parsed::ConditionOperators::Greater, 2, match_condition);
case Parsed::Function::Greater: {
return element_function(Parsed::Function::Greater, 2, match_function);
}
case Parsed::ConditionOperators::Less: {
return element_condition(Parsed::ConditionOperators::Less, 2, match_condition);
case Parsed::Function::Less: {
return element_function(Parsed::Function::Less, 2, match_function);
}
case Parsed::ConditionOperators::GreaterEqual: {
return element_condition(Parsed::ConditionOperators::GreaterEqual, 2, match_condition);
case Parsed::Function::GreaterEqual: {
return element_function(Parsed::Function::GreaterEqual, 2, match_function);
}
case Parsed::ConditionOperators::LessEqual: {
return element_condition(Parsed::ConditionOperators::LessEqual, 2, match_condition);
case Parsed::Function::LessEqual: {
return element_function(Parsed::Function::LessEqual, 2, match_function);
}
case Parsed::ConditionOperators::Different: {
return element_condition(Parsed::ConditionOperators::Different, 2, match_condition);
case Parsed::Function::Different: {
return element_function(Parsed::Function::Different, 2, match_function);
}
case Parsed::Function::ReadJson: {
Parsed::ElementExpression result = Parsed::ElementExpression(Parsed::Function::ReadJson);
result.command = input;
return result;
}
}
return element(Parsed::Type::Expression, {{"command", input}});
}
json parse_level(const std::string& input, const std::string& path) {
json result;
std::vector<std::shared_ptr<Parsed::Element>> parse_level(const std::string& input, const std::string& path) {
std::vector<std::shared_ptr<Parsed::Element>> result;
std::vector<Regex> regex_delimiters = get_values(regex_map_delimiters);
@@ -568,7 +601,7 @@ public:
current_position = match_delimiter.end_position();
std::string string_prefix = match_delimiter.prefix();
if (not string_prefix.empty()) {
result += element(Parsed::Type::String, {{"text", string_prefix}});
result.emplace_back( std::make_shared<Parsed::ElementString>(string_prefix) );
}
std::string delimiter_inner = match_delimiter.str(1);
@@ -590,27 +623,24 @@ public:
const std::string item_name = match_command.str(1);
const std::string list_name = match_command.str(2);
result += element(Parsed::Type::Loop, {{"item", item_name}, {"list", parse_expression(list_name)}, {"inner", loop_match.inner()}});
result.emplace_back( std::make_shared<Parsed::ElementLoop>(item_name, parse_expression(list_name), loop_match.inner()));
} else {
throw std::runtime_error("Parser error: Unknown loop command.");
}
break;
}
case Parsed::Statement::Condition: {
json condition_result = element(Parsed::Type::Condition, {{"children", json::array()}});
auto condition_container = std::make_shared<Parsed::ElementConditionContainer>();
const Regex regex_condition{"(if|else if|else) ?(.*)"};
Match condition_match = match_delimiter;
MatchClosed else_if_match = search_closed_on_level(input, match_delimiter.regex(), regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
while (else_if_match.found()) {
condition_match = else_if_match.close_match;
Match match_command;
if (std::regex_match(else_if_match.open_match.str(1), match_command, regex_condition)) {
condition_result["children"] += element(Parsed::Type::ConditionBranch, {{"inner", else_if_match.inner()}, {"condition_type", match_command.str(1)}, {"condition", parse_condition(match_command.str(2))}});
condition_container->children.push_back( std::make_shared<Parsed::ElementConditionBranch>(else_if_match.inner(), match_command.str(1), parse_expression(match_command.str(2))) );
}
else_if_match = search_closed_on_level(input, match_delimiter.regex(), regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
@@ -622,7 +652,7 @@ public:
Match match_command;
if (std::regex_match(else_match.open_match.str(1), match_command, regex_condition)) {
condition_result["children"] += element(Parsed::Type::ConditionBranch, {{"inner", else_match.inner()}, {"condition_type", match_command.str(1)}, {"condition", parse_condition(match_command.str(2))}});
condition_container->children.push_back( std::make_shared<Parsed::ElementConditionBranch>(else_match.inner(), match_command.str(1), parse_expression(match_command.str(2))) );
}
}
@@ -630,18 +660,18 @@ public:
Match match_command;
if (std::regex_match(last_if_match.open_match.str(1), match_command, regex_condition)) {
condition_result["children"] += element(Parsed::Type::ConditionBranch, {{"inner", last_if_match.inner()}, {"condition_type", match_command.str(1)}, {"condition", parse_condition(match_command.str(2))}});
condition_container->children.push_back( std::make_shared<Parsed::ElementConditionBranch>(last_if_match.inner(), match_command.str(1), parse_expression(match_command.str(2))) );
}
current_position = last_if_match.end_position();
result += condition_result;
result.emplace_back(condition_container);
break;
}
case Parsed::Statement::Include: {
std::string included_filename = path + match_statement.str(1);
Template included_template = parse_template(included_filename);
for (json element : included_template.parsed_template) {
result += element;
for (auto element : included_template.parsed_template.children) {
result.emplace_back(element);
}
break;
}
@@ -651,11 +681,11 @@ public:
break;
}
case Parsed::Delimiter::Expression: {
result += parse_expression(delimiter_inner);
result.emplace_back( std::make_shared<Parsed::ElementExpression>(parse_expression(delimiter_inner)) );
break;
}
case Parsed::Delimiter::Comment: {
result += element(Parsed::Type::Comment, {{"text", delimiter_inner}});
result.emplace_back( std::make_shared<Parsed::ElementComment>(delimiter_inner) );
break;
}
default: { throw std::runtime_error("Parser error: Unknown delimiter."); }
@@ -664,19 +694,20 @@ public:
match_delimiter = search(input, regex_delimiters, current_position);
}
if (current_position < input.length()) {
result += element(Parsed::Type::String, {{"text", input.substr(current_position)}});
result.emplace_back( std::make_shared<Parsed::ElementString>(input.substr(current_position)) );
}
return result;
}
json parse_tree(json current_element, const std::string& path) {
if (current_element.find("inner") != current_element.end()) {
current_element["children"] = parse_level(current_element["inner"], path);
current_element.erase("inner");
std::shared_ptr<Parsed::Element> parse_tree(std::shared_ptr<Parsed::Element> current_element, const std::string& path) {
if (not current_element->inner.empty()) {
current_element->children = parse_level(current_element->inner, path);
current_element->inner.clear();
}
if (current_element.find("children") != current_element.end()) {
for (auto& child: current_element["children"]) {
if (not current_element->children.empty()) {
for (auto& child: current_element->children) {
child = parse_tree(child, path);
}
}
@@ -684,15 +715,15 @@ public:
}
Template parse(const std::string& input) {
json parsed = parse_tree({{"inner", input}}, "./")["children"];
return Template(parsed);
auto parsed = parse_tree(std::make_shared<Parsed::Element>(Parsed::Element(Parsed::Type::String, input)), "./");
return Template(*parsed);
}
Template parse_template(const std::string& filename) {
std::string text = load_file(filename);
std::string input = load_file(filename);
std::string path = filename.substr(0, filename.find_last_of("/\\") + 1);
json parsed = parse_tree({{"inner", text}}, path)["children"];
return Template(parsed);
auto parsed = parse_tree(std::make_shared<Parsed::Element>(Parsed::Element(Parsed::Type::String, input)), path);
return Template(*parsed);
}
std::string load_file(const std::string& filename) {
@@ -704,12 +735,15 @@ public:
/*!
@brief Environment class
*/
class Environment {
const std::string global_path;
ElementNotation elementNotation = ElementNotation::Pointer;
Parser parser;
Parser parser = Parser();
public:
Environment(): Environment("./") { }
@@ -735,16 +769,25 @@ public:
elementNotation = elementNotation_;
}
std::string render(const std::string& input, json data) {
Template parse(const std::string& input) {
Template parsed = parser.parse(input);
parsed.elementNotation = elementNotation;
return parsed.render(data);
return parsed;
}
Template parse_template(const std::string& filename) {
Template parsed = parser.parse_template(global_path + filename);
parsed.elementNotation = elementNotation;
return parsed;
}
std::string render(const std::string& input, json data) {
return parse(input).render(data);
}
std::string render_template(const std::string& filename, json data) {
Template parsed = parser.parse_template(global_path + filename);
parsed.elementNotation = elementNotation;
return parsed.render(data);
return parse_template(filename).render(data);
}
std::string render_template_with_json_file(const std::string& filename, const std::string& filename_data) {
@@ -754,7 +797,7 @@ public:
void write(const std::string& filename, json data, const std::string& filename_out) {
std::ofstream file(global_path + filename_out);
file << render_template_with_json_file(filename, data);
file << render_template(filename, data);
file.close();
}

View File

@@ -6,7 +6,7 @@
using json = nlohmann::json;
TEST_CASE("Files handling") {
TEST_CASE("loading") {
inja::Environment env = inja::Environment();
json data;
data["name"] = "Jeff";
@@ -24,7 +24,7 @@ TEST_CASE("Files handling") {
}
}
TEST_CASE("Complete files") {
TEST_CASE("complete-files") {
inja::Environment env = inja::Environment("data/");
for (std::string test_name : {"simple-file", "nested", "nested-line"}) {

View File

@@ -1,10 +0,0 @@
#include "catch.hpp"
#include "nlohmann/json.hpp"
#include "inja.hpp"
TEST_CASE("Dot to pointer notation") {
CHECK( inja::dot_to_json_pointer_notation("person.names.surname") == "/person/names/surname" );
CHECK( inja::dot_to_json_pointer_notation("guests.2") == "/guests/2" );
}

View File

@@ -1,131 +0,0 @@
#include "catch.hpp"
#include "nlohmann/json.hpp"
#include "inja.hpp"
using json = nlohmann::json;
using Type = inja::Parsed::Type;
/* TEST_CASE("Parse structure") {
inja::Parser parser = inja::Parser();
SECTION("Basic string") {
std::string test = "lorem ipsum";
json result = {{{"type", Type::String}, {"text", "lorem ipsum"}}};
CHECK( parser.parse(test) == result );
}
SECTION("Empty string") {
std::string test = "";
json result = {};
CHECK( parser.parse(test) == result );
}
SECTION("Variable") {
std::string test = "{{ name }}";
json result = {{{"type", Type::Expression}, {"command", "name"}}};
CHECK( parser.parse(test) == result );
}
SECTION("Combined string and variables") {
std::string test = "Hello {{ name }}!";
json result = {
{{"type", Type::String}, {"text", "Hello "}},
{{"type", Type::Expression}, {"command", "name"}},
{{"type", Type::String}, {"text", "!"}}
};
CHECK( parser.parse(test) == result );
}
SECTION("Multiple variables") {
std::string test = "Hello {{ name }}! I come from {{ city }}.";
json result = {
{{"type", Type::String}, {"text", "Hello "}},
{{"type", Type::Expression}, {"command", "name"}},
{{"type", Type::String}, {"text", "! I come from "}},
{{"type", Type::Expression}, {"command", "city"}},
{{"type", Type::String}, {"text", "."}}
};
CHECK( parser.parse(test) == result );
}
SECTION("Loops") {
std::string test = "open {% for e in list %}lorem{% endfor %} closing";
json result = {
{{"type", Type::String}, {"text", "open "}},
{{"type", Type::Loop}, {"item", "e"}, {"list", "list"}, {"children", {
{{"type", Type::String}, {"text", "lorem"}}
}}},
{{"type", Type::String}, {"text", " closing"}}
};
CHECK( parser.parse(test) == result );
}
SECTION("Nested loops") {
std::string test = "{% for e in list %}{% for b in list2 %}lorem{% endfor %}{% endfor %}";
json result = {
{{"type", Type::Loop}, {"item", "e"}, {"list", "list"}, {"children", {
{{"type", Type::Loop}, {"item", "b"}, {"list", "list2"}, {"children", {
{{"type", Type::String}, {"text", "lorem"}}
}}}
}}}
};
CHECK( parser.parse(test) == result );
}
SECTION("Basic conditional") {
std::string test = "{% if true %}Hello{% endif %}";
json result = {
{{"type", Type::Condition}, {"children", {
{{"type", Type::ConditionBranch}, {"command", "if true"}, {"children", {
{{"type", Type::String}, {"text", "Hello"}}
}}}
}}}
};
CHECK( parser.parse(test) == result );
}
SECTION("If/else if/else conditional") {
std::string test = "if: {% if maybe %}first if{% else if perhaps %}first else if{% else if sometimes %}second else if{% else %}test else{% endif %}";
json result = {
{{"type", Type::String}, {"text", "if: "}},
{{"type", Type::Condition}, {"children", {
{{"type", Type::ConditionBranch}, {"command", "if maybe"}, {"children", {
{{"type", Type::String}, {"text", "first if"}}
}}},
{{"type", Type::ConditionBranch}, {"command", "else if perhaps"}, {"children", {
{{"type", Type::String}, {"text", "first else if"}}
}}},
{{"type", Type::ConditionBranch}, {"command", "else if sometimes"}, {"children", {
{{"type", Type::String}, {"text", "second else if"}}
}}},
{{"type", Type::ConditionBranch}, {"command", "else"}, {"children", {
{{"type", Type::String}, {"text", "test else"}}
}}},
}}}
};
CHECK( parser.parse(test) == result );
}
SECTION("Comments") {
std::string test = "{# lorem ipsum #}";
json result = {{{"type", Type::Comment}, {"text", "lorem ipsum"}}};
CHECK( parser.parse(test) == result );
}
SECTION("Line Statements") {
std::string test = R"(## if true
lorem ipsum
## endif)";
json result = {
{{"type", Type::Condition}, {"children", {
{{"type", Type::ConditionBranch}, {"command", "if true"}, {"children", {
{{"type", Type::String}, {"text", "lorem ipsum"}}
}}}
}}}
};
CHECK( parser.parse(test) == result );
}
} */

View File

@@ -3,10 +3,11 @@
#include "inja.hpp"
using json = nlohmann::json;
TEST_CASE("Renderer") {
TEST_CASE("types") {
inja::Environment env = inja::Environment();
json data;
data["name"] = "Peter";
@@ -18,12 +19,12 @@ TEST_CASE("Renderer") {
data["brother"]["daughter0"] = { { "name", "Maria" } };
data["is_happy"] = true;
SECTION("Basic") {
CHECK( env.render("Hello World!", data) == "Hello World!" );
SECTION("basic") {
CHECK( env.render("", data) == "" );
CHECK( env.render("Hello World!", data) == "Hello World!" );
}
SECTION("Variables") {
SECTION("variables") {
CHECK( env.render("Hello {{ name }}!", data) == "Hello Peter!" );
CHECK( env.render("{{ name }}", data) == "Peter" );
CHECK( env.render("{{name}}", data) == "Peter" );
@@ -32,19 +33,22 @@ TEST_CASE("Renderer") {
CHECK( env.render("Hello {{ names/1 }}!", data) == "Hello Seb!" );
CHECK( env.render("Hello {{ brother/name }}!", data) == "Hello Chris!" );
CHECK( env.render("Hello {{ brother/daughter0/name }}!", data) == "Hello Maria!" );
CHECK_THROWS_WITH( env.render("{{unknown}}", data), "Did not found json element: unknown" );
}
SECTION("Comments") {
SECTION("comments") {
CHECK( env.render("Hello{# This is a comment #}!", data) == "Hello!" );
CHECK( env.render("{# --- #Todo --- #}", data) == "" );
}
SECTION("Loops") {
SECTION("loops") {
CHECK( env.render("{% for name in names %}a{% endfor %}", data) == "aa" );
CHECK( env.render("Hello {% for name in names %}{{ name }} {% endfor %}!", data) == "Hello Jeff Seb !" );
CHECK( env.render("Hello {% for name in names %}{{ index }}: {{ name }}, {% endfor %}!", data) == "Hello 0: Jeff, 1: Seb, !" );
}
SECTION("Conditionals") {
SECTION("conditionals") {
CHECK( env.render("{% if is_happy %}Yeah!{% endif %}", data) == "Yeah!" );
CHECK( env.render("{% if is_sad %}Yeah!{% endif %}", data) == "" );
CHECK( env.render("{% if is_sad %}Yeah!{% else %}Nooo...{% endif %}", data) == "Nooo..." );
@@ -54,10 +58,12 @@ TEST_CASE("Renderer") {
CHECK( env.render("{% if age != 28 %}Right{% else %}Wrong{% endif %}", data) == "Right" );
CHECK( env.render("{% if age >= 30 %}Right{% else %}Wrong{% endif %}", data) == "Wrong" );
CHECK( env.render("{% if age in [28, 29, 30] %}True{% endif %}", data) == "True" );
CHECK( env.render("{% if age == 28 %}28{% else if age == 29 %}29{% endif %}", data) == "29" );
CHECK( env.render("{% if age == 26 %}26{% else if age == 27 %}27{% else if age == 28 %}28{% else %}29{% endif %}", data) == "29" );
}
}
TEST_CASE("Render functions") {
TEST_CASE("functions") {
inja::Environment env = inja::Environment();
json data;
@@ -66,60 +72,115 @@ TEST_CASE("Render functions") {
data["names"] = {"Jeff", "Seb", "Peter", "Tom"};
data["temperature"] = 25.6789;
SECTION("Upper") {
SECTION("upper") {
CHECK( env.render("{{ upper(name) }}", data) == "PETER" );
CHECK( env.render("{{ upper( name ) }}", data) == "PETER" );
CHECK( env.render("{{ upper(city) }}", data) == "NEW YORK" );
CHECK( env.render("{{ upper(upper(name)) }}", data) == "PETER" );
CHECK_THROWS_WITH( env.render("{{ upper(5) }}", data), "[json.exception.type_error.302] type must be string, but is number" );
CHECK_THROWS_WITH( env.render("{{ upper(true) }}", data), "[json.exception.type_error.302] type must be string, but is boolean" );
}
SECTION("Lower") {
SECTION("lower") {
CHECK( env.render("{{ lower(name) }}", data) == "peter" );
CHECK( env.render("{{ lower(city) }}", data) == "new york" );
CHECK_THROWS_WITH( env.render("{{ lower(5.45) }}", data), "[json.exception.type_error.302] type must be string, but is number" );
}
SECTION("Range") {
// CHECK( env.render("range(4)", data) == std::vector<int>({0, 1, 2, 3}) );
SECTION("range") {
CHECK( env.render("{{ range(2) }}", data) == "[0,1]" );
CHECK( env.render("{{ range(4) }}", data) == "[0,1,2,3]" );
CHECK_THROWS_WITH( env.render("{{ range(name) }}", data), "[json.exception.type_error.302] type must be number, but is string" );
}
SECTION("Length") {
SECTION("length") {
CHECK( env.render("{{ length(names) }}", data) == "4" );
CHECK_THROWS_WITH( env.render("{{ length(5) }}", data), "[json.exception.type_error.302] type must be array, but is number" );
}
SECTION("Round") {
SECTION("round") {
CHECK( env.render("{{ round(4, 0) }}", data) == "4.0" );
CHECK( env.render("{{ round(temperature, 2) }}", data) == "25.68" );
CHECK_THROWS_WITH( env.render("{{ round(name, 2) }}", data), "[json.exception.type_error.302] type must be number, but is string" );
}
SECTION("DivisibleBy") {
SECTION("divisibleBy") {
CHECK( env.render("{{ divisibleBy(50, 5) }}", data) == "true" );
CHECK( env.render("{{ divisibleBy(12, 3) }}", data) == "true" );
CHECK( env.render("{{ divisibleBy(11, 3) }}", data) == "false" );
CHECK_THROWS_WITH( env.render("{{ divisibleBy(name, 2) }}", data), "[json.exception.type_error.302] type must be number, but is string" );
}
SECTION("Odd") {
SECTION("odd") {
CHECK( env.render("{{ odd(11) }}", data) == "true" );
CHECK( env.render("{{ odd(12) }}", data) == "false" );
CHECK_THROWS_WITH( env.render("{{ odd(name) }}", data), "[json.exception.type_error.302] type must be number, but is string" );
}
SECTION("Even") {
SECTION("even") {
CHECK( env.render("{{ even(11) }}", data) == "false" );
CHECK( env.render("{{ even(12) }}", data) == "true" );
CHECK_THROWS_WITH( env.render("{{ even(name) }}", data), "[json.exception.type_error.302] type must be number, but is string" );
}
}
TEST_CASE("Renderer other syntax") {
TEST_CASE("combinations") {
inja::Environment env = inja::Environment();
json data;
data["name"] = "Peter";
data["city"] = "Brunswick";
data["age"] = 29;
data["names"] = {"Jeff", "Seb"};
data["brother"]["name"] = "Chris";
data["brother"]["daughters"] = {"Maria", "Helen"};
data["brother"]["daughter0"] = { { "name", "Maria" } };
data["is_happy"] = true;
SECTION("Other expression syntax") {
CHECK( env.render("{% if upper(\"Peter\") == \"PETER\" %}TRUE{% endif %}", data) == "TRUE" );
CHECK( env.render("{% if lower(upper(name)) == \"peter\" %}TRUE{% endif %}", data) == "TRUE" );
CHECK( env.render("{% for i in range(4) %}{{ index1 }}{% endfor %}", data) == "1234" );
}
TEST_CASE("templates") {
inja::Environment env = inja::Environment();
inja::Template temp = env.parse("{% if is_happy %}{{ name }}{% else %}{{ city }}{% endif %}");
json data;
data["name"] = "Peter";
data["city"] = "Brunswick";
data["is_happy"] = true;
CHECK( temp.render(data) == "Peter" );
data["is_happy"] = false;
CHECK( temp.render(data) == "Brunswick" );
}
TEST_CASE("other-syntax") {
json data;
data["name"] = "Peter";
data["city"] = "Brunswick";
data["age"] = 29;
data["names"] = {"Jeff", "Seb"};
data["brother"]["name"] = "Chris";
data["brother"]["daughters"] = {"Maria", "Helen"};
data["brother"]["daughter0"] = { { "name", "Maria" } };
data["is_happy"] = true;
SECTION("variables") {
inja::Environment env = inja::Environment();
env.setElementNotation(inja::ElementNotation::Dot);
CHECK( env.render("{{ name }}", data) == "Peter" );
CHECK( env.render("Hello {{ names.1 }}!", data) == "Hello Seb!" );
CHECK( env.render("Hello {{ brother.name }}!", data) == "Hello Chris!" );
CHECK( env.render("Hello {{ brother.daughter0.name }}!", data) == "Hello Maria!" );
CHECK_THROWS_WITH( env.render("{{unknown}}", data), "Did not found json element: unknown" );
}
SECTION("other expression syntax") {
inja::Environment env = inja::Environment();
CHECK( env.render("Hello {{ name }}!", data) == "Hello Peter!" );
@@ -130,7 +191,7 @@ TEST_CASE("Renderer other syntax") {
CHECK( env.render("Hello (& name &)!", data) == "Hello Peter!" );
}
SECTION("Other comment syntax") {
SECTION("other comment syntax") {
inja::Environment env = inja::Environment();
env.setComment("\\(&", "&\\)");

View File

@@ -4,11 +4,16 @@
TEST_CASE("Basic search in string") {
TEST_CASE("dot to pointer") {
CHECK( inja::dot_to_json_pointer_notation("person.names.surname") == "/person/names/surname" );
CHECK( inja::dot_to_json_pointer_notation("guests.2") == "/guests/2" );
}
TEST_CASE("basic-search") {
std::string input = "lorem ipsum dolor it";
inja::Regex regex("i(.*)m");
SECTION("Basic search from start") {
SECTION("from start") {
inja::Match match = inja::search(input, regex, 0);
CHECK( match.found() == true );
CHECK( match.position() == 6 );
@@ -18,17 +23,17 @@ TEST_CASE("Basic search in string") {
CHECK( match.str(1) == "psu" );
}
SECTION("Basic search from position") {
SECTION("from position") {
inja::Match match = inja::search(input, regex, 8);
CHECK( match.found() == false );
CHECK( match.length() == 0 );
}
}
TEST_CASE("Search in string with multiple possible regexes") {
TEST_CASE("search-with-multiple-possible-regexes") {
std::string input = "lorem ipsum dolor amit estas tronum.";
SECTION("Basic 1") {
SECTION("basic 1") {
std::vector<inja::Regex> regex_patterns = { inja::Regex("tras"), inja::Regex("do(\\w*)or"), inja::Regex("es(\\w*)as"), inja::Regex("ip(\\w*)um") };
inja::Match match = inja::search(input, regex_patterns, 0);
CHECK( match.regex_number() == 3 );
@@ -36,7 +41,7 @@ TEST_CASE("Search in string with multiple possible regexes") {
CHECK( match.str(1) == "s" );
}
SECTION("Basic 2") {
SECTION("basic 2") {
std::vector<inja::Regex> regex_patterns = { inja::Regex("tras"), inja::Regex("ip(\\w*)um"), inja::Regex("do(\\w*)or"), inja::Regex("es(\\w*)as") };
inja::Match match = inja::search(input, regex_patterns, 0);
CHECK( match.regex_number() == 1 );
@@ -45,7 +50,7 @@ TEST_CASE("Search in string with multiple possible regexes") {
}
}
TEST_CASE("Search on level") {
TEST_CASE("search-on-level") {
std::string input = "(% up %)(% up %)Test(% N1 %)(% down %)...(% up %)(% N2 %)(% up %)(% N3 %)(% down %)(% N4 %)(% down %)(% N5 %)(% down %)";
inja::Regex regex_statement("\\(\\% (.*?) \\%\\)");
@@ -53,7 +58,7 @@ TEST_CASE("Search on level") {
inja::Regex regex_level_down("down");
inja::Regex regex_search("N(\\d+)");
SECTION("First instance") {
SECTION("first instance") {
inja::Match open_match = inja::search(input, regex_statement, 0);
CHECK( open_match.position() == 0 );
CHECK( open_match.end_position() == 8 );
@@ -64,7 +69,7 @@ TEST_CASE("Search on level") {
CHECK( match.end_position() == 109 );
}
SECTION("Second instance") {
SECTION("second instance") {
inja::Match open_match = inja::search(input, regex_statement, 4);
CHECK( open_match.position() == 8 );