From 5ee2c6662e11e235951915057be659c8986ed01a Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Sat, 8 Nov 2025 12:50:25 +0100 Subject: [PATCH] feat: invalid page for unavailable resources Accessing resources which are not available, the invalid page will be rendered instead. The original attempt for access will be logged. --- src/content.zig | 10 ++++++++-- src/main.zig | 21 ++++++++++++++++++--- src/model.zig | 6 ++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/content.zig b/src/content.zig index 0eb6699..452dce9 100644 --- a/src/content.zig +++ b/src/content.zig @@ -57,10 +57,16 @@ pub fn Content(App: type) type { }, .blog => |path| { model.page.deinit(this.allocator); + model.document.deinit(this.allocator); + errdefer { + if (path) |p| this.allocator.free(p); + model.document = .invalidPage; + model.page = .{ .blog = null }; + } + + model.document = .init(try std.fs.cwd().readFileAlloc(if (path) |p| p else "./doc/blog.md", this.allocator, .unlimited)); 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 => {}, diff --git a/src/main.zig b/src/main.zig index 205f792..f8382aa 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,11 @@ -// usage: tui_website +// TODO +// - should the path containing the contents of the tui-website reside in the +// hardcode `./doc` directory or shall this become a compilation argument to +// allow for custom directory locations? + +// usage: tui_website +// : partial path to the document without the file extension and the beginning `./doc` path +// ending `/` (as used from the web-world) are trimmed automatically pub fn main() !void { errdefer |err| log.err("Application Error: {any}", .{err}); @@ -106,7 +113,11 @@ pub fn main() !void { // -> enforce a specific structure? // -> in case an invalid path is provided the 404 error page shall be shown // -> reporte an corresponding error! (such that I can see that in the log) - app.postEvent(.{ .blog = try allocator.dupe(u8, path) }); + var blog = path[0..path.len]; + blog = std.mem.trimStart(u8, blog, "./"); + blog = std.mem.trimEnd(u8, blog, ".md"); + blog = std.mem.trimEnd(u8, blog, "/"); + app.postEvent(.{ .blog = try std.fmt.allocPrint(allocator, "./doc/{s}.md", .{blog}) }); } arg_it.deinit(); @@ -127,10 +138,12 @@ pub fn main() !void { // pre event handling switch (event) { .key => |key| { - if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(); + if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit(); // test if the event handling is working correctly if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = allocator.dupe(u8, "./doc/test.md") catch unreachable }); }, + .about => log.info(ResourceRequestFormat, .{"./doc/about.md"}), + .blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}), .err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }), else => {}, } @@ -161,6 +174,8 @@ pub const panic = App.panic_handler; pub const std_options = zlog.std_options; const log = std.log.scoped(.default); +const ResourceRequestFormat = "Requesting resource: `{s}`"; + const std = @import("std"); const assert = std.debug.assert; const zlog = @import("zlog"); diff --git a/src/model.zig b/src/model.zig index b08dbb1..555edc4 100644 --- a/src/model.zig +++ b/src/model.zig @@ -33,6 +33,12 @@ pub const Document = struct { date, }; + pub const invalidPage: @This() = .{ + .title = "Page not found", + .content = "Requested page does not exist", + .ptr = undefined, + }; + pub fn init(content: []const u8) @This() { if (std.mem.startsWith(u8, content, "---\n")) { var document: @This() = .{ .ptr = content };