mstch/src/utils.cpp

35 lines
1.1 KiB
C++
Raw Normal View History

2015-04-12 13:35:13 +00:00
#include "utils.hpp"
2015-04-09 18:41:27 +00:00
2015-04-12 14:19:55 +00:00
#include <regex>
2015-04-12 13:25:16 +00:00
#include <boost/algorithm/string/replace.hpp>
2015-04-09 18:41:27 +00:00
2015-04-12 14:19:55 +00:00
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;
2015-04-12 15:11:49 +00:00
std::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}");
2015-04-12 14:19:55 +00:00
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();
}
2015-04-09 18:41:27 +00:00
std::string mstch::html_escape(std::string str) {
2015-04-12 13:25:16 +00:00
boost::replace_all(str, "&", "&amp;");
boost::replace_all(str, "'", "&#39;");
boost::replace_all(str, "\"", "&quot;");
boost::replace_all(str, "<", "&lt;");
boost::replace_all(str, ">", "&gt;");
boost::replace_all(str, "/", "&#x2F;");
2015-04-09 18:41:27 +00:00
return str;
}