From 1f3d0da79bac633ebbb41d4107f9861461e1acb0 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Sun, 12 Apr 2015 16:19:55 +0200 Subject: [PATCH] move strip_whitespace to utils --- src/mstch.cpp | 22 +--------------------- src/utils.cpp | 21 +++++++++++++++++++++ src/utils.hpp | 1 + 3 files changed, 23 insertions(+), 21 deletions(-) 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); }