aboutsummaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-30 19:20:16 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-30 19:20:16 +0200
commita9e8d0e9c929bec830b086e473ef1362e1f873d9 (patch)
tree9ff2e2c2f49f5ca918c976dde000e0ee61a7b8bf /src/eval
parenta36f5482ad90d6899d56e016666a2ca09240abd6 (diff)
refactor(typst): generalize template
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/math.zig47
-rw-r--r--src/eval/template.typ (renamed from src/eval/template_math_content.typ)14
-rw-r--r--src/eval/template_block.typ8
-rw-r--r--src/eval/template_content.typ8
-rw-r--r--src/eval/template_math_block.typ16
5 files changed, 57 insertions, 36 deletions
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_math_content.typ b/src/eval/template.typ
index b4faf43..18a2695 100644
--- a/src/eval/template_math_content.typ
+++ b/src/eval/template.typ
@@ -1,16 +1,18 @@
-#let display(body) = context {
+#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 margin = 4pt
let m = measure(body)
+
+ let data = param(m)
+
set page(
fill: none,
- margin: margin,
- width: m.width + margin*2,
- height: m.height + margin*2,
+ margin: data.margin,
+ width: data.width,
+ height: data.height,
)
+
body
}
-#display()[$!!$]
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()[$ !! $]