Static template generator for C
git clone git://soophie.de/ductape
blob: cbcf79c71f7db8944b2836e36f0ca59862cd8807 (3532 bytes)[doc][blame][raw]
1
# ductape3
Static template generator for C5
_ductape_ is static template generator with dynamic linking for C.7
```graph8
+---------+ (dlopen)9
+---> | .so | ---------------------- load ------------------------+10
+---------+ (ductape) | +---------+ |11
| .html | --- compile ---+ v12
+---------+ | +---------+ +----------+ (cc) +----------+13
+---> | .h | --- include ---> | main.c | --- compile ---> | main |14
+---------+ +----------+ +----------+15
```17
## Installation19
```20
git clone git@soophie.de:/srv/git/ductape21
cd ductape22
sudo make install23
```25
## Quick Start27
Create a `macro.h` file which needs to define `DUC_OUT` for storing the handle the content is later written to and `DUC_WRITE` which handles consuming and writing onto the handle.29
The file can be extended with custom macros (like `STRING`, `NUMBER`, `MARKDOWN`, etc.), to simplify writing more complex templates.31
```c32
#define DUC_OUT ((str_t *) out) // cast custom handle33
#define DUC_WRITE($str) { str_append(DUC_OUT, $str); } // define custom write function35
#define STRING DUC_WRITE36
```38
The template `example.html` is defined (could be any kind of language), which includes the previously defined macro header file, followed by the start of the template definition `DUC_TEMPLDEF` with the first parameter being the UID and succeeding with varies parameters which allow for injecting dynamic data. The end of the template is marked with `DUC_ENDTEMPL`. Inside the template definition. the content is rendered, while content outside will be ignored.40
The symbol `%` marks the delimiter between template and C code. In order to use the delimiter in the template it needs to be repeated twice to escape the character. All parameters passed into the template are bound to the `ctx` variable.42
```html43
% #include "macro.h" %45
% DUC_TEMPLDEF(46
example,47
char *message;48
) %50
<p>% STRING(ctx->message) %</p>52
% DUC_ENDTEMPL %53
```55
The template can be compiled with the the installed CLI command `ductape`. Simply pass a list of files to the command and it will generate the corresponding `.h` (header) and `.so` (shared library) files based on the specified UID of the template.57
```58
ductape example.html59
```61
Finally, a main function can be defined which imports the compiled `example.h` header file, which contains everything needed to dynamically render the template, such as the `example_t` context structure passed into the template, as well as the `example_render` function which renders the template into the specified `out` handle.63
```c64
#include "example.h"66
int main(void) {67
str_t out = STR_NULL;68
example_t ctx = {69
.message = "It works!",70
};71
int ret = example_render(&out, &ctx);72
if (ret == 0) {73
printf("%s", (char *) str_get(&out))74
str_free(&out);75
}76
return ret;77
}78
```80
## License82
This project is licensed under the **GNU General Public License v3.0 (GPLv3)**.84
**What this means:** You are free to use, study, and modify this framework. However, if you distribute a program that uses this framework (including selling it), you **must** provide the full source code of your program under the same GPLv3 license.86
See the [LICENSE](?license) file for the full legal text.