@@ 104,6 104,8 @@ pub fn main() !void {
directory_name,
entry.name,
});
+ defer allocator.free(path);
+
var note = Note.init(entry.name, allocator);
try readBody(path, ¬e.body);
try notes.append(note);
@@ 111,8 113,13 @@ pub fn main() !void {
}
// Populate all note metadata required to generate a new note body.
+ // Populate in two passes, references first, because backlinks will look
+ // for references to the current note.
+ for (notes.items) |*note| {
+ try note.populateReferences(allocator);
+ }
for (notes.items) |*note| {
- try note.populateNoteData(notes, allocator);
+ try note.populateBacklinks(notes, allocator);
}
// Assemble and write new note bodies.
@@ 121,7 128,7 @@ pub fn main() !void {
defer buf.deinit();
try note.generateBody(&buf, allocator);
- note.deinit();
+ note.deinitData(allocator);
// Optional debugging.
if (debug_mode) {
@@ 88,12 88,15 @@ fn appendRef(buf: *util.CharBuffer, referent: []const u8, allocator: mem.Allocat
util.ref_prefix,
referent,
});
+ defer allocator.free(ref);
+
try buf.appendSlice(ref);
try buf.append('\n');
}
pub const Note = struct {
name: []const u8,
+
body: util.CharBuffer,
backlinks: std.ArrayList([]const u8),
references: std.ArrayList([]const u8),
@@ 107,9 110,12 @@ pub const Note = struct {
};
}
- pub fn deinit(self: @This()) void {
+ pub fn deinitData(self: @This(), allocator: mem.Allocator) void {
self.body.deinit();
self.backlinks.deinit();
+ for (self.references.items) |reference| {
+ allocator.free(reference);
+ }
self.references.deinit();
}
@@ 205,15 211,15 @@ pub const Note = struct {
buf: *util.CharBuffer,
allocator: mem.Allocator,
) !void {
- var found_one = false;
+ var has_a_backlink = false;
for (self.backlinks.items) |backlink| {
- found_one = true;
+ has_a_backlink = true;
try appendRef(buf, backlink, allocator);
}
// Backlinks padding.
- if (found_one) {
+ if (has_a_backlink) {
try buf.append('\n');
}
}
@@ 236,6 242,7 @@ pub const Note = struct {
// Dedupe lowered and escaped references so we only link out once per
// reference.
var seen = std.BufSet.init(allocator);
+ defer seen.deinit();
// Escape and add forward references to note.
for (self.references.items) |phrase| {
@@ 253,11 260,6 @@ pub const Note = struct {
}
}
- pub fn populateNoteData(self: *@This(), notes: std.ArrayList(Note), allocator: mem.Allocator) !void {
- try self.populateBacklinks(notes, allocator);
- try self.populateReferences(allocator);
- }
-
pub fn generateBody(
self: *@This(),
buf: *util.CharBuffer,