diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 21:30:32 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 21:30:32 +0200 |
| commit | ec6f949c48323a0bea244b723af547f60e0db33f (patch) | |
| tree | f20497988d2e2c675153c60892cfe8e5715b7fb3 /src/content.zig | |
| parent | fe3e8fe67b7f655699be321796af70d4bdff230f (diff) | |
style(ast): split paragraph and content
Diffstat (limited to 'src/content.zig')
| -rw-r--r-- | src/content.zig | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/content.zig b/src/content.zig new file mode 100644 index 0000000..01d933b --- /dev/null +++ b/src/content.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const Lexed = @import("lexer/Lexed.zig"); +const Lexer = @import("lexer/Lexer.zig"); +const Element = @import("dom/Element.zig"); +const parser = @import("parser.zig"); +const link = @import("link.zig"); +const testing = @import("testing.zig"); +const doTest = testing.do; +const doTestError = testing.doError; + +pub const Error = error{ ModifierNotClosed, IllegalPlacement } || Lexer.Error; + +pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { + var content = Element.initEmpty(alloc); + errdefer content.deinit(); + var v = (try l.next(alloc)).?; + defer v.deinit(); + switch (v.kind) { + .literal => { + const el = try Element.initLitEscaped(alloc, v.content.items); + try content.appendContent(el); + }, + .bold => try content.appendContent(try parseModifier(alloc, l, .bold, "b")), + .italic => try content.appendContent(try parseModifier(alloc, l, .italic, "em")), + .code => try content.appendContent(try parseModifier(alloc, l, .code, "code")), + else => return Error.IllegalPlacement, + } + return content; +} + +fn parseModifier(alloc: Allocator, l: *Lexer, knd: Lexed.Kind, tag: []const u8) Error!Element { + var el = try Element.init(alloc, .content, tag); + errdefer el.deinit(); + while (l.nextKind()) |it| { + if (it == knd) { + // consuming the finisher + var v = (try l.next(alloc)).?; + v.deinit(); + return el; + } + if (it.isDelimiter()) return Error.ModifierNotClosed; + try el.appendContent(try parse(alloc, l)); + } + return Error.ModifierNotClosed; +} |
