update: add querystring
This commit is contained in:
parent
1e0329a597
commit
0a0c7c9e8b
3 changed files with 100 additions and 3 deletions
|
|
@ -24,6 +24,11 @@ pub const NodeType = enum {
|
||||||
debug,
|
debug,
|
||||||
partialdef,
|
partialdef,
|
||||||
partial,
|
partial,
|
||||||
|
querystring,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const QueryStringNode = struct {
|
||||||
|
modifications: []const []const u8, // array de "key=value" ou "key=None"
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const PartialDefNode = struct {
|
pub const PartialDefNode = struct {
|
||||||
|
|
@ -172,6 +177,7 @@ pub const Node = struct {
|
||||||
lorem: ?LoremNode = null,
|
lorem: ?LoremNode = null,
|
||||||
partialdef: ?PartialDefNode = null,
|
partialdef: ?PartialDefNode = null,
|
||||||
partial: ?PartialNode = null,
|
partial: ?PartialNode = null,
|
||||||
|
querystring: ?QueryStringNode = null,
|
||||||
csrf_token: bool = false,
|
csrf_token: bool = false,
|
||||||
debug: bool = false,
|
debug: bool = false,
|
||||||
|
|
||||||
|
|
@ -277,6 +283,10 @@ pub const Node = struct {
|
||||||
.partial => if (self.partial) |p| {
|
.partial => if (self.partial) |p| {
|
||||||
allocator.free(p.name);
|
allocator.free(p.name);
|
||||||
},
|
},
|
||||||
|
.querystring => if (self.querystring) |qs| {
|
||||||
|
for (qs.modifications) |m| allocator.free(m);
|
||||||
|
allocator.free(qs.modifications);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -403,7 +413,7 @@ pub const Parser = struct {
|
||||||
return Node{
|
return Node{
|
||||||
.type = .partialdef,
|
.type = .partialdef,
|
||||||
.partialdef = .{
|
.partialdef = .{
|
||||||
.name = try allocator.dupe(u8, name),
|
.name = name,
|
||||||
.body = try body.toOwnedSlice(allocator),
|
.body = try body.toOwnedSlice(allocator),
|
||||||
.raw_open = raw_open,
|
.raw_open = raw_open,
|
||||||
.raw_close = raw_close,
|
.raw_close = raw_close,
|
||||||
|
|
@ -1195,6 +1205,7 @@ pub const Parser = struct {
|
||||||
const raw_open = node.tag.?.raw;
|
const raw_open = node.tag.?.raw;
|
||||||
|
|
||||||
const duped_name = try allocator.dupe(u8, partial_name);
|
const duped_name = try allocator.dupe(u8, partial_name);
|
||||||
|
errdefer allocator.free(duped_name);
|
||||||
|
|
||||||
allocator.free(node.tag.?.name);
|
allocator.free(node.tag.?.name);
|
||||||
allocator.free(node.tag.?.args);
|
allocator.free(node.tag.?.args);
|
||||||
|
|
@ -1652,6 +1663,58 @@ pub const Parser = struct {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (std.mem.eql(u8, tag_name, "querystring")) {
|
||||||
|
const args = node.tag.?.args;
|
||||||
|
|
||||||
|
var modifications = std.ArrayList([]const u8){};
|
||||||
|
defer modifications.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;
|
||||||
|
var in_quote = false;
|
||||||
|
var quote_char: u8 = 0;
|
||||||
|
if (args[i] == '"' or args[i] == '\'') {
|
||||||
|
in_quote = true;
|
||||||
|
quote_char = args[i];
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < args.len) {
|
||||||
|
if (in_quote) {
|
||||||
|
if (args[i] == quote_char) {
|
||||||
|
i += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (std.ascii.isWhitespace(args[i])) break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mod_str = std.mem.trim(u8, args[start..i], " \t\r\n\"'");
|
||||||
|
try modifications.append(allocator, try allocator.dupe(u8, mod_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.free(node.tag.?.name);
|
||||||
|
allocator.free(node.tag.?.args);
|
||||||
|
|
||||||
|
std.debug.print("3.0 - na real sou um querystring\n", .{});
|
||||||
|
std.debug.print("===================================\n", .{});
|
||||||
|
|
||||||
|
try list.append(allocator, Node{
|
||||||
|
.type = .querystring,
|
||||||
|
.querystring = .{
|
||||||
|
.modifications = try modifications.toOwnedSlice(allocator),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, tag_name, "csrf_token")) {
|
if (std.mem.eql(u8, tag_name, "csrf_token")) {
|
||||||
// Verifica se tem argumentos (não deve ter)
|
// Verifica se tem argumentos (não deve ter)
|
||||||
if (node.tag.?.args.len > 0 and !std.mem.allEqual(u8, node.tag.?.args, ' ')) {
|
if (node.tag.?.args.len > 0 and !std.mem.allEqual(u8, node.tag.?.args, ' ')) {
|
||||||
|
|
|
||||||
|
|
@ -772,3 +772,37 @@ test "parse partial uso" {
|
||||||
try testing.expect(nodes[1].type == .partial);
|
try testing.expect(nodes[1].type == .partial);
|
||||||
try testing.expectEqualStrings("cabecalho", nodes[1].partial.?.name);
|
try testing.expectEqualStrings("cabecalho", nodes[1].partial.?.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "parse querystring simples" {
|
||||||
|
const allocator = testing.allocator;
|
||||||
|
const template = "Link: {% querystring \"page=2\" %}";
|
||||||
|
const nodes = try parser.parse(allocator, template);
|
||||||
|
defer {
|
||||||
|
for (nodes) |node| node.deinit(allocator);
|
||||||
|
allocator.free(nodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
try testing.expectEqual(@as(usize, 2), nodes.len);
|
||||||
|
try testing.expect(nodes[1].type == .querystring);
|
||||||
|
const qs = nodes[1].querystring.?;
|
||||||
|
try testing.expectEqual(@as(usize, 1), qs.modifications.len);
|
||||||
|
try testing.expectEqualStrings("page=2", qs.modifications[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parse querystring múltiplos" {
|
||||||
|
const allocator = testing.allocator;
|
||||||
|
const template = "{% querystring \"ordenar=-nome\" \"pagina\" None %}";
|
||||||
|
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 == .querystring);
|
||||||
|
const qs = nodes[0].querystring.?;
|
||||||
|
try testing.expectEqual(@as(usize, 3), qs.modifications.len);
|
||||||
|
try testing.expectEqualStrings("ordenar=-nome", qs.modifications[0]);
|
||||||
|
try testing.expectEqualStrings("pagina", qs.modifications[1]);
|
||||||
|
try testing.expectEqualStrings("None", qs.modifications[2]);
|
||||||
|
}
|
||||||
|
|
|
||||||
4
todo.md
4
todo.md
|
|
@ -18,7 +18,7 @@
|
||||||
- [x] now
|
- [x] now
|
||||||
- [x] partial
|
- [x] partial
|
||||||
- [x] partialdef
|
- [x] partialdef
|
||||||
- [ ] querystring
|
- [x] querystring
|
||||||
- [ ] regroup
|
- [ ] regroup
|
||||||
- [ ] resetcycle
|
- [ ] resetcycle
|
||||||
- [x] spaceless
|
- [x] spaceless
|
||||||
|
|
@ -111,7 +111,7 @@ ___
|
||||||
- [x] debug
|
- [x] debug
|
||||||
- [x] lorem
|
- [x] lorem
|
||||||
- [x] partial / partialdef
|
- [x] partial / partialdef
|
||||||
- [ ] querystring
|
- [x] querystring
|
||||||
- [ ] regroup
|
- [ ] regroup
|
||||||
- [ ] resetcycle
|
- [ ] resetcycle
|
||||||
- [ ] templatetag
|
- [ ] templatetag
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue