aboutsummaryrefslogtreecommitdiff
path: root/src/list.zig
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-25 19:54:26 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-25 19:54:26 +0200
commitb0902c05ffc84d282e10a0179e041948d49fabf8 (patch)
tree7a8a51b48f16d448915bf13c6b41cb176ac5d506 /src/list.zig
parent0a0d579f34e12639c89d890d25be038c5ae81e00 (diff)
feat(): supports list unordored
Yes, these files were missing...
Diffstat (limited to 'src/list.zig')
-rw-r--r--src/list.zig71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/list.zig b/src/list.zig
new file mode 100644
index 0000000..1375d86
--- /dev/null
+++ b/src/list.zig
@@ -0,0 +1,71 @@
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+const Token = @import("lexer/Token.zig");
+const Lexer = @import("lexer/Lexer.zig");
+const Element = @import("eval/Element.zig");
+const paragraph = @import("paragraph.zig");
+const testing = @import("testing.zig");
+const doTest = testing.do;
+const doTestError = testing.doError;
+
+pub fn parseOrdored(alloc: Allocator, l: *Lexer) !Element {
+ const el = try Element.list.Ordored.init(alloc);
+ errdefer el.deinit(alloc);
+ try parse(alloc, &el.content, l, .list_ordored);
+ return el.element();
+}
+
+pub fn parseUnordored(alloc: Allocator, l: *Lexer) !Element {
+ const el = try Element.list.Unordored.init(alloc);
+ errdefer el.deinit(alloc);
+ try parse(alloc, &el.content, l, .list_unordored);
+ return el.element();
+}
+
+fn parse(alloc: Allocator, content: *std.ArrayList(Element), l: *Lexer, comptime kind: Token.Kind) !void {
+ while (l.peek()) |next| {
+ switch (next.kind) {
+ kind => {
+ l.consume();
+ continue;
+ },
+ .weak_delimiter => {
+ l.consume();
+ if (l.peek()) |it| if (it.kind != kind) return;
+ continue;
+ },
+ .strong_delimiter => return,
+ else => {
+ try content.append(alloc, try paragraph.parseLine(alloc, l));
+ },
+ }
+ }
+}
+
+test "parse ordored list" {
+ const alloc = std.testing.allocator;
+
+ try doTest(parseOrdored, alloc,
+ \\. one
+ \\. two
+ , "<ol><li>one</li><li>two</li></ol>");
+ try doTest(parseOrdored, alloc,
+ \\. one
+ \\. two
+ \\no more
+ , "<ol><li>one</li><li>two</li></ol>");
+}
+
+test "parse unordored list" {
+ const alloc = std.testing.allocator;
+
+ try doTest(parseUnordored, alloc,
+ \\- one
+ \\- two
+ , "<ul><li>one</li><li>two</li></ul>");
+ try doTest(parseUnordored, alloc,
+ \\- one
+ \\- two
+ \\no more
+ , "<ul><li>one</li><li>two</li></ul>");
+}