2015-04-09 20:41:27 +02:00
|
|
|
#include <regex>
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-04-12 15:35:13 +02:00
|
|
|
#include "mstch/mstch.hpp"
|
|
|
|
#include "render_context.hpp"
|
2015-04-09 20:41:27 +02:00
|
|
|
|
|
|
|
using namespace mstch;
|
|
|
|
|
|
|
|
std::string strip_whitespace(std::string tmplt) {
|
2015-04-10 12:56:08 +02:00
|
|
|
std::regex comment_match("\\{\\{![^\\}]*\\}\\}");
|
|
|
|
tmplt = std::regex_replace(tmplt, comment_match, "{{!}}");
|
2015-04-09 20:41:27 +02:00
|
|
|
std::ostringstream out;
|
|
|
|
std::istringstream in(tmplt);
|
|
|
|
std::string line;
|
|
|
|
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, "");
|
2015-04-12 15:25:16 +02:00
|
|
|
if (no_tags != line && std::regex_match(no_tags, whitespace_match)) {
|
2015-04-09 20:41:27 +02:00
|
|
|
out << std::regex_replace(line, std::regex("\\s"), "");
|
|
|
|
} else {
|
|
|
|
out << line;
|
|
|
|
if(!in.eof()) out << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string mstch::render(
|
2015-04-11 16:29:12 +02:00
|
|
|
const std::string& tmplt,
|
|
|
|
const object& root_object,
|
2015-04-09 20:41:27 +02:00
|
|
|
const std::map<std::string,std::string>& partials)
|
|
|
|
{
|
2015-04-10 12:56:08 +02:00
|
|
|
return render_context(root_object, partials)
|
|
|
|
.render(strip_whitespace(tmplt));
|
2015-04-09 20:41:27 +02:00
|
|
|
}
|