diff options
Diffstat (limited to 'Cool/demos')
| -rw-r--r-- | Cool/demos/cool.html | 56 | ||||
| -rwxr-xr-x | Cool/demos/doc_generator | bin | 0 -> 28016 bytes | |||
| -rw-r--r-- | Cool/demos/doc_generator.c | 80 | ||||
| -rw-r--r-- | Cool/demos/view.cool | 9 | ||||
| -rw-r--r-- | Cool/demos/view.cool.c | 10 |
5 files changed, 155 insertions, 0 deletions
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 @@ + <div class="api-card"> + <h3 class="api-name">cool_sv_write</h3> + <code class="language-c">COOLDEF void cool_sv_write(Cool_StrView sv) </code> + <div class="api-description"> + Write string view to output. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_html</h3> + <code class="language-c">COOLDEF void cool_html(Cool_StrView sv) </code> + <div class="api-description"> + SAFELY write HTML to a buffer. Handles escaping malicious sequences. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_html_raw</h3> + <code class="language-c">COOLDEF void cool_html_raw(Cool_StrView sv) </code> + <div class="api-description"> + Writes HTML to buffer without escaping sequences. Vulnerable to XSS. Use with caution. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_html_raw_cstr</h3> + <code class="language-c">COOLDEF void cool_html_raw_cstr(const char *str, size_t len) </code> + <div class="api-description"> + Writes unsafe HTML using C strings. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_html_txt</h3> + <code class="language-c">COOLDEF void cool_html_txt(const char *str, size_t len) </code> + <div class="api-description"> + Meant for inner text. Escapes '<', '>' and '&'. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_htmlf</h3> + <code class="language-c">COOLDEF void cool_htmlf(const char *fmt, ...) </code> + <div class="api-description"> + SAFELY write formatted string to buffer. + + </div> + </div> + <div class="api-card"> + <h3 class="api-name">cool_htmlf_raw</h3> + <code class="language-c">COOLDEF void cool_htmlf_raw(const char *fmt, ...) </code> + <div class="api-description"> + Writes formatting HTML string to buffer without escaping sequences. Vulnerable to XSS. Use with caution. + + </div> + </div> diff --git a/Cool/demos/doc_generator b/Cool/demos/doc_generator Binary files differnew file mode 100755 index 0000000..a41a10c --- /dev/null +++ b/Cool/demos/doc_generator 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 <stdio.h> +#include <string.h> +#include <ctype.h> +#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) { + <div class="api-card"> + <h3 class="api-name">{ name }</h3> + <code class="language-c">{ func } </code> + <div class="api-description"> + { desc } + </div> + </div> +} 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(" <div class=\"api-card\">\n <h3 class=\"api-name\">")); + cool_html_txt(name, strlen(name)); + cool_html_raw(COOL_SV("</h3>\n <code class=\"language-c\">")); + cool_html_txt(func, strlen(func)); + cool_html_raw(COOL_SV(" </code>\n <div class=\"api-description\">\n ")); + cool_html_txt(desc, strlen(desc)); + cool_html_raw(COOL_SV("\n </div>\n </div>\n")); +} + |
