From a9e8d0e9c929bec830b086e473ef1362e1f873d9 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 30 Apr 2026 19:20:16 +0200 Subject: refactor(typst): generalize template --- src/eval/math.zig | 47 ++++++++++++++++++++++++++------------ src/eval/template.typ | 18 +++++++++++++++ src/eval/template_block.typ | 8 +++++++ src/eval/template_content.typ | 8 +++++++ src/eval/template_math_block.typ | 16 ------------- src/eval/template_math_content.typ | 16 ------------- 6 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 src/eval/template.typ create mode 100644 src/eval/template_block.typ create mode 100644 src/eval/template_content.typ delete mode 100644 src/eval/template_math_block.typ delete mode 100644 src/eval/template_math_content.typ (limited to 'src/eval') diff --git a/src/eval/math.zig b/src/eval/math.zig index aeec997..fa95918 100644 --- a/src/eval/math.zig +++ b/src/eval/math.zig @@ -5,8 +5,10 @@ const HTML = Element.HTML; const Element = @import("Element.zig"); const Node = Element.Node; -const content_template = @embedFile("template_math_content.typ"); -const block_template = @embedFile("template_math_block.typ"); +const common_template = @embedFile("template.typ"); +const content_template = @embedFile("template_content.typ"); +const block_template = @embedFile("template_block.typ"); +const apply_template = "#show: display.with(param)\n\n"; pub const Error = error{InvalidTypstTemplate} || Allocator.Error; @@ -28,19 +30,18 @@ fn escape(alloc: Allocator, content: []const u8) ![]const u8 { } fn generateFile(alloc: Allocator, template: []const u8, content: []const u8) Error![]const u8 { - var iter = std.mem.splitSequence(u8, template, "!!"); - const beg = iter.next() orelse return Error.InvalidTypstTemplate; - const end = iter.next() orelse return Error.InvalidTypstTemplate; - if (iter.next() != null) return Error.InvalidTypstTemplate; - - var acc = try std.ArrayList(u8).initCapacity(alloc, beg.len + end.len + content.len); - try acc.appendSlice(alloc, beg); + var acc = try std.ArrayList(u8).initCapacity( + alloc, + common_template.len + template.len + apply_template.len + content.len, + ); + try acc.appendSlice(alloc, common_template); + try acc.appendSlice(alloc, template); + try acc.appendSlice(alloc, apply_template); try acc.appendSlice(alloc, content); - try acc.appendSlice(alloc, end); return try acc.toOwnedSlice(alloc); } -fn Math(comptime template: []const u8) type { +fn Math(comptime template: []const u8, comptime modFn: *const fn (Allocator, []const u8) Allocator.Error![]const u8) type { return struct { content: ?[]const u8 = null, node: Node, @@ -67,7 +68,7 @@ fn Math(comptime template: []const u8) type { var arena = std.heap.ArenaAllocator.init(alloc); defer arena.deinit(); const escaped = try escape(arena.allocator(), content); - const file = generateFile(arena.allocator(), template, escaped) catch |err| switch (err) { + const file = generateFile(arena.allocator(), template, try modFn(arena.allocator(), escaped)) catch |err| switch (err) { Error.InvalidTypstTemplate => @panic("invalid template"), Error.OutOfMemory => return Error.OutOfMemory, }; @@ -77,8 +78,26 @@ fn Math(comptime template: []const u8) type { }; } -pub const Content = Math(content_template); -pub const Block = Math(block_template); +pub const Content = Math(content_template, mathContent); +pub const Block = Math(block_template, mathBlock); + +fn mathContent(alloc: Allocator, content: []const u8) Allocator.Error![]const u8 { + const escaped = try escape(alloc, content); + var acc = try std.ArrayList(u8).initCapacity(alloc, escaped.len + 2); + try acc.append(alloc, '$'); + try acc.appendSlice(alloc, escaped); + try acc.append(alloc, '$'); + return try acc.toOwnedSlice(alloc); +} + +fn mathBlock(alloc: Allocator, content: []const u8) Allocator.Error![]const u8 { + const escaped = try escape(alloc, content); + var acc = try std.ArrayList(u8).initCapacity(alloc, escaped.len + 4); + try acc.appendSlice(alloc, "$ "); + try acc.appendSlice(alloc, escaped); + try acc.appendSlice(alloc, " $"); + return try acc.toOwnedSlice(alloc); +} fn doTest(alloc: Allocator, v: []const u8, r: []const u8) !void { const escaped = try escape(alloc, v); diff --git a/src/eval/template.typ b/src/eval/template.typ new file mode 100644 index 0000000..18a2695 --- /dev/null +++ b/src/eval/template.typ @@ -0,0 +1,18 @@ +#let display(param, body) = context { + show math.equation: set text(font: "New Computer Modern Math") + show math.text: set text(font: "New Computer Modern") + + let m = measure(body) + + let data = param(m) + + set page( + fill: none, + margin: data.margin, + width: data.width, + height: data.height, + ) + + body +} + diff --git a/src/eval/template_block.typ b/src/eval/template_block.typ new file mode 100644 index 0000000..6ce1b37 --- /dev/null +++ b/src/eval/template_block.typ @@ -0,0 +1,8 @@ +#let param(m, margin: 4pt) = { + return ( + margin: margin, + width: m.width + margin*2, + height: m.height + margin*2, + ) +} + diff --git a/src/eval/template_content.typ b/src/eval/template_content.typ new file mode 100644 index 0000000..6ce1b37 --- /dev/null +++ b/src/eval/template_content.typ @@ -0,0 +1,8 @@ +#let param(m, margin: 4pt) = { + return ( + margin: margin, + width: m.width + margin*2, + height: m.height + margin*2, + ) +} + diff --git a/src/eval/template_math_block.typ b/src/eval/template_math_block.typ deleted file mode 100644 index b36f063..0000000 --- a/src/eval/template_math_block.typ +++ /dev/null @@ -1,16 +0,0 @@ -#let display(body) = context { - show math.equation: set text(font: "New Computer Modern Math") - show math.text: set text(font: "New Computer Modern") - - let margin = 4pt - let m = measure(body) - set page( - fill: none, - margin: margin, - width: m.width + margin*2, - height: m.height + margin*2, - ) - body -} - -#display()[$ !! $] diff --git a/src/eval/template_math_content.typ b/src/eval/template_math_content.typ deleted file mode 100644 index b4faf43..0000000 --- a/src/eval/template_math_content.typ +++ /dev/null @@ -1,16 +0,0 @@ -#let display(body) = context { - show math.equation: set text(font: "New Computer Modern Math") - show math.text: set text(font: "New Computer Modern") - - let margin = 4pt - let m = measure(body) - set page( - fill: none, - margin: margin, - width: m.width + margin*2, - height: m.height + margin*2, - ) - body -} - -#display()[$!!$] -- cgit v1.2.3