aboutsummaryrefslogtreecommitdiff
path: root/src/code.zig
blob: 7b7d23a075bafef4eef0c34e7c1f9190405bdabf (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const eql = std.mem.eql;
const Token = @import("lexer/Token.zig");
const Lexer = @import("lexer/Lexer.zig");
const Element = @import("eval/Element.zig");
const testing = @import("testing.zig");
const doTest = testing.do;
const doTestError = testing.doError;

pub const Error = error{InvalidCodeBlock} || Allocator.Error;

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    _ = l.next();
    var beg = l.next() orelse return Error.InvalidCodeBlock;
    var data: ?[]const u8 = null;
    switch (beg.kind) {
        .literal => {
            data = beg.content;
            beg = l.next() orelse return Error.InvalidCodeBlock;
            if (!beg.kind.isDelimiter()) return Error.InvalidCodeBlock;
        },
        else => if (!beg.kind.isDelimiter()) return Error.InvalidCodeBlock,
    }
    const code = try Element.Code.init(alloc);
    code.attribute = data;
    const el = try Element.Figure.init(alloc, code.element());
    errdefer el.deinit(alloc);
    while (l.next()) |it| {
        if (it.kind == .code_block) return Error.InvalidCodeBlock;
        if (it.kind.isDelimiter()) {
            const next = l.peek() orelse return Error.InvalidCodeBlock;
            if (next.kind == .code_block) break;
        }
        try code.content.append(alloc, (try Element.Literal.init(alloc, it.content)).element());
        // restore modifications done by the lexer
        if (it.kind.requiresSpace())
            try code.content.append(alloc, (try Element.Literal.init(alloc, " ")).element());
    }
    var end = l.next() orelse return Error.InvalidCodeBlock;
    if (end.kind != .code_block) return Error.InvalidCodeBlock;
    end = l.next() orelse return el.element();
    if (!end.kind.isDelimiter()) return Error.InvalidCodeBlock;
    return el.element();
}

test "code" {
    const alloc = std.testing.allocator;

    try doTest(parse, alloc,
        \\```
        \\hey
        \\```
    , "<figure><pre><code>hey</code></pre></figure>");
    try doTest(parse, alloc,
        \\```td another
        \\hey
        \\```
    , "<figure><pre data-code=\"td another\"><code>hey</code></pre></figure>");
    // cannot test content with \n

    try doTestError(parse, alloc, "```", Error.InvalidCodeBlock);
    try doTestError(parse, alloc,
        \\```
        \\hey
    , Error.InvalidCodeBlock);
    try doTestError(parse, alloc,
        \\```
        \\hey```
    , Error.InvalidCodeBlock);
    try doTestError(parse, alloc,
        \\```
        \\hey
        \\``` nope
    , Error.InvalidCodeBlock);
}