feat(content): parse document preemble
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Has been cancelled

Show title stored in preemble of markdown file if available.
This commit is contained in:
2025-11-01 17:41:33 +01:00
parent 74bf941820
commit 55861114dc
4 changed files with 73 additions and 22 deletions

View File

@@ -1,5 +1,7 @@
# Blog ---
title: Welcome to my Blog
date: 2025-11-01
---
This is the main entry page for my blog. I plan on writing on things I feel like worth sharing, mentioning for other developers, tinkers like me. This is the main entry page for my blog. I plan on writing on things I feel like worth sharing, mentioning for other developers, tinkers like me.
The blog is currently a work in progress and will be updated once a first version of the tui website supports more dynamic contents. The blog is currently a work in progress and will be updated once a first version of the tui website supports more dynamic contents.

View File

@@ -52,8 +52,14 @@ pub fn Content(App: type) type {
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void { fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx)); const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) { switch (event) {
.about => model.page = .about, .about => {
.blog => model.page = .blog, model.page = .about;
model.document = .init(@embedFile("about"));
},
.blog => {
model.page = .blog;
model.document = .init(@embedFile("blog"));
},
else => {}, else => {},
} }
this.page = model.page; this.page = model.page;
@@ -63,10 +69,7 @@ pub fn Content(App: type) type {
_ = ctx; _ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
const text = switch (model.page) { const text = model.document.content;
.about => @embedFile("about"),
.blog => @embedFile("blog"),
};
var index: usize = 0; var index: usize = 0;
for (0..size.y) |row| { for (0..size.y) |row| {
@@ -89,12 +92,8 @@ pub fn Content(App: type) type {
pub fn Title(App: type) type { pub fn Title(App: type) type {
return struct { return struct {
text: []const u8,
pub fn init() @This() { pub fn init() @This() {
return .{ return .{};
.text = "<Title>",
};
} }
pub fn element(this: *@This()) App.Element { pub fn element(this: *@This()) App.Element {
@@ -106,10 +105,13 @@ pub fn Title(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)); const this: *const @This() = @ptrCast(@alignCast(ctx));
_ = this;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y)); assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
for (0.., this.text) |idx, cp| {
if (model.document.title) |text| {
for (0.., text) |idx, cp| {
cells[idx].style.fg = .green; cells[idx].style.fg = .green;
cells[idx].style.emphasis = &.{.bold}; cells[idx].style.emphasis = &.{.bold};
cells[idx].cp = cp; cells[idx].cp = cp;
@@ -118,6 +120,7 @@ pub fn Title(App: type) type {
if (idx == cells.len - 1) break; if (idx == cells.len - 1) break;
} }
} }
}
}; };
} }

View File

@@ -6,7 +6,9 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var app: App = .init(.{
.document = .init(@embedFile("blog")),
});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();
@@ -18,6 +20,8 @@ pub fn main() !void {
}, },
}, .{}); }, .{});
defer container.deinit(); defer container.deinit();
var content_container: App.Container = undefined;
defer content_container.deinit();
// header with navigation buttons and content's title // header with navigation buttons and content's title
{ {
@@ -60,7 +64,7 @@ pub fn main() !void {
// main actual tui_website page content // main actual tui_website page content
{ {
var content: Content = .init(allocator); var content: Content = .init(allocator);
const content_container: App.Container = try .init(allocator, .{}, content.element()); content_container = try .init(allocator, .{}, content.element());
var scrollable: App.Scrollable = .init(content_container, .enabled(.green)); var scrollable: App.Scrollable = .init(content_container, .enabled(.green));
// intermediate container for *padding* containing the scrollable `Content` // intermediate container for *padding* containing the scrollable `Content`

View File

@@ -1,6 +1,48 @@
page: Pages = .blog, page: Pages = .blog,
document: Document = undefined,
pub const Pages = enum { pub const Pages = enum {
about, about,
blog, blog,
}; };
pub const Document = struct {
// preemble:
title: ?[]const u8 = null,
date: ?[]const u8 = null,
content: []const u8,
pub fn init(content: []const u8) @This() {
if (std.mem.startsWith(u8, content, "---\n")) {
var document: @This() = .{ .content = undefined };
// we assume the content includes a preemble
var iter = std.mem.splitSequence(u8, content, "---\n");
assert(iter.next().?.len == 0);
if (iter.next()) |preemble| {
var lines = std.mem.splitScalar(u8, preemble, '\n');
while (lines.next()) |line| {
var entry = std.mem.splitSequence(u8, line, ": ");
const key = entry.next().?;
const value = entry.next();
assert(entry.next() == null);
if (std.meta.stringToEnum(Preemble, key)) |k| switch (k) {
.title => document.title = value,
.date => document.date = value,
};
}
}
document.content = iter.next().?;
return document;
} else return .{
.content = content,
};
}
};
const Preemble = enum {
title,
date,
};
const std = @import("std");
const assert = std.debug.assert;