mod(node2buffer): add styling to parsed contents
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 29s

This commit is contained in:
2024-10-13 16:39:51 +02:00
parent a01619911d
commit a3d0b7af9a
3 changed files with 88 additions and 24 deletions

View File

@@ -3,45 +3,69 @@ const std = @import("std");
const vaxis = @import("vaxis");
const zmd = @import("zmd");
pub fn toBuffer(node: *zmd.Node, allocator: std.mem.Allocator, input: []const u8, array: *std.ArrayList(vaxis.Cell), s: vaxis.Cell.Style) !void {
const content = switch (node.token.element.type) {
.text => input[node.token.start..node.token.end],
.code, .block => node.content,
else => "",
};
var style = s;
pub fn toBuffer(
node: *zmd.Node,
allocator: std.mem.Allocator,
input: []const u8,
array: *std.ArrayList(vaxis.Cell),
sty: vaxis.Cell.Style,
start: ?usize,
) !void {
var next_start: ?usize = start;
var style = sty;
// FIXME: support list display
// TODO: improve code block listings (i.e. add line numbers with corresponding seperator, etc.)
// determine general styling changes
switch (node.token.element.type) {
.bold => {
style.bold = true;
},
.bold_close => {
style.bold = false;
next_start = node.token.start;
style.fg = .{ .index = 5 };
},
.italic => {
style.italic = true;
},
.italic_close => {
style.italic = false;
next_start = node.token.start;
style.fg = .{ .index = 2 };
},
.block => {
// TODO: what should I do with blocks?
},
.block_close => {
// TODO: what should I do with blocks?
style.fg = .{ .index = 7 };
next_start = node.token.start;
},
.code => {
style.dim = true;
next_start = node.token.start;
style.fg = .{ .index = 6 };
},
.code_close => {
style.dim = false;
.href => {
next_start = node.token.start;
style.fg = .{ .index = 8 };
},
else => {},
}
// determine content that needs to be displayed
const content = value: {
switch (node.token.element.type) {
.text => break :value input[node.token.start..node.token.end],
.bold_close, .italic_close, .block_close, .code_close, .title_close, .href_close => {
if (next_start) |s| {
next_start = null;
const e = node.token.end;
std.log.debug("start - end: {d}..{d}", .{ s, e });
std.log.debug("content: {s}", .{input[s..e]});
break :value input[s..e];
}
break :value "";
},
else => {
break :value "";
},
}
};
// display content
switch (node.token.element.type) {
.root, .none, .eof => {},
.linebreak, .paragraph => {
try array.append(.{
.char = .{ .grapheme = "\n" },
@@ -111,7 +135,31 @@ pub fn toBuffer(node: *zmd.Node, allocator: std.mem.Allocator, input: []const u8
},
}
// close styling after creating the corresponding cells
switch (node.token.element.type) {
.bold_close => {
style.bold = false;
style.fg = .default;
},
.italic_close => {
style.italic = false;
style.fg = .default;
},
.block_close => {
// TODO: what should I do with blocks?
style.fg = .default;
},
.code_close => {
style.fg = .default;
},
.href_close => {
style.fg = .default;
},
else => {},
}
// run conversion for all childrens
for (node.children.items) |child_node| {
try toBuffer(child_node, allocator, input, array, style);
try toBuffer(child_node, allocator, input, array, style, next_start);
}
}