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 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/zilch/html/Attribute.zig (limited to 'src/zilch/html/Attribute.zig') 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('"'); +} -- cgit v1.2.3-70-g09d2