diff options
Diffstat (limited to 'src/z')
| -rw-r--r-- | src/z/Component.zig | 20 | ||||
| -rw-r--r-- | src/z/html/Fragment.zig | 15 | ||||
| -rw-r--r-- | src/z/html/Tag.zig | 2 | ||||
| -rw-r--r-- | src/z/html/parser.zig | 173 | ||||
| -rw-r--r-- | src/z/html/root.zig | 2 |
5 files changed, 206 insertions, 6 deletions
diff --git a/src/z/Component.zig b/src/z/Component.zig index ba328b6..deab261 100644 --- a/src/z/Component.zig +++ b/src/z/Component.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const Fragment = @import("root.zig").html.Fragment; const Element = @import("root.zig").html.Element; const Attribute = @import("root.zig").html.Attribute; const Object = @import("Object.zig"); @@ -6,7 +7,7 @@ const Object = @import("Object.zig"); const Self = @This(); name: []const u8, -html: Element, +html: Fragment, css: []const u8, link: type, @@ -63,7 +64,9 @@ pub fn exportLinks(self: *const Self) void { }, ); - comptime self.exportFromHtml(self.html); + for (self.html.elements) |element| { + comptime self.exportFromHtml(element); + } } fn linkAttributes(self: *const Self, attributes: []const Attribute) []const Attribute { @@ -116,10 +119,17 @@ pub fn toElement(self: *const Self) []const Element { .node( "template", .{ .id = "@" ++ self.name }, - &.{ + [_]Element{ .node("style", .{}, &.{.text(self.css)}), - comptime self.linkHtmlRecursive(self.html), - }, + } ++ (html: { + var elements: []const Element = &.{}; + + for (self.html.elements) |element| { + elements = elements ++ .{self.linkHtmlRecursive(element)}; + } + + break :html elements; + }), ), .node( "script", diff --git a/src/z/html/Fragment.zig b/src/z/html/Fragment.zig new file mode 100644 index 0000000..d05ad4a --- /dev/null +++ b/src/z/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/z/html/Tag.zig b/src/z/html/Tag.zig index 56644ec..90b5641 100644 --- a/src/z/html/Tag.zig +++ b/src/z/html/Tag.zig @@ -13,7 +13,7 @@ pub const Content = union(enum) { }; name: []const u8, -attributes: []const Attribute = &.{}, +attributes: []const Attribute, content: Content, pub fn tag(name: []const u8, attributes: anytype, children: []const Element) Self { diff --git a/src/z/html/parser.zig b/src/z/html/parser.zig new file mode 100644 index 0000000..ee926f1 --- /dev/null +++ b/src/z/html/parser.zig @@ -0,0 +1,173 @@ +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 alphanumeric: []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 { + 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, alphanumeric) 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 = "" }; + } + + 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/z/html/root.zig b/src/z/html/root.zig index fe58e75..7bf91e8 100644 --- a/src/z/html/root.zig +++ b/src/z/html/root.zig @@ -2,3 +2,5 @@ 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"); |