aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/c.zig3
-rw-r--r--src/example.html5
-rw-r--r--src/main.zig61
3 files changed, 69 insertions, 0 deletions
diff --git a/src/c.zig b/src/c.zig
new file mode 100644
index 0000000..a6e8974
--- /dev/null
+++ b/src/c.zig
@@ -0,0 +1,3 @@
+pub usingnamespace @cImport({
+ @cInclude("HtmlTree.h");
+}); \ No newline at end of file
diff --git a/src/example.html b/src/example.html
new file mode 100644
index 0000000..51bc51a
--- /dev/null
+++ b/src/example.html
@@ -0,0 +1,5 @@
+<html>
+ <body>
+ <div class="cool">hello world</div>
+ </body>
+</html> \ No newline at end of file
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