From 392659e22c4530080e03e964c1bb3ff24a59d840 Mon Sep 17 00:00:00 2001 From: Hong Shick Pak Date: Sat, 26 Oct 2024 21:00:21 -0700 Subject: [PATCH] fix: make it work again --- docs/feed.xml | 4 ++-- docs/post/new-blog/index.html | 2 +- posts/0001-new-blog.md | 1 + src/atom.zig | 7 +++++- src/posts.zig | 45 ++++++++++++++++++++--------------- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/docs/feed.xml b/docs/feed.xml index 5dff4f8..e5f1463 100644 --- a/docs/feed.xml +++ b/docs/feed.xml @@ -2,7 +2,7 @@ Hong's Blog -December 28, 2022 +October 27, 2024 Hong Shick Pak @@ -333,7 +333,7 @@ in the future.</p> New Blog Oct 31, 2021 - November 6, 2021 + Nov 6, 2021 https://hspak.dev/post/new-blog/ diff --git a/docs/post/new-blog/index.html b/docs/post/new-blog/index.html index 6e0e4a2..ffeb762 100644 --- a/docs/post/new-blog/index.html +++ b/docs/post/new-blog/index.html @@ -26,7 +26,7 @@

New Blog

-
Oct 31, 2021 (Updated at: November 6, 2021)
+
Oct 31, 2021 (Updated at: Nov 6, 2021)

Hello world!

This will now be my third blog re-write and each have only ever had a single diff --git a/posts/0001-new-blog.md b/posts/0001-new-blog.md index beeee3a..d226b37 100644 --- a/posts/0001-new-blog.md +++ b/posts/0001-new-blog.md @@ -3,6 +3,7 @@ Title: New Blog Description: Creating yaks for me to shave or how many more blog rewrites am I going to go through? Draft: false Publish Date: Oct 31, 2021 +Updated Date: Nov 6, 2021 --- Hello world! diff --git a/src/atom.zig b/src/atom.zig index 0dcbcef..b3923f5 100644 --- a/src/atom.zig +++ b/src/atom.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Posts = @import("posts.zig").Posts; +const PlaceholderText = @import("posts.zig").PlaceholderText; const time = @import("time.zig"); pub const Atom = struct { @@ -61,7 +62,11 @@ pub const Atom = struct { _ = try stream.write("\n"); try stream.print(" {s}\n", .{item.meta.title}); try stream.print(" {s}\n", .{item.meta.created_at}); - try stream.print(" {s}\n", .{item.updated_at}); + if (!std.mem.eql(u8, item.meta.updated_at, PlaceholderText)) { + try stream.print(" {s}\n", .{item.meta.updated_at}); + } else { + try stream.print(" {s}\n", .{item.meta.created_at}); + } try stream.print( \\ , .{item.meta.name}); diff --git a/src/posts.zig b/src/posts.zig index f9423ea..34c4e24 100644 --- a/src/posts.zig +++ b/src/posts.zig @@ -4,7 +4,7 @@ const partials = @import("partials.zig"); const time = @import("time.zig"); const koino = @import("koino"); -const PlaceholderText = "missing!!!"; +pub const PlaceholderText = "missing!!!"; const PostError = error{ MissingName, MissingTitle, @@ -18,7 +18,7 @@ pub const Posts = struct { const Self = @This(); pub fn init(allocator: std.mem.Allocator, path: []const u8) !Posts { - var posts_dir = fs.openDirAbsolute(path, .{ .iterate = true, .access_sub_paths = false, .no_follow = true }) catch |err| switch (err) { + var posts_dir = std.fs.cwd().openDir(path, .{ .iterate = true, .access_sub_paths = false, .no_follow = true }) catch |err| switch (err) { error.FileNotFound => return PostError.MissingPosts, else => unreachable, }; @@ -29,7 +29,7 @@ pub const Posts = struct { while (try iter.next()) |next| { const post_path = try fs.path.join(allocator, &[_][]const u8{ path, next.name }); const post = try allocator.create(Post); - post.* = try Post.init(allocator, post_path); + post.* = try Post.init(allocator, post_path, next.name); try list.append(post); } std.sort.insertion(*Post, list.items, {}, newerFile); @@ -45,6 +45,12 @@ pub const Posts = struct { } pub fn writePost(self: Self) !void { + // Just nuke the directories so we are guaranteed to never be stale. + try fs.cwd().deleteTree("docs/draft"); + try fs.cwd().deleteTree("docs/post"); + try fs.cwd().makeDir("docs/draft"); + try fs.cwd().makeDir("docs/post"); + for (self.list.items) |post| { try post.printPost(); } @@ -78,7 +84,6 @@ const Post = struct { allocator: std.mem.Allocator, full_path: []const u8, file: fs.File, - updated_at: []const u8, stat: fs.File.Stat, parsedHTML: std.ArrayList(u8), meta: struct { @@ -87,19 +92,20 @@ const Post = struct { desc: []const u8, draft: bool, created_at: []const u8, + updated_at: []const u8, }, + id: u16, - pub fn init(allocator: std.mem.Allocator, full_path: []const u8) !Post { + pub fn init(allocator: std.mem.Allocator, full_path: []const u8, file_path: []const u8) !Post { var post_file = try fs.cwd().openFile(full_path, .{ .mode = .read_only }); const stat = try post_file.stat(); - const updated_at = try time.formatUnixTime(allocator, stat.mtime); + const id = try std.fmt.parseUnsigned(u16, file_path[0..4], 10); var post = Post{ .allocator = allocator, .full_path = full_path, .file = post_file, .parsedHTML = std.ArrayList(u8).init(allocator), - .updated_at = updated_at, .stat = stat, .meta = .{ .name = PlaceholderText, @@ -107,7 +113,9 @@ const Post = struct { .desc = PlaceholderText, .draft = true, .created_at = PlaceholderText, + .updated_at = PlaceholderText, }, + .id = id, }; try post.parsePost(); return post; @@ -118,16 +126,11 @@ const Post = struct { } pub fn printPost(self: *Self) !void { - const postState = if (self.meta.draft) "draft" else "post"; - const post_dir_path = try fs.path.join(self.allocator, &[_][]const u8{ "docs", postState, self.meta.name }); + const post_state = if (self.meta.draft) "draft" else "post"; + const posts_dir = try fs.path.join(self.allocator, &[_][]const u8{ "docs", post_state }); + const post_dir_path = try fs.path.join(self.allocator, &[_][]const u8{ posts_dir, self.meta.name }); const post_index_path = try fs.path.join(self.allocator, &[_][]const u8{ post_dir_path, "index.html" }); - fs.cwd().makeDir(post_dir_path) catch |err| switch (err) { - error.PathAlreadyExists => {}, - else => return err, - }; - fs.cwd().deleteFile(post_index_path) catch |err| switch (err) { - else => {}, - }; + try fs.cwd().makeDir(post_dir_path); var output_file: std.fs.File = undefined; std.debug.print("[ ] creating: {s}\n", .{post_index_path}); @@ -137,8 +140,8 @@ const Post = struct { try partials.writeHeader(output_file, false, self.meta.title); var updated = std.ArrayList(u8).init(self.allocator); - if (!std.mem.eql(u8, self.meta.created_at, self.updated_at)) { - try updated.writer().print("(Updated at: {s})", .{self.updated_at}); + if (!std.mem.eql(u8, self.meta.created_at, self.meta.updated_at) and !std.mem.eql(u8, self.meta.updated_at, PlaceholderText)) { + try updated.writer().print("(Updated at: {s})", .{self.meta.updated_at}); } const stream = output_file.writer(); try stream.print( @@ -221,6 +224,10 @@ const Post = struct { var iter = std.mem.split(u8, line, ":"); _ = iter.next().?; self.meta.created_at = try self.trimWhitespace(iter.next().?); + } else if (std.mem.eql(u8, line[0..13], "Updated Date:")) { + var iter = std.mem.split(u8, line, ":"); + _ = iter.next().?; + self.meta.updated_at = try self.trimWhitespace(iter.next().?); } return true; } @@ -257,5 +264,5 @@ const Post = struct { }; fn newerFile(_: void, p1: *Post, p2: *Post) bool { - return p1.stat.mtime > p2.stat.mtime; + return p1.id > p2.id; }