Created
January 29, 2023 02:23
-
-
Save louisswarren/998b935b55c25016f0ca44bfe9ce02e7 to your computer and use it in GitHub Desktop.
HTML generation in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
static const char *closebuff[1024]; | |
static const char tabs[] = "\t\t\t\t\t\t\t\t"; | |
void | |
put(int *t, const char *msg) | |
{ | |
const char *indent = tabs + sizeof(tabs) - 1 - *t; | |
printf("%s%s\n", indent < tabs ? tabs : indent, msg); | |
} | |
void | |
push(int *t, const char *open, const char *close) | |
{ | |
put(t, open); | |
closebuff[(*t)++] = close; | |
} | |
void | |
pop(int *t) | |
{ | |
--(*t); | |
put(t, closebuff[*t]); | |
} | |
void | |
delegate(void (*f)(int *t), int *t) | |
{ | |
int s = *t; | |
f(&s); | |
while (s > *t) { | |
--s; | |
put(&s, closebuff[s]); | |
} | |
} | |
void | |
mk_head(int *t) | |
{ | |
put(t, "<title>Hello!</title>"); | |
} | |
void | |
mk_foot(int *t) | |
{ | |
put(t, "<hr>"); | |
push(t, "<span class=\"thanks\">", "</span>"); | |
put(t, "Thanks!"); | |
} | |
void | |
mk_body(int *t) | |
{ | |
push(t, "<main>", "</main>"); | |
push(t, "<p>", "</p>"); | |
put(t, "Hello"); | |
pop(t); | |
delegate(mk_foot, t); | |
} | |
void | |
mk_html(int *t) | |
{ | |
push(t, "<html>", "</html>"); | |
delegate(mk_head, t); | |
delegate(mk_body, t); | |
} | |
int | |
main(void) | |
{ | |
int t = 0; | |
delegate(mk_html, &t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure if I actually like this kind of thing, even if it were implemented better