add(node2buffer): convert raw and code blocks with leading line numbers

This commit is contained in:
2024-10-19 16:12:31 +02:00
parent 03a4b571d3
commit 002f4f4310

View File

@@ -3,6 +3,8 @@ const std = @import("std");
const vaxis = @import("vaxis"); const vaxis = @import("vaxis");
const zmd = @import("zmd"); const zmd = @import("zmd");
const digits = "0123456789";
pub fn toBuffer( pub fn toBuffer(
node: *zmd.Node, node: *zmd.Node,
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
@@ -43,6 +45,7 @@ pub fn toBuffer(
}, },
.link => { .link => {
style.fg = .{ .index = 3 }; style.fg = .{ .index = 3 };
style.ul_style = .single;
}, },
else => {}, else => {},
} }
@@ -128,7 +131,6 @@ pub fn toBuffer(
} }
}, },
.link => { .link => {
style.ul_style = .single;
const uri = input[node.token.start + 1 + node.title.?.len + 2 .. node.token.start + 1 + node.title.?.len + 2 + node.href.?.len]; const uri = input[node.token.start + 1 + node.title.?.len + 2 .. node.token.start + 1 + node.title.?.len + 2 + node.href.?.len];
for (content, 0..) |_, i| { for (content, 0..) |_, i| {
try array.append(.{ try array.append(.{
@@ -138,6 +140,67 @@ pub fn toBuffer(
}); });
} }
}, },
.block_close, .code_close => {
// generate a `block` i.e.
// 01 | ...
// 02 | ...
// 03 | ...
// ...
// 10 | ...
var rows: usize = 0;
var c: usize = 0;
// TODO: would be cool to not have to re-iterate over the contents
for (content, 0..) |char, i| {
if (char == '\n') {
// NOTE: start after the ```<language>
if (c == 0) {
c = i + 1;
}
rows += 1;
}
}
rows = rows -| 1;
const pad = vaxis.widgets.LineNumbers.numDigits(rows);
for (1..rows + 1) |r| {
try array.append(.{
.char = .{ .grapheme = " " },
.style = .{ .dim = true },
});
try array.append(.{
.char = .{ .grapheme = " " },
.style = .{ .dim = true },
});
for (1..pad + 1) |i| {
const digit = vaxis.widgets.LineNumbers.extractDigit(r, pad - i);
try array.append(.{
.char = .{ .grapheme = digits[digit .. digit + 1] },
.style = .{ .dim = true },
});
}
try array.append(.{
.char = .{ .grapheme = " " },
.style = .{ .dim = true },
});
try array.append(.{
.char = .{ .grapheme = "" },
.style = .{ .dim = true },
});
try array.append(.{
.char = .{ .grapheme = " " },
.style = .{ .dim = true },
});
for (c..content.len) |c_i| {
try array.append(.{
.char = .{ .grapheme = content[c_i .. c_i + 1] },
.style = style,
});
if (content[c_i] == '\n') {
c = c_i + 1;
break;
}
}
}
},
else => { else => {
for (content, 0..) |_, i| { for (content, 0..) |_, i| {
try array.append(.{ try array.append(.{