diff --git a/README.md b/README.md index d73a679..4067195 100644 --- a/README.md +++ b/README.md @@ -83,14 +83,14 @@ Environment env = Environment("../path/templates/"); Environment env = Environment("../path/templates/", "../path/results/"); // Choose between JSON pointer or dot notation to access elements -env.setElementNotation(inja::ElementNotation::Pointer); // (default) e.g. time/start -env.setElementNotation(inja::ElementNotation::Dot); // e.g. time.start +env.set_element_notation(inja::ElementNotation::Pointer); // (default) e.g. time/start +env.set_element_notation(inja::ElementNotation::Dot); // e.g. time.start // With other opening and closing strings (here the defaults, as regex) -env.setVariables("\\{\\{", "\\}\\}"); // Variables {{ }} -env.setComments("\\{#", "#\\}"); // Comments {# #} -env.setStatements("\\{\\%", "\\%\\}"); // Statements {% %} for many things, see below -env.setLineStatements("##"); // Line statement ## (just an opener) +env.set_variables("\\{\\{", "\\}\\}"); // Variables {{ }} +env.set_comments("\\{#", "#\\}"); // Comments {# #} +env.set_statements("\\{\\%", "\\%\\}"); // Statements {% %} for many things, see below +env.set_line_statements("##"); // Line statement ## (just an opener) ``` ### Variables diff --git a/src/inja.hpp b/src/inja.hpp index 0ed29d0..70e95be 100644 --- a/src/inja.hpp +++ b/src/inja.hpp @@ -60,8 +60,8 @@ public: explicit Match(size_t offset): std::match_results(), offset_(offset) { } explicit Match(size_t offset, const Regex& regex): std::match_results(), offset_(offset), regex_(regex) { } - void setGroupOffset(unsigned int group_offset) { group_offset_ = group_offset; } - void setRegex(Regex regex) { regex_ = regex; } + void set_group_offset(unsigned int group_offset) { group_offset_ = group_offset; } + void set_regex(Regex regex) { regex_ = regex; } size_t position() const { return offset_ + std::match_results::position(); } size_t end_position() const { return position() + length(); } @@ -82,7 +82,7 @@ public: explicit MatchType(const Match& obj) : Match(obj) { } MatchType(Match&& obj) : Match(std::move(obj)) { } - void setType(T type) { type_ = type; } + void set_type(T type) { type_ = type; } T type() const { return type_; } }; @@ -149,9 +149,9 @@ inline MatchType search(const std::string& input, std::map& regexes for (unsigned int i = 1; i < search_match.size(); i++) { if (search_match.length(i) > 0) { - search_match.setGroupOffset(i); - search_match.setType(class_vector[regex_mark_counts[i]]); - search_match.setRegex(regexes_vector[regex_mark_counts[i]]); + search_match.set_group_offset(i); + search_match.set_type(class_vector[regex_mark_counts[i]]); + search_match.set_regex(regexes_vector[regex_mark_counts[i]]); return search_match; } } @@ -188,8 +188,8 @@ inline MatchType match(const std::string& input, std::map regexes) MatchType match; for (const auto e : regexes) { if (std::regex_match(input.cbegin(), input.cend(), match, e.second)) { - match.setType(e.first); - match.setRegex(e.second); + match.set_type(e.first); + match.set_regex(e.second); return match; } } @@ -317,10 +317,10 @@ struct Parsed { class Template { public: const Parsed::Element parsed_template; - ElementNotation elementNotation; + ElementNotation element_notation; 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) { } + explicit Template(const Parsed::Element& parsed_template, ElementNotation element_notation): parsed_template(parsed_template), element_notation(element_notation) { } template bool eval_expression(const Parsed::ElementExpression& element, json data) { @@ -420,7 +420,7 @@ public: if ( json::accept(element.command) ) { return json::parse(element.command); } // Json Raw Data std::string input = element.command; - switch (elementNotation) { + switch (element_notation) { case ElementNotation::Pointer: { if (input[0] != '/') { input.insert(0, "/"); } break; @@ -447,13 +447,13 @@ public: switch (element->type) { case Parsed::Type::Main: { throw std::runtime_error("Main type in renderer."); } case Parsed::Type::String: { - auto elementString = std::static_pointer_cast(element); - result += elementString->text; + auto element_string = std::static_pointer_cast(element); + result += element_string->text; break; } case Parsed::Type::Expression: { - auto elementExpression = std::static_pointer_cast(element); - json variable = eval_expression(*elementExpression, data); + auto element_expression = std::static_pointer_cast(element); + json variable = eval_expression(*element_expression, data); if (variable.is_string()) { result += variable.get(); @@ -465,10 +465,10 @@ public: break; } case Parsed::Type::Loop: { - auto elementLoop = std::static_pointer_cast(element); - const std::string item_name = elementLoop->item; + auto element_loop = std::static_pointer_cast(element); + const std::string item_name = element_loop->item; - const std::vector list = eval_expression>(elementLoop->list, data); + const std::vector list = eval_expression>(element_loop->list, data); for (unsigned int i = 0; i < list.size(); i++) { json data_loop = data; data_loop[item_name] = list[i]; @@ -476,16 +476,16 @@ public: data_loop["index1"] = i + 1; data_loop["is_first"] = (i == 0); data_loop["is_last"] = (i == list.size() - 1); - result += Template(*elementLoop, elementNotation).render(data_loop); + result += Template(*element_loop, element_notation).render(data_loop); } break; } case Parsed::Type::Condition: { - auto elementCondition = std::static_pointer_cast(element); - for (auto branch: elementCondition->children) { - auto elementBranch = std::static_pointer_cast(branch); - if (elementBranch->condition_type == Parsed::Condition::Else || eval_expression(elementBranch->condition, data)) { - result += Template(*elementBranch, elementNotation).render(data); + auto element_condition = std::static_pointer_cast(element); + for (auto branch: element_condition->children) { + auto element_branch = std::static_pointer_cast(branch); + if (element_branch->condition_type == Parsed::Condition::Else || eval_expression(element_branch->condition, data)) { + result += Template(*element_branch, element_notation).render(data); break; } } @@ -733,7 +733,7 @@ class Environment { const std::string input_path; const std::string output_path; - ElementNotation elementNotation = ElementNotation::Pointer; + ElementNotation element_notation = ElementNotation::Pointer; Parser parser = Parser(); @@ -742,36 +742,36 @@ public: explicit Environment(const std::string& global_path): input_path(global_path), output_path(global_path), parser() { } explicit Environment(const std::string& input_path, const std::string& output_path): input_path(input_path), output_path(output_path), parser() { } - void setStatement(const std::string& open, const std::string& close) { + void set_statement(const std::string& open, const std::string& close) { parser.regex_map_delimiters[Parsed::Delimiter::Statement] = Regex{open + "\\s*(.+?)\\s*" + close}; } - void setLineStatement(const std::string& open) { + void set_line_statement(const std::string& open) { parser.regex_map_delimiters[Parsed::Delimiter::LineStatement] = Regex{"(?:^|\\n)" + open + "\\s*(.+)\\s*"}; } - void setExpression(const std::string& open, const std::string& close) { + void set_expression(const std::string& open, const std::string& close) { parser.regex_map_delimiters[Parsed::Delimiter::Expression] = Regex{open + "\\s*(.+?)\\s*" + close}; } - void setComment(const std::string& open, const std::string& close) { + void set_comment(const std::string& open, const std::string& close) { parser.regex_map_delimiters[Parsed::Delimiter::Comment] = Regex{open + "\\s*(.+?)\\s*" + close}; } - void setElementNotation(const ElementNotation elementNotation_) { - elementNotation = elementNotation_; + void set_element_notation(const ElementNotation element_notation_) { + element_notation = element_notation_; } Template parse(const std::string& input) { Template parsed = parser.parse(input); - parsed.elementNotation = elementNotation; + parsed.element_notation = element_notation; return parsed; } Template parse_template(const std::string& filename) { Template parsed = parser.parse_template(input_path + filename); - parsed.elementNotation = elementNotation; + parsed.element_notation = element_notation; return parsed; } diff --git a/test/src/unit-renderer.cpp b/test/src/unit-renderer.cpp index 623ec8e..9b59f3a 100644 --- a/test/src/unit-renderer.cpp +++ b/test/src/unit-renderer.cpp @@ -181,7 +181,7 @@ TEST_CASE("other-syntax") { SECTION("variables") { inja::Environment env = inja::Environment(); - env.setElementNotation(inja::ElementNotation::Dot); + env.set_element_notation(inja::ElementNotation::Dot); CHECK( env.render("{{ name }}", data) == "Peter" ); CHECK( env.render("Hello {{ names.1 }}!", data) == "Hello Seb!" ); @@ -196,7 +196,7 @@ TEST_CASE("other-syntax") { CHECK( env.render("Hello {{ name }}!", data) == "Hello Peter!" ); - env.setExpression("\\(&", "&\\)"); + env.set_expression("\\(&", "&\\)"); CHECK( env.render("Hello {{ name }}!", data) == "Hello {{ name }}!" ); CHECK( env.render("Hello (& name &)!", data) == "Hello Peter!" ); @@ -204,7 +204,7 @@ TEST_CASE("other-syntax") { SECTION("other comment syntax") { inja::Environment env = inja::Environment(); - env.setComment("\\(&", "&\\)"); + env.set_comment("\\(&", "&\\)"); CHECK( env.render("Hello {# Test #}", data) == "Hello {# Test #}" ); CHECK( env.render("Hello (& Test &)", data) == "Hello " );