@@ -4,7 +4,7 @@ const partials = @import("partials.zig");
4
4
const time = @import ("time.zig" );
5
5
const koino = @import ("koino" );
6
6
7
- const PlaceholderText = "missing!!!" ;
7
+ pub const PlaceholderText = "missing!!!" ;
8
8
const PostError = error {
9
9
MissingName ,
10
10
MissingTitle ,
@@ -18,7 +18,7 @@ pub const Posts = struct {
18
18
const Self = @This ();
19
19
20
20
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 ) {
22
22
error .FileNotFound = > return PostError .MissingPosts ,
23
23
else = > unreachable ,
24
24
};
@@ -29,7 +29,7 @@ pub const Posts = struct {
29
29
while (try iter .next ()) | next | {
30
30
const post_path = try fs .path .join (allocator , &[_ ][]const u8 { path , next .name });
31
31
const post = try allocator .create (Post );
32
- post .* = try Post .init (allocator , post_path );
32
+ post .* = try Post .init (allocator , post_path , next . name );
33
33
try list .append (post );
34
34
}
35
35
std .sort .insertion (* Post , list .items , {}, newerFile );
@@ -45,6 +45,12 @@ pub const Posts = struct {
45
45
}
46
46
47
47
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
+
48
54
for (self .list .items ) | post | {
49
55
try post .printPost ();
50
56
}
@@ -78,7 +84,6 @@ const Post = struct {
78
84
allocator : std.mem.Allocator ,
79
85
full_path : []const u8 ,
80
86
file : fs.File ,
81
- updated_at : []const u8 ,
82
87
stat : fs.File.Stat ,
83
88
parsedHTML : std .ArrayList (u8 ),
84
89
meta : struct {
@@ -87,27 +92,30 @@ const Post = struct {
87
92
desc : []const u8 ,
88
93
draft : bool ,
89
94
created_at : []const u8 ,
95
+ updated_at : []const u8 ,
90
96
},
97
+ id : u16 ,
91
98
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 {
93
100
var post_file = try fs .cwd ().openFile (full_path , .{ .mode = .read_only });
94
101
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 );
96
103
97
104
var post = Post {
98
105
.allocator = allocator ,
99
106
.full_path = full_path ,
100
107
.file = post_file ,
101
108
.parsedHTML = std .ArrayList (u8 ).init (allocator ),
102
- .updated_at = updated_at ,
103
109
.stat = stat ,
104
110
.meta = .{
105
111
.name = PlaceholderText ,
106
112
.title = PlaceholderText ,
107
113
.desc = PlaceholderText ,
108
114
.draft = true ,
109
115
.created_at = PlaceholderText ,
116
+ .updated_at = PlaceholderText ,
110
117
},
118
+ .id = id ,
111
119
};
112
120
try post .parsePost ();
113
121
return post ;
@@ -118,16 +126,11 @@ const Post = struct {
118
126
}
119
127
120
128
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 });
123
132
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 );
131
134
132
135
var output_file : std.fs.File = undefined ;
133
136
std .debug .print ("[ ] creating: {s}\n " , .{post_index_path });
@@ -137,8 +140,8 @@ const Post = struct {
137
140
try partials .writeHeader (output_file , false , self .meta .title );
138
141
139
142
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 });
142
145
}
143
146
const stream = output_file .writer ();
144
147
try stream .print (
@@ -221,6 +224,10 @@ const Post = struct {
221
224
var iter = std .mem .split (u8 , line , ":" );
222
225
_ = iter .next ().? ;
223
226
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 ().? );
224
231
}
225
232
return true ;
226
233
}
@@ -257,5 +264,5 @@ const Post = struct {
257
264
};
258
265
259
266
fn newerFile (_ : void , p1 : * Post , p2 : * Post ) bool {
260
- return p1 .stat . mtime > p2 .stat . mtime ;
267
+ return p1 .id > p2 .id ;
261
268
}
0 commit comments