aboutsummaryrefslogtreecommitdiff
path: root/src/zeroscript/html
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-09 14:01:20 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-09 14:01:20 +0200
commit32a251aa9645de070727a6ec91490d3e9219ec7c (patch)
tree5dd55205c5df68fa449e766ec2a8d248e21bd2be /src/zeroscript/html
parent09e8d4548361ece6151b914709c39e254eba65e3 (diff)
rename z framework to zeroscript
Diffstat (limited to 'src/zeroscript/html')
-rw-r--r--src/zeroscript/html/Attribute.zig45
-rw-r--r--src/zeroscript/html/Document.zig11
-rw-r--r--src/zeroscript/html/Fragment.zig15
-rw-r--r--src/zeroscript/html/Tag.zig53
-rw-r--r--src/zeroscript/html/element.zig30
-rw-r--r--src/zeroscript/html/parser.zig174
-rw-r--r--src/zeroscript/html/root.zig6
7 files changed, 334 insertions, 0 deletions
diff --git a/src/zeroscript/html/Attribute.zig b/src/zeroscript/html/Attribute.zig
new file mode 100644
index 0000000..1db49a0
--- /dev/null
+++ b/src/zeroscript/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("&quot;");
+ value = value[index + 1..];
+ }
+
+ try writer.writeAll(value);
+ try writer.writeByte('"');
+}
diff --git a/src/zeroscript/html/Document.zig b/src/zeroscript/html/Document.zig
new file mode 100644
index 0000000..b1122e1
--- /dev/null
+++ b/src/zeroscript/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("<!DOCTYPE html>{f}", .{self.root});
+}
diff --git a/src/zeroscript/html/Fragment.zig b/src/zeroscript/html/Fragment.zig
new file mode 100644
index 0000000..d05ad4a
--- /dev/null
+++ b/src/zeroscript/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/zeroscript/html/Tag.zig b/src/zeroscript/html/Tag.zig
new file mode 100644
index 0000000..90b5641
--- /dev/null
+++ b/src/zeroscript/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("</{s}>", .{self.name});
+ },
+ .void => {},
+ }
+}
diff --git a/src/zeroscript/html/element.zig b/src/zeroscript/html/element.zig
new file mode 100644
index 0000000..739eb0b
--- /dev/null
+++ b/src/zeroscript/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/zeroscript/html/parser.zig b/src/zeroscript/html/parser.zig
new file mode 100644
index 0000000..38b28bb
--- /dev/null
+++ b/src/zeroscript/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("</{s}>", .{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/zeroscript/html/root.zig b/src/zeroscript/html/root.zig
new file mode 100644
index 0000000..7bf91e8
--- /dev/null
+++ b/src/zeroscript/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");