const std = @import("std"); const log = std.log.scoped(.gen); const Context = @import("main.zig").Context; const fs = @import("fs.zig"); pub fn generate(ctx: *Context) !void { ctx.progress_start("generate"); defer ctx.progress_end(); try genIndexHtml(ctx); log.info("Generated index.", .{}); try genSite(ctx); log.info("Generated {d} files.", .{ctx.generated}); log.info("Generated {d} tildes.", .{ctx.tilde}); } fn genIndexHtml(ctx: *Context) !void { ctx.progress_start("index.html"); defer ctx.progress_end(); const srcpathIndex = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.PAGE_PATH}, &.{"index.htm"}); defer ctx.allocator().free(srcpathIndex); const f = try fs.FileSwapper.createAt(ctx.allocator(), srcpathIndex); defer f.close(); try f.writer().writeAll( \\ \\ ); } fn genSite(ctx: *Context) !void { for (ctx.pages.items) |p| { const pname = std.fs.path.basename(p); ctx.progress_start(pname); defer ctx.progress_end(); if (std.mem.startsWith(u8, pname, "meta.")) { try meta(ctx, p); continue; } const dstname = try fs.strip_extension(p); const srcpath = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.PAGE_PATH}, &.{ dstname, ".htm" }); defer ctx.allocator().free(srcpath); const dstpath = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{ dstname, ".html" }); defer ctx.allocator().free(dstpath); const rel_dstpath = try std.fs.path.relative(ctx.allocator(), Context.params.SITE_PATH, dstpath); defer ctx.allocator().free(rel_dstpath); log.debug("generating {s} at {s}", .{ p, rel_dstpath }); if (std.fs.path.dirname(rel_dstpath)) |d| { const dstdir = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{d}); defer ctx.allocator().free(dstdir); try std.fs.cwd().makePath(dstdir); } const f = try std.fs.cwd().createFile(dstpath, .{}); 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); 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 { const mtime = try fs.get_mtime(allocator, srcpath); defer allocator.free(mtime); // HEAD try f.writer().print( \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\{[SITE_NAME]s} — {[PAGE_NAME]s} \\ \\ \\
\\{[SITE_NAME]s} \\ , .{ .SOURCE = srcpath, .LICENSE_URL = Context.params.LICENSE_URL, .PAGE_NAME = page_name, .SITE_NAME = Context.params.SITE_NAME, }); // NAV try f.writer().writeAll( \\ \\
\\ ); // MAIN try f.writer().print( \\
\\ , .{}); try inject(allocator, f.writer().any(), srcpath); try f.writer().writeAll( \\
\\ ); // FOOTER try f.writer().print( \\ \\ , .{ .LICENSE_HOLDER = Context.params.LICENSE_HOLDER, .LICENSE_NAME = Context.params.LICENSE_NAME, .LICENSE_URL = Context.params.LICENSE_URL, .LICENSE_YEAR = Context.params.LICENSE_YEAR, .MTIME = mtime, .PATH = srcpath, .PUBLIC_INBOX = Context.params.PUBLIC_INBOX, .SOURCE_URL = Context.params.SOURCE_URL, }); try f.writer().writeAll( \\[⏶] \\ ); try f.writer().writeAll( \\ \\ \\ ); } // build a virtual html file, currently either: // - meta.nav: used for populating the global navigation menu // - meta.tilde: used to create ~foobar files fn meta(ctx: *Context, p: []const u8) !void { ctx.meta += 1; if (std.mem.startsWith(u8, p, "meta.nav.")) return; if (std.mem.startsWith(u8, p, "meta.tilde.")) { const dot = std.mem.indexOfPosLinear(u8, p, 11, "."); const dstname = p[11..dot.?]; const srcpath = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.PAGE_PATH}, &.{p}); defer ctx.allocator().free(srcpath); const dstpath = try fs.join_path_and_filename(ctx.allocator(), &.{Context.params.LINK_PATH}, &.{ "~", dstname }); defer ctx.allocator().free(dstpath); const link = blk: { const f = try std.fs.cwd().openFile(srcpath, .{ .mode = .read_only }); defer f.close(); const content = try f.readToEndAlloc(ctx.allocator(), std.os.linux.PATH_MAX); defer ctx.allocator().free(content); const trimmed = std.mem.trim(u8, content, " \n\t\r"); break :blk try ctx.allocator().dupe(u8, trimmed); }; defer ctx.allocator().free(link); const f = try std.fs.cwd().createFile(dstpath, .{}); defer f.close(); try f.writer().print( \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ , .{ .LINK = link, }); ctx.tilde += 1; return; } return error.unknownMeta; } // parses srcpath, resolves refs and portals and writes everything to w fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8) !void { const inc = std.fs.cwd().openFile(srcpath, .{ .mode = .read_only }) catch |err| { if (err == error.FileNotFound) log.warn("Missing {s} include.", .{srcpath}); return err; }; defer inc.close(); var buf_writer = std.io.bufferedWriter(w); var out_stream = buf_writer.writer(); var buf_reader = std.io.bufferedReader(inc.reader()); var in_stream = buf_reader.reader(); var arena = std.heap.ArenaAllocator.init(allocator); defer arena.deinit(); while (in_stream.readUntilDelimiterOrEofAlloc( arena.allocator(), '\n', buf_reader.buf.len, ) catch { @panic("Failed to read from stream"); }) |chunk_| { defer _ = arena.reset(.free_all); var chunk = chunk_; templating: while (true) { if (std.mem.indexOfPosLinear(u8, chunk, 0, "{")) |start| { if (std.mem.indexOfPosLinear(u8, chunk, start + 1, "}")) |end| { try out_stream.writeAll(chunk[0..start]); template(allocator, out_stream.any(), chunk[start + 1 .. end]) catch @panic("Failed to resolve template"); chunk = chunk[end + 1 ..]; } else break :templating; } else break :templating; } try out_stream.writeAll(chunk); try out_stream.writeByte('\n'); try buf_writer.flush(); } try buf_writer.flush(); } // implements various reference types that can be used in .htm templates fn template(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { switch (refname[0]) { '/' => return try portal(allocator, w, refname[1..], false), '@' => return try ref_link(allocator, w, refname[1..]), '+' => return try ref_name(allocator, w, refname[1..]), else => return try ref(allocator, w, refname), } } fn portal(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8, insert_heading: bool) anyerror!void { const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_"); defer allocator.free(basename); const refpathPage = try fs.join_path_and_filename(allocator, &.{Context.params.PAGE_PATH}, &.{ basename, ".htm" }); defer allocator.free(refpathPage); const refpathSite = try fs.join_path_and_filename(allocator, &.{Context.params.SITE_PATH}, &.{ basename, ".html" }); defer allocator.free(refpathSite); if (insert_heading) try w.print( \\

{[NAME]s} \\ , .{ .ID = basename, .NAME = refname, .PATH = refpathSite, }); try inject(allocator, w, refpathPage); // XXX: track ref count? } fn ref(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_"); defer allocator.free(basename); const refpathSite = try fs.join_path_and_filename(allocator, &.{Context.params.SITE_PATH}, &.{ basename, ".html" }); defer allocator.free(refpathSite); try w.print( \\{[NAME]s} , .{ .NAME = refname, .PATH = refpathSite, }); // XXX: track ref count? } fn ref_link(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_"); defer allocator.free(basename); const refpathSite = try fs.join_path_and_filename(allocator, &.{Context.params.SITE_PATH}, &.{ basename, ".html" }); defer allocator.free(refpathSite); try w.print("/{s}", .{refpathSite}); } fn ref_name(_: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { try w.print("{s}", .{refname}); }