aboutsummaryrefslogtreecommitdiff
path: root/src/callout.zig
blob: 1c756d610e27e1538f68d048c639eccc23527240 (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
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 paragraph = @import("paragraph.zig");
const doTest = testing.do;
const doTestError = testing.doError;

pub const Error = error{InvalidCallout} || paragraph.Error || Allocator.Error;

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    _ = l.next();
    var beg = l.next() orelse return Error.InvalidCallout;
    var kind: ?[]const u8 = null;
    var title: ?[]const u8 = null;
    switch (beg.kind) {
        .literal => {
            var iter = std.mem.splitAny(u8, beg.content, " ");
            kind = iter.first();
            if (iter.peek() != null) title = iter.buffer[iter.index.?..];
            beg = l.next() orelse return Error.InvalidCallout;
            if (!beg.kind.isDelimiter()) return Error.InvalidCallout;
        },
        else => if (!beg.kind.isDelimiter()) return Error.InvalidCallout,
    }
    var root = try Element.Root.init(alloc);
    while (l.peek()) |it| {
        if (it.kind == .callout) {
            l.consume();
            break;
        }
        if (it.kind.isDelimiter()) {
            const next = l.peek() orelse return Error.InvalidCallout;
            if (next.kind == .callout) {
                l.consume();
                break;
            }
        }
        root.append(try paragraph.parse(root.allocator(), l));
        _ = l.peek() orelse return Error.InvalidCallout;
    }
    var el = try Element.Callout.init(alloc, root.element());
    el.kind = kind;
    el.title = title;
    const end = l.next() orelse return el.element();
    if (!end.kind.isDelimiter()) return Error.InvalidCallout;
    return el.element();
}

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

    try doTest(parse, alloc,
        \\:::
        \\hey
        \\:::
    , "<div data-callout=\"default\" class=\"callout\"><h4>default</h4><p>hey</p></div>");
    try doTest(parse, alloc,
        \\:::info
        \\hey
        \\:::
    , "<div data-callout=\"info\" class=\"callout\"><h4>info</h4><p>hey</p></div>");
    try doTest(parse, alloc,
        \\::: info Title
        \\hey
        \\:::
    , "<div data-callout=\"info\" class=\"callout\"><h4>Title</h4><p>hey</p></div>");
    // cannot test content with \n

    try doTestError(parse, alloc, ":::", Error.InvalidCallout);
    try doTestError(parse, alloc,
        \\:::
        \\hey
    , Error.InvalidCallout);
    try doTestError(parse, alloc,
        \\:::
        \\hey:::
    , Error.IllegalPlacement);
    try doTestError(parse, alloc,
        \\:::
        \\hey
        \\::: nope
    , Error.InvalidCallout);
}