From 96945468b693684b72b68a9135a51ca993a284ad Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 11 Feb 2022 02:03:22 -0800 Subject: [PATCH] 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 --- src/astgen.zig | 13 +++++++++++-- src/lib.zig | 11 ++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/astgen.zig b/src/astgen.zig index 8bf65b40dc6e94924f17c4235c08c7f4914be1c5..bbb5e107ef86c6d3d638d8774481bd315ba75145 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -10,7 +10,7 @@ pub const Value = union(enum) { element: Element, attr: Attr, string: string, - replacement: []const string, + replacement: Replacement, block: Block, body: Body, function: Fn, @@ -27,6 +27,11 @@ pub const Attr = struct { value: union(enum) { string: string, body: Body }, }; +pub const Replacement = struct { + arms: []const string, + raw: bool = false, +}; + pub const Block = struct { name: Type, args: []const []const string, @@ -172,7 +177,11 @@ const Parser = struct { .args = self.doArgs(), } }; } - return Value{ .replacement = self.doReplacement() }; + if (self.tryEatSymbol("{")) { + defer self.eatSymbol("}"); + return Value{ .replacement = .{ .arms = self.doReplacement(), .raw = true } }; + } + return Value{ .replacement = .{ .arms = self.doReplacement() } }; } return Value{ .element = self.doElement() }; } diff --git a/src/lib.zig b/src/lib.zig index 2157d1c47aceda13e05ad14f9a85169bddda0a89..97b20a584d885eb6f11bdb3e417d2f8a8ef6f560 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -72,13 +72,18 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va .string => |v| { try writer.writeAll(v[1 .. v.len - 1]); }, - .replacement => |v| { + .replacement => |repl| { + const v = repl.arms; const x = if (comptime std.mem.eql(u8, v[0], "this")) search(v[1..], data) else search(v, ctx); const TO = @TypeOf(x); const TI = @typeInfo(TO); if (comptime std.meta.trait.isZigString(TO)) { - const s: []const u8 = x; + if (repl.raw) { + try writer.writeAll(x); + return; + } + const s: string = x; for (s) |c| { if (entityLookupBefore(&[_]u8{c})) |ent| { try writer.writeAll(ent.entity); @@ -170,7 +175,7 @@ fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comptime va const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2}); @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx); } - const repvalue = astgen.Value{ .replacement = &.{"this"} }; + const repvalue = astgen.Value{ .replacement = .{ .arms = &.{"this"} } }; try @call(.{}, func, args); try do(Ctx, alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1); return; -- 2.54.0