From a3f01eb7d4b2050d81c5b76fe9e3c069a84cf17c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 19 Aug 2023 14:04:31 +0200 Subject: Initial commit --- .gitignore | 2 ++ .gitmodules | 3 +++ LICENSE | 19 +++++++++++++++++ README.md | 1 + build.zig | 47 ++++++++++++++++++++++++++++++++++++++++++ depends/html-tree | 1 + src/c.zig | 3 +++ src/example.html | 5 +++++ src/main.zig | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 142 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.zig create mode 160000 depends/html-tree create mode 100644 src/c.zig create mode 100644 src/example.html create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e73c965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache/ +zig-out/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..adea1a1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "depends/html-tree"] + path = depends/html-tree + url = https://repo.dec05eba.com/html-tree diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bc8b574 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) dec05eba + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..507f780 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Example of using [html-tree](https://git.dec05eba.com/html-tree/about/) in zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..5ecfc3d --- /dev/null +++ b/build.zig @@ -0,0 +1,47 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "html-tree-zig-example", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const libHtmlTree = buildHtmlTree(b, &target, optimize); + exe.linkLibrary(libHtmlTree); + exe.linkLibC(); + exe.addIncludePath(.{ .path = "depends/html-tree/depends/html-parser/include" }); + exe.addIncludePath(.{ .path = "depends/html-tree/include" }); + + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} + +fn buildHtmlTree(b: *std.Build, target: *const std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile { + const lib = b.addStaticLibrary(.{ + .name = "html-tree", + .target = target.*, + .optimize = optimize, + }); + + lib.addCSourceFiles(&.{ + "depends/html-tree/depends/html-parser/src/HtmlParser.c", + "depends/html-tree/src/HtmlTree.c", + }, &.{}); + + lib.addIncludePath(.{ .path = "depends/html-tree/depends/html-parser/include" }); + lib.linkLibC(); + return lib; +} \ No newline at end of file diff --git a/depends/html-tree b/depends/html-tree new file mode 160000 index 0000000..fab7a00 --- /dev/null +++ b/depends/html-tree @@ -0,0 +1 @@ +Subproject commit fab7a00da328debef4fbeba9eb21fc02cf21675d 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 @@ + + +
hello world
+ + \ 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("\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 -- cgit v1.2.3