srctree

Gregory Mullen parent e4ba2460 969ddae7
delete ignore directive

src/template-compiler.zig added: 35, removed: 30, total 5
@@ -147,7 +147,7 @@ fn emitVars(a: Allocator, fdata: []const u8, current: *AbstTree) !void {
var kind = try bufPrint(&buffer, ": []const u8,\n", .{});
 
switch (drct.otherwise) {
.required, .ignore => {},
.required => {},
.default => |str| {
kind = try bufPrint(&buffer, ": []const u8 = \"{s}\",\n", .{str});
},
 
src/template.zig added: 35, removed: 30, total 5
@@ -1,6 +1,8 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const eql = std.mem.eql;
const endsWith = std.mem.endsWith;
const indexOfScalar = std.mem.indexOfScalar;
const allocPrint = std.fmt.allocPrint;
const log = std.log.scoped(.Verse);
 
@@ -35,7 +37,7 @@ pub const Template = struct {
};
 
fn tailPath(path: []const u8) []const u8 {
if (std.mem.indexOf(u8, path, "/")) |i| {
if (indexOfScalar(u8, path, '/')) |i| {
return path[i + 1 ..];
}
return path[0..0];
@@ -118,7 +120,17 @@ pub fn findTemplate(comptime name: []const u8) Template {
return bi;
}
}
@compileError("template " ++ name ++ " not found!");
 
var errstr: [:0]const u8 = "Template " ++ name ++ " not found!";
inline for (builtin) |bi| {
if (comptime endsWith(u8, bi.name, name)) {
errstr = errstr ++ "\nDid you mean" ++ " " ++ bi.name ++ "?";
}
}
// If you're reading this, it's probably because your template.html is
// either missing, not included in the build.zig search dirs, or typo'd.
// But it's important for you to know... I hope you have a good day :)
@compileError(errstr);
}
 
pub fn PageData(comptime name: []const u8) type {
 
src/template/comptime.zig added: 35, removed: 30, total 5
@@ -6,7 +6,7 @@ pub const FileData = struct {
};
 
pub const data: [Found.names.len]FileData = blk: {
@setEvalBranchQuota(5000);
@setEvalBranchQuota(1000);
var t: [Found.names.len]FileData = undefined;
for (Found.names, &t) |file, *dst| {
dst.* = FileData{
 
src/template/directive.zig added: 35, removed: 30, total 5
@@ -9,7 +9,6 @@ pub const Directive = @This();
 
pub const Otherwise = union(enum) {
required: void,
ignore: void,
delete: void,
default: []const u8,
template: *const Template.Template,
@@ -255,7 +254,7 @@ fn validChar(c: u8) bool {
fn calcBodyS(comptime _: []const u8, _: []const u8, blob: []const u8, end: usize) ?struct { Otherwise, usize } {
if (blob.len <= end) return null;
return .{
.{ .ignore = {} },
.{ .required = {} },
end + 1,
};
}
@@ -274,8 +273,11 @@ fn calcBody(comptime keyword: []const u8, noun: []const u8, blob: []const u8) ?s
else => return null,
}
 
var start = 1 + (indexOf(u8, blob, ">") orelse return null);
var start = 1 + (indexOfPosLinear(u8, blob, 0, ">") orelse return null);
var close_pos: usize = indexOfPosLinear(u8, blob, 0, close) orelse return null;
// count is not comptime compliant while it uses indexOfPos increasing
// backward branches. I've raised the quota, but complicated templates might
// require a naive implementation
var skip = count(u8, blob[start..close_pos], open);
while (skip > 0) : (skip -= 1) {
close_pos = indexOfPosLinear(u8, blob, close_pos + 1, close) orelse close_pos;
@@ -448,7 +450,6 @@ pub fn formatTyped(d: Directive, comptime T: type, ctx: T, out: anytype) !void {
switch (d.otherwise) {
.default => |str| try out.writeAll(str),
// Not really an error, just instruct caller to print original text
.ignore => return error.IgnoreDirective,
.required => return error.VariableMissing,
.delete => {},
.template => |template| {
 
src/template/page.zig added: 35, removed: 30, total 5
@@ -66,7 +66,7 @@ pub fn PageRuntime(comptime PageDataType: type) type {
}
 
pub fn Page(comptime template: Template, comptime PageDataType: type) type {
@setEvalBranchQuota(5000);
@setEvalBranchQuota(10000);
var found_offsets: []const Offset = &[0]Offset{};
var pblob = template.blob;
var index: usize = 0;
@@ -160,31 +160,23 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
}
 
const ptr: [*]const u8 = @ptrCast(&self.data);
var vari: ?[]const u8 = null;
switch (directive.otherwise) {
.required => {
const vari: *const []const u8 = @ptrCast(@alignCast(&ptr[offset]));
try out.writeAll(vari.*);
},
.ignore => {
const vari: *const ?[]const u8 = @ptrCast(@alignCast(&ptr[offset]));
if (vari.*) |v|
try out.writeAll(v);
vari = @as(*const []const u8, @ptrCast(@alignCast(&ptr[offset]))).*;
},
.delete => {
const vari: *const ?[]const u8 = @ptrCast(@alignCast(&ptr[offset]));
if (vari.*) |v|
try out.writeAll(v);
vari = @as(*const ?[]const u8, @ptrCast(@alignCast(&ptr[offset]))).*;
},
.default => |default| {
const vari: *const ?[]const u8 = @ptrCast(@alignCast(&ptr[offset]));
if (vari.*) |v| {
try out.writeAll(v);
} else {
try out.writeAll(default);
}
const sptr: *const ?[]const u8 = @ptrCast(@alignCast(&ptr[offset]));
vari = if (sptr.*) |sp| sp else default;
},
else => unreachable,
}
if (vari) |v| {
try out.writeAll(v);
}
}
},
else => {