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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const HTML = @import("html/Element.zig");
const Element = @import("Element.zig");
const Node = Element.Node;
pub const Code = struct {
content: std.ArrayList(Element),
attribute: ?[]const u8 = null,
node: Node = .{
.ptr = undefined,
.vtable = .{ .element = fromNode },
},
const Self = @This();
pub fn init(alloc: Allocator) !*Self {
const v = try alloc.create(Self);
v.* = .{ .content = try .initCapacity(alloc, 2) };
v.node.ptr = v;
return v;
}
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;
}
fn fromNode(context: *anyopaque) Element {
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, "pre");
if (self.attribute) |attr| try el.base.setAttribute("data-code", attr);
var code = try HTML.Content.init(alloc, "code");
var root = try HTML.Root.init(alloc);
for (self.content.items) |it| root.append(try it.html(root.allocator()));
code.content = root.element();
el.content = code.element();
return el.element();
}
};
pub const Figure = struct {
content: Element,
caption: ?Element = null,
node: Node = .{
.ptr = undefined,
.vtable = .{ .element = fromNode },
},
const Self = @This();
pub fn init(alloc: Allocator, content: Element) !*Self {
const v = try alloc.create(Self);
v.* = .{ .content = content };
v.node.ptr = v;
return v;
}
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;
}
fn fromNode(context: *anyopaque) Element {
const self: *Self = @ptrCast(@alignCast(context));
return self.element();
}
fn html(context: *anyopaque, parent: Allocator) HTML.Error!HTML {
const self: *Self = @ptrCast(@alignCast(context));
var el = try HTML.Content.init(parent, "figure");
var root = try HTML.Root.init(parent);
const alloc = root.allocator();
root.append(try self.content.html(alloc));
el.content = root.element();
const caption = self.caption orelse return el.element();
var figcap = try HTML.Content.init(alloc, "figcaption");
figcap.content = try caption.html(alloc);
root.append(figcap.element());
return el.element();
}
};
pub const Callout = struct {
content: Element,
title: ?[]const u8 = null,
kind: ?[]const u8 = null,
node: Node = .{
.ptr = undefined,
.vtable = .{ .element = fromNode },
},
const Self = @This();
pub fn init(alloc: Allocator, content: Element) !*Self {
const v = try alloc.create(Self);
v.* = .{ .content = content };
v.node.ptr = v;
return v;
}
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;
}
fn fromNode(context: *anyopaque) Element {
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, "div");
try el.base.appendClass("callout");
const kind = self.kind orelse "default";
try el.base.setAttribute("data-callout", kind);
const root = try HTML.Root.init(alloc);
const title = try HTML.Content.init(alloc, "h4");
title.content = (try HTML.Literal.init(alloc, self.title orelse kind)).element();
root.append(title.element());
root.append(try self.content.html(alloc));
el.content = root.element();
return el.element();
}
};
pub const Quote = struct {
content: Element,
attribution: ?Element = null,
node: Node = .{
.ptr = undefined,
.vtable = .{ .element = fromNode },
},
const Self = @This();
pub fn init(alloc: Allocator, content: Element) !*Self {
const v = try alloc.create(Self);
v.* = .{ .content = content };
v.node.ptr = v;
return v;
}
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;
}
fn fromNode(context: *anyopaque) Element {
const self: *Self = @ptrCast(@alignCast(context));
return self.element();
}
fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
const self: *Self = @ptrCast(@alignCast(context));
const quote = try Element.Simple("blockquote").init(alloc);
quote.content = self.content;
var el = try Figure.init(alloc, quote.element());
el.caption = self.attribution;
return try el.element().html(alloc);
}
};
|