diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/src/main.zig b/src/main.zig index 82ea8f9..1deddf3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -76,10 +76,6 @@ const Cli = struct { .handler = &Cli.build, }, .{ - .name = "mdn", - .handler = &Cli.markdown, - }, - .{ .name = "gen", .flags = &.{ .{ .name = "-f" }, @@ -109,9 +105,6 @@ const Cli = struct { \\ build [-f, --force] SOURCE-FILE TARGET-DIR \\ builds while resolving references \\ - \\ mdn SOURCE-FILE TARGET-DIR - \\ build markdown files (using pandoc) - \\ \\ gen [-f, --force] [-l, --list] ROOT-DIR TARGET-DIR \\ runs build for relevant files \\ @@ -229,11 +222,17 @@ const Cli = struct { continue; } + // process markdown first (if required) if (mdn.contains(md_name)) { const mdnNode = if (!DEBUG) mdnProgress.start(md_name, 0); defer if (!DEBUG) mdnNode.end(); - try pandoc(io, gpa, Io.Dir.cwd(), md_path, full_path); + var source = try File.init(io, root_dir, md_name, null); + defer source.close(io); + + const b = try Builder.init(gpa, root_dir, source, target_dir); + defer b.deinit(gpa); + try b.build(io, gpa); } const htmNode = if (!DEBUG) htmProgress.start(path, 0); @@ -246,28 +245,7 @@ const Cli = struct { defer b.deinit(gpa); try b.build(io, gpa); } - } - - fn markdown(io: Io, gpa: Allocator, _: FlagSet, args: [][]const u8) !void { - if (args.len < 2) return Cli.usage(); - - const source_path = args[0]; - const target_path = args[1]; - log.info("mdn : target={s} source={s}", .{ target_path, source_path }); - - try pandoc(io, gpa, Io.Dir.cwd(), source_path, target_path); - } - - // TODO: integrate into builder? - fn pandoc(io: Io, gpa: Allocator, root_dir: Io.Dir, source: []const u8, target: []const u8) !void { - const pandoc_command = try std.fmt.allocPrint(gpa, - \\pandoc --from=markdown+wikilinks_title_after_pipe+autolink_bare_uris --to=html5 --lua-filter=preproc.lua --strip-comments --output={s} {s} - , .{ target, source }); - defer gpa.free(pandoc_command); - - try root_dir.access(io, "preproc.lua", .{}); // check preproc script - try shell(io, pandoc_command, .{ .cwd = .{ .dir = root_dir } }); } fn serve(io: Io, _: Allocator, _: FlagSet, args: [][]const u8) !void { @@ -371,6 +349,11 @@ const Builder = struct { log.debug("{s}: building", .{self.source.path}); const source_stem = std.fs.path.stem(self.source.path); + const source_ext = std.fs.path.extension(self.source.path); + + // handle markdown files + if (std.mem.eql(u8, source_ext, ".md")) + return try self.markdown(io, gpa); // handle meta pages before even creating target_path if (std.mem.startsWith(u8, source_stem, "meta.tilde")) { @@ -624,6 +607,37 @@ const Builder = struct { try self.finalize(gpa, target_fd); log.debug("{s}: done", .{self.source.path}); } + + fn markdown(self: *Builder, io: Io, gpa: Allocator) !void { + log.debug("mdn : {s}", .{self.source.path}); + + const source_stem = std.fs.path.stem(self.source.path); + const target_path = try std.fmt.allocPrint(gpa, "{s}.htm", .{source_stem}); + defer gpa.free(target_path); + + if (try targetExists(io, gpa, self.root_dir, target_path, self.root_dir)) return; + const target_fd = try self.root_dir.createFile(io, target_path, .{ + .lock = .exclusive, + .read = true, + .truncate = true, + }); + defer target_fd.close(io); + + const pandoc_command = try std.fmt.allocPrint(gpa, + \\pandoc --from=markdown+wikilinks_title_after_pipe+autolink_bare_uris --to=html5 --lua-filter=../../preproc.lua --strip-comments --output={s} {s} + , .{ target_path, self.source.path }); + defer gpa.free(pandoc_command); + + try self.root_dir.access(io, "../../preproc.lua", .{}); // check preproc script + try shell(io, pandoc_command, .{ .cwd = .{ .dir = self.root_dir } }); + + // track source + try self.parts.append(gpa, try gpa.dupe(u8, self.source.path)); + try self.dgsts.append(gpa, try self.source.digest()); + + try self.finalize(gpa, target_fd); + log.debug("{s}: done", .{self.source.path}); + } }; // helpers |