aboutsummaryrefslogtreecommitdiff
path: root/src/eval/blocks.zig
blob: 63a8f121f589963de3548c6fa92815847b7a4adf (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const std = @import("std");
const Allocator = std.mem.Allocator;
const HTML = @import("html/Element.zig");
const Element = @import("Element.zig");

pub const Code = struct {
    content: std.ArrayList(Element),
    attribute: ?[]const u8 = null,

    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 = .{ .deinit = destroy, .html = Self.html } };
    }

    pub fn deinit(self: *Self, alloc: Allocator) void {
        destroy(self, alloc);
    }

    fn destroy(context: *anyopaque, alloc: Allocator) void {
        var self: *Self = @ptrCast(@alignCast(context));
        for (self.content.items) |it| it.deinit(alloc);
        self.content.deinit(alloc);
        alloc.destroy(self);
    }

    fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
        const self: *Self = @ptrCast(@alignCast(context));
        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");
        for (self.content.items) |it| try code.append(try it.html(alloc));
        try el.append(code.element());
        return el.element();
    }
};

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.Content.init(alloc, "figure");
        try el.append(try self.content.html(alloc));
        const caption = self.caption orelse return el.element();
        var figcap = try HTML.Content.init(alloc, "figcaption");
        try figcap.append(try caption.html(alloc));
        try el.append(figcap.element());
        return el.element();
    }
};