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
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const Token = @import("lexer/Token.zig");
const Lexer = @import("lexer/Lexer.zig");
const Element = @import("dom/Element.zig");
const parser = @import("parser.zig");
const link = @import("link.zig");
const content = @import("content.zig");
const testing = @import("testing.zig");
const doTest = testing.do;
const doTestError = testing.doError;
pub const Error = content.Error || link.Error || Lexer.Error || Allocator.Error;
pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
var el = try Element.init(alloc, .content, "p");
errdefer el.deinit();
while (l.peek()) |next| {
switch (next.kind) {
// because nextKind returns only an hint for the next rune
.strong_delimiter => return el,
.weak_delimiter => {
l.consume();
const future = l.peek() orelse return el;
switch (future.kind) {
.literal, .italic, .code, .bold, .link => try el.appendContent(try Element.initLit(alloc, " ")),
else => return el,
}
},
else => try el.appendContent(try parseLine(alloc, l)),
}
}
return el;
}
pub fn parseLine(alloc: Allocator, l: *Lexer) Error!Element {
var line = Element.initEmpty(alloc);
errdefer line.deinit();
while (l.peek()) |next| {
switch (next.kind) {
.weak_delimiter, .strong_delimiter => return line,
.link => {
var el = try link.parse(alloc, l);
errdefer el.deinit();
try line.appendContent(el);
},
else => {
var el = try content.parse(alloc, l);
errdefer el.deinit();
try line.appendContent(el);
},
}
}
return line;
}
test "parse paragraphs" {
var arena = std.heap.DebugAllocator(.{}).init;
defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{});
const alloc = arena.allocator();
try doTest(parse, alloc, "hello world", "<p>hello world</p>");
try doTest(parse, alloc, "*hello* world", "<p><b>hello</b> world</p>");
try doTest(parse, alloc, "*he_ll_o* world", "<p><b>he<em>ll</em>o</b> world</p>");
try doTest(parse, alloc, "(foo)", "<p>(foo)</p>");
try doTest(parse, alloc, "[](bar)", "<p><a href=\"bar\">bar</a></p>");
try doTest(parse, alloc, "[foo](bar)", "<p><a href=\"bar\">foo</a></p>");
try doTest(parse, alloc, "hello [foo](bar) world", "<p>hello <a href=\"bar\">foo</a> world</p>");
try doTestError(parse, alloc, "hello *world", Error.ModifierNotClosed);
try doTestError(parse, alloc, "hello *wo_rld*", Error.ModifierNotClosed);
try doTestError(parse, alloc, "*hell*o *wo_rld*", Error.ModifierNotClosed);
try doTestError(parse, alloc, "hello ::: world", Error.IllegalPlacement);
}
|