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) {...@@ -10,7 +10,7 @@ pub const Value = union(enum) {
10 element: Element,10 element: Element,
11 attr: Attr,11 attr: Attr,
12 string: string,12 string: string,
13 replacement: []const string,13 replacement: Replacement,
14 block: Block,14 block: Block,
15 body: Body,15 body: Body,
16 function: Fn,16 function: Fn,
...@@ -27,6 +27,11 @@ pub const Attr = struct {...@@ -27,6 +27,11 @@ pub const Attr = struct {
27 value: union(enum) { string: string, body: Body },27 value: union(enum) { string: string, body: Body },
28};28};
2929
30pub const Replacement = struct {
31 arms: []const string,
32 raw: bool = false,
33};
34
30pub const Block = struct {35pub const Block = struct {
31 name: Type,36 name: Type,
32 args: []const []const string,37 args: []const []const string,
...@@ -172,7 +177,11 @@ const Parser = struct {...@@ -172,7 +177,11 @@ const Parser = struct {
172 .args = self.doArgs(),177 .args = self.doArgs(),
173 } };178 } };
174 }179 }
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() } };
176 }185 }
177 return Value{ .element = self.doElement() };186 return Value{ .element = self.doElement() };
178 }187 }
src/lib.zig+8-3
...@@ -72,13 +72,18 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va...@@ -72,13 +72,18 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va
72 .string => |v| {72 .string => |v| {
73 try writer.writeAll(v[1 .. v.len - 1]);73 try writer.writeAll(v[1 .. v.len - 1]);
74 },74 },
75 .replacement => |v| {75 .replacement => |repl| {
76 const v = repl.arms;
76 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(v[1..], data) else search(v, ctx);77 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(v[1..], data) else search(v, ctx);
77 const TO = @TypeOf(x);78 const TO = @TypeOf(x);
78 const TI = @typeInfo(TO);79 const TI = @typeInfo(TO);
7980
80 if (comptime std.meta.trait.isZigString(TO)) {81 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;
82 for (s) |c| {87 for (s) |c| {
83 if (entityLookupBefore(&[_]u8{c})) |ent| {88 if (entityLookupBefore(&[_]u8{c})) |ent| {
84 try writer.writeAll(ent.entity);89 try writer.writeAll(ent.entity);
...@@ -170,7 +175,7 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va...@@ -170,7 +175,7 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va
170 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2});175 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2});
171 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);176 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);
172 }177 }
173 const repvalue = astgen.Value{ .replacement = &.{"this"} };178 const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"} } };
174 try @call(.{}, func, args);179 try @call(.{}, func, args);
175 try do(Ctx, alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);180 try do(Ctx, alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);
176 return;181 return;