@@ -7,7 +7,11 @@ const Kind = enum {
directive,
};
const Offset = struct { usize, usize, Kind };
const Offset = struct {
start: usize,
end: usize,
kind: Kind,
};
pub fn PageRuntime(comptime PageDataType: type) type {
return struct {
@@ -70,18 +74,30 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
if (indexOfScalar(u8, pblob, '<')) |offset| {
pblob = pblob[offset..];
if (index != offset and offset != 0) {
found_offsets = found_offsets ++ [_]Offset{.{ index, index + offset, .slice }};
found_offsets = found_offsets ++ [_]Offset{.{
.start = index,
.end = index + offset,
.kind = .slice,
}};
}
index += offset;
if (Directive.init(pblob)) |drct| {
const end = drct.tag_block.len;
found_offsets = found_offsets ++ [_]Offset{.{ index, index + end, .directive }};
found_offsets = found_offsets ++ [_]Offset{.{
.start = index,
.end = index + end,
.kind = .directive,
}};
pblob = pblob[end..];
index += end;
} else {
if (indexOfPosLinear(u8, pblob, 1, "<")) |next| {
if (index != next) {
found_offsets = found_offsets ++ [_]Offset{.{ index, index + next, .slice }};
found_offsets = found_offsets ++ [_]Offset{.{
.start = index,
.end = index + next,
.kind = .slice,
}};
}
index += next;
pblob = pblob[next..];
@@ -90,7 +106,11 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
} else break;
}
if (index != pblob.len) {
found_offsets = found_offsets ++ [_]Offset{.{ index, index + pblob.len, .slice }};
found_offsets = found_offsets ++ [_]Offset{.{
.start = index,
.end = index + pblob.len,
.kind = .slice,
}};
}
const offset_len = found_offsets.len;
const offsets: [offset_len]Offset = found_offsets[0..offset_len].*;
@@ -113,18 +133,19 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
return try out.writeAll(blob);
var last_end: usize = 0;
for (Self.DataOffsets) |offs| {
const start = offs[0];
const end = offs[1];
switch (offs[2]) {
.slice => try out.writeAll(blob[start..end]),
for (Self.DataOffsets) |os| {
switch (os.kind) {
.slice => try out.writeAll(blob[os.start..os.end]),
.directive => {
if (Directive.init(blob[start..])) |drct| {
if (Directive.init(blob[os.start..os.end])) |drct| {
drct.formatTyped(PageDataType, self.data, out) catch |err| switch (err) {
error.IgnoreDirective => try out.writeAll(blob[start..end]),
error.IgnoreDirective => try out.writeAll(blob[os.start..os.end]),
error.VariableMissing => {
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[start..end]});
try out.writeAll(blob[start..end]);
if (!is_test) log.err(
"Template Error, variable missing {{{s}}}",
.{blob[os.start..os.end]},
);
try out.writeAll(blob[os.start..os.end]);
},
else => return err,
};
@@ -135,7 +156,7 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
}
},
}
last_end = end;
last_end = os.end;
} else {
return try out.writeAll(blob[last_end..]);
}