move strip_whitespace to utils

This commit is contained in:
Daniel Sipka 2015-04-12 16:19:55 +02:00
parent 23e8b46879
commit 1f3d0da79b
3 changed files with 23 additions and 21 deletions

View file

@ -1,31 +1,11 @@
#include <regex>
#include <iostream>
#include "mstch/mstch.hpp"
#include "render_context.hpp"
#include "utils.hpp"
using namespace mstch;
std::string strip_whitespace(std::string tmplt) {
std::regex comment_match("\\{\\{![^\\}]*\\}\\}");
tmplt = std::regex_replace(tmplt, comment_match, "{{!}}");
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, "");
if (no_tags != line && std::regex_match(no_tags, whitespace_match)) {
out << std::regex_replace(line, std::regex("\\s"), "");
} else {
out << line;
if(!in.eof()) out << std::endl;
}
}
return out.str();
}
std::string mstch::render(
const std::string& tmplt,
const object& root_object,

View file

@ -1,7 +1,28 @@
#include "utils.hpp"
#include <regex>
#include <boost/algorithm/string/replace.hpp>
std::string mstch::strip_whitespace(std::string tmplt) {
std::regex comment_match("\\{\\{![^\\}]*\\}\\}");
tmplt = std::regex_replace(tmplt, comment_match, "{{!}}");
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, "");
if (no_tags != line && std::regex_match(no_tags, whitespace_match)) {
out << std::regex_replace(line, std::regex("\\s"), "");
} else {
out << line;
if(!in.eof()) out << std::endl;
}
}
return out.str();
}
std::string mstch::html_escape(std::string str) {
boost::replace_all(str, "&", "&amp;");
boost::replace_all(str, "'", "&#39;");

View file

@ -4,6 +4,7 @@
#include <string>
namespace mstch {
std::string strip_whitespace(std::string tmplt);
std::string html_escape(std::string str);
}