~cricket/jazz

ref: 1783c2ca502b71708f1867ecdc9277fd3f76fa09 jazz/jazz.zig -rw-r--r-- 3.9 KiB
1783c2cac piapiac small formatting update 5 months ago
                                                                                
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
// THIS IS A PROGRAMME FOR MYSELF!!
// you're free to try to use it for your own site or whatever, but
// 1. it's not well documented
// 2. i am not concerned with style; this code may be unreadable !
// 3. it sucks ass !
// you have been warned .

const std = @import("std");
const debug = std.debug;
const fs = std.fs;
const heap = std.heap;
const log = std.log;
const mem = std.mem;

const ckt = @import("zckt");
const Ckt = ckt.Parser;

const index_file = .{
    @embedFile("index/head.html"),
    @embedFile("index/segment.html"),
    @embedFile("index/tail.html"),
};

const rss_file = .{
    @embedFile("rss/head.xml"),
    @embedFile("rss/segment.xml"),
    @embedFile("rss/tail.xml"),
};

const header_file = @embedFile("header.html");
const post_file = @embedFile("post.html");

const post_files = .{
    // RANT - ckt.html
    @embedFile("posts/ckt.ckt"),

    // HELLO WORLD - hello.html
    @embedFile("posts/hello.ckt"),
};

pub fn main() !void {
    // stop complaining zig
    @setEvalBranchQuota(1000000);
    var arena = heap.ArenaAllocator.init(heap.page_allocator);
    defer arena.deinit();

    const a = &arena.allocator;

    log.info("generating tha jazz", .{});
    const cwd = fs.cwd();

    log.info("create or open out/", .{});
    // silently fail if out exists
    cwd.makeDir("out") catch {};
    const out = try cwd.openDir("out", .{});

    log.info("open static/\n", .{});
    const static = try cwd.openDir("static", .{.iterate=true});
    var static_iter = static.iterate();

    while (try static_iter.next()) |entry| {
        const name = entry.name;
        log.info("static/{0s} -> out/{0s}", .{name});
        try static.copyFile(name, out, name, .{});
    }

    // GET POSTS
    debug.print("\n",.{});
    var posts: [post_files.len]ckt.Table = undefined;
    inline for (post_files) |post, i| {
        posts[i] = try Ckt.parse(post, a);
        const name = posts[i].getString("filename") orelse unreachable;
        log.info("open & parse posts/{s}",.{name});
    }

    // CREATE INDEX FILE
    debug.print("\n",.{});
    log.info("create out/index.html", .{});
    var index = try out.createFile("index.html", .{.truncate=true});
    log.info("write header", .{});
    try index.writer().print(header_file, .{"index.html", "WELCOME TO CRICKET'S BLOG!"});
    log.info("write index head\n",.{});
    _ = try index.write(index_file[0]);

    // CREATE RSS FILE
    log.info("create out/rss.xml", .{});
    var rss = try out.createFile("rss.xml", .{.truncate=true});
    log.info("write rss head\n",.{});
    _ = try rss.write(rss_file[0]);

    // LOOP THROUGH POST FILES
    log.info("loop through post files:", .{});
    for (posts) |*post| {
        const filename = post.getString("filename") orelse unreachable;
        const title = post.getString("title") orelse unreachable;
        const description = post.getString("description") orelse unreachable;
        const pub_date = post.getString("pubdate") orelse unreachable;
        const last_build_date = post.getString("lastbuilddate") orelse unreachable;
        const content = post.getString("content") orelse unreachable;

        log.info("post {s}", .{title});
        log.info("write index segment",.{});
        try index.writer().print(index_file[1],.{filename,title,description,pub_date});

        log.info("write rss segment",.{});
        try rss.writer().print(rss_file[1],.{title,filename,description,pub_date,last_build_date});

        log.info("create out/{s}",.{filename});
        var file = try out.createFile(filename, .{.truncate=true});
        log.info("write header",.{});
        try file.writer().print(header_file, .{title,description});
        log.info("write out/{s}\n",.{filename});
        try file.writer().print(post_file,.{filename,title,description,pub_date,content});
    }

    // WRITE INDEX & RSS TAIL
    log.info("write index tail",.{});
    _ = try index.write(index_file[2]);

    log.info("write rss tail",.{});
    _ = try rss.write(rss_file[2]);
}