pax_global_header 0000666 0000000 0000000 00000000064 12653735516 0014527 g ustar 00root root 0000000 0000000 52 comment=ff459067bd02e80dc399006bb610238223d41c50
mstch-1.0.2/ 0000775 0000000 0000000 00000000000 12653735516 0012645 5 ustar 00root root 0000000 0000000 mstch-1.0.2/.appveyor.yml 0000664 0000000 0000000 00000002633 12653735516 0015317 0 ustar 00root root 0000000 0000000 environment:
matrix:
- GENERATOR: "Visual Studio 12 2013"
CONFIG: Release
BOOST_ROOT: "C:\\Libraries\\boost"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost\\lib32-msvc-12.0"
- GENERATOR: "Visual Studio 14 2015"
CONFIG: Release
BOOST_ROOT: "C:\\Libraries\\boost_1_59_0"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost_1_59_0\\lib32-msvc-14.0"
- GENERATOR: "Visual Studio 14 2015 Win64"
CONFIG: Release
BOOST_ROOT: "C:\\Libraries\\boost_1_59_0"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost_1_59_0\\lib64-msvc-14.0"
- GENERATOR: "Visual Studio 12 2013"
CONFIG: Debug
BOOST_ROOT: "C:\\Libraries\\boost"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost\\lib32-msvc-12.0"
- GENERATOR: "Visual Studio 14 2015"
CONFIG: Debug
BOOST_ROOT: "C:\\Libraries\\boost_1_59_0"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost_1_59_0\\lib32-msvc-14.0"
- GENERATOR: "Visual Studio 14 2015 Win64"
CONFIG: Debug
BOOST_ROOT: "C:\\Libraries\\boost_1_59_0"
BOOST_LIBRARYDIR: "C:\\Libraries\\boost_1_59_0\\lib64-msvc-14.0"
install:
- git submodule init
- git submodule update
build_script:
- mkdir build
- cd build
- cmake "-G%GENERATOR%" -DBOOST_ROOT="%BOOST_ROOT%" -DBOOST_LIBRARYDIR="%BOOST_LIBRARYDIR%" -DBoost_USE_STATIC_LIBS=ON -DWITH_UNIT_TESTS=ON ..
- cmake --build . --config "%CONFIG%"
test_script:
- ctest --build-config "%CONFIG%"
mstch-1.0.2/.gitignore 0000664 0000000 0000000 00000000015 12653735516 0014631 0 ustar 00root root 0000000 0000000 .idea
build*
mstch-1.0.2/.gitmodules 0000664 0000000 0000000 00000000760 12653735516 0015025 0 ustar 00root root 0000000 0000000 [submodule "vendor/Catch"]
path = vendor/Catch
url = https://github.com/philsquared/Catch.git
[submodule "vendor/spec"]
path = vendor/spec
url = https://github.com/mustache/spec.git
[submodule "vendor/benchmark"]
path = vendor/benchmark
url = https://github.com/google/benchmark.git
[submodule "vendor/headerize"]
path = vendor/headerize
url = https://github.com/no1msd/headerize.git
[submodule "vendor/rapidjson"]
path = vendor/rapidjson
url = https://github.com/miloyip/rapidjson.git
mstch-1.0.2/.travis.yml 0000664 0000000 0000000 00000006742 12653735516 0014767 0 ustar 00root root 0000000 0000000 language: cpp
sudo: false
matrix:
include:
- env:
- COMPILER=g++-5
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- g++-5
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=g++-4.9
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- g++-4.9
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=g++-4.8
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- g++-4.8
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=g++-4.7
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- g++-4.7
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=clang++-3.5
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- llvm-toolchain-precise-3.5
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- clang-3.5
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=clang++-3.6
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- clang-3.6
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=clang++-3.7
- WITH_UNIT_TESTS=ON
addons:
apt:
sources:
- llvm-toolchain-precise-3.7
- ubuntu-toolchain-r-test
- boost-latest
- george-edison55-precise-backports
packages:
- clang-3.7
- cmake
- cmake-data
- libboost1.55-dev
- libboost-regex1.55-dev
- libboost-program-options1.55-dev
os: linux
- env:
- COMPILER=clang++
- WITH_UNIT_TESTS=ON
os: osx
before_script:
- export CXX=${COMPILER}
- ${COMPILER} --version
- mkdir build
- cd build
- cmake -DWITH_UNIT_TESTS=${WITH_UNIT_TESTS} ..
script: make
after_script: make test
mstch-1.0.2/CMakeLists.txt 0000664 0000000 0000000 00000001210 12653735516 0015377 0 ustar 00root root 0000000 0000000 cmake_minimum_required(VERSION 3.0.2)
project(mstch)
option(WITH_UNIT_TESTS "enable building unit test executable" OFF)
option(WITH_BENCHMARK "enable building benchmark executable" OFF)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
set(CMAKE_BUILD_TYPE Release)
set(mstch_VERSION 1.0.1)
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -O3")
endif()
add_subdirectory(src)
if(WITH_UNIT_TESTS)
enable_testing()
add_subdirectory(vendor/headerize)
add_subdirectory(test)
endif()
if(WITH_BENCHMARK)
add_subdirectory(vendor/benchmark)
add_subdirectory(benchmark)
endif()
mstch-1.0.2/LICENSE 0000664 0000000 0000000 00000002070 12653735516 0013651 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2015 Daniel Sipka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
mstch-1.0.2/README.md 0000664 0000000 0000000 00000016434 12653735516 0014134 0 ustar 00root root 0000000 0000000 # mstch - {{mustache}} templates in C++11

mstch is a complete implementation of [{{mustache}}](http://mustache.github.io/)
templates using modern C++. It's compliant with [specifications](https://github.com/mustache/spec)
v1.1.3, including the lambda module.
[](http://badge.fury.io/gh/no1msd%2Fmstch)
[](https://travis-ci.org/no1msd/mstch)
[](https://ci.appveyor.com/project/no1msd/mstch)
## Supported features
mstch supports the complete feature set described in the `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html):
- JSON-like data structure using [Boost.Variant](http://www.boost.org/libs/variant)
- variables, sections, inverted sections
- partials
- changing the delimiter
- C++11 lambdas
- C++ objects as view models
## Basic usage
```c++
#include
#include
int main() {
std::string view{"{{#names}}Hi {{name}}!\n{{/names}}"};
mstch::map context{
{"names", mstch::array{
mstch::map{{"name", std::string{"Chris"}}},
mstch::map{{"name", std::string{"Mark"}}},
mstch::map{{"name", std::string{"Scott"}}},
}}
};
std::cout << mstch::render(view, context) << std::endl;
return 0;
}
```
The output of this example will be:
```html
Hi Chris!
Hi Mark!
Hi Scott!
```
### Data structure
The types in the example above, `mstch::array` and `mstch::map` are actually
aliases for standard types:
```c++
using map = std::map;
using array = std::vector;
```
`mstch::node` is a `boost::variant` that can hold a `std::string`, `int`,
`double`, `bool`, `mstch::lambda` or a `std::shared_ptr`
(see below), also a map or an array recursively. Essentially it works just like
a JSON object.
Note that when using a `std::string` as value you must explicitly specify the
type, since a `const char*` literal like `"foobar"` would be implicitly
converted to `bool`. Alternatively you can use [C++14 string_literals](http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s)
if your compiler supports it.
## Advanced usage
### Partials
Partials can be passed in a `std::map` as the third parameter of the
`mstch::render` function:
```c++
std::string view{"{{#names}}{{> user}}{{/names}}"};
std::string user_view{"{{name}}\n"};
mstch::map context{
{"names", mstch::array{
mstch::map{{"name", std::string{"Chris"}}},
mstch::map{{"name", std::string{"Mark"}}},
mstch::map{{"name", std::string{"Scott"}}},
}}
};
std::cout << mstch::render(view, context, {{"user", user_view}}) << std::endl;
```
Output:
```html
ChrisMarkScott
```
### Lambdas
C++11 lambda expressions can be used to add logic to your templates. Like a
`const char*` literal, lambdas can be implicitly converted to `bool`, so they
must be wrapped in a `mstch::lambda` object when used in a `mstch::node`. The
lambda expression passed to `mstch::lambda` must itself return a `mstch::node`.
The returned node will be rendered to a string, then it will be parsed as a
template.
The lambda expression accepts either no parameters:
```c++
std::string view{"Hello {{lambda}}!"};
mstch::map context{
{"lambda", mstch::lambda{[]() -> mstch::node {
return std::string{"World"};
}}}
};
std::cout << mstch::render(view, context) << std::endl;
```
Output:
```html
Hello World!
```
Or it accepts a `const std::string&` that gets the unrendered literal block:
```c++
std::string view{"{{#bold}}{{yay}} :){{/bold}}"};
mstch::map context{
{"yay", std::string{"Yay!"}},
{"bold", mstch::lambda{[](const std::string& text) -> mstch::node {
return "" + text + "";
}}}
};
std::cout << mstch::render(view, context) << std::endl;
```
Output:
```html
Yay! :)
```
### Objects
Custom objects can also be used as context for rendering templates. The class
must inherit from `mstch::object`, and register it's exported methods with
`register_methods`. Exported methods must have the return type of `mstch::node`.
Objects must be created as a `std::shared_ptr`.
```c++
class example: public mstch::object {
public:
example(): m_value(1) {
register_methods(this, {
{"count", &example::count},
{"names", &example::names}
});
}
mstch::node count() {
return m_value++;
}
mstch::node names() {
return mstch::array{
std::string{"Chris"}, std::string{"Mark"}, std::string{"Scott"}};
}
private:
int m_value;
};
std::string view{"{{#names}}{{count}}: {{.}}\n{{/names}}"};
const auto context = std::make_shared();
std::cout << mstch::render(view, context) << std::endl;
```
Output:
```html
1: Chris
2: Mark
3: Scott
```
### Custom escape function
By default, mstch uses HTML escaping on the output, as per specification. This
is not useful if your output is not HTML, so mstch provides a way to supply
your own escape implementation. Just assign any callable object to the static
`mstch::config::escape`, which is an initially empty
`std::function`.
For example you can turn off escaping entirely with a lambda:
```c++
mstch::config::escape = [](const std::string& str) -> std::string {
return str;
};
```
## Requirements
- A C++ compiler with decent C++11 support. Currently tested with:
- GCC 4.7, 4.8, 4.9, 5.1
- clang 3.5, 3.6, 3.7 (both libstdc++ and libc++ are supported)
- MSVC 2013, 2015
- Boost 1.54+ for [Boost.Variant](http://www.boost.org/libs/variant)
- CMake 3.0+ for building
## Using mstch in your project
If you are using CMake, the easiest way to include mstch in your project is to
copy the whole directory to your source tree, and use `add_subdirectory` in your
CMakeLists.txt. This will set a variable named `mstch_INCLUDE_DIR` that contains
its include path, and add a static library target named `mstch`. For example:
```cmake
add_subdirectory(external/mstch)
include_directories(${mstch_INCLUDE_DIR})
target_link_libraries(your_project mstch)
```
If you prefer to install the library globally, you can simply do the following
from the root of the source tree:
```bash
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install
```
The install command may require root privileges. This will also install CMake
config files, so you can use use `find_package` in your CMakeLists.txt:
```cmake
find_package(mstch)
target_link_libraries(your_project mstch::mstch)
```
## Running the unit tests
Unit tests are using the [Catch](https://github.com/philsquared/Catch) framework
and [rapidjson](http://rapidjson.org/) to parse the
[Mustache specifications](https://github.com/mustache/spec), all of which are
included in the repository as git submodules. Various
[Boost](http://www.boost.org/) libraries are also required to build them.
Don't forget to initialize submodules:
```bash
$ git submodule init
$ git submodule update
```
To build and run the unit tests:
```bash
$ mkdir build
$ cd build
$ cmake -DWITH_UNIT_TESTS=ON ..
$ make
$ make test
```
## License
mstch is licensed under the [MIT license](https://github.com/no1msd/mstch/blob/master/LICENSE).
mstch-1.0.2/benchmark/ 0000775 0000000 0000000 00000000000 12653735516 0014577 5 ustar 00root root 0000000 0000000 mstch-1.0.2/benchmark/CMakeLists.txt 0000664 0000000 0000000 00000000316 12653735516 0017337 0 ustar 00root root 0000000 0000000 include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/vendor/benchmark/include)
add_executable(mstch_benchmark benchmark_main.cpp)
target_link_libraries(mstch_benchmark mstch benchmark)
mstch-1.0.2/benchmark/benchmark_main.cpp 0000664 0000000 0000000 00000002140 12653735516 0020236 0 ustar 00root root 0000000 0000000 #include
#include "mstch/mstch.hpp"
static void basic_usage(benchmark::State& state) {
std::string comment_tmp{
"
{{header}}
"
"{{#comments}}
{{name}}
"
"
{{body}}
{{/comments}}
"};
auto comment_view = mstch::map{
{"header", std::string{"My Post Comments"}},
{"comments", mstch::array{
mstch::map{{"name", std::string{"Joe"}}, {"body", std::string{"Thanks for this post!"}}},
mstch::map{{"name", std::string{"Sam"}}, {"body", std::string{"Thanks for this post!"}}},
mstch::map{{"name", std::string{"Heather"}}, {"body", std::string{"Thanks for this post!"}}},
mstch::map{{"name", std::string{"Kathy"}}, {"body", std::string{"Thanks for this post!"}}},
mstch::map{{"name", std::string{"George"}}, {"body", std::string{"Thanks for this post!"}}}}}};
while (state.KeepRunning())
mstch::render(comment_tmp, comment_view);
}
BENCHMARK(basic_usage);
BENCHMARK_MAIN();
mstch-1.0.2/cmake/ 0000775 0000000 0000000 00000000000 12653735516 0013725 5 ustar 00root root 0000000 0000000 mstch-1.0.2/cmake/mstch-config.cmake 0000664 0000000 0000000 00000000070 12653735516 0017305 0 ustar 00root root 0000000 0000000 include("${CMAKE_CURRENT_LIST_DIR}/mstch-targets.cmake") mstch-1.0.2/include/ 0000775 0000000 0000000 00000000000 12653735516 0014270 5 ustar 00root root 0000000 0000000 mstch-1.0.2/include/mstch/ 0000775 0000000 0000000 00000000000 12653735516 0015406 5 ustar 00root root 0000000 0000000 mstch-1.0.2/include/mstch/mstch.hpp 0000664 0000000 0000000 00000005544 12653735516 0017245 0 ustar 00root root 0000000 0000000 #pragma once
#include
#include
{{header}}
" "{{#comments}} {{/comments}}
{{name}}
" "{{body}}