From 2ccea4e0a128225ab6877aa3b9ffebef9f81e213 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Fri, 2 Oct 2026 10:28:11 +0200 Subject: let formatLink handle url fragments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- src/main.zig | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 60aa3a0..7a90f95 100644 --- a/src/main.zig +++ b/src/main.zig @@ -361,7 +361,7 @@ const Cli = struct { ); for (back_refs.?) |back_ref| { log.debug("{s}: backref path={s}", .{ target.path, back_ref }); - const back_ref_ = try formatRef(gpa, back_ref, LINK_PREFIX, ""); + const back_ref_ = try formatRef(gpa, back_ref, LINK_PREFIX); defer gpa.free(back_ref_); try buf.writer.print( \\{s} @@ -409,7 +409,7 @@ const Cli = struct { for (index.items) |e| { if (std.mem.eql(u8, std.fs.path.stem(e), std.fs.path.stem(target.path))) continue; log.debug("{s}: index path={s}", .{ target.path, e }); - const e_ = try formatRef(gpa, std.fs.path.stem(e), LINK_PREFIX, ""); + const e_ = try formatRef(gpa, std.fs.path.stem(e), LINK_PREFIX); defer gpa.free(e_); try buf.writer.print( \\
  • {s}
  • @@ -869,12 +869,7 @@ const Builder = struct { } } - const ref_ = try formatRef( - gpa, - ref, - self.options.link_prefix, - if (fragment) |i| ref_str[i..] else "", - ); + const ref_ = try formatRef(gpa, ref_str, self.options.link_prefix); defer gpa.free(ref_); try w.writeAll(ref_); @@ -1013,20 +1008,23 @@ fn formatName(gpa: Allocator, name: []const u8) ![]u8 { return try std.mem.replaceOwned(u8, gpa, name, "_", " "); } -fn formatLink(gpa: Allocator, name: []const u8, prefix: []const u8) ![]u8 { - const link_name = try std.fmt.allocPrint(gpa, "{s}.html", .{name}); +fn formatLink(gpa: Allocator, name_str: []const u8, prefix: []const u8) ![]u8 { + const fragment = std.mem.indexOf(u8, name_str, "#"); + const name = if (fragment) |i| name_str[0..i] else name_str; + + const link_name = try std.fmt.allocPrint(gpa, "{s}.html{s}", .{ name, if (fragment) |i| name_str[i..] else "" }); defer gpa.free(link_name); return try std.fs.path.join(gpa, &.{ prefix, link_name }); } -fn formatRef(gpa: Allocator, name: []const u8, link_prefix: []const u8, fragment: []const u8) ![]u8 { +fn formatRef(gpa: Allocator, name: []const u8, link_prefix: []const u8) ![]u8 { const link = try formatLink(gpa, name, link_prefix); defer gpa.free(link); const text = try formatName(gpa, name); defer gpa.free(text); return try std.fmt.allocPrint(gpa, - \\{s} - , .{ link, fragment, text }); + \\{s} + , .{ link, text }); } fn targetExists( -- cgit 1.4.1