diff options
author | dec05eba <dec05eba@protonmail.com> | 2023-08-19 14:04:31 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2023-08-19 14:04:31 +0200 |
commit | a3f01eb7d4b2050d81c5b76fe9e3c069a84cf17c (patch) | |
tree | e3fcac5bd7d1d017dbf604bcd94d44ffebbc1457 /src/main.zig |
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..66a762e --- /dev/null +++ b/src/main.zig @@ -0,0 +1,61 @@ +const std = @import("std"); +const c = @import("c.zig"); + +const example = @embedFile("example.html"); + +pub fn main() !void { + var html_tree: c.HtmlTree = undefined; + if(c.html_parse_to_tree(&html_tree, example.ptr, example.len) != 0) + return error.FailedToParseHtml; + defer c.html_tree_deinit(&html_tree); + html_node_print(&html_tree.root_node); + + const html = html_tree.root_node.first_child; + const body = html.*.node.first_child; + const div = c.html_node_find_child(&body.*.node, "div", "class", "cool") orelse error.UnableToFindHtmlNode; + std.debug.print("div {any}\n", .{div}); +} + +fn html_attributes_print(attr: [*c]c.HtmlAttribute) void { + var a = attr; + while(a != null) { + std.debug.print("{s}=\"{s}\" ", .{html_string_view_to_slice(a.*.key), html_string_view_to_slice(a.*.value)}); + a = a.*.next; + } +} + +fn html_node_child_print(node_child: [*c]c.HtmlNodeChild) void { + var node = node_child; + while(node != null) { + html_node_print(&node.*.node); + node = node.*.next; + } +} + +fn html_node_print(node: [*c]c.HtmlNode) void { + const n: *c.HtmlNode = @ptrCast(node); + switch(n.node_type) { + c.HTML_NODE_NODE => { + std.debug.print("\n<{s} ", .{html_string_view_to_slice(n.name_or_value)}); + html_attributes_print(n.first_attr); + std.debug.print(">", .{}); + html_node_child_print(n.first_child); + std.debug.print("</{s}>\n", .{html_string_view_to_slice(n.name_or_value)}); + }, + c.HTML_NODE_TEXT => { + std.debug.print("{s}", .{html_string_view_to_slice(n.name_or_value)}); + }, + c.HTML_NODE_JS => { + std.debug.print("{s}", .{html_string_view_to_slice(n.name_or_value)}); + }, + else => {}, + } +} + +fn html_string_view_to_slice(view: c.HtmlStringView) []const u8 { + if(view.size == 0) { + return ""; + } else { + return view.data[0..view.size]; + } +}
\ No newline at end of file |