From 95428c41f0ee3ac108cf1a4acfaa67157ad954dc Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 24 May 2026 18:00:38 +0800 Subject: Build DOM. --- main.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index 00a184a..faeb618 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,41 @@ #include "mem.h" #include "dom.h" +#include "tags.h" + +static void node_print(const struct node *n, const char *prefix, int last) +{ + const char *connector = last ? "└── " : "├── "; + const char *extension = last ? " " : "│ "; + char new_prefix[256]; + struct node *c; + + printf("%s%s", prefix, connector); + + if (n->tag == TAG_TEXT) + printf("%.*s\n", (int)n->textlen, n->text); + else + printf("%s\n", tag_to_str(n->tag)); + + snprintf(new_prefix, sizeof(new_prefix), "%s%s", prefix, extension); + + for (c = n->first_child; c; c = c->next_sibling) + node_print(c, new_prefix, c->next_sibling == NULL); +} + +void dom_print(const struct node *root) +{ + struct node *c; + + printf("%s\n", tag_to_str(root->tag)); + for (c = root->first_child; c; c = c->next_sibling) + node_print(c, "", c->next_sibling == NULL); +} int main(int argc, char *argv[]) { + struct node *root; + if (argc < 2) errx(1, "usage: glacier "); @@ -27,8 +59,10 @@ int main(int argc, char *argv[]) html[len] = '\0'; fclose(file); - init_dom(html); + root = dom_init(html); + dom_print(root); + dom_free(); free(html); return 0; -- cgit v1.2.3