const std = @import("std");
const blog_processor = @import("blog_processor");
-pub fn main() !void {
- const alloc = std.heap.page_allocator;
- const arena = std.heap.ArenaAllocator.init(alloc);
- _ = arena;
+pub fn main(init: std.process.Init) !void {
+ var dirName: []const u8 = undefined;
+ var args_iter = std.process.Args.iterate(init.minimal.args);
+ _ = args_iter.next(); // Skip exec name
+ while (args_iter.next()) |arg| {
+ dirName = arg;
+ break;
+ }
+
+ try blog_processor.processBlog(init.arena.allocator(), init.io, dirName);
}
-// const std = @import("std");
+const std = @import("std");
-// const md_parser = @import("./markdown_parser.zig");
-// const html = @import("./html_formatter.zig");
+const md_parser = @import("./markdown_parser.zig");
+const html = @import("./html_formatter.zig");
+
+pub fn processBlog(alloc: std.mem.Allocator, io: std.Io, dirName: []const u8) !void {
+ const dir = try std.Io.Dir.openDirAbsolute(io, dirName, .{ .iterate = true});
+ defer dir.close(io);
+
+ var iter = dir.iterate();
+ while(try iter.next(io)) |entry| {
+ try processFile(alloc, dir, io, entry.name);
+ }
+}
+
+fn processFile(alloc: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, fileName: []const u8) !void {
+ if (!std.mem.eql(u8, ".md", std.fs.path.extension(fileName))) return;
+
+ const postContents = try std.Io.Dir.readFileAlloc(dir, io, fileName, alloc, .unlimited);
+
+ var mdDoc = try md_parser.MarkdownDoc.parse(postContents, alloc);
+ defer mdDoc.deinit(alloc);
+
+ // TODO: Customize this via command line
+ const templateFile = "templates/index.html";
+ const template = try std.Io.Dir.readFileAlloc(
+ std.Io.Dir.cwd(), io, templateFile, alloc, .unlimited);
+ var formatter = html.HtmlFormatter.init(.{ .template = template });
+ defer formatter.deinit(alloc);
+
+ const htmlOut = try formatter.format(alloc, mdDoc);
+
+ // TODO: Change to file stem
+ const baseName = std.fs.path.basename(fileName);
+ const ext = ".html";
+ const newFileName = try alloc.alloc(u8, baseName.len + ext.len);
+ defer alloc.free(newFileName);
+
+ @memcpy(newFileName.ptr, baseName);
+ @memcpy(newFileName[baseName.len..].ptr, ext);
+
+ // TODO: Put them in public directory
+ try std.Io.Dir.cwd().writeFile(io, .{
+ .data = htmlOut,
+ .sub_path = newFileName,
+ });
+}
test "all tests" {
_ = @import("./markdown_parser.zig");
+++ /dev/null
-<!DOCTYPE html>
-<html>
-
-<head>
- <meta charset="utf-8">
- <title>Alexander Goussas | Programming</title>
- <meta name="author" content="Alexander Goussas">
- <meta name="description" content="Blog about programming">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link href="https://cdn.jsdelivr.net/npm/
[email protected]/dist/cdn/beer.min.css" rel="stylesheet">
- <link href="styles.css" rel="stylesheet">
-</head>
-
-<body>
- <h1>Alexander Goussas</h1>
- <h2>Blog about programming and friends</h2>
-
- <section id="posts">
- <ul>
- <li><a href="posts/05-05-2026-how-i-read-500-page-books-in-a-weekend.html">How I read 500 page books in a weekend</a></li>
- </ul>
- </section>
- <script src="js/script.js"></script>
-</body>
-
-</html>
--- /dev/null
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta charset="utf-8">
+ <title>Alexander Goussas | Programming</title>
+ <meta name="author" content="Alexander Goussas">
+ <meta name="description" content="Blog about programming">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <link href="https://cdn.jsdelivr.net/npm/
[email protected]/dist/cdn/beer.min.css" rel="stylesheet">
+ <link href="styles.css" rel="stylesheet">
+</head>
+
+<body>
+ <h1>Alexander Goussas</h1>
+ <h2>Blog about programming and friends</h2>
+
+ <section id="posts">
+ <ul>
+ <li><a href="posts/05-05-2026-how-i-read-500-page-books-in-a-weekend.html">How I read 500 page books in a weekend</a></li>
+ </ul>
+ </section>
+ <script src="js/script.js"></script>
+</body>
+
+</html>