diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 13:52:25 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 13:52:25 +0200 |
| commit | ec30b659dfd337f5edf512ea78032fe18251b704 (patch) | |
| tree | 8fe9cfd4b965c7ffbac7f190681b6ac5ac9f8ae7 /src/z/html | |
| parent | 0a2f6f8cc48258fca60df2f193790b2d94a9d0f6 (diff) | |
refactor html module
Diffstat (limited to 'src/z/html')
| -rw-r--r-- | src/z/html/Attribute.zig | 45 | ||||
| -rw-r--r-- | src/z/html/Document.zig | 11 | ||||
| -rw-r--r-- | src/z/html/Tag.zig | 53 | ||||
| -rw-r--r-- | src/z/html/element.zig | 30 | ||||
| -rw-r--r-- | src/z/html/root.zig | 4 |
5 files changed, 143 insertions, 0 deletions
diff --git a/src/z/html/Attribute.zig b/src/z/html/Attribute.zig new file mode 100644 index 0000000..1db49a0 --- /dev/null +++ b/src/z/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/z/html/Document.zig b/src/z/html/Document.zig new file mode 100644 index 0000000..b1122e1 --- /dev/null +++ b/src/z/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/z/html/Tag.zig b/src/z/html/Tag.zig new file mode 100644 index 0000000..56644ec --- /dev/null +++ b/src/z/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/z/html/element.zig b/src/z/html/element.zig new file mode 100644 index 0000000..739eb0b --- /dev/null +++ b/src/z/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/z/html/root.zig b/src/z/html/root.zig new file mode 100644 index 0000000..fe58e75 --- /dev/null +++ b/src/z/html/root.zig @@ -0,0 +1,4 @@ +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"); |