about summary refs log tree commit diff
path: root/src/exe.zig
blob: d5bc239ceba6e8232192dfcef4a1646d985a0532 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const std = @import("std");

const log = std.log.scoped(.exe);
const Context = @import("main.zig").Context;
const fs = @import("fs.zig");

pub fn run_pandoc(ctx: *Context) !void {
    ctx.progress_start("Processing markdown files");
    defer ctx.progress_end();

    var pool: std.Thread.Pool = undefined;
    try pool.init(.{
        .n_jobs = (try std.Thread.getCpuCount()) - 1,
        .allocator = ctx.allocator(),
    });
    defer pool.deinit();

    for (ctx.markdown.items) |*page| {
        // the inline struct trick is a workardoun for Thread.Pool::spawn not
        // accepting functions that can error:
        // https://github.com/ziglang/zig/issues/18810
        try pool.spawn(struct {
            fn worker(worker_ctx: *Context, worker_page: *const []const u8) void {
                pandoc_worker(worker_ctx, worker_page.*) catch |err| std.log.err("pandoc worker failed with: {}", .{err});
            }
        }.worker, .{ ctx, page });
    }

    log.info("Processed {d} markdown files.", .{ctx.markdown.items.len});
}

fn pandoc_worker(ctx: *Context, page: []const u8) !void {
    const source_path = try std.fs.path.join(ctx.allocator(), &.{ Context.params.PAGE_PATH, page });
    defer ctx.allocator().free(source_path);
    const target_path = try fs.replace_extension(ctx.allocator(), source_path, "htm");
    defer ctx.allocator().free(target_path);

    const f = try fs.FileSwapper.createAt(ctx.allocator(), target_path);
    defer f.close();

    try f.writer().print(
        \\<!-- Generated file, you may edit and discard {s} :) -->
        \\
    ,
        .{page},
    );

    // get mtime of the source document
    const mtime = try fs.get_mtime(ctx.allocator(), source_path);
    const mtime_str = try fs.format_mtime(ctx.allocator(), mtime, "%s");
    defer ctx.allocator().free(mtime_str);

    // make pandoc runs reproducable
    var env_map = std.process.EnvMap.init(ctx.allocator());
    defer env_map.deinit();
    try env_map.put("SOURCE_DATE_EPOCH", mtime_str);

    try capture_stdout(ctx, &.{
        Context.params.PANDOC,
        "--from",
        "markdown+wikilinks_title_after_pipe+autolink_bare_uris",
        "--to",
        "html5",
        "--lua-filter",
        // TODO: embed the preproc filter
        Context.params.PANDOC_PREPROC,
        "--strip-comments",
        "--output=-",
        source_path,
    }, &env_map, f.writer().any());
}

pub fn capture_stdout(ctx: *Context, argv: []const []const u8, env_map: ?*std.process.EnvMap, buf: std.io.AnyWriter) !void {
    const command = try std.mem.join(ctx.allocator(), " ", argv);
    defer ctx.allocator().free(command);
    const label = try std.fmt.allocPrint(ctx.allocator(), "running {s}", .{command});
    defer ctx.allocator().free(label);

    ctx.progress_start(label);
    defer ctx.progress_end();

    var p = std.process.Child.init(argv, std.heap.page_allocator);
    p.stdout_behavior = .Pipe;
    p.progress_node = ctx.progress_nodes.getLast();
    p.env_map = env_map;

    try p.spawn();

    try buf.writeFile(p.stdout.?);

    const result = try p.wait();
    _ = result;
}