feat(content): routing to provided .blog paths
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m7s

This is a starting point to dynamically loading `markdown` files as blog
entries. With this change the *doc/about.md* and *doc/blog.md* files
are no longer builded into the executable and instead read from the
filesystem during runtime (along with the new test file *doc/test.md*).
This commit is contained in:
2025-11-01 22:36:48 +01:00
parent e4c3f69821
commit a877ac3e7d
7 changed files with 138 additions and 59 deletions

View File

@@ -1,11 +1,12 @@
pub fn Content(App: type) type {
return struct {
page: App.Model.Pages,
allocator: Allocator,
document: *const App.Model.Document,
pub fn init(allocator: Allocator) @This() {
_ = allocator;
return .{
.page = .blog,
.allocator = allocator,
.document = undefined,
};
}
@@ -22,10 +23,7 @@ pub fn Content(App: type) type {
fn minSize(ctx: *anyopaque, size: zterm.Point) zterm.Point {
const this: *const @This() = @ptrCast(@alignCast(ctx));
const text = switch (this.page) {
.about => @embedFile("about"),
.blog => @embedFile("blog"),
};
const text = this.document.content;
var index: usize = 0;
var new_size: zterm.Point = .{ .x = size.x };
@@ -48,17 +46,25 @@ pub fn Content(App: type) type {
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.init => this.document = &model.document,
.about => {
model.page.deinit(this.allocator);
model.page = .about;
model.document = .init(@embedFile("about"));
model.document.deinit(this.allocator);
model.document = .init(try std.fs.cwd().readFileAlloc("./doc/about.md", this.allocator, .unlimited));
this.document = &model.document;
},
.blog => {
model.page = .blog;
model.document = .init(@embedFile("blog"));
.blog => |path| {
model.page.deinit(this.allocator);
model.page = .{ .blog = path };
model.document.deinit(this.allocator);
model.document = .init(try std.fs.cwd().readFileAlloc(if (path) |p| p else "./doc/blog.md", this.allocator, .unlimited));
this.document = &model.document;
},
else => {},
}
this.page = model.page;
}
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
@@ -88,10 +94,6 @@ pub fn Content(App: type) type {
pub fn Title(App: type) type {
return struct {
pub fn init() @This() {
return .{};
}
pub fn element(this: *@This()) App.Element {
return .{
.ptr = this,
@@ -122,15 +124,8 @@ pub fn Title(App: type) type {
pub fn InfoBanner(App: type) type {
return struct {
left_text: []const u8,
right_text: []const u8,
pub fn init() @This() {
return .{
.left_text = "Build with zig",
.right_text = "Yves Biener (@yves-biener)",
};
}
left_text: []const u8 = "Build with zig",
right_text: []const u8 = "Yves Biener (@yves-biener)",
pub fn element(this: *@This()) App.Element {
return .{
@@ -141,7 +136,7 @@ pub fn InfoBanner(App: type) type {
};
}
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *const @This() = @ptrCast(@alignCast(ctx));
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -154,6 +149,22 @@ pub fn InfoBanner(App: type) type {
cells[idx].cp = cp;
}
switch (model.page) {
.about => {},
.blog => |path| if (path) |p| page: {
const start_idx = (size.x -| p.len) / 2;
if (start_idx <= this.left_text.len) break :page;
for (start_idx.., p) |idx, cp| {
// NOTE do not write over the contents of this `Container`'s `Size`
if (idx == cells.len) break;
cells[idx].style.fg = .default;
cells[idx].cp = cp;
}
},
}
var start_idx = size.x -| this.right_text.len;
if (start_idx <= this.left_text.len) start_idx = this.left_text.len + 1;