see-phit

A C++ HTML template engine that uses compile time HTML parsing

  • 所有者: rep-movsd/see-phit
  • 平台:
  • 许可证: GNU Lesser General Public License v3.0
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

see-phit

See-phit is a compile time HTML templating library written in modern C++.

You write plain HTML as C++ string literals and it is parsed at compile time into a DOM like data structure.

It makes your "stringly typed" HTML text into an actual strongly typed DSL.

C++14 is required to compile - it implements a fairly complete HTML parser as constexpr functions.
Before constexpr, the way to make C++ DSLs was by (ab)using operator overloading in ingenious ways, but now we can actually have a DSL as a user literal string and let the compiler compile your DSL into C++

Example:

#include <iostream>
#include "seephit.h"
using namespace std;



int main()
{
  constexpr auto parser =
    R"*(
    <span >
    <p  color="red" height='10' >{{name}} is a {{profession}} in {{city}}</p  >
    </span>
    )*"_html;
    
  spt::tree spt_tree(parser);
  
  spt::template_dict dct;
  dct["name"] = "Mary";
  dct["profession"] = "doctor";
  dct["city"] = "London";
  
  spt_tree.root.render(cerr, dct);
  cerr << endl;
  
  dct["city"] = "New York";
  dct["name"] = "John";
  dct["profession"] = "janitor";

  spt_tree.root.render(cerr, dct);
  cerr << endl;
}

produces the following output

<HTML>
  <span>
    <p COLOR='red' HEIGHT='10'>
    Mary is a doctor in London
    </p>
  </span>
</HTML>

<HTML>
  <span>
    <p COLOR='red' HEIGHT='10'>
    John is a janitor in New York
    </p>
  </span>
</HTML>

The program will fail to compile if the HTML is malformed - We attempt to make the compiler generate the most sensible error message:

For example, the following fragment:

<DIV>
This is a bad closing tag
</DIVV>

Generates the following compile errors in gcc:

$ g++ --std=c++14  -Wall main.cpp
In file included from seephit.h:21:0,
                 from main.cpp:3:
parse_error.h: In instantiation of 'constexpr spt::Error<ROW, COL, WHAT>::Error() [with int ROW = 4; int COL = 3; WHAT = spt::Mismatched_Close_Tag]':
main.cpp:13:3:   required from here
parse_error.h:40:15: error: incompatible types in assignment of 'const int' to 'char [0]'
     SPTParser = fatal;
     ~~~~~~~~~~^~~~~~~

And the following in clang:

$ clang++ --std=c++14 -Wall main.cpp
In file included from main.cpp:3:
In file included from ./seephit.h:21:
./parse_error.h:40:15: error: array type 'char [0]' is not assignable
    SPTParser = fatal;
    ~~~~~~~~~ ^
main.cpp:13:3: note: in instantiation of member function 'spt::Error<4, 3, spt::Mismatched_Close_Tag>::Error' requested here
  REPORT_ERRORS(parser);
  ^
./parse_error_generated.h:100:94: note: expanded from macro 'REPORT_ERRORS'
spt::IF<hasErr, spt::Error<parser.errRow, parser.errCol, spt::MsgToType<parser.err>::type>> {};
                                                                                             ^
1 error generated.

Some complicated template magic has been implemented to show the ROW and COLUMN in the text where the error occured.
gcc actually prints ROW = xxx and COL = xxx, which is great!
If your IDE does background parsing, it will indicate that your HTML template is malformed as you type it.

Limitations

The number of maximum nodes and attributes per parse is hardcoded to 1024.

Future plans

Add more complicated templating functionality with loops, conditionals and perhaps lambdas, and also allow this to be used on the frontend JS with emscripten.

Optimize the hell out of the templating engine

主要指标

概览
名称与所有者rep-movsd/see-phit
主编程语言C++
编程语言C++ (语言数: 5)
平台
许可证GNU Lesser General Public License v3.0
所有者活动
创建于2017-07-10 10:55:12
推送于2019-02-18 13:06:30
最后一次提交2019-02-18 18:36:29
发布数0
用户参与
星数390
关注者数17
派生数18
提交数99
已启用问题?
问题数24
打开的问题数7
拉请求数8
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?