diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-30 17:56:55 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-30 17:56:55 +0200 |
| commit | a13df3ca580dc31544ef092d5d37e18089af3517 (patch) | |
| tree | cfcf8c1d8322ee8e97783e263105eac9f5c00f33 /src/eval/blocks.zig | |
| parent | 5ecd48d4b9f928beb4143f88e802ee4d9e25a2bd (diff) | |
refactor(element): generalize element creation
Diffstat (limited to 'src/eval/blocks.zig')
| -rw-r--r-- | src/eval/blocks.zig | 43 |
1 files changed, 8 insertions, 35 deletions
diff --git a/src/eval/blocks.zig b/src/eval/blocks.zig index 9357799..03b0d00 100644 --- a/src/eval/blocks.zig +++ b/src/eval/blocks.zig @@ -22,15 +22,7 @@ pub const Code = struct { } pub fn element(self: *Self) Element { - return .{ .ptr = self, .vtable = .{ - .html = html, - .node = getNode, - } }; - } - - fn getNode(context: *anyopaque) *Node { - const self: *Self = @ptrCast(@alignCast(context)); - return &self.node; + return Element.Wrapper(Self, html).init(self); } fn fromNode(context: *anyopaque) Element { @@ -38,8 +30,7 @@ pub const Code = struct { return self.element(); } - fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { - const self: *Self = @ptrCast(@alignCast(context)); + fn html(self: *Self, alloc: Allocator) HTML.Error!HTML { var el = try HTML.Content.init(alloc, "pre"); if (self.attribute) |attr| try el.base.setAttribute("data-code", attr); var code = try HTML.Content.init(alloc, "code"); @@ -69,12 +60,7 @@ pub const Figure = struct { } pub fn element(self: *Self) Element { - return .{ .ptr = self, .vtable = .{ .html = html, .node = getNode } }; - } - - fn getNode(context: *anyopaque) *Node { - const self: *Self = @ptrCast(@alignCast(context)); - return &self.node; + return Element.Wrapper(Self, html).init(self); } fn fromNode(context: *anyopaque) Element { @@ -82,8 +68,7 @@ pub const Figure = struct { return self.element(); } - fn html(context: *anyopaque, parent: Allocator) HTML.Error!HTML { - const self: *Self = @ptrCast(@alignCast(context)); + fn html(self: *Self, parent: Allocator) HTML.Error!HTML { var el = try HTML.Content.init(parent, "figure"); var root = try HTML.Root.init(parent); const alloc = root.allocator(); @@ -116,12 +101,7 @@ pub const Callout = struct { } pub fn element(self: *Self) Element { - return .{ .ptr = self, .vtable = .{ .html = html, .node = getNode } }; - } - - fn getNode(context: *anyopaque) *Node { - const self: *Self = @ptrCast(@alignCast(context)); - return &self.node; + return Element.Wrapper(Self, html).init(self); } fn fromNode(context: *anyopaque) Element { @@ -129,8 +109,7 @@ pub const Callout = struct { return self.element(); } - fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { - const self: *Self = @ptrCast(@alignCast(context)); + fn html(self: *Self, alloc: Allocator) HTML.Error!HTML { var el = try HTML.Content.init(alloc, "div"); try el.base.appendClass("callout"); const kind = self.kind orelse "default"; @@ -162,12 +141,7 @@ pub const Quote = struct { } pub fn element(self: *Self) Element { - return .{ .ptr = self, .vtable = .{ .html = html, .node = getNode } }; - } - - fn getNode(context: *anyopaque) *Node { - const self: *Self = @ptrCast(@alignCast(context)); - return &self.node; + return Element.Wrapper(Self, html).init(self); } fn fromNode(context: *anyopaque) Element { @@ -175,8 +149,7 @@ pub const Quote = struct { return self.element(); } - fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { - const self: *Self = @ptrCast(@alignCast(context)); + fn html(self: *Self, alloc: Allocator) HTML.Error!HTML { const quote = try HTML.Content.init(alloc, "blockquote"); quote.content = try self.content.html(alloc); return quote.element(); |
