aboutsummaryrefslogtreecommitdiff
path: root/src/eval/Title.zig
blob: 1841ce70d10b539a821d2617defd873272ba39ec (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const HTML = Parent.HTML;
const Parent = @import("Element.zig");
const Node = Parent.Node;

level: u3,
content: Parent,
node: Node = .{
    .ptr = undefined,
    .vtable = .{ .element = fromNode },
},

const Self = @This();

pub fn init(alloc: Allocator, level: u3, content: Parent) !*Self {
    const v = try alloc.create(Self);
    v.* = .{ .level = level, .content = content };
    v.node.ptr = v;
    return v;
}

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

fn getNode(context: *anyopaque) *Node {
    const self: *Self = @ptrCast(@alignCast(context));
    return &self.node;
}

fn fromNode(context: *anyopaque) Parent {
    const self: *Self = @ptrCast(@alignCast(context));
    return self.element();
}

fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
    const self: *Self = @ptrCast(@alignCast(context));
    var el = try HTML.Content.init(alloc, switch (self.level) {
        1 => "h1",
        2 => "h2",
        3 => "h3",
        4 => "h4",
        5 => "h5",
        6 => "h6",
        else => unreachable,
    });
    el.content = try self.content.html(alloc);
    return el.element();
}