aboutsummaryrefslogtreecommitdiff
path: root/src/eval/list.zig
blob: 2ba136a6946f564aa35dfe5c0b81da1172ddfd07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const std = @import("std");
const Allocator = std.mem.Allocator;
const HTML = Element.HTML;
const Element = @import("Element.zig");

fn List(comptime tag: []const u8) type {
    return struct {
        content: std.ArrayList(Element),

        const Self = @This();

        pub fn init(alloc: Allocator) !*Self {
            const v = try alloc.create(Self);
            v.* = .{
                .content = try .initCapacity(alloc, 2),
            };
            return v;
        }

        pub fn element(self: *Self) Element {
            return .{ .ptr = self, .vtable = .{ .html = html } };
        }

        fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
            const self: *Self = @ptrCast(@alignCast(context));
            var el = try HTML.Content.init(alloc, tag);
            var root = try HTML.Root.init(alloc);
            el.content = root.element();
            for (self.content.items) |it| {
                var li = try HTML.Content.init(root.allocator(), "li");
                li.content = try it.html(root.allocator());
                root.append(li.element());
            }
            return el.element();
        }
    };
}

pub const Ordored = List("ol");
pub const Unordored = List("ul");