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

@@ -37,6 +37,10 @@ pub fn update(this: *@This(), event: Event) ?Event {
// Home // Home
return .{ .path = "./doc/home.md" }; return .{ .path = "./doc/home.md" };
} }
if (key.matches('t', .{})) {
// test
return .{ .path = "./doc/test.md" };
}
}, },
else => {}, else => {},
} }
@@ -55,6 +59,7 @@ pub fn draw(this: *@This(), win: vaxis.Window) void {
\\ \\
\\**a** about \\**a** about
\\**h** home \\**h** home
\\**t** test
; ;
var zmd = Zmd.init(this.allocator); var zmd = Zmd.init(this.allocator);
defer zmd.deinit(); defer zmd.deinit();
@@ -63,7 +68,7 @@ pub fn draw(this: *@This(), win: vaxis.Window) void {
defer cells.deinit(); defer cells.deinit();
zmd.parse(msg) catch @panic("failed to parse markdown file"); zmd.parse(msg) catch @panic("failed to parse markdown file");
node2buffer.toBuffer(zmd.nodes.items[0], this.allocator, msg, &cells, .{}) catch @panic("failed to transform to cell array"); node2buffer.toBuffer(zmd.nodes.items[0], this.allocator, msg, &cells, .{}, null) catch @panic("failed to transform to cell array");
var col: usize = 0; var col: usize = 0;
var row: usize = 0; var row: usize = 0;

View File

@@ -77,7 +77,14 @@ pub fn update(this: *@This(), event: Event) void {
this.buffer.clearRetainingCapacity(); this.buffer.clearRetainingCapacity();
zmd.parse(this.contents.?) catch @panic("failed to parse markdown contents"); zmd.parse(this.contents.?) catch @panic("failed to parse markdown contents");
node2buffer.toBuffer(zmd.nodes.items[0], this.allocator, this.contents.?, &this.buffer, .{}) catch @panic("failed to transform to cell array"); node2buffer.toBuffer(
zmd.nodes.items[0],
this.allocator,
this.contents.?,
&this.buffer,
.{},
null,
) catch @panic("failed to transform to cell array");
}, },
else => {}, else => {},
} }
@@ -95,7 +102,11 @@ pub fn draw(this: *@This(), win: vaxis.Window) void {
for (this.buffer.items) |cell| { for (this.buffer.items) |cell| {
if (bounds.above(pos.y)) break; if (bounds.above(pos.y)) break;
// if (!bounds.colInside(pos.x)) continue; // FIX: how can I prevent writes out of bounce of the viewport?
if (!bounds.colInside(pos.x)) {
pos.x = 0;
pos.y += 1;
}
this.view.writeCell(win, pos.x, pos.y, cell); this.view.writeCell(win, pos.x, pos.y, cell);
if (std.mem.eql(u8, cell.char.grapheme, "\n")) { if (std.mem.eql(u8, cell.char.grapheme, "\n")) {

View File

@@ -3,45 +3,69 @@ const std = @import("std");
const vaxis = @import("vaxis"); const vaxis = @import("vaxis");
const zmd = @import("zmd"); 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 { pub fn toBuffer(
const content = switch (node.token.element.type) { node: *zmd.Node,
.text => input[node.token.start..node.token.end], allocator: std.mem.Allocator,
.code, .block => node.content, input: []const u8,
else => "", array: *std.ArrayList(vaxis.Cell),
}; sty: vaxis.Cell.Style,
var style = s; 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 // determine general styling changes
switch (node.token.element.type) { switch (node.token.element.type) {
.bold => { .bold => {
style.bold = true; style.bold = true;
}, next_start = node.token.start;
.bold_close => { style.fg = .{ .index = 5 };
style.bold = false;
}, },
.italic => { .italic => {
style.italic = true; style.italic = true;
}, next_start = node.token.start;
.italic_close => { style.fg = .{ .index = 2 };
style.italic = false;
}, },
.block => { .block => {
// TODO: what should I do with blocks? // TODO: what should I do with blocks?
}, style.fg = .{ .index = 7 };
.block_close => { next_start = node.token.start;
// TODO: what should I do with blocks?
}, },
.code => { .code => {
style.dim = true; next_start = node.token.start;
style.fg = .{ .index = 6 };
}, },
.code_close => { .href => {
style.dim = false; next_start = node.token.start;
style.fg = .{ .index = 8 };
}, },
else => {}, 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) { switch (node.token.element.type) {
.root, .none, .eof => {},
.linebreak, .paragraph => { .linebreak, .paragraph => {
try array.append(.{ try array.append(.{
.char = .{ .grapheme = "\n" }, .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| { for (node.children.items) |child_node| {
try toBuffer(child_node, allocator, input, array, style); try toBuffer(child_node, allocator, input, array, style, next_start);
} }
} }