authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-02-11 02:03:22 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-02-11 02:03:22 -08:00
log96945468b693684b72b68a9135a51ca993a284ad
tree1f0c64346099639803c420cdc89e4449258fb785
parentbbac5cad9776dec9ea2e7d110cfcd0e7381a962c

add '{{foo}}' syntax support for "raw" replacements

these are for when the 'foo' variable contains html you want to print use at your own risk

2 files changed, 19 insertions(+), 5 deletions(-)

src/astgen.zig+11-2
......@@ -10,7 +10,7 @@ pub const Value = union(enum) {
1010 element: Element,
1111 attr: Attr,
1212 string: string,
13 replacement: []const string,
13 replacement: Replacement,
1414 block: Block,
1515 body: Body,
1616 function: Fn,
......@@ -27,6 +27,11 @@ pub const Attr = struct {
2727 value: union(enum) { string: string, body: Body },
2828};
2929
30pub const Replacement = struct {
31 arms: []const string,
32 raw: bool = false,
33};
34
3035pub const Block = struct {
3136 name: Type,
3237 args: []const []const string,
......@@ -172,7 +177,11 @@ const Parser = struct {
172177 .args = self.doArgs(),
173178 } };
174179 }
175 return Value{ .replacement = self.doReplacement() };
180 if (self.tryEatSymbol("{")) {
181 defer self.eatSymbol("}");
182 return Value{ .replacement = .{ .arms = self.doReplacement(), .raw = true } };
183 }
184 return Value{ .replacement = .{ .arms = self.doReplacement() } };
176185 }
177186 return Value{ .element = self.doElement() };
178187 }
src/lib.zig+8-3
......@@ -72,13 +72,18 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va
7272 .string => |v| {
7373 try writer.writeAll(v[1 .. v.len - 1]);
7474 },
75 .replacement => |v| {
75 .replacement => |repl| {
76 const v = repl.arms;
7677 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(v[1..], data) else search(v, ctx);
7778 const TO = @TypeOf(x);
7879 const TI = @typeInfo(TO);
7980
8081 if (comptime std.meta.trait.isZigString(TO)) {
81 const s: []const u8 = x;
82 if (repl.raw) {
83 try writer.writeAll(x);
84 return;
85 }
86 const s: string = x;
8287 for (s) |c| {
8388 if (entityLookupBefore(&[_]u8{c})) |ent| {
8489 try writer.writeAll(ent.entity);
......@@ -170,7 +175,7 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va
170175 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2});
171176 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);
172177 }
173 const repvalue = astgen.Value{ .replacement = &.{"this"} };
178 const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"} } };
174179 try @call(.{}, func, args);
175180 try do(Ctx, alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);
176181 return;