diff --git a/src/lib.zig b/src/lib.zig index 34634599c6028de48f96ff0ed41aa2a059bccec3..973303423833a13ac97f16570e79306fb8f2ddfb 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -24,6 +24,7 @@ const std = @import("std"); const range = @import("range").range; +const htmlentities = @import("htmlentities"); const tokenize = @import("./tokenize.zig"); const astgen = @import("./astgen.zig"); @@ -75,7 +76,14 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype const TO = @TypeOf(x); if (comptime std.meta.trait.isZigString(TO)) { - try writer.print("{s}", .{x}); + const s: []const u8 = x; + for (s) |c| { + if (entityLookupBefore(&[_]u8{c})) |ent| { + try writer.writeAll(ent.entity); + } else { + try writer.writeAll(&[_]u8{c}); + } + } return; } @compileError("pek: compile: unsupported type: " ++ @typeName(TO)); @@ -101,3 +109,15 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype fn search(comptime T: anytype, comptime args: []const []const u8) @TypeOf(if (args.len == 1) @field(T, args[0]) else search(@field(T, args[0]), args[1..])) { return if (args.len == 1) @field(T, args[0]) else search(@field(T, args[0]), args[1..]); } + +fn entityLookupBefore(in: []const u8) ?htmlentities.Entity { + for (htmlentities.ENTITIES) |e| { + if (!std.mem.endsWith(u8, e.entity, ";")) { + continue; + } + if (std.mem.eql(u8, e.characters, in)) { + return e; + } + } + return null; +} diff --git a/src/main.zig b/src/main.zig index 881e56fb2c2a0cb036ebec973e9bbfdefdf89a2d..596f87c81b66fc6a3f12f271f8e3656d70557237 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,6 +21,7 @@ const example_document = \\ li({this.name}", "{this.state.code}) \\ /each/ \\ ) + \\ p("Spooky text: "{spooky}) \\ ) \\) ; @@ -39,5 +40,6 @@ pub fn main() !void { .{ .name = "Houston", .state = .{ .code = "TX" } }, .{ .name = "Phoenix", .state = .{ .code = "AZ" } }, }, + .spooky = "I better not be in bold.", }); } diff --git a/zig.mod b/zig.mod index 83b771ae9c9397286ace2a44d93ca90b9da3b2a5..db3ca76e716f1a99ea23c5edf69c3ff41fa73d5b 100644 --- a/zig.mod +++ b/zig.mod @@ -5,3 +5,4 @@ license: AGPL-3.0 description: An HTML preprocessor with a builtin template engine. dependencies: - src: git https://github.com/nektro/zig-range + - src: git https://github.com/kivikakk/htmlentities.zig diff --git a/zigmod.lock b/zigmod.lock index 4abcde2af0172615d652f2b48a13934c6821abf0..388460f90435c2285013396ac1fccf49f60b9849 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,2 +1,3 @@ 2 git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f +git https://github.com/kivikakk/htmlentities.zig commit-c3cbe4cb3a9a9bd60e7c9fedcf9b97e087b98d6c