update: add url
This commit is contained in:
parent
b7550e9b01
commit
0891dca8bf
3 changed files with 599 additions and 499 deletions
|
|
@ -15,6 +15,7 @@ pub const NodeType = enum {
|
||||||
filter_block,
|
filter_block,
|
||||||
autoescape,
|
autoescape,
|
||||||
spaceless,
|
spaceless,
|
||||||
|
url,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const AutoescapeNode = struct {
|
pub const AutoescapeNode = struct {
|
||||||
|
|
@ -24,6 +25,11 @@ pub const AutoescapeNode = struct {
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const UrlNode = struct {
|
||||||
|
name: []const u8,
|
||||||
|
args: []const []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub const SpacelessNode = struct {
|
pub const SpacelessNode = struct {
|
||||||
body: []Node,
|
body: []Node,
|
||||||
raw_open: []const u8,
|
raw_open: []const u8,
|
||||||
|
|
@ -122,6 +128,7 @@ pub const Node = struct {
|
||||||
filter_block: ?FilterBlockNode = null,
|
filter_block: ?FilterBlockNode = null,
|
||||||
autoescape: ?AutoescapeNode = null,
|
autoescape: ?AutoescapeNode = null,
|
||||||
spaceless: ?SpacelessNode = null,
|
spaceless: ?SpacelessNode = null,
|
||||||
|
url: ?UrlNode = null,
|
||||||
|
|
||||||
pub fn deinit(self: Node, allocator: std.mem.Allocator) void {
|
pub fn deinit(self: Node, allocator: std.mem.Allocator) void {
|
||||||
switch (self.type) {
|
switch (self.type) {
|
||||||
|
|
@ -193,6 +200,11 @@ pub const Node = struct {
|
||||||
.now => if (self.now) |n| allocator.free(n.format),
|
.now => if (self.now) |n| allocator.free(n.format),
|
||||||
.extends => if (self.extends) |e| allocator.free(e.parent_name),
|
.extends => if (self.extends) |e| allocator.free(e.parent_name),
|
||||||
.super => {},
|
.super => {},
|
||||||
|
.url => if (self.url) |u| {
|
||||||
|
allocator.free(u.name);
|
||||||
|
for (u.args) |a| allocator.free(a);
|
||||||
|
allocator.free(u.args);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -1223,6 +1235,60 @@ pub const Parser = struct {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (std.mem.eql(u8, tag_name, "url")) {
|
||||||
|
const args = node.tag.?.args;
|
||||||
|
|
||||||
|
var arg_list = std.ArrayList([]const u8){};
|
||||||
|
defer arg_list.deinit(allocator);
|
||||||
|
|
||||||
|
var i: usize = 0;
|
||||||
|
// Pula o nome da view (entre aspas)
|
||||||
|
while (i < args.len and std.ascii.isWhitespace(args[i])) : (i += 1) {}
|
||||||
|
if (i >= args.len or args[i] != '\'') return error.InvalidUrlSyntax;
|
||||||
|
i += 1;
|
||||||
|
const view_start = i;
|
||||||
|
while (i < args.len and args[i] != '\'') : (i += 1) {}
|
||||||
|
if (i >= args.len or args[i] != '\'') return error.InvalidUrlSyntax;
|
||||||
|
const view_name = args[view_start..i];
|
||||||
|
i += 1;
|
||||||
|
|
||||||
|
const duped_view = try allocator.dupe(u8, view_name);
|
||||||
|
|
||||||
|
// Agora os argumentos
|
||||||
|
while (i < args.len) {
|
||||||
|
while (i < args.len and std.ascii.isWhitespace(args[i])) : (i += 1) {}
|
||||||
|
if (i >= args.len) break;
|
||||||
|
|
||||||
|
const arg_start = i;
|
||||||
|
if (args[i] == '"' or args[i] == '\'') {
|
||||||
|
const quote = args[i];
|
||||||
|
i += 1;
|
||||||
|
while (i < args.len and args[i] != quote) : (i += 1) {}
|
||||||
|
if (i >= args.len) return error.UnclosedQuoteInUrl;
|
||||||
|
i += 1;
|
||||||
|
} else {
|
||||||
|
while (i < args.len and !std.ascii.isWhitespace(args[i])) : (i += 1) {}
|
||||||
|
}
|
||||||
|
const arg = args[arg_start..i];
|
||||||
|
try arg_list.append(allocator, try allocator.dupe(u8, arg));
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.free(node.tag.?.name);
|
||||||
|
allocator.free(node.tag.?.args);
|
||||||
|
|
||||||
|
std.debug.print("3.0 - na real sou uma url\n", .{});
|
||||||
|
std.debug.print("===================================\n", .{});
|
||||||
|
|
||||||
|
try list.append(allocator, Node{
|
||||||
|
.type = .url,
|
||||||
|
.url = .{
|
||||||
|
.name = duped_view,
|
||||||
|
.args = try arg_list.toOwnedSlice(allocator),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Para tags normais
|
// Para tags normais
|
||||||
std.debug.print("===================================\n", .{});
|
std.debug.print("===================================\n", .{});
|
||||||
try list.append(allocator, node);
|
try list.append(allocator, node);
|
||||||
|
|
|
||||||
1030
src/parser_test.zig
1030
src/parser_test.zig
File diff suppressed because it is too large
Load diff
2
todo.md
2
todo.md
|
|
@ -97,7 +97,7 @@ ___
|
||||||
- [x] autoescape — segurança importante
|
- [x] autoescape — segurança importante
|
||||||
- [x] spaceless — remove espaços em branco
|
- [x] spaceless — remove espaços em branco
|
||||||
- [x] verbatim — como raw
|
- [x] verbatim — como raw
|
||||||
- [ ] url — reverse de URLs (quando tiver routing)
|
- [x] url — reverse de URLs (quando tiver routing)
|
||||||
- [ ] cycle — alternar valores em loop
|
- [ ] cycle — alternar valores em loop
|
||||||
- [ ] firstof — fallback de variáveis
|
- [ ] firstof — fallback de variáveis
|
||||||
- [ ] load — para custom tags/filters (futuro)
|
- [ ] load — para custom tags/filters (futuro)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue