about summary refs log tree commit diff
path: root/src/gen.zig
blob: 8def5cf997ba6e17b34f46be9a3a00fe87eb7c12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const std = @import("std");

const os = @import("os.zig");

const log = std.log.scoped(.gen);

const DELIM_START: []const u8 = "{[";
const DELIM_END: []const u8 = "]}";

pub const BuildOpts = struct {
    root_dir: std.Io.Dir,
    site_dir: std.Io.Dir,

    page_path: []const u8,
    page_name: []const u8,

    meta: struct {
        source_url: []const u8,
        site_name: []const u8,
        license_url: []const u8,
        license_holder: []const u8,
        license_name: []const u8,
        license_year: []const u8,
        public_inbox: []const u8,
    },
};

// build a single html file
pub fn build_htm(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, opts: BuildOpts) !void {
    const stat = try opts.root_dir.statFile(io, opts.page_path, .{});
    const mtime_str = try os.formatTimestamp(gpa, stat.mtime, "%F");
    defer gpa.free(mtime_str);

    // HEAD
    try w.print(
        \\<!-- Generated file, do not edit, you may edit and discard {[SOURCE]s} :) -->
        \\<!DOCTYPE html>
        \\<html lang="en">
        \\<head>
        \\<meta charset="utf-8">
        \\<meta name="thumbnail" content="/media/services/thumbnail.jpg">
        \\<meta name="viewport" content="width=device-width,initial-scale=1">
        \\<link rel="preload" href="/media/interface/logo.svg" as="image">
        \\<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/links/rss.xml">
        \\<link rel="stylesheet" type="text/css" href="/links/main.css">
        \\<link rel="shortcut icon" type="image/png" href="/media/services/icon.png">
        \\<link rel="license" href="{[LICENSE_URL]s}">
        \\<title>{[SITE_NAME]s} &mdash; {[PAGE_NAME]s}</title>
        \\</head>
        \\<body>
        \\<header>
        \\<a href="/"><img src="/media/interface/logo.svg" alt="{[SITE_NAME]s}"></a>
        \\
    , .{
        .SOURCE = opts.page_path,
        .PAGE_NAME = opts.page_name,
        .LICENSE_URL = opts.meta.license_url,
        .SITE_NAME = opts.meta.site_name,
    });

    // NAV
    try w.writeAll(
        \\<nav>
    );
    template_portal(io, gpa, w, "meta.nav", opts) catch |err| {
        if (err != error.FileNotFound) return err;
    };
    try w.writeAll(
        \\</nav>
        \\</header>
        \\
    );

    // MAIN
    try w.writeAll(
        \\<main>
        \\
    );
    try inject(io, gpa, w, opts);
    try w.writeAll(
        \\</main>
        \\
    );

    // FOOTER
    try w.print(
        \\<footer>
        \\Edited on {[MTIME]s} <a href="{[SOURCE_URL]s}/{[PATH]s}" target="_blank">[src]</a>
        \\<a href="mailto:{[PUBLIC_INBOX]s}">[comments]</a>
        \\<br>
        \\<b>{[LICENSE_HOLDER]s}</b>
        \\&copy; {[LICENSE_YEAR]s}
        \\&mdash;
        \\<a href="{[LICENSE_URL]s}" target="_blank">{[LICENSE_NAME]s}</a>
        \\</footer>
        \\
    , .{
        .MTIME = mtime_str,
        .PATH = opts.page_path,
        .LICENSE_HOLDER = opts.meta.license_holder,
        .LICENSE_NAME = opts.meta.license_name,
        .LICENSE_URL = opts.meta.license_url,
        .LICENSE_YEAR = opts.meta.license_year,
        .PUBLIC_INBOX = opts.meta.public_inbox,
        .SOURCE_URL = opts.meta.source_url,
    });

    try w.writeAll(
        \\<a href="#top">[&#9206;]</a>
        \\
    );

    try w.writeAll(
        \\</body>
        \\</html>
    );

    try w.flush();
}

// parses srcpath, resolves refs and portals and writes everything to w
fn inject(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, opts: BuildOpts) !void {
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();

    const inc = opts.root_dir.openFile(io, opts.page_path, .{ .mode = .read_only }) catch |err| {
        if (err == error.FileNotFound)
            log.warn("Missing {s} include.", .{opts.page_path});

        return err;
    };
    defer inc.close(io);

    const buf = try arena.allocator().alloc(u8, 1024);
    var r = inc.readerStreaming(io, buf);

    var chunkw = std.Io.Writer.Allocating.init(arena.allocator());
    defer chunkw.deinit();

    while (true) {
        defer chunkw.clearRetainingCapacity(); // reset chunk

        _ = r.interface.streamDelimiter(&chunkw.writer, '\n') catch |err|
            if (err == error.EndOfStream) break else return err;

        _ = r.interface.toss(1); // skip newline delimiter

        var chunk = chunkw.written();
        templating: while (true) {
            if (std.mem.indexOfPosLinear(u8, chunk, 0, DELIM_START)) |start| {
                if (std.mem.indexOfPosLinear(u8, chunk, start + 1, DELIM_END)) |end| {
                    try w.writeAll(chunk[0..start]);
                    template(io, gpa, w, chunk[(start + DELIM_START.len)..end], opts) catch |err| switch (err) {
                        error.FileNotFound => {
                            // log.warn("ref {s} not found", .{chunk[start + 1 .. end]});
                            // trim already written chunk and exit templating
                            chunk = chunk[start..];
                            break :templating;
                        },
                        else => @panic("Failed to resolve template"),
                    };
                    chunk = chunk[end + DELIM_END.len ..];
                } else break :templating;
            } else break :templating;
        }

        try w.writeAll(chunk);
        try w.writeByte('\n');
        try w.flush();
    }

    try w.flush();
}

// implements various reference types that can be used in .htm templates
fn template(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, ref_name: []const u8, opts: BuildOpts) anyerror!void {
    switch (ref_name[0]) {
        '/' => return try template_portal(io, gpa, w, ref_name[1..], opts),
        //     '$' => return try ref_link(allocator, w, refname[1..]),
        //     '+' => return try ref_name(allocator, w, refname[1..]),
        else => return try template_ref(io, gpa, w, ref_name, opts),
    }
}

// insert link
fn template_ref(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, ref_name: []const u8, opts: BuildOpts) anyerror!void {
    log.debug("template:ref (ref={s})", .{ref_name});

    const ref_path = try std.fmt.allocPrint(gpa, "{s}.html", .{ref_name});
    defer gpa.free(ref_path);

    _ = io;
    _ = opts;

    try w.print(
        \\<a href="{s}">{s}</a>
    , .{ ref_path, ref_name });
}

// insert contents
fn template_portal(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, ref_name: []const u8, opts: BuildOpts) anyerror!void {
    log.debug("template:portal (ref={s})", .{ref_name});

    const ref_path = try std.fmt.allocPrint(gpa, "{s}.htm", .{ref_name});
    defer gpa.free(ref_path);

    try opts.root_dir.access(io, ref_path, .{});

    var iopts = opts;
    iopts.page_path = ref_path;
    iopts.page_name = ref_name;
    try inject(io, gpa, w, iopts);
}

// build a meta file
pub fn build_meta(io: std.Io, gpa: std.mem.Allocator, w: *std.Io.Writer, opts: BuildOpts) !void {
    if (std.mem.startsWith(u8, opts.page_name, "meta.nav")) return;

    if (std.mem.startsWith(u8, opts.page_name, "meta.tilde")) {
        const name = std.mem.cutPrefix(u8, opts.page_name, "meta.tilde.").?;
        const path = try std.fmt.allocPrint(gpa, "~{s}", .{name});
        defer gpa.free(path);

        const link = blk: {
            const content = try opts.root_dir.readFileAlloc(io, opts.page_path, gpa, .unlimited);
            break :blk std.mem.trim(u8, content, " \n\t\r");
        };
        defer gpa.free(link);

        try w.interface.print(
            \\<!DOCTYPE html>
            \\<html lang="en">
            \\<head>
            \\<meta charset="utf-8">
            \\<meta http-equiv="refresh" content="0; url={[LINK]s}">
            \\<meta name="fediverse:creator" content="@robertgzr@toot.berlin">
            \\<link rel="stylesheet" type="text/css" href="links/main.css">
            \\</head>
            \\<body>
            \\<noscript>Continue to <a href="{[LINK]s}">{[LINK]s}</a>.</noscript>
            \\<script>
            \\'use strict'
            \\window.location.replace("{[LINK]s}")
            \\</script>
            \\</body>
            \\</html>
            \\
        , .{
            .LINK = link,
        });
        try w.interface.flush();
    }

    return error.UnsupportedMeta;
}