From b0902c05ffc84d282e10a0179e041948d49fabf8 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sat, 25 Apr 2026 19:54:26 +0200 Subject: feat(): supports list unordored Yes, these files were missing... --- src/eval/list.zig | 50 +++++++++++++++++++++++++++++++++++++++ src/list.zig | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.zig | 1 + 3 files changed, 122 insertions(+) create mode 100644 src/eval/list.zig create mode 100644 src/list.zig (limited to 'src') diff --git a/src/eval/list.zig b/src/eval/list.zig new file mode 100644 index 0000000..6954fd1 --- /dev/null +++ b/src/eval/list.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const HTML = Element.HTML; +const Element = @import("Element.zig"); + +fn List(comptime tag: []const u8) type { + return struct { + content: std.ArrayList(Element), + + 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 = 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.init(alloc, .content, tag); + errdefer el.deinit(); + for (self.content.items) |it| { + var li = try HTML.init(alloc, .content, "li"); + try li.appendContent(try it.html(alloc)); + try el.appendContent(li); + } + return el; + } + }; +} + +pub const Ordored = List("ol"); +pub const Unordored = List("ul"); 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 + , "
  1. one
  2. two
"); + try doTest(parseOrdored, alloc, + \\. one + \\. two + \\no more + , "
  1. one
  2. two
"); +} + +test "parse unordored list" { + const alloc = std.testing.allocator; + + try doTest(parseUnordored, alloc, + \\- one + \\- two + , ""); + try doTest(parseUnordored, alloc, + \\- one + \\- two + \\no more + , ""); +} diff --git a/src/parser.zig b/src/parser.zig index 752267e..8c170e7 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -56,6 +56,7 @@ fn gen(parent: Allocator, l: *Lexer) Error!Document { // other blocks .title => try title.parse(alloc, l), .list_ordored => try list.parseOrdored(alloc, l), + .list_unordored => try list.parseUnordored(alloc, l), .weak_delimiter, .strong_delimiter => { l.consume(); continue :base; -- cgit v1.2.3