Skip to content

Commit 392659e

Browse files
committed
fix: make it work again
1 parent 2400779 commit 392659e

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

docs/feed.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feed xmlns="http://www.w3.org/2005/Atom">
33
<title>Hong's Blog</title>
44
<link href="https://hspak.dev/"/>
5-
<updated>December 28, 2022</updated>
5+
<updated>October 27, 2024</updated>
66
<author>
77
<name>Hong Shick Pak</name>
88
</author>
@@ -333,7 +333,7 @@ in the future.&lt;/p&gt;
333333
<entry>
334334
<title>New Blog</title>
335335
<published>Oct 31, 2021</published>
336-
<updated>November 6, 2021</updated>
336+
<updated>Nov 6, 2021</updated>
337337
<link href="https://hspak.dev/post/new-blog/" type="text/html"/>
338338
<id>https://hspak.dev/post/new-blog/</id>
339339
<content type="html">

docs/post/new-blog/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</div>
2727
<div class="block">
2828
<h2>New Blog</h2>
29-
<div class="date">Oct 31, 2021 (Updated at: November 6, 2021)</div>
29+
<div class="date">Oct 31, 2021 (Updated at: Nov 6, 2021)</div>
3030
<div class="body">
3131
<p>Hello world!</p>
3232
<p>This will now be my third blog re-write and each have only ever had a single

posts/0001-new-blog.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Title: New Blog
33
Description: Creating yaks for me to shave or how many more blog rewrites am I going to go through?
44
Draft: false
55
Publish Date: Oct 31, 2021
6+
Updated Date: Nov 6, 2021
67
---
78

89
Hello world!

src/atom.zig

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22
const Posts = @import("posts.zig").Posts;
3+
const PlaceholderText = @import("posts.zig").PlaceholderText;
34
const time = @import("time.zig");
45

56
pub const Atom = struct {
@@ -61,7 +62,11 @@ pub const Atom = struct {
6162
_ = try stream.write("<entry>\n");
6263
try stream.print(" <title>{s}</title>\n", .{item.meta.title});
6364
try stream.print(" <published>{s}</published>\n", .{item.meta.created_at});
64-
try stream.print(" <updated>{s}</updated>\n", .{item.updated_at});
65+
if (!std.mem.eql(u8, item.meta.updated_at, PlaceholderText)) {
66+
try stream.print(" <updated>{s}</updated>\n", .{item.meta.updated_at});
67+
} else {
68+
try stream.print(" <updated>{s}</updated>\n", .{item.meta.created_at});
69+
}
6570
try stream.print(
6671
\\ <link href="https://hspak.dev/post/{s}/" type="text/html"/>
6772
, .{item.meta.name});

src/posts.zig

+26-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const partials = @import("partials.zig");
44
const time = @import("time.zig");
55
const koino = @import("koino");
66

7-
const PlaceholderText = "missing!!!";
7+
pub const PlaceholderText = "missing!!!";
88
const PostError = error{
99
MissingName,
1010
MissingTitle,
@@ -18,7 +18,7 @@ pub const Posts = struct {
1818
const Self = @This();
1919

2020
pub fn init(allocator: std.mem.Allocator, path: []const u8) !Posts {
21-
var posts_dir = fs.openDirAbsolute(path, .{ .iterate = true, .access_sub_paths = false, .no_follow = true }) catch |err| switch (err) {
21+
var posts_dir = std.fs.cwd().openDir(path, .{ .iterate = true, .access_sub_paths = false, .no_follow = true }) catch |err| switch (err) {
2222
error.FileNotFound => return PostError.MissingPosts,
2323
else => unreachable,
2424
};
@@ -29,7 +29,7 @@ pub const Posts = struct {
2929
while (try iter.next()) |next| {
3030
const post_path = try fs.path.join(allocator, &[_][]const u8{ path, next.name });
3131
const post = try allocator.create(Post);
32-
post.* = try Post.init(allocator, post_path);
32+
post.* = try Post.init(allocator, post_path, next.name);
3333
try list.append(post);
3434
}
3535
std.sort.insertion(*Post, list.items, {}, newerFile);
@@ -45,6 +45,12 @@ pub const Posts = struct {
4545
}
4646

4747
pub fn writePost(self: Self) !void {
48+
// Just nuke the directories so we are guaranteed to never be stale.
49+
try fs.cwd().deleteTree("docs/draft");
50+
try fs.cwd().deleteTree("docs/post");
51+
try fs.cwd().makeDir("docs/draft");
52+
try fs.cwd().makeDir("docs/post");
53+
4854
for (self.list.items) |post| {
4955
try post.printPost();
5056
}
@@ -78,7 +84,6 @@ const Post = struct {
7884
allocator: std.mem.Allocator,
7985
full_path: []const u8,
8086
file: fs.File,
81-
updated_at: []const u8,
8287
stat: fs.File.Stat,
8388
parsedHTML: std.ArrayList(u8),
8489
meta: struct {
@@ -87,27 +92,30 @@ const Post = struct {
8792
desc: []const u8,
8893
draft: bool,
8994
created_at: []const u8,
95+
updated_at: []const u8,
9096
},
97+
id: u16,
9198

92-
pub fn init(allocator: std.mem.Allocator, full_path: []const u8) !Post {
99+
pub fn init(allocator: std.mem.Allocator, full_path: []const u8, file_path: []const u8) !Post {
93100
var post_file = try fs.cwd().openFile(full_path, .{ .mode = .read_only });
94101
const stat = try post_file.stat();
95-
const updated_at = try time.formatUnixTime(allocator, stat.mtime);
102+
const id = try std.fmt.parseUnsigned(u16, file_path[0..4], 10);
96103

97104
var post = Post{
98105
.allocator = allocator,
99106
.full_path = full_path,
100107
.file = post_file,
101108
.parsedHTML = std.ArrayList(u8).init(allocator),
102-
.updated_at = updated_at,
103109
.stat = stat,
104110
.meta = .{
105111
.name = PlaceholderText,
106112
.title = PlaceholderText,
107113
.desc = PlaceholderText,
108114
.draft = true,
109115
.created_at = PlaceholderText,
116+
.updated_at = PlaceholderText,
110117
},
118+
.id = id,
111119
};
112120
try post.parsePost();
113121
return post;
@@ -118,16 +126,11 @@ const Post = struct {
118126
}
119127

120128
pub fn printPost(self: *Self) !void {
121-
const postState = if (self.meta.draft) "draft" else "post";
122-
const post_dir_path = try fs.path.join(self.allocator, &[_][]const u8{ "docs", postState, self.meta.name });
129+
const post_state = if (self.meta.draft) "draft" else "post";
130+
const posts_dir = try fs.path.join(self.allocator, &[_][]const u8{ "docs", post_state });
131+
const post_dir_path = try fs.path.join(self.allocator, &[_][]const u8{ posts_dir, self.meta.name });
123132
const post_index_path = try fs.path.join(self.allocator, &[_][]const u8{ post_dir_path, "index.html" });
124-
fs.cwd().makeDir(post_dir_path) catch |err| switch (err) {
125-
error.PathAlreadyExists => {},
126-
else => return err,
127-
};
128-
fs.cwd().deleteFile(post_index_path) catch |err| switch (err) {
129-
else => {},
130-
};
133+
try fs.cwd().makeDir(post_dir_path);
131134

132135
var output_file: std.fs.File = undefined;
133136
std.debug.print("[ ] creating: {s}\n", .{post_index_path});
@@ -137,8 +140,8 @@ const Post = struct {
137140
try partials.writeHeader(output_file, false, self.meta.title);
138141

139142
var updated = std.ArrayList(u8).init(self.allocator);
140-
if (!std.mem.eql(u8, self.meta.created_at, self.updated_at)) {
141-
try updated.writer().print("(Updated at: {s})", .{self.updated_at});
143+
if (!std.mem.eql(u8, self.meta.created_at, self.meta.updated_at) and !std.mem.eql(u8, self.meta.updated_at, PlaceholderText)) {
144+
try updated.writer().print("(Updated at: {s})", .{self.meta.updated_at});
142145
}
143146
const stream = output_file.writer();
144147
try stream.print(
@@ -221,6 +224,10 @@ const Post = struct {
221224
var iter = std.mem.split(u8, line, ":");
222225
_ = iter.next().?;
223226
self.meta.created_at = try self.trimWhitespace(iter.next().?);
227+
} else if (std.mem.eql(u8, line[0..13], "Updated Date:")) {
228+
var iter = std.mem.split(u8, line, ":");
229+
_ = iter.next().?;
230+
self.meta.updated_at = try self.trimWhitespace(iter.next().?);
224231
}
225232
return true;
226233
}
@@ -257,5 +264,5 @@ const Post = struct {
257264
};
258265

259266
fn newerFile(_: void, p1: *Post, p2: *Post) bool {
260-
return p1.stat.mtime > p2.stat.mtime;
267+
return p1.id > p2.id;
261268
}

0 commit comments

Comments
 (0)