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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const DOMElement = @import("dom/Element.zig");
const Parent = @This();
vtable: struct {
deinit: *const fn (*anyopaque, Allocator) void,
dom: *const fn (*anyopaque, Allocator) DOMElement.Error!DOMElement,
},
ptr: *anyopaque,
pub fn renderHTML(self: Parent, alloc: Allocator) DOMElement.Error![]const u8 {
var el = try self.vtable.dom(self.ptr, alloc);
defer el.deinit();
return el.render(alloc);
}
pub fn deinit(self: Parent, alloc: Allocator) void {
self.vtable.deinit(self.ptr, alloc);
}
fn dom(self: Parent, alloc: Allocator) DOMElement.Error!DOMElement {
return self.vtable.dom(self.ptr, alloc);
}
pub const Paragraph = Modifier("p");
pub const Bold = Modifier("b");
pub const Italic = Modifier("em");
pub const Code = Modifier("code");
pub const Empty = struct {
content: std.ArrayList(Parent),
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) Parent {
return .{ .ptr = self, .vtable = .{ .deinit = destroy, .dom = Self.dom } };
}
pub fn deinit(self: *Self, alloc: Allocator) void {
destroy(self, alloc);
}
fn destroy(context: *anyopaque, alloc: Allocator) void {
const self: *Self = @ptrCast(@alignCast(context));
for (self.content.items) |it| it.deinit(alloc);
self.content.deinit(alloc);
alloc.destroy(self);
}
fn dom(context: *anyopaque, alloc: Allocator) DOMElement.Error!DOMElement {
const self: *Self = @ptrCast(@alignCast(context));
var el = DOMElement.initEmpty(alloc);
errdefer el.deinit();
for (self.content.items) |it| try el.appendContent(try it.dom(alloc));
return el;
}
};
pub const Literal = struct {
content: []const u8,
const Self = @This();
pub fn init(alloc: Allocator, content: []const u8) !*Self {
const v = try alloc.create(Self);
v.* = .{ .content = content };
return v;
}
pub fn element(self: *Self) Parent {
return .{ .ptr = self, .vtable = .{ .deinit = destroy, .dom = Self.dom } };
}
pub fn deinit(self: *Self, alloc: Allocator) void {
destroy(self, alloc);
}
fn destroy(context: *anyopaque, alloc: Allocator) void {
const self: *Self = @ptrCast(@alignCast(context));
alloc.destroy(self);
}
fn dom(context: *anyopaque, alloc: Allocator) DOMElement.Error!DOMElement {
const self: *Self = @ptrCast(@alignCast(context));
return DOMElement.initLitEscaped(alloc, self.content);
}
};
pub fn Modifier(comptime tag: []const u8) type {
return struct {
content: std.ArrayList(Parent),
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) Parent {
return .{ .ptr = self, .vtable = .{ .deinit = destroy, .dom = Self.dom } };
}
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 dom(context: *anyopaque, alloc: Allocator) DOMElement.Error!DOMElement {
const self: *Self = @ptrCast(@alignCast(context));
var el = try DOMElement.init(alloc, .content, tag);
errdefer el.deinit();
for (self.content.items) |it| try el.appendContent(try it.dom(alloc));
return el;
}
};
}
pub const Link = struct {
link: []const u8,
content: Parent,
target: ?[]const u8 = null,
const Self = @This();
pub fn init(alloc: Allocator, content: Parent, link: []const u8) !*Self {
const v = try alloc.create(Self);
v.* = .{
.content = content,
.link = link,
};
return v;
}
pub fn element(self: *Self) Parent {
return .{ .ptr = self, .vtable = .{ .deinit = destroy, .dom = Self.dom } };
}
pub fn deinit(self: *Self, alloc: Allocator) void {
destroy(self, alloc);
}
fn destroy(context: *anyopaque, alloc: Allocator) void {
var self: *Self = @ptrCast(@alignCast(context));
self.content.deinit(alloc);
alloc.destroy(self);
}
fn dom(context: *anyopaque, alloc: Allocator) DOMElement.Error!DOMElement {
const self: *Self = @ptrCast(@alignCast(context));
var el = try DOMElement.init(alloc, .content, "a");
errdefer el.deinit();
try el.appendContent(try self.content.dom(alloc));
try el.setAttribute("href", self.link);
if (self.target) |target| try el.setAttribute("target", target);
return el;
}
};
pub const Title = struct {
level: u3,
content: Parent,
const Self = @This();
pub fn init(alloc: Allocator, level: u3, content: Parent) !*Self {
const v = try alloc.create(Self);
v.* = .{ .level = level, .content = content };
return v;
}
pub fn element(self: *Self) Parent {
return .{ .ptr = self, .vtable = .{ .deinit = destroy, .dom = Self.dom } };
}
pub fn deinit(self: *Self, alloc: Allocator) void {
self.element().deinit(alloc);
}
fn destroy(context: *anyopaque, alloc: Allocator) void {
var self: *Self = @ptrCast(@alignCast(context));
self.content.deinit(alloc);
alloc.destroy(self);
}
fn dom(context: *anyopaque, alloc: Allocator) DOMElement.Error!DOMElement {
const self: *Self = @ptrCast(@alignCast(context));
var el = try DOMElement.init(alloc, .content, switch (self.level) {
1 => "h1",
2 => "h2",
3 => "h3",
4 => "h4",
5 => "h5",
6 => "h6",
else => unreachable,
});
errdefer el.deinit();
try el.appendContent(try self.content.dom(alloc));
return el;
}
};
fn doTest(alloc: Allocator, el: Parent, exp: []const u8) !void {
const got = try el.renderHTML(alloc);
defer alloc.free(got);
std.testing.expect(std.mem.eql(u8, got, exp)) catch |err| {
std.debug.print("{s}\n", .{got});
return err;
};
}
test "paragraph" {
const alloc = std.testing.allocator;
const lit = (try Literal.init(alloc, "hello world")).element();
try doTest(alloc, lit, "hello world");
var p = try Paragraph.init(alloc);
try p.content.append(alloc, lit);
defer p.deinit(alloc);
try doTest(alloc, p.element(), "<p>hello world</p>");
const link = (try Link.init(alloc, (try Literal.init(alloc, "foo")).element(), "example.org")).element();
try doTest(alloc, link, "<a href=\"example.org\">foo</a>");
try p.content.append(alloc, link);
try doTest(alloc, p.element(), "<p>hello world<a href=\"example.org\">foo</a></p>");
}
|