partial support
This commit is contained in:
parent
1f3d0da79b
commit
8ef0950a07
@ -1,5 +1,6 @@
|
||||
#include "render_context.hpp"
|
||||
#include "state/outside_section.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <regex>
|
||||
|
||||
@ -14,6 +15,8 @@ render_context::render_context(
|
||||
objects{object},
|
||||
state(new state::outside_section)
|
||||
{
|
||||
for(auto& partial: this->partials)
|
||||
partial.second = strip_whitespace(partial.second);
|
||||
}
|
||||
|
||||
render_context::render_context(
|
||||
@ -55,3 +58,7 @@ std::string render_context::render(const std::string& t) {
|
||||
output << state->render(*this, token(it->str()));
|
||||
return output.str();
|
||||
}
|
||||
|
||||
std::string render_context::render_partial(const std::string& partial_name) {
|
||||
return (partials.count(partial_name))?render(partials.at(partial_name)):"";
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace mstch {
|
||||
const mstch::node& find_node(
|
||||
const std::string& token,
|
||||
const std::deque<object>& current_objects);
|
||||
const std::map<std::string,std::string>& partials;
|
||||
std::map<std::string,std::string> partials;
|
||||
std::deque<mstch::object> objects;
|
||||
std::unique_ptr<state::render_state> state;
|
||||
public:
|
||||
@ -26,6 +26,7 @@ namespace mstch {
|
||||
const mstch::object& object, const render_context& context);
|
||||
const mstch::node& get_node(const std::string& token);
|
||||
std::string render(const std::string& tmplt);
|
||||
std::string render_partial(const std::string& partial_name);
|
||||
template<class T, class... Args>
|
||||
void set_state(Args&&... args) {
|
||||
state = std::unique_ptr<state::render_state>(
|
||||
|
@ -29,7 +29,7 @@ std::string state::outside_section::render(
|
||||
case token_type::text:
|
||||
return token.raw();
|
||||
case token_type::partial:
|
||||
break;//render_context(mstch::object{{".", i}}, ctx).render(section);
|
||||
return ctx.render_partial(token.content());
|
||||
case token_type::section_close:
|
||||
// TODO ERROR
|
||||
break;
|
||||
|
@ -9,7 +9,7 @@ std::string mstch::strip_whitespace(std::string tmplt) {
|
||||
std::ostringstream out;
|
||||
std::istringstream in(tmplt);
|
||||
std::string line;
|
||||
std::regex tag_match("\\{{2}[ ]*[#|/|^|!]{1}[^\\}]*\\}{2}");
|
||||
std::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}");
|
||||
std::regex whitespace_match("^\\s*$");
|
||||
while (std::getline(in, line)) {
|
||||
std::string no_tags = std::regex_replace(line, tag_match, "");
|
||||
|
@ -23,7 +23,9 @@ endforeach(data_file)
|
||||
file(GLOB string_files RELATIVE
|
||||
"${CMAKE_SOURCE_DIR}/test/data"
|
||||
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
|
||||
"${CMAKE_SOURCE_DIR}/test/data/*.txt")
|
||||
"${CMAKE_SOURCE_DIR}/test/data/*.txt"
|
||||
"${CMAKE_SOURCE_DIR}/test/data/*.partial"
|
||||
)
|
||||
|
||||
foreach(string_file ${string_files})
|
||||
list(APPEND genargs "-S${string_file}")
|
||||
|
@ -1,3 +0,0 @@
|
||||
({
|
||||
array: ['1', '2', '3', '4']
|
||||
})
|
@ -1,8 +0,0 @@
|
||||
({
|
||||
numbers: [
|
||||
{i: '1'},
|
||||
{i: '2'},
|
||||
{i: '3'},
|
||||
{i: '4'}
|
||||
]
|
||||
})
|
@ -1,3 +0,0 @@
|
||||
({
|
||||
numbers: ['1', '2', '3', '4']
|
||||
})
|
@ -1,3 +0,0 @@
|
||||
({
|
||||
foo: 1
|
||||
})
|
@ -1,6 +0,0 @@
|
||||
({
|
||||
title: function () {
|
||||
return "Welcome";
|
||||
},
|
||||
again: "Goodbye"
|
||||
})
|
@ -1,2 +0,0 @@
|
||||
<h1>{{title}}</h1>
|
||||
{{>partial}}
|
@ -1 +0,0 @@
|
||||
Again, {{again}}!
|
@ -1,2 +0,0 @@
|
||||
<h1>Welcome</h1>
|
||||
Again, Goodbye!
|
@ -1,14 +0,0 @@
|
||||
({
|
||||
greeting: function () {
|
||||
return "Welcome";
|
||||
},
|
||||
farewell: function () {
|
||||
return "Fair enough, right?";
|
||||
},
|
||||
name: "Chris",
|
||||
value: 10000,
|
||||
taxed_value: function () {
|
||||
return this.value - (this.value * 0.4);
|
||||
},
|
||||
in_ca: true
|
||||
})
|
@ -1,3 +0,0 @@
|
||||
<h1>{{greeting}}</h1>
|
||||
{{>partial}}
|
||||
<h3>{{farewell}}</h3>
|
@ -1,5 +0,0 @@
|
||||
Hello {{name}}
|
||||
You have just won ${{value}}!
|
||||
{{#in_ca}}
|
||||
Well, ${{ taxed_value }}, after taxes.
|
||||
{{/in_ca}}
|
@ -1,5 +0,0 @@
|
||||
<h1>Welcome</h1>
|
||||
Hello Chris
|
||||
You have just won $10000!
|
||||
Well, $6000, after taxes.
|
||||
<h3>Fair enough, right?</h3>
|
@ -1,14 +0,0 @@
|
||||
({
|
||||
greeting: function () {
|
||||
return "Welcome";
|
||||
},
|
||||
farewell: function () {
|
||||
return "Fair enough, right?";
|
||||
},
|
||||
name: "Chris",
|
||||
value: 10000,
|
||||
taxed_value: function () {
|
||||
return this.value - (this.value * 0.4);
|
||||
},
|
||||
in_ca: true
|
||||
})
|
@ -1,3 +0,0 @@
|
||||
<h1>{{ greeting }}</h1>
|
||||
{{> partial }}
|
||||
<h3>{{ farewell }}</h3>
|
@ -1,5 +0,0 @@
|
||||
Hello {{ name}}
|
||||
You have just won ${{value }}!
|
||||
{{# in_ca }}
|
||||
Well, ${{ taxed_value }}, after taxes.
|
||||
{{/ in_ca }}
|
@ -1,5 +0,0 @@
|
||||
<h1>Welcome</h1>
|
||||
Hello Chris
|
||||
You have just won $10000!
|
||||
Well, $6000, after taxes.
|
||||
<h3>Fair enough, right?</h3>
|
@ -1,7 +0,0 @@
|
||||
({
|
||||
bold: function(){
|
||||
return function(text, render) {
|
||||
return "<b>" + render(text) + "</b>";
|
||||
}
|
||||
}
|
||||
})
|
@ -1,3 +0,0 @@
|
||||
{{> partial}}
|
||||
|
||||
<p>some more text</p>
|
@ -1 +0,0 @@
|
||||
{{#bold}}Hello There{{/bold}}
|
@ -1,3 +0,0 @@
|
||||
<b>Hello There</b>
|
||||
|
||||
<p>some more text</p>
|
3
test/data/partial_array.data
Normal file
3
test/data/partial_array.data
Normal file
@ -0,0 +1,3 @@
|
||||
mstch::object{
|
||||
{"array", mstch::array{std::string{"1"}, std::string{"2"}, std::string{"3"}, std::string{"4"}}}
|
||||
}
|
8
test/data/partial_array_of_partials.data
Normal file
8
test/data/partial_array_of_partials.data
Normal file
@ -0,0 +1,8 @@
|
||||
mstch::object{
|
||||
{"numbers", mstch::array{
|
||||
mstch::object{{"i", std::string{"1"}}},
|
||||
mstch::object{{"i", std::string{"2"}}},
|
||||
mstch::object{{"i", std::string{"3"}}},
|
||||
mstch::object{{"i", std::string{"4"}}}
|
||||
}}
|
||||
}
|
3
test/data/partial_array_of_partials_implicit.data
Normal file
3
test/data/partial_array_of_partials_implicit.data
Normal file
@ -0,0 +1,3 @@
|
||||
mstch::object{
|
||||
{"numbers", mstch::array{std::string{"1"}, std::string{"2"}, std::string{"3"}, std::string{"4"}}}
|
||||
}
|
3
test/data/partial_empty.data
Normal file
3
test/data/partial_empty.data
Normal file
@ -0,0 +1,3 @@
|
||||
mstch::object{
|
||||
{"foo", 1}
|
||||
}
|
@ -6,6 +6,10 @@
|
||||
|
||||
using namespace mstchtest;
|
||||
|
||||
#define MSTCH_PARTIAL_TEST(x) TEST_CASE(#x) { \
|
||||
REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data, {{"partial", x ## _partial}})); \
|
||||
}
|
||||
|
||||
#define MSTCH_TEST(x) TEST_CASE(#x) { \
|
||||
REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data)); \
|
||||
}
|
||||
@ -39,6 +43,10 @@ MSTCH_TEST(nesting_same_name)
|
||||
MSTCH_TEST(null_lookup_array)
|
||||
MSTCH_TEST(null_lookup_object)
|
||||
MSTCH_TEST(null_view)
|
||||
MSTCH_PARTIAL_TEST(partial_array)
|
||||
MSTCH_PARTIAL_TEST(partial_array_of_partials)
|
||||
MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
|
||||
MSTCH_PARTIAL_TEST(partial_empty)
|
||||
MSTCH_TEST(recursion_with_same_names)
|
||||
MSTCH_TEST(reuse_of_enumerables)
|
||||
MSTCH_TEST(section_as_context)
|
||||
|
Loading…
Reference in New Issue
Block a user