Skip to content
This repository was archived by the owner on Oct 9, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,7 @@ std::string template_t::render_tags(const std::string& tmplate,
{
try
{
// get value
std::string s = ctx.get(key)[0][key];
// escape backslash in std::string
const std::string f = "\\";
size_t found = s.find(f);
while(found != std::string::npos)
{
s.replace(found,f.length(),"\\\\");
found = s.find(f, found+2);
}
repl.assign(s);
repl.assign(ctx.get(key)[0][key]);
}
catch(int i) { repl.assign(""); }
}
Expand All @@ -140,8 +130,8 @@ std::string template_t::render_tags(const std::string& tmplate,
}

// replace
ret += regex_replace(text, tag, repl,
boost::match_default | boost::format_all);
ret += boost::regex_replace(text, tag, repl,
boost::match_default | boost::format_all | boost::regex_constants::format_literal);
// change delimiter after was removed
if (modifier == "=")
{
Expand Down Expand Up @@ -250,7 +240,7 @@ std::string template_t::render_sections(const std::string& tmplate,
}
else repl.assign("");
ret += boost::regex_replace(text, section, repl,
boost::match_default | boost::format_all);
boost::match_default | boost::format_all | boost::regex_constants::format_literal);
rest.assign(matches[0].second, end);
start = matches[0].second;
}
Expand Down Expand Up @@ -335,7 +325,7 @@ std::string template_t::html_escape(const std::string& s)
std::string repl;
repl = escape_lut[key];
ret += boost::regex_replace(text, escape_chars, repl,
boost::match_default | boost::format_all);
boost::match_default | boost::format_all | boost::regex_constants::format_literal);
rest.assign(matches[0].second, end);
start = matches[0].second;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/test_simple_plustache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class SimpleTest : public ::testing::Test
std::string template_string;
std::string result_notfound;
std::string notfound;
std::string result_dollars;
std::string dollars;
std::string result_parens;
std::string parens;
std::map<std::string, std::string> ctx;
std::string file;

Expand All @@ -30,6 +34,8 @@ class SimpleTest : public ::testing::Test
{
template_string = "text {{title}} text";
notfound = "text {{fitle}} text";
dollars = "text {{dollars}}";
parens = "text {{ open_paren }}inside{{ close_paren }}";
file = "multiple.mustache";

std::ofstream myfile;
Expand All @@ -38,10 +44,16 @@ class SimpleTest : public ::testing::Test
myfile.close();

ctx["title"] = "replaced";
ctx["dollars"] = "$0";
ctx["open_paren"] = "(";
ctx["close_paren"] = ")";

Plustache::template_t t;
result_string = t.render(template_string, ctx);
result_file = t.render(file, ctx);
result_notfound = t.render(notfound, ctx);
result_dollars = t.render(dollars, ctx);
result_parens = t.render(parens, ctx);
}

virtual void TearDown()
Expand All @@ -68,3 +80,15 @@ TEST_F(SimpleTest, TestSimpleNotFoundMustacheFromString)
const std::string expected = "text text";
EXPECT_EQ(expected, result_notfound);
}

TEST_F(SimpleTest, TestDollarSignsInValue)
{
const std::string expected = "text $0";
EXPECT_EQ(expected, result_dollars);
}

TEST_F(SimpleTest, TestParensInValue)
{
const std::string expected = "text (inside)";
EXPECT_EQ(expected, result_parens);
}