From 0b2af471b6d5dfc4eaf53639102c1b854bbb324b Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 12 Oct 2026 09:43:19 +0200 Subject: first version of zilch --- src/zilch/html/Attribute.zig | 45 +++++++++++ src/zilch/html/Document.zig | 11 +++ src/zilch/html/Fragment.zig | 15 ++++ src/zilch/html/Tag.zig | 53 +++++++++++++ src/zilch/html/element.zig | 30 ++++++++ src/zilch/html/parser.zig | 174 +++++++++++++++++++++++++++++++++++++++++++ src/zilch/html/root.zig | 6 ++ 7 files changed, 334 insertions(+) create mode 100644 src/zilch/html/Attribute.zig create mode 100644 src/zilch/html/Document.zig create mode 100644 src/zilch/html/Fragment.zig create mode 100644 src/zilch/html/Tag.zig create mode 100644 src/zilch/html/element.zig create mode 100644 src/zilch/html/parser.zig create mode 100644 src/zilch/html/root.zig (limited to 'src/zilch/html') diff --git a/src/zilch/html/Attribute.zig b/src/zilch/html/Attribute.zig new file mode 100644 index 0000000..1db49a0 --- /dev/null +++ b/src/zilch/html/Attribute.zig @@ -0,0 +1,45 @@ +const std = @import("std"); + +const Self = @This(); + +name: []const u8, +value: []const u8, + +pub fn from(attributes: anytype) []const Self { + const T = @TypeOf(attributes); + const info = @typeInfo(T); + + if (info != .@"struct") { + @compileError("struct needed"); + } + + const fields = info.@"struct".fields; + + var attrs: []const Self = &.{}; + + for (fields) |field| { + const name = field.name; + const value = @field(attributes, name); + + const next_attr: []const Self = &.{.{ .name = name, .value = value }}; + attrs = attrs ++ next_attr; + } + + return attrs; +} + +pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try writer.writeAll(self.name); + try writer.writeAll("=\""); + + var value: []const u8 = self.value; + + while (std.mem.findScalar(u8, value, '"')) |index| { + try writer.writeAll(value[0..index]); + try writer.writeAll("""); + value = value[index + 1..]; + } + + try writer.writeAll(value); + try writer.writeByte('"'); +} diff --git a/src/zilch/html/Document.zig b/src/zilch/html/Document.zig new file mode 100644 index 0000000..b1122e1 --- /dev/null +++ b/src/zilch/html/Document.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const Element = @import("root.zig").Element; + +const Self = @This(); + +root: Element, + +pub fn format(self: Self, writer: *std.Io.Writer) !void { + try writer.print("{f}", .{self.root}); +} diff --git a/src/zilch/html/Fragment.zig b/src/zilch/html/Fragment.zig new file mode 100644 index 0000000..d05ad4a --- /dev/null +++ b/src/zilch/html/Fragment.zig @@ -0,0 +1,15 @@ +const std = @import("std"); +const mod = @import("root.zig"); +const Element = mod.Element; + +const Self = @This(); + +elements: []const Element, + +pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + for (self.elements) |element| { + try element.format(writer); + } +} + +pub const parse = mod.parser.fragment; diff --git a/src/zilch/html/Tag.zig b/src/zilch/html/Tag.zig new file mode 100644 index 0000000..90b5641 --- /dev/null +++ b/src/zilch/html/Tag.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const ctutils = @import("ctutils"); + +const mod = @import("root.zig"); +const Attribute = mod.Attribute; +const Element = mod.Element; + +const Self = @This(); + +pub const Content = union(enum) { + children: []const Element, + void: void, +}; + +name: []const u8, +attributes: []const Attribute, +content: Content, + +pub fn tag(name: []const u8, attributes: anytype, children: []const Element) Self { + return .{ + .name = name, + .attributes = Attribute.from(attributes), + .content = .{ .children = children }, + }; +} + +pub fn @"void"(name: []const u8, attributes: anytype) Self { + return .{ + .name = name, + .attributes = Attribute.from(attributes), + .content = .void, + }; +} + +pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try writer.writeByte('<'); + try writer.writeAll(self.name); + + for (self.attributes) |attribute| { + try writer.writeByte(' '); + try attribute.format(writer); + } + + try writer.writeByte('>'); + + switch (self.content) { + .children => |children| { + for (children) |child| try child.format(writer); + try writer.print("", .{self.name}); + }, + .void => {}, + } +} diff --git a/src/zilch/html/element.zig b/src/zilch/html/element.zig new file mode 100644 index 0000000..739eb0b --- /dev/null +++ b/src/zilch/html/element.zig @@ -0,0 +1,30 @@ +const std = @import("std"); +const mod = @import("root.zig"); +const Attribute = mod.Attribute; +const Tag = mod.Tag; + +pub const Element = union(enum) { + const Self = @This(); + + tag: Tag, + string: []const u8, + + pub fn node(name: []const u8, attributes: anytype, children: []const Element) Self { + return .{ .tag = .tag(name, attributes, children) }; + } + + pub fn @"void"(name: []const u8, attribute: anytype) Self { + return .{ .tag = .void(name, attribute) }; + } + + pub fn text(content: []const u8) Self { + return .{ .string = content }; + } + + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try switch (self.*) { + .string => |content| writer.writeAll(content), + .tag => |n| n.format(writer), + }; + } +}; diff --git a/src/zilch/html/parser.zig b/src/zilch/html/parser.zig new file mode 100644 index 0000000..38b28bb --- /dev/null +++ b/src/zilch/html/parser.zig @@ -0,0 +1,174 @@ +const std = @import("std"); +const mod = @import("root.zig"); +const Element = mod.Element; +const Attribute = mod.Attribute; +const Tag = mod.Tag; +const Fragment = mod.Fragment; + +const tag_name: []const u8 = std.ascii.letters ++ .{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' }; +const void_elements: std.StaticStringMap(void) = .initComptime(.{ + .{"area"}, + .{"base"}, + .{"br"}, + .{"col"}, + .{"embed"}, + .{"hr"}, + .{"img"}, + .{"input"}, + .{"link"}, + .{"meta"}, + .{"param"}, + .{"source"}, + .{"track"}, + .{"wbr"}, +}); + +pub fn fragment(buffer: []const u8) Fragment { + @setEvalBranchQuota(buffer.len * 1000); + var elements: []const Element = &.{}; + var buf = buffer; + + while (buf.len > 0) { + const e, buf = element(buf); + elements = elements ++ .{e}; + } + + return .{ .elements = elements }; +} + +pub fn element(buffer: []const u8) struct { Element, []const u8 } { + if (buffer.len == 0) @panic("EOB, expected element"); + + switch (buffer[0]) { + '<' => { + const t, const b = tag(buffer); + return .{ .{ .tag = t }, b }; + }, + else => { + const t, const b = text(buffer); + return .{ .{ .string = t }, b }; + }, + } +} + +pub fn tag(buffer: []const u8) struct { Tag, []const u8 } { + if (buffer.len == 0) @panic("EOB, expected tag"); + + if (buffer[0] != '<') @panic("expected '<'"); + + var buf: []const u8 = buffer[1..]; + + const name = name: { + const index = std.mem.findNone(u8, buf, tag_name) orelse @panic("uncosed tag"); + defer buf = buf[index..]; + break :name buf[0..index]; + }; + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + var attributes: []const Attribute = &.{}; + + while (buf.len > 0 and buf[0] != '>') { + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + const attr, buf = attribute(buf); + attributes = attributes ++ .{attr}; + } + + if (buf.len == 0 or buf[0] != '>') @panic("expected tag-close"); + + buf = buf[1..]; + + if (void_elements.has(name)) { + return .{.{ + .name = name, + .attributes = attributes, + .content = .void, + }}; + } + + var children: []const Element = &.{}; + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + const closing = std.fmt.comptimePrint("", .{name}); + + while (buf.len > 0 and !std.mem.startsWith(u8, buf, closing)) { + const child, buf = element(buf); + children = children ++ .{child}; + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + } + + if (!std.mem.startsWith(u8, buf, closing)) @panic("expected closing-tag"); + + buf = buf[closing.len..]; + + return .{ + .{ + .name = name, + .attributes = attributes, + .content = .{ .children = children }, + }, + buf, + }; +} + +pub fn text(buffer: []const u8) struct { []const u8, []const u8 } { + const index = std.mem.findScalar(u8, buffer, '<') orelse buffer.len; + return .{ + std.mem.trim(u8, buffer[0..index], &std.ascii.whitespace), + buffer[index..], + }; +} + +pub fn attribute(buffer: []const u8) struct { Attribute, []const u8 } { + var buf = buffer; + + const name = name: { + const index = std.mem.findAny( + u8, + buf, + ">=\"'" ++ std.ascii.whitespace, + ) orelse @panic("EOB, expected attr-value"); + defer buf = buf[index..]; + break :name buf[0..index]; + }; + + if (name.len == 0) @panic("expected attr-name"); + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + if (buf.len == 0 or buf[0] == '>') { + return .{ .{ .name = name, .value = "" }, buf }; + } + + if (buf[0] != '=') @panic("expected attr-eql"); + + buf = std.mem.trimStart(u8, buf[1..], &std.ascii.whitespace); + + const delim = switch (buf[0]) { + '"' => "\"", + '\'' => "'", + else => std.ascii.whitespace ++ "=\"'<>`", + }; + + if (delim.len == 1) { + buf = buf[1..]; + } + + const value = value: { + const index = std.mem.findAny(u8, buf, delim) orelse if (delim.len == 1) + @panic("unclosed attr-value") + else + buf.len; + defer buf = buf[index + @intFromBool(delim.len == 1) ..]; + break :value buf[0..index]; + }; + + return .{ + .{ + .name = name, + .value = value, + }, + buf, + }; +} diff --git a/src/zilch/html/root.zig b/src/zilch/html/root.zig new file mode 100644 index 0000000..7bf91e8 --- /dev/null +++ b/src/zilch/html/root.zig @@ -0,0 +1,6 @@ +pub const Attribute = @import("Attribute.zig"); +pub const Tag = @import("Tag.zig"); +pub const Element = @import("element.zig").Element; +pub const Document = @import("Document.zig"); +pub const Fragment = @import("Fragment.zig"); +pub const parser = @import("parser.zig"); -- cgit v1.2.3-70-g09d2