GithubHelp home page GithubHelp logo

hhy5277 / see-phit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rep-movsd/see-phit

0.0 3.0 0.0 89 KB

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

License: GNU Lesser General Public License v3.0

C++ 88.58% JavaScript 8.74% Shell 2.67%

see-phit's Introduction

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 compiler 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]':
  parse_error.h:46:26:   required from here
  parse_error.h:40:15: error: incompatible types in assignment of 'const int' to 'char [0]'
      SPTParser = fatal;
      ~~~~~~~~~~^~~~~~~
  In file included from parse_error.h:6:0,
                  from seephit.h:21,
                  from main.cpp:3:
  main.cpp: In function 'int main()':
  parse_error_generated.h:100:93: note: synthesized method 'constexpr spt::IF<true, spt::Error<4, 3, spt::Mismatched_Close_Tag> >::IF()' first required here
  spt::IF<hasErr, spt::Error<parser.errRow, parser.errCol, spt::MsgToType<parser.err>::type>> p;
                                                                                              ^
  main.cpp:11:3: note: in expansion of macro 'REPORT_ERRORS'
    REPORT_ERRORS(parser);
    ^~~~~~~~~~~~~

And the following in clang:

$ clang++ --std=c++14 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;
    ~~~~~~~~~ ^
./parse_error.h:46:26: note: in instantiation of member function 'spt::Error<4, 3, spt::Mismatched_Close_Tag>::Error' requested here
template<class T> struct IF<true, T> { T it; };
                         ^
main.cpp:11:3: note: implicit default constructor for 'spt::IF<true, spt::Error<4, 3, spt::Mismatched_Close_Tag> >' first required here
  REPORT_ERRORS(parser);
  ^
./parse_error_generated.h:100:93: note: expanded from macro 'REPORT_ERRORS'
spt::IF<hasErr, spt::Error<parser.errRow, parser.errCol, spt::MsgToType<parser.err>::type>> p;
                                                                                            ^
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

see-phit's People

Contributors

agauniyal avatar dumptyd avatar rep-movsd avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.