~clarity/sitegen

926e7f9594ddd5ca7de7373ae41f70509c4adf1d — Clarity Flowers a month ago fe2003c root
A little bit less data-driven, for readability
1 files changed, 33 insertions(+), 28 deletions(-)

M src/main.zig
M src/main.zig => src/main.zig +33 -28
@@ 1211,15 1211,21 @@ const Template = struct {
    footer: Tokens,
    text: []const u8,

    /// TOKENS      = *TOKEN
    /// TOKEN       = text / VARIABLE / CONDITIONAL
    /// VARIABLE    = variable [ format end ]
    /// CONDITIONAL = conditional variable *TOKEN end
    const Token = struct {
        const Type = enum { variable, format, conditional, end, text };
        const Value = union(enum) {
            variable: Variable,
            format,
            conditional: u32,
            end,
            text,
        };

        type: Type,
        value: Value,
        position: usize,
        value: union {
            variable: Variable,
            jump: usize,
        } = undefined,
    };

    const Variable = enum { title, file, dir, written, updated, back_text, back, parent_name, parent };


@@ 1258,50 1264,50 @@ const Template = struct {
        var tokens = &header_tokens;
        var index: usize = 0;

        try tokens.append(allocator, .{ .type = .text, .position = 0 });
        try tokens.append(allocator, .{ .value = .text, .position = 0 });

        while (index < text.len) {
            if (conditionals.len == 0 and tokens != &footer_tokens) {
                if (parseLiteral(text, index, "{{content}}")) |new_index| {
                    try tokens.append(allocator, .{ .type = .end, .position = index });
                    try tokens.append(allocator, .{ .value = .end, .position = index });
                    tokens = &footer_tokens;
                    index = new_index;
                    try tokens.append(allocator, .{ .type = .text, .position = index });
                    try tokens.append(allocator, .{ .value = .text, .position = index });
                    continue;
                }
            }
            if (parseVariableStart(text, index)) |res| {
                if (parseLiteral(text, res.new_pos, "|") != null) {
                    if (std.mem.indexOfPos(u8, text, res.new_pos + 1, "}}")) |end| {
                        try tokens.append(allocator, .{ .type = .variable, .position = index, .value = .{ .variable = res.data } });
                        try tokens.append(allocator, .{ .type = .format, .position = res.new_pos + 1 });
                        try tokens.append(allocator, .{ .type = .end, .position = end });
                        try tokens.append(allocator, .{ .type = .text, .position = end + 2 });
                        try tokens.append(allocator, .{ .value = .{ .variable = res.data }, .position = index });
                        try tokens.append(allocator, .{ .value = .format, .position = res.new_pos + 1 });
                        try tokens.append(allocator, .{ .value = .end, .position = end });
                        try tokens.append(allocator, .{ .value = .text, .position = end + 2 });
                        index = end + 2;
                        continue;
                    }
                }
                if (parseLiteral(text, res.new_pos, "}}") != null) {
                    try tokens.append(allocator, .{ .type = .variable, .position = index, .value = .{ .variable = res.data } });
                    try tokens.append(allocator, .{ .value = .{ .variable = res.data }, .position = index });
                    index = res.new_pos + 2;
                    try tokens.append(allocator, .{ .type = .text, .position = index });
                    try tokens.append(allocator, .{ .value = .text, .position = index });
                    continue;
                } else if (conditionals.len < conditionals_buffer.len and parseLiteral(text, res.new_pos, "?") != null) {
                    conditionals.len = conditionals.len + 1;
                    conditionals[conditionals.len - 1] = tokens.len;
                    try tokens.append(allocator, .{ .type = .conditional, .position = index });
                    try tokens.append(allocator, .{ .type = .variable, .position = index, .value = .{ .variable = res.data } });
                    try tokens.append(allocator, .{ .value = .{ .conditional = undefined }, .position = index });
                    try tokens.append(allocator, .{ .value = .{ .variable = res.data }, .position = index });
                    index = res.new_pos + 1;
                    try tokens.append(allocator, .{ .type = .text, .position = index });
                    try tokens.append(allocator, .{ .value = .text, .position = index });
                    continue;
                }
            }
            if (conditionals.len > 0 and parseLiteral(text, index, "}}") != null) {
                try tokens.append(allocator, .{ .type = .end, .position = index });
                tokens.items(.value)[conditionals[conditionals.len - 1]] = .{ .jump = tokens.len };
                try tokens.append(allocator, .{ .value = .end, .position = index });
                tokens.items(.value)[conditionals[conditionals.len - 1]].conditional = @intCast(u32, tokens.len);
                conditionals.len = conditionals.len - 1;
                index += 2;
                try tokens.append(allocator, .{ .type = .text, .position = index });
                try tokens.append(allocator, .{ .value = .text, .position = index });
                continue;
            }
            index += 1;


@@ 1363,14 1369,13 @@ const Template = struct {
        file: FileInfo,
        writer: anytype,
    ) @TypeOf(writer).Error!void {
        const tags = template.items(.type);
        const values = template.items(.value);
        const positions = template.items(.position);
        var i: usize = 0;
        while (i < tags.len) {
            switch (tags[i]) {
        while (i < values.len) {
            switch (values[i]) {
                .text => try writer.writeAll(content(positions, i, text)),
                .conditional => {
                .conditional => |jump| {
                    if (switch (values[i + 1].variable) {
                        .written, .title, .file, .back_text => true,
                        .dir => file.dir != null,


@@ 1381,14 1386,14 @@ const Template = struct {
                    }) {
                        i += 1;
                    } else {
                        i = values[i].jump;
                        i = jump;
                        continue;
                    }
                },
                .end, .format => {},
                .variable => switch (values[i].variable) {
                    .written => {
                        if (i + 1 < tags.len and tags[i + 1] == .format) {
                        if (i + 1 < values.len and values[i + 1] == .format) {
                            try info.created.formatRuntime(
                                content(positions, i + 1, text),
                                writer,


@@ 1399,7 1404,7 @@ const Template = struct {
                    },
                    .updated => {
                        if (info.changes.len > 0) {
                            if (i + 1 < tags.len and tags[i + 1] == .format) {
                            if (i + 1 < values.len and values[i + 1] == .format) {
                                try info.changes[info.changes.len - 1].date.formatRuntime(
                                    content(positions, i + 1, text),
                                    writer,