From cc3429220b19df8c5ca6abf28a02cd38d08de32d Mon Sep 17 00:00:00 2001 From: Vasco Date: Mon, 24 Aug 2026 17:47:30 +0100 Subject: Cool 0.0.0. Rend 1.0.4. --- Cool/demos/cool.html | 56 +++++++++++++++++++++++++++++++ Cool/demos/doc_generator | Bin 0 -> 28016 bytes Cool/demos/doc_generator.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ Cool/demos/view.cool | 9 +++++ Cool/demos/view.cool.c | 10 ++++++ 5 files changed, 155 insertions(+) create mode 100644 Cool/demos/cool.html create mode 100755 Cool/demos/doc_generator create mode 100644 Cool/demos/doc_generator.c create mode 100644 Cool/demos/view.cool create mode 100644 Cool/demos/view.cool.c (limited to 'Cool/demos') diff --git a/Cool/demos/cool.html b/Cool/demos/cool.html new file mode 100644 index 0000000..b75c841 --- /dev/null +++ b/Cool/demos/cool.html @@ -0,0 +1,56 @@ +
+

cool_sv_write

+ COOLDEF void cool_sv_write(Cool_StrView sv) +
+ Write string view to output. + +
+
+
+

cool_html

+ COOLDEF void cool_html(Cool_StrView sv) +
+ SAFELY write HTML to a buffer. Handles escaping malicious sequences. + +
+
+
+

cool_html_raw

+ COOLDEF void cool_html_raw(Cool_StrView sv) +
+ Writes HTML to buffer without escaping sequences. Vulnerable to XSS. Use with caution. + +
+
+
+

cool_html_raw_cstr

+ COOLDEF void cool_html_raw_cstr(const char *str, size_t len) +
+ Writes unsafe HTML using C strings. + +
+
+
+

cool_html_txt

+ COOLDEF void cool_html_txt(const char *str, size_t len) +
+ Meant for inner text. Escapes '<', '>' and '&'. + +
+
+
+

cool_htmlf

+ COOLDEF void cool_htmlf(const char *fmt, ...) +
+ SAFELY write formatted string to buffer. + +
+
+
+

cool_htmlf_raw

+ COOLDEF void cool_htmlf_raw(const char *fmt, ...) +
+ Writes formatting HTML string to buffer without escaping sequences. Vulnerable to XSS. Use with caution. + +
+
diff --git a/Cool/demos/doc_generator b/Cool/demos/doc_generator new file mode 100755 index 0000000..a41a10c Binary files /dev/null and b/Cool/demos/doc_generator differ diff --git a/Cool/demos/doc_generator.c b/Cool/demos/doc_generator.c new file mode 100644 index 0000000..5074cf6 --- /dev/null +++ b/Cool/demos/doc_generator.c @@ -0,0 +1,80 @@ +/* + * This simple demo illustrates how I use this library to generate my documentation. + * Could be used either statically or live because it's as simple as executing doc_generator + * with the given header file and reading from stdout. + */ + +#include +#include +#include +#define COOL_IMPLEMENTATION +#include "../cool.h" + +#include "view.cool.c" + +int parse_header_line(const char *line, char *func_name, char *func_decl, char **comment_out); + +int main(int argc, char **argv) { + + if (argc < 2) { + puts("Usage: doc_generator [FILE]"); + return 1; + } + + FILE *header_file = fopen(argv[1], "r"); + + char *line_buf = malloc(256); + size_t len = 0; + while (getline(&line_buf, &len, header_file) > 0) { + char func_name[256]; + char func_decl[256]; + char *comment_text = NULL; + + if (parse_header_line(line_buf, func_name, func_decl, &comment_text)) { + Api(func_name, func_decl, comment_text); + } + } + + free(line_buf); + + return 0; +} + +int parse_header_line(const char *line, char *func_name, char *func_decl, char **comment_out) { + char *comment_ptr = strstr(line, "//"); + if (!comment_ptr) return 0; + + *comment_out = comment_ptr + 2; + while (**comment_out == ' ' || **comment_out == '\t') { + (*comment_out)++; + } + + size_t decl_len = comment_ptr - line; + if (decl_len >= 256) decl_len = 255; + strncpy(func_decl, line, decl_len); + func_decl[decl_len] = '\0'; + + while (decl_len > 0 && (isspace((unsigned char)func_decl[decl_len - 1]) || func_decl[decl_len - 1] == ';')) { + func_decl[--decl_len] = '\0'; + } + + char *open_paren = strchr(func_decl, '('); + if (!open_paren) return 0; + + char *end_name = open_paren - 1; + while (end_name > func_decl && isspace((unsigned char)*end_name)) { + end_name--; + } + + char *start_name = end_name; + while (start_name > func_decl && (isalnum((unsigned char)start_name[-1]) || start_name[-1] == '_')) { + start_name--; + } + + size_t name_len = (end_name - start_name) + 1; + if (name_len >= 256) name_len = 255; + strncpy(func_name, start_name, name_len); + func_name[name_len] = '\0'; + + return 1; +} diff --git a/Cool/demos/view.cool b/Cool/demos/view.cool new file mode 100644 index 0000000..238cfaf --- /dev/null +++ b/Cool/demos/view.cool @@ -0,0 +1,9 @@ +COOL void Api(char *name, char *func, char *desc) { +
+

{ name }

+ { func } +
+ { desc } +
+
+} diff --git a/Cool/demos/view.cool.c b/Cool/demos/view.cool.c new file mode 100644 index 0000000..458072e --- /dev/null +++ b/Cool/demos/view.cool.c @@ -0,0 +1,10 @@ +void Api(char *name, char *func, char *desc) { + cool_html_raw(COOL_SV("
\n

")); + cool_html_txt(name, strlen(name)); + cool_html_raw(COOL_SV("

\n ")); + cool_html_txt(func, strlen(func)); + cool_html_raw(COOL_SV(" \n
\n ")); + cool_html_txt(desc, strlen(desc)); + cool_html_raw(COOL_SV("\n
\n
\n")); +} + -- cgit v1.3.1