Skip to content

Commit

Permalink
removed all specialized iterators in favor of []const Value
Browse files Browse the repository at this point in the history
also cleaned up some dead files
  • Loading branch information
kristoff-it committed Oct 11, 2024
1 parent 11f7147 commit 1e79cdb
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 992 deletions.
4 changes: 2 additions & 2 deletions src/context/Array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub const Builtins = struct {
pub const slice = struct {
pub const signature: Signature = .{
.params = &.{ .Int, .{ .Opt = .Int } },
.ret = .Array,
.ret = .{ .Many = .any },
};
pub const description =
\\Slices an array from the first value (inclusive) to the
Expand Down Expand Up @@ -133,7 +133,7 @@ pub const Builtins = struct {
pub const at = struct {
pub const signature: Signature = .{
.params = &.{.Int},
.ret = .Value,
.ret = .any,
};
pub const description =
\\Returns the value at the provided index.
Expand Down
187 changes: 0 additions & 187 deletions src/context/Iterator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ _superhtml_context: superhtml.utils.IteratorContext(Value, Template) = .{},
_impl: Impl,

pub const Impl = union(enum) {
page_it: PageIterator,
// page_slice_it: SliceIterator(*const Page),
translation_it: TranslationIterator,
map_it: MapIterator,
// dynamic_it: SliceIterator(ziggy.dynamic.Value),
value_it: SliceIterator(Value),

pub fn len(impl: Impl) usize {
Expand Down Expand Up @@ -107,48 +102,6 @@ pub const Builtins = struct {
return it._superhtml_context.up();
}
};
// pub const len = struct {
// pub const signature: Signature = .{ .ret = .int };
// pub const description =
// \\Returns the total number of elements in this loop.
// ;
// pub const examples =
// \\$loop.len()
// ;

// pub fn call(
// it: Iterator,
// _: Allocator,
// args: []const Value,
// ) !Value {
// const bad_arg = .{ .err = "expected 0 arguments" };
// if (args.len != 0) return bad_arg;
// const l = it._len orelse return .{
// .err = "this iterator doesn't know its total length",
// };
// return Value.from(l);
// }
// };
// pub const @"len?" = struct {
// pub const signature: Signature = .{ .ret = .{ .opt = .int } };
// pub const description =
// \\Returns the total number of elements in this loop.
// \\
// ;
// pub const examples =
// \\$loop.len?()
// ;

// pub fn call(
// it: Iterator,
// _: Allocator,
// args: []const Value,
// ) !Value {
// const bad_arg = .{ .err = "expected 0 arguments" };
// if (args.len != 0) return bad_arg;
// return Value.from(it._len);
// }
// };
};

fn SliceIterator(comptime Element: type) type {
Expand All @@ -168,143 +121,3 @@ fn SliceIterator(comptime Element: type) type {
}
};
}

pub const PageIterator = struct {
idx: usize = 0,

site: *const Site,
parent_section_path: ?[]const u8,
list: std.mem.TokenIterator(u8, .scalar),
_len: usize,

pub fn init(
site: *const Site,
parent_section_path: ?[]const u8,
src: []const u8,
) PageIterator {
return .{
.site = site,
.parent_section_path = parent_section_path,
.list = std.mem.tokenizeScalar(u8, src, '\n'),
._len = std.mem.count(u8, src, "\n"),
};
}

pub fn len(it: PageIterator) usize {
return it._len;
}

pub fn next(it: *PageIterator, gpa: Allocator) !?*const Page {
_ = gpa;

const next_page = it.list.next() orelse return null;
defer it.idx += 1;

const page = context.pageGet(
it.site,
next_page,
it.parent_section_path,
it.idx,
false,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.PageLoad => @panic("TODO: report page load errors"),
};

return page;
}
};

pub const TranslationIterator = struct {
idx: usize = 0,
page: *const Page,
_len: usize,

pub fn init(
page: *const Page,
) TranslationIterator {
return .{
.page = page,
._len = if (page.translation_key == null)
context.allSites().len
else
page._meta.key_variants.len,
};
}

pub fn len(it: TranslationIterator) usize {
return it._len;
}

pub fn next(it: *TranslationIterator, gpa: Allocator) !?*const Page {
_ = gpa;
if (it.idx >= it._len) return null;

defer it.idx += 1;

const t: Page.Translation = if (it.page.translation_key == null) .{
.site = &context.allSites()[it.idx],
.md_rel_path = it.page._meta.md_rel_path,
} else it.page._meta.key_variants[it.idx];

const page = context.pageGet(
t.site,
t.md_rel_path,
null,
null,
false,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.PageLoad => @panic("trying to access a non-existent localized variant of a page is an error for now, sorry! give the same translation key to all variants of this page and you won't see this error anymore."),
};

return page;
}
};

pub const MapIterator = struct {
idx: usize = 0,
it: std.StringArrayHashMap(ziggy.dynamic.Value).Iterator,
_len: usize,
filter: ?[]const u8 = null,

pub fn init(
it: std.StringArrayHashMap(ziggy.dynamic.Value).Iterator,
filter: ?[]const u8,
) MapIterator {
const f = filter orelse return .{ .it = it, ._len = it.len };
var filter_it = it;
var count: usize = 0;
while (filter_it.next()) |elem| {
if (std.mem.indexOf(u8, elem.key_ptr.*, f) != null) count += 1;
}
return .{ .it = it, ._len = count, .filter = f };
}

pub fn len(it: MapIterator) usize {
return it._len;
}

pub fn next(it: *MapIterator, _: Allocator) !?Map.KV {
if (it.idx >= it._len) return null;

while (it.it.next()) |elem| {
const f = it.filter orelse {
it.idx += 1;
return .{
.key = elem.key_ptr.*,
.value = elem.value_ptr.*,
};
};
if (std.mem.indexOf(u8, elem.key_ptr.*, f) != null) {
it.idx += 1;
return .{
.key = elem.key_ptr.*,
.value = elem.value_ptr.*,
};
}
}

unreachable;
}
};
53 changes: 37 additions & 16 deletions src/context/Map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,8 @@ pub const Builtins = struct {
const bad_arg = .{ .err = "expected 0 arguments" };
if (args.len != 0) return bad_arg;

return .{
.iterator = try context.Iterator.init(gpa, .{
.map_it = context.Iterator.MapIterator.init(
map.value.fields.iterator(),
null,
),
}),
};
const kvs = try keyValueArray(gpa, map, null);
return context.Array.init(gpa, Value, kvs) catch unreachable;
}
};

Expand Down Expand Up @@ -217,14 +211,8 @@ pub const Builtins = struct {
else => return bad_arg,
};

return .{
.iterator = try context.Iterator.init(gpa, .{
.map_it = context.Iterator.MapIterator.init(
map.value.fields.iterator(),
filter,
),
}),
};
const kvs = try keyValueArray(gpa, map, filter);
return context.Array.init(gpa, Value, kvs) catch unreachable;
}
};
};
Expand All @@ -241,3 +229,36 @@ pub const KV = struct {
};
pub const Builtins = struct {};
};

fn keyValueArray(gpa: Allocator, map: Map, filter: ?[]const u8) ![]const Value {
if (filter) |f| {
var buf = std.ArrayList(Value).init(gpa);
var it = map.value.fields.iterator();

while (it.next()) |next| {
if (std.mem.indexOf(u8, next.key_ptr.*, f) != null) {
try buf.append(.{
.map_kv = .{
.key = next.key_ptr.*,
.value = next.value_ptr.*,
},
});
}
}
return buf.toOwnedSlice();
} else {
// no filter
const kvs = try gpa.alloc(Value, map.value.fields.count());
var it = map.value.fields.iterator();
for (kvs) |*kv| {
const next = it.next() orelse unreachable;
kv.* = .{
.map_kv = .{
.key = next.key_ptr.*,
.value = next.value_ptr.*,
},
};
}
return kvs;
}
}
60 changes: 47 additions & 13 deletions src/context/Page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,48 @@ pub const Builtins = struct {
args: []const Value,
) !Value {
if (args.len != 0) return .{ .err = "expected 0 arguments" };
return .{
.iterator = try context.Iterator.init(gpa, .{
.translation_it = context.Iterator.TranslationIterator.init(p),
}),
};

// if (true) return .{
// .iterator = try context.Iterator.init(gpa, .{
// .translation_it = context.Iterator.TranslationIterator.init(p),
// }),
// };

const all_sites = context.allSites();

// Because of a limitation of how indexing works, we
// assume that all translations will be present if there is no
// translation_key specified.
const total_variants = if (p.translation_key == null)
all_sites.len
else
p._meta.key_variants.len;

const localized_pages = try gpa.alloc(Value, total_variants);

var last_page = p;
for (localized_pages, 0..) |*lp, idx| {
const t: Page.Translation = if (p.translation_key == null) .{
.site = &all_sites[idx],
.md_rel_path = last_page._meta.md_rel_path,
} else last_page._meta.key_variants[idx];

const found_page = context.pageGet(
t.site,
t.md_rel_path,
null,
null,
false,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.PageLoad => @panic("trying to access a non-existent localized variant of a page is an error for now, sorry! give the same translation key to all variants of this page and you won't see this error anymore."),
};

lp.* = .{ .page = found_page };
last_page = found_page;
}

return context.Array.init(gpa, Value, localized_pages) catch unreachable;
}
};

Expand Down Expand Up @@ -502,12 +539,6 @@ pub const Builtins = struct {
) !Value {
if (args.len != 0) return .{ .err = "expected 0 arguments" };
return context.pageFind(.{ .subpages = p });
// return self._pages.call(gpa, .{
// .md_rel_path = p,
// .url_path_prefix = self._meta.site._meta.url_path_prefix,
// .parent_section_path = p[0 .. p.len - "index.md".len],
// .kind = .subpages,
// });
}
};

Expand Down Expand Up @@ -681,9 +712,12 @@ pub const Builtins = struct {
if (args.len != 0) return .{ .err = "expected 0 arguments" };

var buf = std.ArrayList(u8).init(gpa);
const ast = p._meta.ast orelse return .{
.err = "only the main page can be rendered for now",
const ast = p._meta.ast orelse unreachable;

if (!p._meta.is_root) return .{
.err = "only the main page can be rendered for now, sorry!",
};

try render.html(gpa, ast, ast.md.root, "", buf.writer());
return String.init(try buf.toOwnedSlice());
}
Expand Down
Loading

0 comments on commit 1e79cdb

Please sign in to comment.