// TODO: just let compiler infer error set through recursive calls
// depends on: https://github.com/ziglang/zig/issues/2971
const std = @import("std");
const c = @cImport({
@cInclude("time.h");
});
const PAGE_PATH = "src/inc";
const SITE_ROOT = ".";
const SITE_PATH = "site";
const DOMAIN_URL = "https://gzr.im";
const SITE_NAME = "gzr.im";
const LICENSE_HOLDER = "robertgzr";
const LICENSE_NAME = "BY-NC-SA 4.0";
const LICENSE_URL = "https://git.sr.ht/~robertgzr/site/blob/rabbits/LICENSE.content";
const SOURCE_URL = "https://git.sr.ht/~robertgzr/site/tree/rabbits";
const BuildErrors = (std.fs.File.OpenError || std.fs.File.WriteError || std.mem.Allocator.Error);
const Context = struct {
allocator: std.mem.Allocator,
pages: [][]const u8,
generated: usize = 0,
meta: usize = 0,
tilde: usize = 0,
};
// also trims off base paths
fn stripExt(path: []const u8) ![]const u8 {
const basename = std.fs.path.basename(path);
const ext = std.fs.path.extension(path);
const ext_idx = std.mem.indexOfPosLinear(u8, basename, 0, ext);
std.debug.assert(ext_idx != null);
return basename[0..ext_idx.?];
}
fn joinIn(allocator: std.mem.Allocator, paths: []const []const u8, filename_parts: []const []const u8) ![]u8 {
const filename = try std.mem.concat(allocator, u8, filename_parts);
defer allocator.free(filename);
var p = try std.ArrayList([]const u8).initCapacity(allocator, paths.len + 1);
defer p.deinit();
p.appendSliceAssumeCapacity(paths);
p.appendAssumeCapacity(filename);
const paths2 = try p.toOwnedSlice();
defer allocator.free(paths2);
std.debug.assert(paths.len + 1 == paths2.len);
return try std.fs.path.join(allocator, paths2);
}
fn portal(allocator: std.mem.Allocator, f: std.fs.File, refname: []const u8, insert_heading: bool) BuildErrors!void {
const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_");
defer allocator.free(basename);
const refpathPage = try joinIn(allocator, &.{PAGE_PATH}, &.{ basename, ".htm" });
defer allocator.free(refpathPage);
const refpathSite = try joinIn(allocator, &.{SITE_PATH}, &.{ basename, ".html" });
defer allocator.free(refpathSite);
if (insert_heading)
try f.writer().print(
\\
{[NAME]s}2>
\\
, .{
.ID = basename,
.NAME = refname,
.PATH = refpathSite,
});
try inject(allocator, f, refpathPage);
// XXX: track ref count?
}
fn template(allocator: std.mem.Allocator, f: std.fs.File, refname: []const u8) BuildErrors!void {
if (std.mem.startsWith(u8, refname, "/"))
return try portal(allocator, f, refname[1..], false);
const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_");
defer allocator.free(basename);
const refpathSite = try joinIn(allocator, &.{SITE_PATH}, &.{ basename, ".html" });
defer allocator.free(refpathSite);
try f.writer().print(
\\{[NAME]s}
\\
, .{
.NAME = refname,
.PATH = refpathSite,
});
// XXX: track ref count?
}
fn inject(allocator: std.mem.Allocator, f: std.fs.File, srcpath: []const u8) BuildErrors!void {
const inc = std.fs.cwd().openFile(srcpath, .{ .mode = .read_only }) catch |err| {
if (err == error.FileNotFound)
std.log.warn("Missing {s} include.", .{srcpath});
return err;
};
defer inc.close();
var buf_writer = std.io.bufferedWriter(f.writer());
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| {
if (std.mem.indexOfPosLinear(u8, chunk, 0, "{")) |start| {
try out_stream.writeAll(chunk[0..start]);
try buf_writer.flush();
if (std.mem.indexOfPosLinear(u8, chunk, start + 1, "}")) |end| {
try template(allocator, f, chunk[start + 1 .. end]);
try buf_writer.flush();
try out_stream.writeAll(chunk[end + 1 ..]);
try buf_writer.flush();
} else {
std.log.err("Templating error in {s}", .{srcpath});
std.os.linux.exit(1);
}
} else {
try out_stream.writeAll(chunk);
try out_stream.writeByte('\n');
}
_ = arena.reset(.free_all);
}
try buf_writer.flush();
}
fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, srcpath: []const u8) BuildErrors!void {
const mtime = blk: {
const attr = try allocator.create(std.os.linux.Stat);
defer allocator.destroy(attr);
const srcpathZ = try allocator.dupeZ(u8, srcpath);
defer allocator.free(srcpathZ);
_ = std.os.linux.stat(srcpathZ, attr);
const mtime = attr.mtime();
// const mtime_str = c.ctime(&mtime.tv_sec);
var mtime_str: [200]u8 = undefined;
const n = c.strftime(
&mtime_str,
mtime_str.len,
"%F",
c.localtime(&mtime.tv_sec),
);
break :blk mtime_str[0..n];
};
// HEAD
try f.writer().print(
\\
\\
\\
\\
\\
\\
\\
\\
\\{[SITE_NAME]s} — {[PAGE_NAME]s}
\\
\\
\\
\\
, .{
.DOMAIN = DOMAIN_URL,
.SITE_NAME = SITE_NAME,
.PAGE_NAME = page_name,
});
// NAV
try f.writer().writeAll(
\\
\\
);
// MAIN
try f.writer().print(
\\
\\
\\
\\
\\{[PAGE_NAME]s}
\\
, .{
.PAGE_NAME = page_name,
});
try inject(allocator, f, srcpath);
try f.writer().writeAll(
\\
\\
\\
);
// FOOTER
try f.writer().print(
\\
, .{
.MTIME = mtime,
.PATH = srcpath,
.LICENSE_HOLDER = LICENSE_HOLDER,
.LICENSE_NAME = LICENSE_NAME,
.LICENSE_URL = LICENSE_URL,
.SOURCE_URL = SOURCE_URL,
});
const n = f.getPos() catch 0;
if (n > 5000)
try f.writer().writeAll(
\\[⏶]
\\
);
try f.writer().writeAll(
\\
\\
);
}
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 joinIn(ctx.allocator, &.{PAGE_PATH}, &.{p});
defer ctx.allocator.free(srcpath);
const dstpath = try joinIn(ctx.allocator, &.{SITE_ROOT}, &.{ "~", 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;
}
fn generate(ctx: *Context) !void {
for (ctx.pages) |p| {
if (std.mem.startsWith(u8, p, "meta.")) {
try meta(ctx, p);
continue;
}
const dstname = try stripExt(p);
const srcpath = try joinIn(ctx.allocator, &.{PAGE_PATH}, &.{ dstname, ".htm" });
defer ctx.allocator.free(srcpath);
const dstpath = try joinIn(ctx.allocator, &.{SITE_PATH}, &.{ dstname, ".html" });
defer ctx.allocator.free(dstpath);
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;
}
}
fn genindex(ctx: *Context) !void {
const srcpathIndex = try joinIn(ctx.allocator, &.{PAGE_PATH}, &.{"index.htm"});
defer ctx.allocator.free(srcpathIndex);
const f = try std.fs.cwd().createFile(srcpathIndex, .{});
defer f.close();
try f.writeAll(
\\
\\
\\
);
// alphanum ascii sort
std.mem.sort(
[]const u8,
ctx.pages,
{},
struct {
fn lessthan(_: void, a: []const u8, b: []const u8) bool {
return std.ascii.lessThanIgnoreCase(a, b);
}
}.lessthan,
);
for (ctx.pages) |p| {
if (std.mem.startsWith(u8, p, "meta.")) continue;
const dstname = try stripExt(p);
const dstpath = try joinIn(ctx.allocator, &.{SITE_PATH}, &.{ dstname, ".html" });
defer ctx.allocator.free(dstpath);
try f.writer().print(
\\- {[LINK]s}
\\
, .{
.HREF = dstpath,
.LINK = dstname,
});
}
try f.writeAll(
\\
\\
);
}
fn index(allocator: std.mem.Allocator, path: []const u8) ![][]const u8 {
var dir = std.fs.cwd().openDir(path, .{ .iterate = true }) catch |err| {
if (err == error.FileNotFound) {
std.log.err("Missing {s} folder.", .{PAGE_PATH});
std.os.linux.exit(1);
} else return err;
};
defer dir.close();
var pages = std.ArrayList([]const u8).init(allocator);
defer pages.deinit();
var dirIter = dir.iterate();
while (try dirIter.next()) |e|
if (e.kind == .file)
if (std.mem.endsWith(u8, e.name, ".htm"))
try pages.append(try allocator.dupe(u8, e.name));
std.log.info("Indexed {d} pages.", .{pages.items.len});
return try pages.toOwnedSlice();
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
const pages = index(allocator, PAGE_PATH) catch {
std.log.err("Failed to index pages.", .{});
std.os.linux.exit(1);
};
defer {
for (pages) |p| allocator.free(p);
allocator.free(pages);
}
std.log.info("Generated index.", .{});
var ctx = Context{
.allocator = allocator,
.pages = pages,
};
try genindex(&ctx);
try generate(&ctx);
std.log.info("Generated {d} files.", .{ctx.generated});
std.log.info("Generated {d} tildes.", .{ctx.tilde});
}