Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Created January 29, 2023 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisswarren/998b935b55c25016f0ca44bfe9ce02e7 to your computer and use it in GitHub Desktop.
Save louisswarren/998b935b55c25016f0ca44bfe9ce02e7 to your computer and use it in GitHub Desktop.
HTML generation in C
#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);
}
@louisswarren
Copy link
Author

Not sure if I actually like this kind of thing, even if it were implemented better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment