about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/exe.zig49
-rw-r--r--src/fs.zig9
-rw-r--r--src/gen.zig29
-rw-r--r--src/main.zig3
4 files changed, 63 insertions, 27 deletions
diff --git a/src/exe.zig b/src/exe.zig
index ef38a67..bfe7370 100644
--- a/src/exe.zig
+++ b/src/exe.zig
@@ -9,19 +9,40 @@ pub fn run_pandoc(ctx: *Context) !void {
     defer ctx.progress_end();
 
     for (ctx.markdown.items) |page| {
-        const targetpath = try fs.replace_extension(ctx.allocator(), page, "md");
-        defer ctx.allocator().free(targetpath);
+        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(), targetpath);
+        const f = try fs.FileSwapper.createAt(ctx.allocator(), target_path);
         defer f.close();
 
-        try run(ctx, &.{ "notify-send", "hi", page });
+        try f.writer().print(
+            \\<!-- Generated file, you may edit and discard {s} :) -->
+            \\
+        ,
+            .{page},
+        );
+
+        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,
+        }, f.writer().any());
     }
-    // @ echo "<!-- Generated file, you may edit and discard $< :) -->" >$@
-    // $(PANDOC) -fmarkdown+wikilinks_title_after_pipe+autolink_bare_uris -thtml5 --lua-filter=$(PANDOC_PREPROC) --strip-comments $< >>$@
+
+    log.info("Processed {d} markdown files.", .{ctx.markdown.items.len});
 }
 
-pub fn run(ctx: *Context, argv: []const []const u8) !void {
+pub fn capture_stdout(ctx: *Context, argv: []const []const u8, 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});
@@ -30,10 +51,14 @@ pub fn run(ctx: *Context, argv: []const []const u8) !void {
     ctx.progress_start(label);
     defer ctx.progress_end();
 
-    const result = try std.process.Child.run(.{
-        .argv = argv,
-        .allocator = std.heap.page_allocator,
-        .progress_node = ctx.progress_nodes.getLast(),
-    });
+    var p = std.process.Child.init(argv, std.heap.page_allocator);
+    p.stdout_behavior = .Pipe;
+    p.progress_node = ctx.progress_nodes.getLast();
+
+    try p.spawn();
+
+    try buf.writeFile(p.stdout.?);
+
+    const result = try p.wait();
     _ = result;
 }
diff --git a/src/fs.zig b/src/fs.zig
index c009afd..8ae9d8e 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -47,6 +47,13 @@ pub const FileSwapper = struct {
         self.tmp.sync() catch
             @panic("sync tmp file");
 
+        // check if target path exists
+        var found = true;
+        std.fs.cwd().access(self.target_path, .{}) catch |e| switch (e) {
+            error.FileNotFound => found = false,
+            else => @panic("unable to stat target path"),
+        };
+
         // swap file into final location
         std.fs.rename(
             std.fs.cwd(),
@@ -60,7 +67,7 @@ pub const FileSwapper = struct {
 
 pub fn replace_extension(alloc: std.mem.Allocator, p: []const u8, new: []const u8) ![]const u8 {
     const dirname = std.fs.path.dirname(p) orelse ".";
-    const basename = try strip_extension(p);
+    const basename = try strip_extension(std.fs.path.basename(p));
     return try join_path_and_filename(
         alloc,
         &.{dirname},
diff --git a/src/gen.zig b/src/gen.zig
index ec26295..dbfb1ba 100644
--- a/src/gen.zig
+++ b/src/gen.zig
@@ -96,23 +96,24 @@ fn genSite(ctx: *Context) !void {
             try std.fs.cwd().makePath(dstdir);
         }
 
-        const f = try std.fs.cwd().createFile(dstpath, .{});
+        const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath);
+        defer f.close();
 
         const page_name = try std.mem.replaceOwned(u8, ctx.allocator(), dstname, "_", " ");
         defer ctx.allocator().free(page_name);
 
-        try build(ctx.allocator(), f, page_name, srcpath);
+        try build(ctx.allocator(), f.writer().any(), page_name, srcpath);
         ctx.generated += 1;
     }
 }
 
 // build a single html file
-fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, srcpath: []const u8) !void {
+fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u8, srcpath: []const u8) !void {
     const mtime = try fs.get_mtime(allocator, srcpath);
     defer allocator.free(mtime);
 
     // HEAD
-    try f.writer().print(
+    try w.print(
         \\<!-- Generated file, do not edit, you may edit and discard {[SOURCE]s} :) -->
         \\<!DOCTYPE html>
         \\<html lang="en">
@@ -138,30 +139,30 @@ fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, sr
     });
 
     // NAV
-    try f.writer().writeAll(
+    try w.writeAll(
         \\<nav>
         \\
     );
-    portal(allocator, f.writer().any(), "meta.nav", false) catch {}; // ignore error
-    try f.writer().writeAll(
+    portal(allocator, w, "meta.nav", false) catch {}; // ignore error
+    try w.writeAll(
         \\</nav>
         \\</header>
         \\
     );
 
     // MAIN
-    try f.writer().print(
+    try w.print(
         \\<main>
         \\
     , .{});
-    try inject(allocator, f.writer().any(), srcpath);
-    try f.writer().writeAll(
+    try inject(allocator, w, srcpath);
+    try w.writeAll(
         \\</main>
         \\
     );
 
     // FOOTER
-    try f.writer().print(
+    try w.print(
         \\<footer>
         \\Edited on {[MTIME]s} <a href="{[SOURCE_URL]s}/{[PATH]s}" target="_blank">[src]</a>
         \\<a href="mailto:{[PUBLIC_INBOX]s}">[comments]</a>
@@ -183,12 +184,12 @@ fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, sr
         .SOURCE_URL = Context.params.SOURCE_URL,
     });
 
-    try f.writer().writeAll(
+    try w.writeAll(
         \\<a href="#top">[&#9206;]</a>
         \\
     );
 
-    try f.writer().writeAll(
+    try w.writeAll(
         \\</body>
         \\</html>
         \\
@@ -221,7 +222,7 @@ fn meta(ctx: *Context, p: []const u8) !void {
         };
         defer ctx.allocator().free(link);
 
-        const f = try std.fs.cwd().createFile(dstpath, .{});
+        const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath);
         defer f.close();
 
         try f.writer().print(
diff --git a/src/main.zig b/src/main.zig
index a32d968..0b4c73a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -39,6 +39,9 @@ pub const Context = struct {
         pub const LICENSE_URL = "https://git.sr.ht/~robertgzr/site/blob/master/LICENSE.content";
         pub const SOURCE_URL = "https://git.sr.ht/~robertgzr/site/tree/master";
         pub const PUBLIC_INBOX = "~robertgzr/public-inbox@lists.sr.ht";
+
+        pub const PANDOC = "pandoc";
+        pub const PANDOC_PREPROC = "preproc.lua";
     };
 
     fn create(parent_alloc: std.mem.Allocator) !*Context {