diff --git a/CMakeLists.txt b/CMakeLists.txt index 28c6377..97d24d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.18) -project(hypertextcpp VERSION 1.1.0 DESCRIPTION "hypertextcpp") +project(hypertextcpp VERSION 1.2.0 DESCRIPTION "hypertextcpp") include(external/seal_lake) diff --git a/README.md b/README.md index 12414d1..52fcacd 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@
- +
-**hypertextcpp** - is a hyperfast HTML templating system for C++ applications. -It provides a highly readable `.htcpp` template file format and a command line utility that transpiles it to C++ HTML rendering code. Include a generated C++ header file in your project, then setup a build system to update it when necessary, and you're all set. +**hypertextcpp** is an HTML templating system for C++ applications. +It provides an .htcpp template file format and a command-line utility that transpiles it to C++ HTML rendering code. Include the generated C++ header file in your project, set up a build system to update it when necessary, and you're all set. A quick example: @@ -50,7 +50,7 @@ int main() ``` -Compile it with your preferred method, launch it, and you will get this output: + Compile it with your preferred method, launch it, and you will get this output: ```console kamchatka-volcano@home:~$ ./todolist_printer @@ -85,26 +85,25 @@ kamchatka-volcano@home:~$ ./todolist_printer ## Template file syntax ### Expressions -**$(**`c++ expression`**)** -Expressions are used to add application's data to the template. It can be any valid C++ expression, with only condition, that its result must be streamable to the default output stream `std::ostream`. Expressions can be placed anywhere in the HTML template, besides the tag names. -In our todolist example, `$(cfg.name)` is an expression adding template config variable `name` to the result page. +**$(**`c++ expression`**)** +Expressions are used to add the application's data to the template. It can be any valid C++ expression, with the only condition that its result must be streamable to the default output stream `std::ostream`. Expressions can be placed anywhere in the HTML template, except in tag names. +In our todolist example, `$(cfg.name)` is an expression that adds the template config variable `name` to the result page. ### Statements -**${**`c++ statement(s)`**}** -Statements are used to add any valid C++ code to the template rendering function. For example, you can add variables, classes declarations or lambdas. Let's say that you don't like the default name `cfg` used for passing data to the template. You can create a reference to it with any name you like and use it later: +**${**`c++ statement(s)`**}** +Statements are used to add any valid C++ code to the template rendering function. For example, you can add variables, class declarations, or lambdas. Let's say you don't like the default name `cfg` used for passing data to the template. You can create a reference to it with any name you like and use it later: ```html ${ auto& param = cfg;}Hello world!
@@ -170,7 +169,7 @@ and to the closing tag for the single-line ones:Hello world!
Last num is $(num)
``` -won't compile because the `num` variable isn't visible outside the `for` block scope generated by loop extension on the `div` tag. +won't compile because the `num` variable isn't visible outside the `for` block scope generated by the loop extension on the `div` tag. ### Sections **\[\[** `text, html elements, statements, expressions or other sections` **]]** -Sections can contain a part of template document, and it's possible to attach control flow extensions to them. Their main usage is adding attributes to HTML elements conditionally. +Sections can contain a part of the template document, and it's possible to attach control flow extensions to them. Their main usage is adding attributes to HTML elements conditionally. -Let's update todolist example by adding a line-through text style to completed tasks: +Let's update the todolist example by adding a line-through text style to completed tasks: [`examples/02/todolist.htcpp`](examples/02/todolist.htcpp) ```html @@ -220,9 +219,10 @@ Tip of the day: Keep your template tidy and don't introduce sections when it's p ### Procedures and partial rendering **#**`procedureName`**(){**`html elements, statements, expressions or sections`**}** -Parts of `htccp` template can be placed inside procedures - parameterless functions capturing the `cfg` variable. They are available for call from the C++ application, so if any part of the page needs to be rendered separately from the whole template, procedures are a big help. -Procedures can only be placed in the top level of `htcpp` template, outside any HTML element. -Let's put the list of tasks from the todolist example in the procedure: +Parts of the `htcpp` template can be placed inside procedures—parameterless functions capturing the `cfg` variable. They are available for call from the C++ application, so if any part of the page needs to be rendered separately from the whole template, procedures are a big help. +Procedures can only be placed at the top level of the `htcpp` template, outside any HTML element. +Let's put the list of tasks from the todolist example in the procedure: + [`examples/03/todolist.htcpp`](examples/03/todolist.htcpp) ```html #taskList(){ @@ -239,7 +239,7 @@ Let's put the list of tasks from the todolist example in the procedure: ``` -Now the tasks list can be output to stdout by itself like that: +Now the tasks list can be output to stdout by itself like this: [`examples/03/todolist_printer.cpp`](examples/03/todolist_printer.cpp) ```cpp //... @@ -278,8 +278,8 @@ Process finished with exit code 0 ``` ### Single header renderer -By default, the **hypertextcpp** transpiler works in a single header mode and generates a C++ header file that you're supposed to simply include in your project. A generated renderer class has a name of `.htcpp` template file. You can override the name by using `--class-name` parameter, or you can specify one of the following flags `--class-pascalcase`, `--class-snakecase` or `--class-lowercase` to use `.htcpp` template's filename converted to the corresponding case as a class name. -Converting template to C++ code each time you modify it is a laborious task, so it makes sense to add this step to your build process. Let's demonstrate on how to do it with CMake and at the same time rename the renderer class. +By default, the **hypertextcpp** transpiler works in a single header mode and generates a C++ header file that you're supposed to simply include in your project. A generated renderer class has the name of the `.htcpp` template file. You can override the name by using the `--class-name` parameter, or you can specify one of the following flags: `--class-pascalcase`, `--class-snakecase`, or `--class-lowercase` to use the `.htcpp` template's filename converted to the corresponding case as a class name. +Converting the template to C++ code each time you modify it is a laborious task, so it makes sense to add this step to your build process. Let's demonstrate how to do it with CMake while renaming the renderer class. [`examples/04/CMakeLists.txt`](examples/04/CMakeLists.txt) ``` @@ -301,11 +301,11 @@ target_compile_features(todolist_printer PUBLIC cxx_std_17) set_target_properties(todolist_printer PROPERTIES CXX_EXTENSIONS OFF) ``` -Now, everytime you change the template, the corresponding header will be regenerated on the next build. +Now, every time you change the template, the corresponding header will be regenerated on the next build. ### Shared library renderer -It can feel quite wasteful to rebuild your project each time the template file is changed, so **hypertextcpp** supports generation of C++ source file for building templates in form of shared libraries and linking them dynamically from your application. -It requires to duplicate the config declaration in `.htccp` template, registering it with `HTCPP_CONFIG` macro both in template and in the application source, generate the renderer code with `--shared-lib` command line flag, build the library and load it using the tiny API installed from the `shared_lib_api/` directory. It sounds scarier than it is, so let's quickly update the todolist example to see how it works. +It can feel quite wasteful to rebuild your project each time the template file is changed, so **hypertextcpp** supports the generation of a C++ source file for building templates in the form of shared libraries and linking them dynamically from your application. +It requires duplicating the config declaration in the .htcpp template, registering it with the `HTCPP_CONFIG` macro in both the template and the application source, generating the renderer code with the `--shared-lib` command line flag, building the library, and loading it using the tiny API installed from the `shared_lib_api/` directory. It sounds scarier than it is, so let's quickly update the todolist example to see how it works. First we need to copy the config structure declaration in the template: [`examples/05/todolist.htcpp`](examples/05/todolist.htcpp) @@ -337,35 +337,22 @@ First we need to copy the config structure declaration in the template: ``` -Be sure to use an exact copy, any mismatch of the config structure between template and application can't be handled gracefully, so if you try to load a template library with different structure you'll definitely crash the application and maybe hurt someone as a result. +Be sure to use an exact copy; any mismatch of the config structure between the template and the application can't be handled gracefully. So, if you try to load a template library with a different structure, you'll definitely crash the application and maybe hurt someone as a result. + +Next, we need to build our template renderer as a library. It's not possible to bundle multiple template files in one library, so we can build a library from a single `.htcpp` file by using the `hypertextcpp_BuildSharedTemplate` CMake function from `build_shared_template.cmake`: -Next, we need to build our template renderer as a library. It's not possible to bundle multiple templates file in one library, so we can easily use a generic CMake file, that builds a library from a single `.htcpp` file: -[`shared_template/CMakeLists.txt`](shared_template/CMakeLists.txt) ``` cmake_minimum_required(VERSION 3.18) -option(NAME "Template name") - -project(${NAME}) - -add_custom_command( - OUTPUT ${NAME}.cpp - COMMAND hypertextcpp ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.htcpp -o ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cpp -s - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.htcpp +hypertextcpp_BuildSharedTemplate( + NAME todolist + OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} ) - -add_library(${NAME} SHARED ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cpp) -target_compile_features(${NAME} PUBLIC cxx_std_17) -set_target_properties(${NAME} PROPERTIES CXX_EXTENSIONS OFF) +add_dependencies(${PROJECT_NAME} todolist) ``` -Copy `todolist.htcpp` in `shared_template/` and execute the following commands: -```console -kamchatka-volcano@home:~/shared_template$ cmake -S . -B build -DNAME=todolist -kamchatka-volcano@home:~/shared_template/build$ cmake --build build -``` -If everything goes right, you'll get the `libtodolist.so` in the build directory. That's our `todolist.htcpp` template compiled as the shared library. -Next, let's modify the `todolist_printer.cpp` to be able to load it: +If everything goes right, you'll get `libtodolist.so` in the build directory. That's our `todolist.htcpp` template compiled as a shared library. +Next, let's modify `todolist_printer.cpp` to be able to load it: [`examples/05/todolist_printer.cpp`](examples/05/todolist_printer.cpp) ```cpp #include