cmake restructure

This commit is contained in:
Daniel Sipka 2015-04-12 15:25:16 +02:00
parent c6164ae8c8
commit c8152686df
16 changed files with 93 additions and 123 deletions

View File

@ -3,52 +3,8 @@ project(mstch)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
find_package(Boost 1.54 COMPONENTS program_options REQUIRED)
include_directories(${CMAKE_BINARY_DIR} src include vendor/include ${Boost_INCLUDE_DIR})
add_subdirectory(src)
add_subdirectory(test)
set(SRC
src/mstch.cpp
src/utils.cpp
src/token.cpp
src/render_context.cpp
src/state/in_section.cpp
src/state/in_inverted_section.cpp
src/state/outside_section.cpp
src/visitor/is_node_empty.cpp
src/visitor/render_node.cpp
src/visitor/render_section.cpp
src/visitor/to_json.cpp)
add_library(mstch STATIC ${SRC})
add_executable(mstch_test test/test_main.cpp)
target_link_libraries(mstch_test mstch)
add_executable(filetoheader test/filetoheader.cpp)
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
file(GLOB data_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.data")
foreach(data_file ${data_files})
list(APPEND genargs "-D${data_file}")
endforeach(data_file)
file(GLOB string_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
"${CMAKE_SOURCE_DIR}/test/data/*.txt")
foreach(string_file ${string_files})
list(APPEND genargs "-S${string_file}")
endforeach(string_file)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/test_data.h
COMMAND ${CMAKE_BINARY_DIR}/filetoheader --output ${CMAKE_BINARY_DIR}/test_data.h --namespace mstchtest ${genargs}
DEPENDS ${CMAKE_BINARY_DIR}/filetoheader
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
set_source_files_properties(${CMAKE_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE)
add_custom_target(test_data_h DEPENDS ${CMAKE_BINARY_DIR}/test_data.h)
add_dependencies(mstch test_data_h)
enable_testing()
add_test(NAME mstch_tests COMMAND mstch_test)

View File

@ -1,14 +0,0 @@
#ifndef _MSTCH_H_
#define _MSTCH_H_
#include "types.h"
namespace mstch {
std::string render(
const std::string& tmplt,
const object& context,
const std::map<std::string,std::string>& partials =
std::map<std::string,std::string>());
}
#endif // _MSTCH_H_

View File

@ -1,5 +1,5 @@
#ifndef _MSTCH_TYPES_H_
#define _MSTCH_TYPES_H_
#ifndef _MSTCH_H_
#define _MSTCH_H_
#include <vector>
#include <map>
@ -14,6 +14,12 @@ namespace mstch {
std::vector<boost::recursive_variant_>>::type;
using object = std::map<const std::string,node>;
using array = std::vector<node>;
std::string render(
const std::string& tmplt,
const object& context,
const std::map<std::string,std::string>& partials =
std::map<std::string,std::string>());
}
#endif //_MSTCH_TYPES_H_
#endif // _MSTCH_H_

21
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
find_package(Boost 1.54 REQUIRED)
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}
${Boost_INCLUDE_DIR})
set(SRC
mstch.cpp
utils.cpp
token.cpp
render_context.cpp
state/in_section.cpp
state/in_inverted_section.cpp
state/outside_section.cpp
visitor/is_node_empty.cpp
visitor/render_node.cpp
visitor/render_section.cpp
visitor/to_json.cpp)
add_library(mstch STATIC ${SRC})

View File

@ -1,7 +1,7 @@
#include <regex>
#include <iostream>
#include "mstch.h"
#include "mstch/mstch.h"
#include "render_context.h"
using namespace mstch;
@ -16,7 +16,7 @@ std::string strip_whitespace(std::string tmplt) {
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)) {
if (no_tags != line && std::regex_match(no_tags, whitespace_match)) {
out << std::regex_replace(line, std::regex("\\s"), "");
} else {
out << line;

View File

@ -5,7 +5,7 @@
#include <sstream>
#include <string>
#include "types.h"
#include "mstch/mstch.h"
#include "state/render_state.h"
namespace mstch {

View File

@ -1,6 +1,7 @@
#include "token.h"
#include "utils.h"
#include <boost/algorithm/string/trim.hpp>
#include <regex>
using namespace mstch;
@ -9,7 +10,7 @@ token::token(const std::string& raw_token): raw_val(raw_token) {
std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
if(std::regex_match(raw_token, token_match)) {
std::string inside = raw_token.substr(2, raw_token.size() - 4);
inside = trim(inside);
boost::trim(inside);
if (inside.size() > 0 && inside.at(0) == '#') {
type_val = token_type::section_open;
content_val = inside.substr(1);
@ -37,7 +38,7 @@ token::token(const std::string& raw_token): raw_val(raw_token) {
type_val = token_type::variable;
content_val = inside;
}
content_val = trim(content_val);
boost::trim(content_val);
} else {
type_val = token_type::text;
content_val = raw_token;

View File

@ -1,46 +1,13 @@
#include "utils.h"
#include <algorithm>
std::string& mstch::ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(
s.begin(),
s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
std::string& mstch::rtrim(std::string& s) {
s.erase(std::find_if(
s.rbegin(),
s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
std::string& mstch::trim(std::string& s) {
return ltrim(rtrim(s));
}
std::string mstch::replace_all(
std::string str,
const std::string& from,
const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
#include <boost/algorithm/string/replace.hpp>
std::string mstch::html_escape(std::string str) {
str = replace_all(str, "&", "&amp;");
str = replace_all(str, "'", "&#39;");
str = replace_all(str, "\"", "&quot;");
str = replace_all(str, "<", "&lt;");
str = replace_all(str, ">", "&gt;");
str = replace_all(str, "/", "&#x2F;");
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;");
return str;
}

View File

@ -4,13 +4,6 @@
#include <string>
namespace mstch {
std::string& ltrim(std::string& s);
std::string& rtrim(std::string& s);
std::string& trim(std::string& s);
std::string replace_all(
std::string str,
const std::string& from,
const std::string& to);
std::string html_escape(std::string str);
}

View File

@ -4,7 +4,7 @@
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "types.h"
#include "mstch/mstch.h"
namespace mstch {
namespace visitor {

View File

@ -4,7 +4,7 @@
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "types.h"
#include "mstch/mstch.h"
#include <set>
namespace mstch {

View File

@ -6,7 +6,7 @@
#include <render_context.h>
#include <set>
#include "types.h"
#include "mstch/mstch.h"
namespace mstch {
namespace visitor {

View File

@ -5,7 +5,7 @@
#include <boost/blank.hpp>
#include <render_context.h>
#include "types.h"
#include "mstch/mstch.h"
namespace mstch {
namespace visitor {

39
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,39 @@
find_package(Boost 1.54 COMPONENTS program_options REQUIRED)
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}
${Boost_INCLUDE_DIR})
add_executable(filetoheader filetoheader.cpp)
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
file(GLOB data_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.data")
foreach(data_file ${data_files})
list(APPEND genargs "-D${data_file}")
endforeach(data_file)
file(GLOB string_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
"${CMAKE_SOURCE_DIR}/test/data/*.txt")
foreach(string_file ${string_files})
list(APPEND genargs "-S${string_file}")
endforeach(string_file)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.h
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/filetoheader --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.h --namespace mstchtest ${genargs}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/filetoheader
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE)
add_custom_target(test_data_h DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.h)
add_executable(mstch_test test_main.cpp)
target_link_libraries(mstch_test mstch)
add_dependencies(mstch_test test_data_h)

View File

@ -1,7 +1,8 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "mstch.h"
#include <catch.hpp>
#include <mstch/mstch.h>
#include "test_data.h"
#define MSTCH_TEST(x,y) TEST_CASE(x) { REQUIRE(y ## _txt == mstch::render(y ## _mustache, y ## _data)); }