diff --git a/src/parser.zig b/src/parser.zig index 82e7f6f..e4f5bd9 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -18,6 +18,11 @@ pub const NodeType = enum { url, cycle, firstof, + load, +}; + +pub const LoadNode = struct { + libraries: []const []const u8, }; pub const AutoescapeNode = struct { @@ -141,6 +146,7 @@ pub const Node = struct { url: ?UrlNode = null, cycle: ?CycleNode = null, firstof: ?FirstOfNode = null, + load: ?LoadNode = null, pub fn deinit(self: Node, allocator: std.mem.Allocator) void { switch (self.type) { @@ -225,6 +231,10 @@ pub const Node = struct { for (fo.values) |v| allocator.free(v); allocator.free(fo.values); }, + .load => if (self.load) |l| { + for (l.libraries) |lib| allocator.free(lib); + allocator.free(l.libraries); + }, } } }; @@ -1396,7 +1406,6 @@ pub const Parser = struct { const value = std.mem.trim(u8, args[start..i], " \t\r\n\"'"); try values.append(allocator, try allocator.dupe(u8, value)); - } allocator.free(node.tag.?.name); @@ -1414,6 +1423,40 @@ pub const Parser = struct { continue; } + if (std.mem.eql(u8, tag_name, "load")) { + const args = node.tag.?.args; + + var libraries = std.ArrayList([]const u8){}; + defer libraries.deinit(allocator); + + var i: usize = 0; + while (i < args.len) { + while (i < args.len and std.ascii.isWhitespace(args[i])) : (i += 1) {} + + if (i >= args.len) break; + + const start = i; + while (i < args.len and !std.ascii.isWhitespace(args[i])) : (i += 1) {} + + const lib_name = args[start..i]; + try libraries.append(allocator, try allocator.dupe(u8, lib_name)); + } + + allocator.free(node.tag.?.name); + allocator.free(node.tag.?.args); + + std.debug.print("3.0 - na real sou um load\n", .{}); + std.debug.print("===================================\n", .{}); + + try list.append(allocator, Node{ + .type = .load, + .load = .{ + .libraries = try libraries.toOwnedSlice(allocator), + }, + }); + continue; + } + // Para tags normais std.debug.print("===================================\n", .{}); try list.append(allocator, node); diff --git a/src/parser_test.zig b/src/parser_test.zig index b9643e6..a341d8e 100644 --- a/src/parser_test.zig +++ b/src/parser_test.zig @@ -609,3 +609,37 @@ test "parse firstof com fallback" { try testing.expectEqualStrings("var2", fo.values[1]); try testing.expectEqualStrings("Nenhum valor", fo.values[2]); } + +test "parse load simples" { + const allocator = testing.allocator; + const template = "{% load i18n humanize %}"; + const nodes = try parser.parse(allocator, template); + defer { + for (nodes) |node| node.deinit(allocator); + allocator.free(nodes); + } + + try testing.expectEqual(@as(usize, 1), nodes.len); + try testing.expect(nodes[0].type == .load); + const l = nodes[0].load.?; + try testing.expectEqual(@as(usize, 2), l.libraries.len); + try testing.expectEqualStrings("i18n", l.libraries[0]); + try testing.expectEqualStrings("humanize", l.libraries[1]); +} + +test "parse load com múltiplas" { + const allocator = testing.allocator; + const template = "{% load admin_urls static %}"; + const nodes = try parser.parse(allocator, template); + defer { + for (nodes) |node| node.deinit(allocator); + allocator.free(nodes); + } + + try testing.expectEqual(@as(usize, 1), nodes.len); + try testing.expect(nodes[0].type == .load); + const l = nodes[0].load.?; + try testing.expectEqual(@as(usize, 2), l.libraries.len); + try testing.expectEqualStrings("admin_urls", l.libraries[0]); + try testing.expectEqualStrings("static", l.libraries[1]); +} diff --git a/todo.md b/todo.md index d266aa0..5996198 100644 --- a/todo.md +++ b/todo.md @@ -1,19 +1,19 @@ # Tags -- [ ] autoescape +- [x] autoescape - [x] block - [x] comment - [ ] csrf_token -- [ ] cycle +- [x] cycle - [ ] debug - [x] extends - [x] filter -- [ ] firstof +- [x] firstof - [x] for - [x] if - [x] ifchanged - [x] include -- [ ] load +- [x] load - [ ] lorem - [x] now - [ ] partial @@ -21,10 +21,10 @@ - [ ] querystring - [ ] regroup - [ ] resetcycle -- [ ] spaceless +- [x] spaceless - [ ] templatetag -- [ ] url -- [ ] verbatim +- [x] url +- [x] verbatim - [ ] widthratio - [x] with @@ -98,7 +98,7 @@ ___ - [x] spaceless — remove espaços em branco - [x] verbatim — como raw - [x] url — reverse de URLs (quando tiver routing) -- [ ] cycle — alternar valores em loop -- [ ] firstof — fallback de variáveis -- [ ] load — para custom tags/filters (futuro) +- [x] cycle — alternar valores em loop +- [x] firstof — fallback de variáveis +- [x] load — para custom tags/filters (futuro) - [ ] csrf_token — quando tiver web