@@ -0,0 +1,89 @@
pub fn main() !void {
const a = std.heap.page_allocator;
if (std.fs.cwd().openFile("index.html", .{})) |_| {
std.debug.print("Delete index.html then rerun imgzen\n", .{});
std.posix.exit(1);
} else |_| {}
var file = try std.fs.cwd().createFile("index.html", .{});
defer file.close();
const out = file.writer();
var bw = std.io.bufferedWriter(out);
defer bw.flush() catch unreachable;
const bout = bw.writer();
try bout.writeAll(index);
const random: []const u8 = undefined;
var list = std.ArrayListUnmanaged([]u8){};
var dir = try std.fs.cwd().openDir(".", .{ .iterate = true });
var itr = dir.iterate();
while (itr.next()) |f_| {
const f = f_ orelse break;
if (f.kind == .file) {
try list.append(a, try a.dupe(u8, f.name));
}
} else |err| {
std.debug.print("directory iter error {}\n", .{err});
std.posix.exit(2);
}
std.sort.pdq([]u8, list.items, {}, cmpStr);
for (list.items) |str| {
try bout.print(" <li><a href=\"{s}\">{s}</a></li>\n", .{ str, str });
}
try bout.writeAll(index_middle);
try bout.print(" <div class=preview><img src=\"{s}\"><div>\n", .{random});
try bout.writeAll(index_last);
}
fn cmpStr(_: void, lhs: []const u8, rhs: []const u8) bool {
return std.mem.order(u8, lhs, rhs) == .lt;
}
const index =
\\<!DOCTYPE html>
\\<html>
\\ <head>
\\ <title>imgzen</title>
\\ <style>
\\ html { color-scheme: light dark; }
\\ body { margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }
\\ ul { margin: 0; padding: 0; }
\\ a { color: antiquewhite; }
\\ a:visited { color: gray; }
\\ a:hover { background: darkred; }
\\ .list { display: inline-block; max-width: 20rem; height: 100vh; overflow: scroll; margin: 0 1em; }
\\ .preview { display: inline-block; width: 79%; vertical-align: top; text-align: center; }
\\ </style>
\\ </head>
\\ <body>
\\ <div class=list>
\\ <ul>
\\
;
const index_middle =
\\ </ul>
\\ </div>
\\
;
const index_last =
\\ <script>
\\ document.querySelectorAll("a").forEach((link) => {
\\ link.addEventListener("mouseover", (ev) => {
\\ document.querySelector("img").src = link.href;
\\ });
\\ });
\\ </script>
\\</body>
\\</html>
\\
;
const std = @import("std");