aboutsummaryrefslogtreecommitdiff
path: root/src/paragraph.zig
blob: caee73e0028221c4d75729799bce23e1d25221e4 (plain)
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
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");

pub const Error = error{ ModifierNotClosed, IllegalPlacement } || Lexer.Error;

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    var el = try Element.init(alloc, .content, "p");
    errdefer el.deinit();
    while (l.nextKind()) |kind| {
        switch (kind) {
            // because nextKind returns only an hint for the next rune
            .weak_delimiter => {
                var v = (try l.next(alloc)).?;
                defer v.deinit();
                if (v.kind == .strong_delimiter) return el;
                const next = l.nextKind() orelse return el;
                switch (next) {
                    .literal, .italic, .code, .bold => 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 content = Element.initEmpty(alloc);
    errdefer content.deinit();
    while (l.nextKind()) |kind| {
        switch (kind) {
            .weak_delimiter, .strong_delimiter => return content,
            else => {
                const el = try parseContent(alloc, l);
                try content.appendContent(el);
            },
        }
    }
    return content;
}

fn parseContent(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 parseContent(alloc, l));
    }
    return Error.ModifierNotClosed;
}

fn doTest(alloc: Allocator, t: []const u8, v: []const u8) !void {
    var l = try Lexer.init(t);
    var p = try parse(alloc, &l);
    defer p.deinit();
    const g = try p.render(alloc);
    defer alloc.free(g);
    std.testing.expect(std.mem.eql(u8, g, v)) catch |err| {
        std.debug.print("{s}\n", .{g});
        return err;
    };
}

fn doTestError(alloc: Allocator, t: []const u8, err: Error) !void {
    var l = try Lexer.init(t);
    _ = parse(alloc, &l) catch |e| return std.testing.expect(err == e);
    return std.testing.expect(false);
}

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(alloc, "hello world", "<p>hello world</p>");
    try doTest(alloc, "*hello* world", "<p><b>hello</b> world</p>");
    try doTest(alloc, "*he_ll_o* world", "<p><b>he<em>ll</em>o</b> world</p>");

    try doTestError(alloc, "hello *world", Error.ModifierNotClosed);
    try doTestError(alloc, "hello *wo_rld*", Error.ModifierNotClosed);
    try doTestError(alloc, "*hell*o *wo_rld*", Error.ModifierNotClosed);
    try doTestError(alloc, "hello ::: world", Error.IllegalPlacement);
}