aboutsummaryrefslogtreecommitdiff
path: root/src/link.zig
blob: 3333e764556023d316e62afe533ebf36d19bd4f7 (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
113
114
115
116
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 Link = Element.paragraph.Link;
const content = @import("content.zig");
const paragraph = @import("paragraph.zig");
const testing = @import("testing.zig");
const doTest = testing.do;
const doTestError = testing.doError;

pub const Error = error{InvalidLink} || content.Error || Allocator.Error;

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    const v = l.next().?;
    if (v.kind != .link) return Error.InvalidLink;
    if (!eql(u8, v.content, "[")) return (try Element.Literal.init(alloc, v.content)).element();
    var el = try Element.Empty.init(alloc);
    errdefer el.deinit(alloc);
    while (l.peek()) |next| {
        switch (next.kind) {
            .weak_delimiter, .strong_delimiter => return Error.InvalidLink,
            .link => {
                l.consume();
                if (!eql(u8, next.content, "](")) return Error.InvalidLink;
                break;
            },
            else => {
                const in = try content.parse(alloc, l);
                try el.content.append(alloc, in);
            },
        }
    }
    const href = l.next() orelse return Error.InvalidLink;
    if (href.kind != .literal) return Error.InvalidLink;
    const finisher = l.next() orelse return Error.InvalidLink;
    if (!finisher.equals(.link, ")")) return Error.InvalidLink;
    var in: Element = undefined;
    if (el.content.items.len > 0) {
        in = el.element();
    } else {
        el.deinit(alloc);
        in = (try Element.Literal.init(alloc, href.content)).element();
    }
    errdefer in.deinit(alloc);
    return (try Link.init(alloc, in, href.content)).element();
}

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

pub fn parseImage(alloc: Allocator, l: *Lexer) ImageError!Element {
    _ = l.next().?;
    const beg = l.next() orelse return ImageError.InvalidImage;
    if (!eql(u8, beg.content, "[")) return ImageError.InvalidImage;
    var it = l.next() orelse return ImageError.InvalidImage;
    var alt: ?[]const u8 = null;
    switch (it.kind) {
        .link => if (!eql(u8, it.content, "](")) return ImageError.InvalidImage,
        .literal => {
            alt = it.content;
            const next = l.next() orelse return ImageError.InvalidImage;
            if (!next.equals(.link, "](")) return ImageError.InvalidImage;
        },
        else => return ImageError.InvalidImage,
    }
    it = l.next() orelse return ImageError.InvalidImage;
    if (it.kind != .literal) return ImageError.InvalidImage;
    const src = it.content;
    it = l.next() orelse return ImageError.InvalidImage;
    if (!it.equals(.link, ")")) return ImageError.InvalidImage;
    const el = try Element.Image.init(alloc, src);
    errdefer el.deinit(alloc);
    el.alt = alt;
    it = l.peek() orelse return el.element();
    switch (it.kind) {
        .strong_delimiter => return el.element(),
        .weak_delimiter => l.consume(),
        else => return ImageError.InvalidImage,
    }
    const p = try paragraph.parse(alloc, l);
    errdefer p.deinit(alloc);
    const p_el: *Element.paragraph.Block = @ptrCast(@alignCast(p.ptr));
    el.source = (try p_el.toEmpty(alloc)).element();
    return el.element();
}

test "parse links" {
    const alloc = std.testing.allocator;

    try doTest(parse, alloc, "[](bar)", "<a href=\"bar\">bar</a>");
    try doTest(parse, alloc, "[foo](bar)", "<a href=\"bar\">foo</a>");
    try doTest(parse, alloc, "[f*o*o](bar)", "<a href=\"bar\">f<b>o</b>o</a>");
    try doTest(parse, alloc, ")", ")");

    try doTestError(parse, alloc, "[foo :::](bar)", Error.IllegalPlacement);
    try doTestError(parse, alloc, "[foo", Error.InvalidLink);
    try doTestError(parse, alloc, "[foo](", Error.InvalidLink);
    try doTestError(parse, alloc, "[foo]()", Error.InvalidLink);
}

test "parse image" {
    const alloc = std.testing.allocator;

    try doTest(parseImage, alloc, "![](src)", "<figure><img src=\"src\"></figure>");
    try doTest(parseImage, alloc, "![alt](src)", "<figure><img src=\"src\" alt=\"alt\"></figure>");

    try doTest(parseImage, alloc,
        \\![bar](foo)
        \\caption
        \\on multiple lines!
        \\
        \\not in
    , "<figure><img src=\"foo\" alt=\"bar\"><figcaption>caption on multiple lines!</figcaption></figure>");
}