├── middle_test.tpl ├── inc_test.tpl ├── cmake ├── ReplicatePythonSourceTree.cmake ├── FindCython.cmake └── UseCython.cmake ├── libRedZone ├── src │ ├── IO │ │ ├── Reader.cpp │ │ ├── Writer.cpp │ │ ├── Writer.h │ │ ├── Reader.h │ │ ├── StringWriter.h │ │ ├── StringWriter.cpp │ │ ├── FileWriter.h │ │ ├── StringReader.h │ │ ├── StringReader.cpp │ │ ├── FileReader.h │ │ ├── FileWriter.cpp │ │ └── FileReader.cpp │ ├── Exception │ │ ├── JsonError.cpp │ │ ├── IOError.cpp │ │ ├── IOError.h │ │ ├── TemplateContextError.h │ │ ├── TemplateSyntaxError.h │ │ ├── Exception.cpp │ │ ├── ExpressionException.cpp │ │ ├── TemplateContextError.cpp │ │ ├── TemplateSyntaxError.cpp │ │ ├── ExpressionException.h │ │ ├── Exception.h │ │ └── JsonError.h │ ├── Template │ │ ├── FileTemplate.h │ │ ├── FileTemplate.cpp │ │ ├── Template.h │ │ └── Template.cpp │ ├── Node │ │ ├── ElseNode.h │ │ ├── Root.cpp │ │ ├── ElseNode.cpp │ │ ├── TextNode.h │ │ ├── Root.h │ │ ├── Variable.h │ │ ├── IncludeNode.h │ │ ├── BlockNode.h │ │ ├── TextNode.cpp │ │ ├── EachNode.h │ │ ├── ExtendsNode.h │ │ ├── IfNode.h │ │ ├── CacheNode.h │ │ ├── BlockNode.cpp │ │ ├── Variable.cpp │ │ ├── Node.cpp │ │ ├── IfNode.cpp │ │ ├── Node.h │ │ ├── EachNode.cpp │ │ ├── IncludeNode.cpp │ │ ├── ExtendsNode.cpp │ │ └── CacheNode.cpp │ ├── Export.h │ ├── Parser │ │ ├── Fragment.h │ │ ├── ExpressionParser.h │ │ ├── Parser.h │ │ ├── Fragment.cpp │ │ ├── Parser.cpp │ │ └── ExpressionParser.cpp │ ├── Context │ │ ├── Context.h │ │ ├── json11.hpp │ │ ├── Context.cpp │ │ └── json11.cpp │ ├── Common.h │ └── Common.cpp └── CMakeLists.txt ├── .gitignore ├── test.json ├── base_test.tpl ├── cython ├── test.py └── RedZone.pyx ├── main.cpp ├── LICENSE ├── test.tpl ├── CMakeLists.txt └── README.md /middle_test.tpl: -------------------------------------------------------------------------------- 1 | {% extends base_test.tpl %} 2 | {% block footer %} 3 | Footer text 2 4 | {% endblock %} 5 | {% endextends %} 6 | -------------------------------------------------------------------------------- /inc_test.tpl: -------------------------------------------------------------------------------- 1 | {# Include successful #} 2 | {{ get( numbers, "first" ) }} should be 5 3 | {{ get( numbers, "second" ) }} should be 11 4 | {{ get( numbers, "third" ) }} should be true 5 | -------------------------------------------------------------------------------- /cmake/ReplicatePythonSourceTree.cmake: -------------------------------------------------------------------------------- 1 | # Note: when executed in the build dir, then CMAKE_CURRENT_SOURCE_DIR is the 2 | # build dir. 3 | file( COPY setup.py src test bin DESTINATION "${CMAKE_ARGV3}" 4 | FILES_MATCHING PATTERN "*.py" ) -------------------------------------------------------------------------------- /libRedZone/src/IO/Reader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Reader.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "Reader.h" 7 | 8 | namespace RedZone { 9 | 10 | Reader::Reader() { 11 | } 12 | 13 | Reader::~Reader() { 14 | } 15 | 16 | } /* namespace RedZone */ 17 | -------------------------------------------------------------------------------- /libRedZone/src/IO/Writer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Writer.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "Writer.h" 7 | 8 | namespace RedZone { 9 | 10 | Writer::Writer() { 11 | } 12 | 13 | Writer::~Writer() { 14 | } 15 | 16 | } /* namespace RedZone */ 17 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/JsonError.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JsonError.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #include "JsonError.h" 8 | 9 | namespace RedZone { 10 | 11 | JsonError::~JsonError() { 12 | 13 | } 14 | 15 | } /* namespace RedZone */ 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Compiled Dynamic libraries 8 | *.so 9 | *.dylib 10 | *.dll 11 | 12 | # Compiled Static libraries 13 | *.lai 14 | *.la 15 | *.a 16 | *.lib 17 | 18 | # Executables 19 | *.exe 20 | *.out 21 | *.app 22 | 23 | build 24 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/IOError.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * IOError.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "IOError.h" 9 | 10 | namespace RedZone { 11 | 12 | IOError::IOError( std::string const & message ) 13 | : Exception( message ) { 14 | 15 | } 16 | 17 | IOError::~IOError() { 18 | } 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /test.json: -------------------------------------------------------------------------------- 1 | { 2 | "items": [ 3 | { "text": "Hello World!", "active": true }, 4 | { "text": "Foo", "active": true }, 5 | { "text": "Bar", "active": false } 6 | ], 7 | "numbers": { 8 | "first": 5, 9 | "second": 11, 10 | "third": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/IOError.h: -------------------------------------------------------------------------------- 1 | /* 2 | * IOError.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Exception.h" 11 | 12 | namespace RedZone { 13 | 14 | class RZ_API IOError: public RedZone::Exception { 15 | public: 16 | IOError( std::string const & message ); 17 | virtual ~IOError(); 18 | }; 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /libRedZone/src/IO/Writer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Writer.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | 11 | #include 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API Writer { 16 | public: 17 | virtual void write( std::string const & data ) = 0; 18 | virtual void flush() = 0; 19 | 20 | virtual ~Writer(); 21 | 22 | protected: 23 | Writer(); 24 | }; 25 | 26 | } /* namespace RedZone */ 27 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/TemplateContextError.h: -------------------------------------------------------------------------------- 1 | /* 2 | * TemplateContextError.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Exception.h" 11 | 12 | namespace RedZone { 13 | 14 | class RZ_API TemplateContextError: public Exception { 15 | public: 16 | TemplateContextError( std::string const & contextVar ); 17 | virtual ~TemplateContextError(); 18 | }; 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/TemplateSyntaxError.h: -------------------------------------------------------------------------------- 1 | /* 2 | * TemplateSyntaxError.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Exception.h" 11 | 12 | namespace RedZone { 13 | 14 | class RZ_API TemplateSyntaxError: public Exception { 15 | public: 16 | TemplateSyntaxError( std::string const & wrongSyntax ); 17 | virtual ~TemplateSyntaxError(); 18 | }; 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /base_test.tpl: -------------------------------------------------------------------------------- 1 | Base tempalte text! 2 | {% block header %} 3 | {% if true %} 4 | Header text 5 | {% endif %} 6 | {% endblock %} 7 | 8 | {% block mainContent %} 9 | This text should not be in output, 10 | if this block is overloaded 11 | {% endblock %} 12 | 13 | {% block additionalContent %} 14 | This text should be here if this block is not overloaded. 15 | {% endblock %} 16 | 17 | {% block footer %} 18 | Footer text 19 | {% endblock %} 20 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/Exception.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Exception.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "Exception.h" 9 | 10 | namespace RedZone { 11 | 12 | Exception::Exception( std::string const & message ) 13 | : m_message( message ) { 14 | 15 | } 16 | 17 | const char * Exception::what() const throw() { 18 | return m_message.c_str(); 19 | } 20 | 21 | Exception::~Exception() { 22 | } 23 | 24 | } /* namespace RedZone */ 25 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/ExpressionException.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ExpressionException.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "ExpressionException.h" 7 | 8 | namespace RedZone 9 | { 10 | 11 | ExpressionException::ExpressionException( std::string const & expr, std::string const & err ) 12 | : Exception( "In expression \"" + expr + "\": " + err ) 13 | { 14 | } 15 | 16 | ExpressionException::~ExpressionException() 17 | { 18 | } 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/TemplateContextError.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * TemplateContextError.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "TemplateContextError.h" 9 | 10 | namespace RedZone { 11 | 12 | TemplateContextError::TemplateContextError( std::string const & contextVar ) 13 | : Exception( "Cannot resolve '" + contextVar + "'" ) { 14 | } 15 | 16 | TemplateContextError::~TemplateContextError() { 17 | } 18 | 19 | } /* namespace RedZone */ 20 | -------------------------------------------------------------------------------- /libRedZone/src/Template/FileTemplate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FileTemplate.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include "Template.h" 10 | 11 | #include 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API FileTemplate: public Template { 16 | public: 17 | FileTemplate( std::string const & filePath ); 18 | virtual ~FileTemplate(); 19 | 20 | private: 21 | std::string m_filePath; 22 | }; 23 | 24 | } /* namespace RedZone */ 25 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/TemplateSyntaxError.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * TemplateSyntaxError.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "TemplateSyntaxError.h" 9 | 10 | namespace RedZone { 11 | 12 | TemplateSyntaxError::TemplateSyntaxError( std::string const & wrongSyntax ) 13 | : Exception( "'" + wrongSyntax + "' seems like invalid syntax." ) { 14 | 15 | } 16 | 17 | TemplateSyntaxError::~TemplateSyntaxError() { 18 | } 19 | 20 | } /* namespace RedZone */ 21 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/ExpressionException.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ExpressionException.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "Exception.h" 10 | 11 | namespace RedZone 12 | { 13 | 14 | class RZ_API ExpressionException : public Exception 15 | { 16 | public: 17 | using Exception::Exception; 18 | 19 | ExpressionException( std::string const & expr, std::string const & err ); 20 | 21 | virtual ~ExpressionException(); 22 | }; 23 | 24 | } /* namespace RedZone */ 25 | -------------------------------------------------------------------------------- /libRedZone/src/IO/Reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Reader.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | 11 | #include 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API Reader { 16 | public: 17 | virtual std::string read( size_t nBytes ) = 0; 18 | virtual std::string readAll() = 0; 19 | virtual std::string id() const = 0; 20 | 21 | virtual ~Reader(); 22 | 23 | protected: 24 | Reader(); 25 | }; 26 | 27 | } /* namespace RedZone */ 28 | -------------------------------------------------------------------------------- /libRedZone/src/Node/ElseNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ElseNode.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Node.h" 11 | 12 | namespace RedZone { 13 | 14 | class Writer; 15 | 16 | class RZ_API ElseNode: public Node { 17 | public: 18 | ElseNode(); 19 | 20 | virtual void render( Writer * stream, Context * context ) const; 21 | 22 | virtual std::string name() const; 23 | 24 | virtual ~ElseNode(); 25 | }; 26 | 27 | } /* namespace RedZone */ 28 | -------------------------------------------------------------------------------- /cython/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding: utf-8 -*- 3 | __author__ = 'jc' 4 | 5 | from RedZone import * 6 | 7 | context = Context({ 8 | "items": [ 9 | {"text": "Hello World!", "active": True}, 10 | {"text": "Foo", "active": True}, 11 | {"text": "Bar", "active": False} 12 | ], 13 | "numbers": { 14 | "first": 5, 15 | "second": 11, 16 | "third": True 17 | } 18 | }) 19 | tpl = FileTemplate('test.tpl') 20 | 21 | print tpl.render(context) 22 | -------------------------------------------------------------------------------- /libRedZone/src/Node/Root.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Root.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "Root.h" 9 | 10 | namespace RedZone { 11 | 12 | Root::Root( std::string id ) 13 | : Node(), m_id( id ) { 14 | 15 | } 16 | 17 | void Root::render( Writer * stream, Context * context ) const { 18 | renderChildren( stream, context ); 19 | } 20 | 21 | std::string Root::name() const { 22 | return "Root"; 23 | } 24 | 25 | Root::~Root() { 26 | } 27 | 28 | } /* namespace RedZone */ 29 | -------------------------------------------------------------------------------- /libRedZone/src/IO/StringWriter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * StringWriter.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | 11 | #include "Writer.h" 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API StringWriter : public Writer { 16 | public: 17 | StringWriter( std::string & string ); 18 | 19 | virtual void write( std::string const & data ); 20 | virtual void flush(); 21 | 22 | virtual ~StringWriter(); 23 | 24 | protected: 25 | std::string & m_string; 26 | }; 27 | 28 | } /* namespace RedZone */ 29 | -------------------------------------------------------------------------------- /libRedZone/src/Node/ElseNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ElseNode.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include "ElseNode.h" 9 | 10 | #include 11 | #include 12 | 13 | namespace RedZone { 14 | 15 | ElseNode::ElseNode() 16 | : Node() { 17 | 18 | } 19 | 20 | void ElseNode::render( Writer *, Context * ) const { 21 | 22 | } 23 | 24 | std::string ElseNode::name() const { 25 | return "Else"; 26 | } 27 | 28 | ElseNode::~ElseNode() { 29 | } 30 | 31 | } /* namespace RedZone */ 32 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/Exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Exception.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | namespace RedZone { 15 | 16 | class RZ_API Exception: public std::exception { 17 | public: 18 | Exception( std::string const & message ); 19 | virtual ~Exception(); 20 | 21 | virtual const char * what() const throw(); 22 | 23 | protected: 24 | std::string m_message; 25 | }; 26 | 27 | } /* namespace RedZone */ 28 | -------------------------------------------------------------------------------- /libRedZone/src/Template/FileTemplate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FileTemplate.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "FileTemplate.h" 14 | 15 | namespace RedZone { 16 | 17 | FileTemplate::FileTemplate( std::string const & filePath ) 18 | : m_filePath( filePath ) { 19 | FileReader in( filePath ); 20 | loadFromStream( &in ); 21 | } 22 | 23 | FileTemplate::~FileTemplate() { 24 | } 25 | 26 | } /* namespace RedZone */ 27 | -------------------------------------------------------------------------------- /libRedZone/src/IO/StringWriter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * StringWriter.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "StringWriter.h" 7 | 8 | #include 9 | 10 | namespace RedZone { 11 | 12 | StringWriter::StringWriter( std::string & string ) 13 | : m_string( string ) { 14 | } 15 | 16 | void StringWriter::write( std::string const & data ) { 17 | std::copy( data.begin(), data.end(), std::back_inserter( m_string ) ); 18 | } 19 | 20 | void StringWriter::flush() { 21 | 22 | } 23 | 24 | StringWriter::~StringWriter() { 25 | } 26 | 27 | } /* namespace RedZone */ 28 | -------------------------------------------------------------------------------- /libRedZone/src/Export.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Export.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #if defined( RZ_STATIC ) 10 | # define RZ_API 11 | #elif defined( __GNUC__ ) && __GNUC__ >= 4 && defined( RZ_EXPORTS ) 12 | # define RZ_API __attribute__ ( ( visibility( "default" ) ) ) 13 | #elif defined( _MSC_VER ) 14 | # if defined( RZ_EXPORTS ) 15 | # define RZ_API __declspec( dllexport ) 16 | # else 17 | # define RZ_API __declspec( dllimport ) 18 | # endif defined( RZ_EXPORTS ) 19 | #else 20 | #define RZ_API 21 | #endif // defined( RZ_STATIC ) 22 | -------------------------------------------------------------------------------- /libRedZone/src/IO/FileWriter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FileWriter.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | 11 | #include "Writer.h" 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API FileWriter : public Writer { 16 | public: 17 | FileWriter( std::string const & fileName ); 18 | 19 | virtual void write( std::string const & data ); 20 | virtual void flush(); 21 | 22 | virtual ~FileWriter(); 23 | 24 | protected: 25 | std::ofstream m_file; 26 | 27 | private: 28 | FileWriter( FileWriter const & ) { } 29 | }; 30 | 31 | } /* namespace RedZone */ 32 | -------------------------------------------------------------------------------- /libRedZone/src/Node/TextNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * TextNode.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Node.h" 11 | 12 | namespace RedZone { 13 | 14 | class Writer; 15 | 16 | class RZ_API TextNode: public Node { 17 | public: 18 | TextNode(); 19 | 20 | virtual void render( Writer * stream, Context * context ) const; 21 | virtual void processFragment( Fragment const * fragment ); 22 | 23 | virtual std::string name() const; 24 | 25 | virtual ~TextNode(); 26 | 27 | protected: 28 | std::string m_text; 29 | }; 30 | 31 | } /* namespace RedZone */ 32 | -------------------------------------------------------------------------------- /libRedZone/src/Node/Root.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Root.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include "Node.h" 13 | 14 | namespace RedZone { 15 | 16 | class Writer; 17 | 18 | class RZ_API Root : public Node { 19 | public: 20 | Root( std::string id ); 21 | 22 | virtual void render( Writer * stream, Context * context ) const; 23 | 24 | virtual std::string name() const; 25 | 26 | inline std::string id() const { return m_id; } 27 | 28 | virtual ~Root(); 29 | 30 | protected: 31 | std::string m_id; 32 | }; 33 | 34 | } /* namespace RedZone */ 35 | -------------------------------------------------------------------------------- /libRedZone/src/IO/StringReader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * StringReader.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | 11 | #include "Reader.h" 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API StringReader : public Reader { 16 | public: 17 | StringReader( std::string const & string ); 18 | 19 | virtual std::string read( size_t nBytes ); 20 | virtual std::string readAll(); 21 | virtual std::string id() const; 22 | 23 | virtual ~StringReader(); 24 | 25 | protected: 26 | std::string const & m_string; 27 | std::string::const_iterator m_iter; 28 | }; 29 | 30 | } /* namespace RedZone */ 31 | -------------------------------------------------------------------------------- /libRedZone/src/Parser/Fragment.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Fragment.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include 10 | 11 | #include "Parser.h" 12 | 13 | namespace RedZone { 14 | 15 | class RZ_API Fragment { 16 | public: 17 | Fragment( std::string const & rawText ); 18 | 19 | std::string cleanFragment() const; 20 | 21 | ElementType type() const; 22 | 23 | std::string raw() const; 24 | std::string clean() const; 25 | 26 | virtual ~Fragment(); 27 | 28 | protected: 29 | std::string m_rawText; 30 | std::string m_cleanText; 31 | }; 32 | 33 | } /* namespace RedZone */ 34 | -------------------------------------------------------------------------------- /libRedZone/src/Node/Variable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Variable.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include 10 | 11 | #include "Node.h" 12 | 13 | namespace RedZone { 14 | 15 | class Fragment; 16 | 17 | class RZ_API Variable: public Node { 18 | public: 19 | Variable(); 20 | 21 | virtual void render( Writer * stream, Context * context ) const; 22 | 23 | virtual void processFragment( Fragment const * fragment ); 24 | 25 | virtual std::string name() const; 26 | 27 | virtual ~Variable(); 28 | 29 | protected: 30 | std::string m_expression; 31 | }; 32 | 33 | } /* namespace RedZone */ 34 | -------------------------------------------------------------------------------- /libRedZone/src/Node/IncludeNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * IncludeNode.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | 12 | #include "Node.h" 13 | 14 | namespace RedZone { 15 | 16 | class Root; 17 | 18 | class RZ_API IncludeNode : public Node { 19 | public: 20 | IncludeNode(); 21 | 22 | virtual void render( Writer * stream, Context * context ) const; 23 | 24 | virtual void processFragment( Fragment const * fragment ); 25 | 26 | virtual std::string name() const; 27 | 28 | virtual ~IncludeNode(); 29 | 30 | protected: 31 | std::string m_includeExpr; 32 | }; 33 | 34 | } /* namespace RedZone */ 35 | -------------------------------------------------------------------------------- /libRedZone/src/Node/BlockNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BlockNode.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "Node.h" 10 | 11 | namespace RedZone { 12 | 13 | class RZ_API BlockNode : public Node { 14 | public: 15 | BlockNode(); 16 | 17 | virtual void render( Writer * stream, Context * context ) const; 18 | virtual void processFragment( Fragment const * fragment ); 19 | virtual void exitScope( std::string const & endTag ); 20 | 21 | virtual std::string name() const; 22 | std::string blockName() const; 23 | 24 | virtual ~BlockNode(); 25 | 26 | protected: 27 | std::string m_blockName; 28 | }; 29 | 30 | } /* namespace RedZone */ 31 | -------------------------------------------------------------------------------- /libRedZone/src/IO/StringReader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * StringReader.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "StringReader.h" 7 | 8 | namespace RedZone { 9 | 10 | StringReader::StringReader( std::string const & string ) 11 | : m_string( string ), m_iter( m_string.begin() ) { 12 | } 13 | 14 | std::string StringReader::read( size_t nBytes ) { 15 | std::string result( m_iter, m_iter + nBytes ); 16 | m_iter += nBytes; 17 | return result; 18 | } 19 | 20 | std::string StringReader::readAll() { 21 | return m_string; 22 | } 23 | 24 | std::string StringReader::id() const { 25 | return ""; 26 | } 27 | 28 | StringReader::~StringReader() { 29 | } 30 | 31 | } /* namespace RedZone */ 32 | -------------------------------------------------------------------------------- /libRedZone/src/Exception/JsonError.h: -------------------------------------------------------------------------------- 1 | /* 2 | * JsonError.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Exception.h" 11 | 12 | #include 13 | 14 | namespace RedZone { 15 | 16 | class RZ_API JsonError: public Exception { 17 | public: 18 | #if defined( RZ_UNIX ) 19 | using Exception::Exception; 20 | #elif defined( RZ_WINDOWS ) 21 | # pragma message( "Stupid M$VC compiler does not support constructor inheritance!" ) 22 | JsonError( std::string const & message ) : Exception( message ) 23 | { 24 | } 25 | #else 26 | # error( "Not implemented yet" ) 27 | #endif 28 | virtual ~JsonError(); 29 | }; 30 | 31 | } /* namespace RedZone */ 32 | -------------------------------------------------------------------------------- /libRedZone/src/Node/TextNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * TextNode.cpp 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | #include "TextNode.h" 8 | 9 | #include 10 | #include 11 | 12 | namespace RedZone { 13 | 14 | TextNode::TextNode() 15 | : Node() { 16 | 17 | } 18 | 19 | void TextNode::render( Writer * stream, Context * context ) const { 20 | (void)context; 21 | stream->write( m_text ); 22 | } 23 | 24 | void TextNode::processFragment( Fragment const * fragment ) { 25 | m_text = fragment->raw(); 26 | } 27 | 28 | std::string TextNode::name() const { 29 | return "Text"; 30 | } 31 | 32 | TextNode::~TextNode() { 33 | } 34 | 35 | } /* namespace RedZone */ 36 | -------------------------------------------------------------------------------- /libRedZone/src/IO/FileReader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FileReader.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "Reader.h" 14 | 15 | namespace RedZone { 16 | 17 | class RZ_API FileReader : public Reader { 18 | public: 19 | FileReader( std::string const & fileName ); 20 | 21 | virtual std::string read( size_t nBytes ); 22 | virtual std::string readAll(); 23 | virtual std::string id() const; 24 | 25 | virtual ~FileReader(); 26 | 27 | protected: 28 | std::ifstream m_file; 29 | std::string m_path; 30 | 31 | private: 32 | FileReader( FileReader const & ) { } 33 | }; 34 | 35 | } /* namespace RedZone */ 36 | -------------------------------------------------------------------------------- /libRedZone/src/Node/EachNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EachNode.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "Node.h" 11 | 12 | namespace RedZone { 13 | 14 | class Writer; 15 | 16 | class RZ_API EachNode: public Node { 17 | public: 18 | EachNode(); 19 | 20 | virtual void render( Writer * stream, Context * context ) const; 21 | virtual void processFragment( Fragment const * fragment ); 22 | virtual void exitScope( std::string const & endTag ); 23 | virtual std::string name() const; 24 | 25 | virtual ~EachNode(); 26 | 27 | protected: 28 | std::string m_container; 29 | std::vector< std::string > m_vars; 30 | }; 31 | 32 | } /* namespace RedZone */ 33 | -------------------------------------------------------------------------------- /libRedZone/src/Template/Template.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Template.h 3 | * 4 | * Created on: 21 ���� 2014 �. 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | namespace RedZone { 16 | 17 | class Context; 18 | class Reader; 19 | class Root; 20 | class Writer; 21 | 22 | class RZ_API Template { 23 | public: 24 | virtual ~Template(); 25 | 26 | void renderToStream( Writer * stream, Context * context ) const; 27 | std::string render( Context * context ) const; 28 | 29 | protected: 30 | Template(); 31 | void loadFromStream( Reader * stream ); 32 | 33 | protected: 34 | std::shared_ptr< Root const > m_root; 35 | }; 36 | 37 | } /* namespace RedZone */ 38 | -------------------------------------------------------------------------------- /libRedZone/src/IO/FileWriter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FileWriter.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "FileWriter.h" 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | namespace RedZone { 14 | 15 | FileWriter::FileWriter( std::string const & fileName ) 16 | : m_file( fileName ) { 17 | if( !m_file.good() ) { 18 | throw IOError( "Can not create " + fileName + " file." ); 19 | } 20 | } 21 | 22 | void FileWriter::write( std::string const & data ) { 23 | std::copy( data.begin(), data.end(), std::ostream_iterator< char >( m_file ) ); 24 | } 25 | 26 | void FileWriter::flush() { 27 | m_file.flush(); 28 | } 29 | 30 | FileWriter::~FileWriter() { 31 | } 32 | 33 | } /* namespace RedZone */ 34 | -------------------------------------------------------------------------------- /libRedZone/src/Node/ExtendsNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ExtendsNode.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "Node.h" 10 | 11 | namespace RedZone { 12 | 13 | class Root; 14 | 15 | class RZ_API ExtendsNode : public Node { 16 | public: 17 | ExtendsNode(); 18 | 19 | virtual void render( Writer * stream, Context * context ) const; 20 | virtual void processFragment( Fragment const * fragment ); 21 | virtual void exitScope( std::string const & endTag ); 22 | 23 | virtual std::string name() const; 24 | 25 | virtual ~ExtendsNode(); 26 | 27 | protected: 28 | std::string m_path; 29 | std::vector< std::shared_ptr< Node > > m_nodesToRender; 30 | std::shared_ptr< Root > m_parentRoot; 31 | }; 32 | 33 | } /* namespace RedZone */ 34 | -------------------------------------------------------------------------------- /libRedZone/src/Parser/ExpressionParser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ExpressionParser.h 3 | * 4 | * Author: jc 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | namespace RedZone 17 | { 18 | 19 | class Context; 20 | 21 | class RZ_API ExpressionParser 22 | { 23 | public: 24 | typedef std::set< char > charset; 25 | 26 | ExpressionParser( Context const * context ); 27 | 28 | json11::Json parse( std::string expression ) const; 29 | 30 | virtual ~ExpressionParser(); 31 | 32 | protected: 33 | json11::Json parseRecursive( std::string expression ) const; 34 | 35 | protected: 36 | Context const * m_context; 37 | charset m_binaryOperatorChars; 38 | }; 39 | 40 | } /* namespace RedZone */ 41 | -------------------------------------------------------------------------------- /libRedZone/src/Node/IfNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * IfNode.h 3 | * 4 | * Created on: 2014 5 | * Author: jc 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include "Node.h" 13 | 14 | namespace RedZone { 15 | 16 | class Writer; 17 | 18 | class RZ_API IfNode: public Node { 19 | public: 20 | 21 | IfNode(); 22 | 23 | virtual void render( Writer * stream, Context * context ) const; 24 | 25 | virtual void processFragment( Fragment const * fragment ); 26 | virtual void exitScope( std::string const & endTag ); 27 | 28 | virtual std::string name() const; 29 | 30 | virtual ~IfNode(); 31 | 32 | protected: 33 | std::string m_expression; 34 | std::vector< std::shared_ptr< Node > > m_ifNodes; 35 | std::vector< std::shared_ptr< Node > > m_elseNodes; 36 | }; 37 | 38 | } /* namespace RedZone */ 39 | -------------------------------------------------------------------------------- /libRedZone/src/IO/FileReader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FileReader.cpp 3 | * 4 | * Author: jc 5 | */ 6 | #include "FileReader.h" 7 | 8 | #include 9 | 10 | namespace RedZone { 11 | 12 | FileReader::FileReader( std::string const & fileName ) 13 | : m_file( fileName ), m_path( fileName ) { 14 | if( !m_file.good() ) { 15 | throw IOError( "Can not open " + fileName + " file." ); 16 | } 17 | } 18 | 19 | std::string FileReader::read( size_t nBytes ) { 20 | std::string result( nBytes, '\0' ); 21 | m_file.read( &result[ 0 ], nBytes ); 22 | return result; 23 | } 24 | 25 | std::string FileReader::readAll() { 26 | return std::string( std::istreambuf_iterator< char >( m_file ), std::istreambuf_iterator< char >() ); 27 | } 28 | 29 | std::string FileReader::id() const { 30 | return m_path; 31 | } 32 | 33 | FileReader::~FileReader() { 34 | } 35 | 36 | } /* namespace RedZone */ 37 | -------------------------------------------------------------------------------- /libRedZone/src/Node/CacheNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CacheNode.h 3 | * 4 | * Created on: 2015 5 | * Author: jc 6 | */ 7 | #pragma once 8 | 9 | #include "Node.h" 10 | 11 | #include 12 | #include 13 | 14 | namespace RedZone { 15 | 16 | class RZ_API CacheNode: public Node { 17 | public: 18 | CacheNode(); 19 | 20 | virtual void render( Writer * stream, Context * context ) const; 21 | virtual void processFragment( Fragment const * fragment ); 22 | virtual void exitScope( std::string const & endTag ); 23 | virtual std::string name() const; 24 | 25 | virtual ~CacheNode(); 26 | 27 | typedef std::tuple< std::chrono::time_point< 28 | std::chrono::system_clock>, std::string > CacheRow; 29 | 30 | protected: 31 | uint64_t m_cacheTime; 32 | std::vector< std::string > m_vars; 33 | static std::unordered_map< size_t, CacheRow > s_cached; 34 | }; 35 | 36 | } /* namespace RedZone */ 37 | -------------------------------------------------------------------------------- /libRedZone/src/Template/Template.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Template.cpp 3 | * 4 | * Created on: 21 ���� 2014 �. 5 | * Author: jc 6 | */ 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "Template.h" 15 | 16 | using namespace std::placeholders; 17 | 18 | namespace RedZone { 19 | 20 | Template::Template() { 21 | } 22 | 23 | void Template::renderToStream( Writer * stream, Context * context ) const { 24 | m_root->render( stream, context ); 25 | } 26 | 27 | std::string Template::render( Context * context ) const { 28 | std::string result; 29 | StringWriter stringWriter( result ); 30 | renderToStream( &stringWriter, context ); 31 | return result; 32 | } 33 | 34 | void Template::loadFromStream( Reader * stream ) { 35 | Parser parser; 36 | m_root.reset( parser.loadFromStream( stream ) ); 37 | } 38 | 39 | Template::~Template() { 40 | } 41 | 42 | } /* namespace RedZone */ 43 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * main.cpp 3 | * 4 | * Created on: 2014. 5 | * Author: jc 6 | */ 7 | 8 | #include 9 | #include 10 | #include