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("\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]; } }