aboutsummaryrefslogtreecommitdiff
path: root/examples/html/main.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-09-15 14:58:37 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-09-15 14:58:37 +0200
commit70670e129ef09acfb28be013d06f2773b5a7d04d (patch)
tree7114a0986c71c4d298799796c0056943c920795c /examples/html/main.c
parent07c59a83d1255e9772afd13299955bf710bbe988 (diff)
Add HTML serializer example
Diffstat (limited to 'examples/html/main.c')
-rw-r--r--examples/html/main.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/examples/html/main.c b/examples/html/main.c
new file mode 100644
index 0000000..c29e360
--- /dev/null
+++ b/examples/html/main.c
@@ -0,0 +1,59 @@
+#include <slweb/html.h>
+#include <dynstr.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+int main()
+{
+ int ret = EXIT_FAILURE;
+ struct dynstr d;
+ struct html_node *const html = html_node_alloc("html"), *head,
+ *meta, *body;
+ static const char text[] = "testing slweb";
+
+ dynstr_init(&d);
+
+ if (!html)
+ {
+ fprintf(stderr, "html_node_alloc_failed\n");
+ goto end;
+ }
+ else if (!(head = html_node_add_child(html, "head")))
+ {
+ fprintf(stderr, "html_node_add_child head failed\n");
+ goto end;
+ }
+ else if (!(meta = html_node_add_child(head, "meta")))
+ {
+ fprintf(stderr, "html_node_add_child meta failed\n");
+ goto end;
+ }
+ else if (html_node_add_attr(meta, "charset", "UTF-8"))
+ {
+ fprintf(stderr, "%s: html_node_add_attr charset failed\n", __func__);
+ goto end;
+ }
+ else if (!(body = html_node_add_child(html, "body")))
+ {
+ fprintf(stderr, "html_node_add_child failed\n");
+ goto end;
+ }
+ else if (html_node_set_value(body, text))
+ {
+ fprintf(stderr, "html_node_set_value failed\n");
+ goto end;
+ }
+ else if (html_serialize(html, &d))
+ {
+ fprintf(stderr, "html_serialize failed\n");
+ goto end;
+ }
+
+ printf("%s", d.str);
+ ret = EXIT_SUCCESS;
+
+end:
+ dynstr_free(&d);
+ html_node_free(html);
+ return ret;
+}