grace/htmlua

HTML Templating Engine

git clone https://git.gae.moe/grace/htmlua

Grace Yoderfeat: zig rewrite0213276

main
2.8 KiB79 linesraw
1const std = @import("std");
2const Io = std.Io;
3const File = Io.File;
4const Allocator = std.mem.Allocator;
5const ArenaAllocator = std.heap.ArenaAllocator;
6const ArrayList = std.ArrayList;
7const StringArrayHashMap = std.StringArrayHashMapUnmanaged;
8
9const HTMLua = @import("htmlua");
10
11const Config = struct {
12    component_path: []const u8,
13    template_path: []const u8,
14};
15
16var component_dir: Io.Dir = undefined;
17
18pub fn main(init: std.process.Init) !void {
19    const io = init.io;
20
21    var arena = std.heap.ArenaAllocator.init(init.gpa);
22    defer arena.deinit();
23    const alloc = arena.allocator();
24
25    var args = init.minimal.args.iterate();
26
27    const cwd = std.Io.Dir.cwd();
28
29    if (!args.skip()) unreachable;
30    const config_file_path = args.next().?;
31    const config_buf = try cwd.readFileAllocOptions(io, config_file_path, alloc, .unlimited, std.mem.Alignment.@"1", '\x00');
32    const config = try std.zon.parse.fromSliceAlloc(
33        Config,
34        alloc,
35        config_buf,
36        null,
37        .{},
38    );
39
40    component_dir = try cwd.openDir(io, config.component_path, .{});
41    const template_dir = try cwd.openDir(io, config.template_path, .{});
42
43    var state: HTMLua.Parser = .{
44        .tags = &[_]HTMLua.ParserTag{
45            HTMLua.tags.Include,
46            HTMLua.tags.Export,
47            HTMLua.tags.Use,
48            HTMLua.tags.Syntax,
49        },
50        .tag_stack = try ArrayList([]const u8).initCapacity(alloc, 256),
51        .exported_parts = try StringArrayHashMap([]const u8).init(alloc, &[0][]const u8{}, &[0][]const u8{}),
52        .component_resolver = struct {
53            pub fn resolve(arena_inner: *ArenaAllocator, io_inner: Io, path: []const u8) anyerror!*Io.Reader {
54                const read_buff = try arena_inner.allocator().alloc(u8, 16384);
55                const clean_path = try HTMLua.utils.sanitizePath(arena_inner.allocator(), path);
56                var file = try component_dir.openFile(io_inner, clean_path, .{ .mode = .read_only });
57                const reader = try arena_inner.allocator().create(File.Reader);
58                reader.* = file.reader(io_inner, read_buff);
59                return &reader.interface;
60            }
61        }.resolve,
62    };
63
64    const request_path = init.environ_map.get("PATH_INFO").?;
65    const clean_request_path = try HTMLua.utils.sanitizePath(alloc, request_path);
66    const file = try template_dir.openFile(io, clean_request_path, .{ .mode = .read_only });
67    defer file.close(io);
68
69    const in_buf = try alloc.alloc(u8, 16384);
70    var in = file.reader(io, in_buf);
71
72    const out_buf = try alloc.alloc(u8, 16384);
73    var out = File.stdout().writer(io, out_buf);
74
75    try out.interface.writeAll("Content-type: text/html\n");
76    try HTMLua.parseTemplate(&arena, io, &state, &in.interface, &out.interface);
77
78    try out.flush();
79}