diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-26 22:29:40 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-26 22:29:40 +0200 |
| commit | ae6ee68d6f4ef79fef609b4d09b543fc06326e95 (patch) | |
| tree | 04f7059b0ed5bb1323280ce9c910f8f154af5eac /src/eval/blocks.zig | |
| parent | 1ce415538f202bf1319ebb0ff6e76aa54f983343 (diff) | |
refactor(eval): generalize figure
Diffstat (limited to 'src/eval/blocks.zig')
| -rw-r--r-- | src/eval/blocks.zig | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/eval/blocks.zig b/src/eval/blocks.zig new file mode 100644 index 0000000..9ad10ba --- /dev/null +++ b/src/eval/blocks.zig @@ -0,0 +1,47 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const HTML = @import("html/Element.zig"); +const Element = @import("Element.zig"); + +pub const Code = Element.Simple("pre"); + +pub const Figure = struct { + content: Element, + caption: ?Element = null, + + const Self = @This(); + + pub fn init(alloc: Allocator, content: Element) !*Self { + const v = try alloc.create(Self); + v.* = .{ .content = content }; + return v; + } + + pub fn element(self: *Self) Element { + return .{ .ptr = self, .vtable = .{ .deinit = destroy, .html = Self.html } }; + } + + pub fn deinit(self: *Self, alloc: Allocator) void { + destroy(self, alloc); + } + + fn destroy(context: *anyopaque, alloc: Allocator) void { + const self: *Self = @ptrCast(@alignCast(context)); + self.content.deinit(alloc); + if (self.caption) |cap| cap.deinit(alloc); + alloc.destroy(self); + } + + fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { + const self: *Self = @ptrCast(@alignCast(context)); + var el = try HTML.init(alloc, .content, "figure"); + errdefer el.deinit(); + try el.appendContent(try self.content.html(alloc)); + const caption = self.caption orelse return el; + var figcap = try HTML.init(alloc, .content, "figcaption"); + errdefer figcap.deinit(); + try figcap.appendContent(try caption.html(alloc)); + try el.appendContent(figcap); + return el; + } +}; |
