@@ -23,6 +23,7 @@ const Humanize = @import("../humanize.zig");
const Ini = @import("../ini.zig");
const Repos = @import("../repos.zig");
const Git = @import("../git.zig");
const Highlighting = @import("../syntax-highlight.zig");
const Commits = @import("repos/commits.zig");
const Diffs = @import("repos/diffs.zig");
@@ -308,21 +309,6 @@ fn treeBlob(ctx: *Context) Error!void {
return tree(ctx, &repo, &files);
}
fn guessLang(name: []const u8) ?[]const u8 {
if (std.mem.endsWith(u8, name, ".zig")) {
return "zig";
} else if (std.mem.endsWith(u8, name, ".html")) {
return "html";
} else if (std.mem.endsWith(u8, name, ".h")) {
return "cpp";
} else if (std.mem.endsWith(u8, name, ".c")) {
return "c";
} else if (std.mem.endsWith(u8, name, ".cpp")) {
return "cpp";
}
return null;
}
const BlameCommit = struct {
sha: []const u8,
parent: ?[]const u8 = null,
@@ -423,8 +409,8 @@ fn blame(ctx: *Context) Error!void {
try source_lines.append('\n');
}
const formatted = if (guessLang(blame_file)) |lang| fmt: {
var pre = try highlight(ctx.alloc, lang, source_lines.items);
const formatted = if (Highlighting.Language.guessFromFilename(blame_file)) |lang| fmt: {
var pre = try Highlighting.highlight(ctx.alloc, lang, source_lines.items);
break :fmt pre[28..][0 .. pre.len - 38];
} else Bleach.sanitizeAlloc(ctx.alloc, source_lines.items, .{}) catch return error.Unknown;
@@ -451,44 +437,6 @@ fn blame(ctx: *Context) Error!void {
try ctx.sendTemplate(&tmpl);
}
fn highlight(a: Allocator, lang: []const u8, text: []const u8) ![]u8 {
var child = std.ChildProcess.init(&[_][]const u8{ "pygmentize", "-f", "html", "-l", lang }, a);
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Ignore;
child.expand_arg0 = .no_expand;
child.spawn() catch unreachable;
const err_mask = std.posix.POLL.ERR | std.posix.POLL.NVAL | std.posix.POLL.HUP;
var poll_fd = [_]std.posix.pollfd{
.{
.fd = child.stdout.?.handle,
.events = std.posix.POLL.IN,
.revents = undefined,
},
};
_ = std.posix.write(child.stdin.?.handle, text) catch unreachable;
std.posix.close(child.stdin.?.handle);
child.stdin = null;
var buf = std.ArrayList(u8).init(a);
const abuf = try a.alloc(u8, 0xffffff);
while (true) {
const events_len = std.posix.poll(&poll_fd, std.math.maxInt(i32)) catch unreachable;
if (events_len == 0) continue;
if (poll_fd[0].revents & std.posix.POLL.IN != 0) {
const amt = std.posix.read(poll_fd[0].fd, abuf) catch unreachable;
if (amt == 0) break;
try buf.appendSlice(abuf[0..amt]);
} else if (poll_fd[0].revents & err_mask != 0) {
break;
}
}
a.free(abuf);
_ = child.wait() catch unreachable;
return try buf.toOwnedSlice();
}
fn wrapLineNumbersBlame(
a: Allocator,
blames: []BlameLine,
@@ -561,8 +509,8 @@ fn blob(ctx: *Context, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
const d2 = reader.readAllAlloc(ctx.alloc, 0xffffff) catch unreachable;
if (guessLang(blb.name)) |lang| {
const pre = try highlight(ctx.alloc, lang, d2);
if (Highlighting.Language.guessFromFilename(blb.name)) |lang| {
const pre = try Highlighting.highlight(ctx.alloc, lang, d2);
formatted = pre[28..][0 .. pre.len - 38];
} else {
formatted = Bleach.sanitizeAlloc(ctx.alloc, d2, .{}) catch return error.Unknown;