aboutsummaryrefslogtreecommitdiff
path: root/src/link.zig
blob: d5ffe3c705719009f3c209c787208830a137475f (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
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("dom/Element.zig");
const content = @import("content.zig");
const testing = @import("testing.zig");
const doTest = testing.do;
const doTestError = testing.doError;

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

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    const data = try parseData(alloc, l);
    const second = data.second orelse return data.first.?;
    var in = if (data.first) |first| first else try Element.initLitEscaped(alloc, second);
    errdefer in.deinit();
    var el = try Element.init(alloc, .content, "a");
    errdefer el.deinit();
    try el.appendContent(in);
    try el.setAttribute("href", second);
    return el;
}

pub const Data = struct {
    first: ?Element,
    second: ?[]const u8,
};

pub fn parseData(alloc: Allocator, l: *Lexer) Error!Data {
    const v = l.next().?;
    if (v.kind != .link) return Error.InvalidLink;
    if (!eql(u8, v.content, "[")) {
        const el = try Element.initLitEscaped(alloc, v.content);
        return .{ .first = el, .second = null };
    }
    var el = Element.initEmpty(alloc);
    errdefer el.deinit();
    while (l.nextKind()) |kind| {
        switch (kind) {
            .weak_delimiter, .strong_delimiter => return Error.InvalidLink,
            .link => {
                const next = l.next().?;
                if (!eql(u8, next.content, "](")) return Error.InvalidLink;
                break;
            },
            else => {
                const in = try content.parse(alloc, l);
                try el.appendContent(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;
    return .{
        .first = if (el.content.items.len > 0) el else null,
        .second = href.content,
    };
}

test "parse links" {
    var arena = std.heap.DebugAllocator(.{}).init;
    defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{});
    const alloc = arena.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);
}