diff --git a/src/mstch.cpp b/src/mstch.cpp index 795365d..af9c84f 100644 --- a/src/mstch.cpp +++ b/src/mstch.cpp @@ -1,31 +1,11 @@ -#include #include #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, diff --git a/src/utils.cpp b/src/utils.cpp index 558c2c7..1f8efcd 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,28 @@ #include "utils.hpp" +#include #include +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, "&", "&"); boost::replace_all(str, "'", "'"); diff --git a/src/utils.hpp b/src/utils.hpp index ff7cdaa..ba4d411 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -4,6 +4,7 @@ #include namespace mstch { + std::string strip_whitespace(std::string tmplt); std::string html_escape(std::string str); }