diff options
Diffstat (limited to 'src/markdown.zig')
| -rw-r--r-- | src/markdown.zig | 119 |
1 files changed, 107 insertions, 12 deletions
diff --git a/src/markdown.zig b/src/markdown.zig index 253cd94..b4d2ab6 100644 --- a/src/markdown.zig +++ b/src/markdown.zig @@ -58,12 +58,12 @@ pub const Parser = struct { continue; } const at = i; - i = runEnd(text, at, c); + i = runEnd(u16, text, at, c); const width = i - at; if (c == '`') { if (width > ticks.len) continue; // Each cursor moves only forward, even when a backtick run is unmatched. - const close = nextTicks(text, @max(i, ticks[width - 1]), width); + const close = nextTicks(u16, text, @max(i, ticks[width - 1]), width); ticks[width - 1] = close; if (close == text.len or close == i) continue; const first = p.marks.items.len; @@ -150,13 +150,13 @@ pub const front: u32 = 1; /// and character; it is left as the next line will find it. An unclosed block runs to the end. /// Frontmatter is a block too, fenced by its delimiters. Only the caller can know that the /// piece's first line `opens_front`: that takes a line further on to close it. -pub fn fenceLine(line: []const u16, open: *u32, opens_front: bool) Line { +pub fn fenceLine(comptime T: type, line: []const T, open: *u32, opens_front: bool) Line { if (opens_front) { open.* = front; return .fence; } if (open.* == front) { - if (!frontDelimiter(u16, line, false)) return .code; + if (!frontDelimiter(T, line, false)) return .code; open.* = 0; return .fence; } @@ -165,16 +165,16 @@ pub fn fenceLine(line: []const u16, open: *u32, opens_front: bool) Line { if (i < line.len and (line[i] == '`' or line[i] == '~')) { const c = line[i]; const tilde = @intFromBool(c == '~'); - const stop = runEnd(line, i, c); + const stop = runEnd(T, line, i, c); const width: u32 = @intCast(stop - i); const rest = line[stop..]; if (open.* == 0) { // A backtick fence's info string has no backticks: that would be inline code. - if (width >= 3 and (c == '~' or std.mem.indexOfScalar(u16, rest, '`') == null)) { + if (width >= 3 and (c == '~' or std.mem.indexOfScalar(T, rest, '`') == null)) { open.* = width << 1 | tilde; return .fence; } - } else if (open.* & 1 == tilde and width >= open.* >> 1 and std.mem.indexOfNone(u16, rest, &.{ ' ', '\t', '\r', '\n' }) == null) { + } else if (open.* & 1 == tilde and width >= open.* >> 1 and std.mem.indexOfNone(T, rest, &.{ ' ', '\t', '\r', '\n' }) == null) { open.* = 0; return .fence; } @@ -182,6 +182,71 @@ pub fn fenceLine(line: []const u16, open: *u32, opens_front: bool) Line { return if (open.* == 0) .text else .code; } +pub const Span = struct { lo: usize, hi: usize }; + +pub const comment_open = 4; // how long "<!--" is, +pub const comment_close = 3; // and "-->" + +/// The HTML comments of a whole piece, in order. What is not surely a comment is prose: to +/// hide text that Markdown then publishes would be the worse mistake. So one that starts a +/// line may run over any others, but elsewhere (and in a list or a quote) it has to close on +/// its own line; and code, frontmatter and what is never closed have none. The file's own +/// records, if they could not be read, stay in sight too. +pub fn Comments(comptime T: type) type { + return struct { + text: []const T, + front: bool, // it opens with frontmatter: see `fenceLine` + at: usize = 0, + open: u32 = 0, + + pub fn next(it: *@This()) ?Span { + const text = it.text; + lines: while (it.at < text.len) { + const stop = if (std.mem.indexOfScalarPos(T, text, it.at, '\n')) |nl| nl + 1 else text.len; + var i = it.at; + if (starts(text, i) and fenceLine(T, text[i..stop], &it.open, it.front and i == 0) != .text) i = stop; + while (i < stop) switch (text[i]) { + '\\' => i += 2, + '`' => { + const body = runEnd(T, text[0..stop], i, '`'); + const close = nextTicks(T, text[0..stop], body, body - i); + i = if (close == stop) body else close + (body - i); + }, + '<' => { + const lo = i; + i += 1; + if (!has(text[lo..], "<!--")) continue; + // "<!-->" is a whole comment. + const close = find(text[0..if (starts(text, lo)) text.len else stop], lo + 2, "-->") orelse continue; + it.at = close + comment_close; + for ([_][]const u8{ "<!-- write_away ", "<!-- overflow\n(", "<!-- overflow -->" }) |record| if (has(text[lo..], record)) continue :lines; + return .{ .lo = lo, .hi = it.at }; + }, + else => i += 1, + }; + it.at = stop; + } + return null; + } + + fn starts(text: []const T, i: usize) bool { + return i == 0 or text[i - 1] == '\n'; + } + + fn has(text: []const T, what: []const u8) bool { + if (text.len < what.len) return false; + for (text[0..what.len], what) |a, b| if (a != b) return false; + return true; + } + + fn find(text: []const T, from: usize, what: []const u8) ?usize { + var i = from; + while (i + what.len <= text.len) : (i += 1) if (has(text[i..], what)) return i; + return null; + } + }; +} + fn blanks(comptime T: type) []const T { return &.{ ' ', '\t', '\r', '\n' }; } @@ -261,20 +326,20 @@ fn punctuation(c: u16) bool { }; } -fn runEnd(text: []const u16, start: usize, c: u16) usize { +fn runEnd(comptime T: type, text: []const T, start: usize, c: T) usize { var i = start + 1; while (i < text.len and text[i] == c) i += 1; return i; } -fn nextTicks(text: []const u16, start: usize, width: usize) usize { +fn nextTicks(comptime T: type, text: []const T, start: usize, width: usize) usize { var i = start; while (i < text.len) { if (text[i] != '`') { i += 1; continue; } - const stop = runEnd(text, i, '`'); + const stop = runEnd(T, text, i, '`'); if (stop - i == width) return i; i = stop; } @@ -298,7 +363,7 @@ test "fences open, hold what looks like Markdown, and close only on their own ki .{ L("~~~ a`b\n"), .fence }, .{ L("unclosed"), .code }, }; - for (lines) |l| try std.testing.expectEqual(l[1], fenceLine(l[0], &open, false)); + for (lines) |l| try std.testing.expectEqual(l[1], fenceLine(u16, l[0], &open, false)); try std.testing.expectEqual(@as(u32, 3 << 1 | 1), open); } @@ -313,7 +378,7 @@ test "frontmatter is a block between its delimiters, and a rule is not one of th .{ L("...\r\n"), .fence }, .{ L("---\n"), .text }, }; - for (lines, 0..) |l, i| try std.testing.expectEqual(l[1], fenceLine(l[0], &open, i == 0)); + for (lines, 0..) |l, i| try std.testing.expectEqual(l[1], fenceLine(u16, l[0], &open, i == 0)); try std.testing.expectEqual(@as(u32, 0), open); try std.testing.expect(frontDelimiter(u8, "---\n", true) and !frontDelimiter(u8, "...\n", true) and !frontDelimiter(u8, " ---", false)); for ([_][]const u16{ L("---\n"), L(" ----- \r\n") }) |line| try std.testing.expectEqual(Rule.hyphens, rule(line)); @@ -321,6 +386,36 @@ test "frontmatter is a block between its delimiters, and a rule is not one of th for ([_][]const u16{ L(""), L("\n"), L("--"), L("--- a\n"), L(" ---\n"), L("\t---"), L("**_"), L("-*-\n"), L("* * item"), L("===") }) |line| try std.testing.expectEqual(Rule.no, rule(line)); } +test "comments: one that starts a line runs on, and nothing Markdown might publish is one" { + const cases = [_]struct { []const u8, []const []const u8 }{ + .{ "a\n<!--\n# b\n\n```\n-->\nc <!-- d --> e<!--f--><!-->\n", &.{ "<!--\n# b\n\n```\n-->", "<!-- d -->", "<!--f-->", "<!-->" } }, + .{ "<!-- a <!-- b --> c -->", &.{"<!-- a <!-- b -->"} }, + // Only its own line can close one in a paragraph, a list or a quote. + .{ "a <!-- b\n\nc -->\n- <!-- d\ne -->\n> <!-- f -->\n <!-- g\n-->", &.{"<!-- f -->"} }, + .{ "```\n<!-- a -->\n```\n`<!-- b -->` ``c ` <!-- d -->`` ` <!-- e -->", &.{"<!-- e -->"} }, + .{ "\\<!-- a --> \\\\<!-- b -->\n<!-- c\n", &.{"<!-- b -->"} }, + .{ "---\nx: 1\n<!-- a -->\n---\n<!-- b -->", &.{"<!-- b -->"} }, + .{ "<!-- overflow\n(a)-->\n<!-- write_away {} -->\n\n<!-- overflow -->\n<!-- overflowing -->", &.{"<!-- overflowing -->"} }, + }; + for (cases) |c| { + const front_matter = std.mem.startsWith(u8, c[0], "---"); + var it: Comments(u8) = .{ .text = c[0], .front = front_matter }; + for (c[1]) |want| { + const got = it.next().?; + try std.testing.expectEqualStrings(want, c[0][got.lo..got.hi]); + } + try std.testing.expectEqual(@as(?Span, null), it.next()); + + // The page reads the same ones, in UTF-16. + var units: [256]u16 = undefined; + const n = try std.unicode.utf8ToUtf16Le(&units, c[0]); + var wide: Comments(u16) = .{ .text = units[0..n], .front = front_matter }; + it = .{ .text = c[0], .front = front_matter }; + while (it.next()) |want| try std.testing.expectEqual(want, wide.next().?); + try std.testing.expectEqual(@as(?Span, null), wide.next()); + } +} + test "links: the text is marked, the target is not read, and an image is left alone" { const alloc = std.testing.allocator; var p: Parser = .{}; |
