aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
l---------src/data/_template.typ1
l---------src/data/_template_block.typ1
l---------src/data/_template_content.typ1
-rw-r--r--src/data/block_1.typ6
-rw-r--r--src/data/block_2.typ6
-rw-r--r--src/data/block_3.typ6
-rw-r--r--src/data/content_1.typ6
-rw-r--r--src/data/content_2.typ6
-rw-r--r--src/data/test_block_1.typ3
-rw-r--r--src/data/test_block_2.typ3
-rw-r--r--src/data/test_block_3.typ3
-rw-r--r--src/data/test_content_1.typ3
-rw-r--r--src/data/test_content_2.typ3
-rw-r--r--src/eval/math.zig47
-rw-r--r--src/eval/template.typ (renamed from src/data/_base.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
-rw-r--r--src/eval/template_math_content.typ16
-rw-r--r--src/math.zig6
-rw-r--r--src/paragraph.zig4
-rw-r--r--src/testing.zig2
22 files changed, 97 insertions, 72 deletions
diff --git a/src/data/_template.typ b/src/data/_template.typ
new file mode 120000
index 0000000..7b99414
--- /dev/null
+++ b/src/data/_template.typ
@@ -0,0 +1 @@
+../eval/template.typ \ No newline at end of file
diff --git a/src/data/_template_block.typ b/src/data/_template_block.typ
new file mode 120000
index 0000000..612a278
--- /dev/null
+++ b/src/data/_template_block.typ
@@ -0,0 +1 @@
+../eval/template_block.typ \ No newline at end of file
diff --git a/src/data/_template_content.typ b/src/data/_template_content.typ
new file mode 120000
index 0000000..3e91e8c
--- /dev/null
+++ b/src/data/_template_content.typ
@@ -0,0 +1 @@
+../eval/template_content.typ \ No newline at end of file
diff --git a/src/data/block_1.typ b/src/data/block_1.typ
new file mode 100644
index 0000000..7b55cff
--- /dev/null
+++ b/src/data/block_1.typ
@@ -0,0 +1,6 @@
+#import "_template.typ": *
+#import "_template_block.typ": *
+
+#show: display.with(param)
+
+$ x $
diff --git a/src/data/block_2.typ b/src/data/block_2.typ
new file mode 100644
index 0000000..b80d056
--- /dev/null
+++ b/src/data/block_2.typ
@@ -0,0 +1,6 @@
+#import "_template.typ": *
+#import "_template_block.typ": *
+
+#show: display.with(param)
+
+$ x^2 $
diff --git a/src/data/block_3.typ b/src/data/block_3.typ
new file mode 100644
index 0000000..4c85a2b
--- /dev/null
+++ b/src/data/block_3.typ
@@ -0,0 +1,6 @@
+#import "_template.typ": *
+#import "_template_block.typ": *
+
+#show: display.with(param)
+
+$ forall x in RR, quad f(x) = x^2 $
diff --git a/src/data/content_1.typ b/src/data/content_1.typ
new file mode 100644
index 0000000..edfec83
--- /dev/null
+++ b/src/data/content_1.typ
@@ -0,0 +1,6 @@
+#import "_template.typ": *
+#import "_template_content.typ": *
+
+#show: display.with(param)
+
+$x$
diff --git a/src/data/content_2.typ b/src/data/content_2.typ
new file mode 100644
index 0000000..01d3a56
--- /dev/null
+++ b/src/data/content_2.typ
@@ -0,0 +1,6 @@
+#import "_template.typ": *
+#import "_template_content.typ": *
+
+#show: display.with(param)
+
+$x^2$
diff --git a/src/data/test_block_1.typ b/src/data/test_block_1.typ
deleted file mode 100644
index 582ab60..0000000
--- a/src/data/test_block_1.typ
+++ /dev/null
@@ -1,3 +0,0 @@
-#import "_base.typ": *
-
-#display[$ x $]
diff --git a/src/data/test_block_2.typ b/src/data/test_block_2.typ
deleted file mode 100644
index cce19c2..0000000
--- a/src/data/test_block_2.typ
+++ /dev/null
@@ -1,3 +0,0 @@
-#import "_base.typ": *
-
-#display[$ x^2 $]
diff --git a/src/data/test_block_3.typ b/src/data/test_block_3.typ
deleted file mode 100644
index 04dae08..0000000
--- a/src/data/test_block_3.typ
+++ /dev/null
@@ -1,3 +0,0 @@
-#import "_base.typ": *
-
-#display[$ forall x in RR, quad f(x) = x^2 $]
diff --git a/src/data/test_content_1.typ b/src/data/test_content_1.typ
deleted file mode 100644
index 171e00b..0000000
--- a/src/data/test_content_1.typ
+++ /dev/null
@@ -1,3 +0,0 @@
-#import "_base.typ": *
-
-#display[$x$]
diff --git a/src/data/test_content_2.typ b/src/data/test_content_2.typ
deleted file mode 100644
index a126a0c..0000000
--- a/src/data/test_content_2.typ
+++ /dev/null
@@ -1,3 +0,0 @@
-#import "_base.typ": *
-
-#display[$x^2$]
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/data/_base.typ b/src/eval/template.typ
index fa5b362..18a2695 100644
--- a/src/data/_base.typ
+++ b/src/eval/template.typ
@@ -1,14 +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
}
+
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()[$!!$]
diff --git a/src/math.zig b/src/math.zig
index e28b3b9..4c164bb 100644
--- a/src/math.zig
+++ b/src/math.zig
@@ -42,15 +42,15 @@ test {
\\$$$
\\x
\\$$$
- , "<figure>" ++ @embedFile("data/test_block_1.svg") ++ "</figure>");
+ , "<figure>" ++ @embedFile("data/block_1.svg") ++ "</figure>");
try doTest(parse, alloc,
\\$$$
\\x^2
\\$$$
- , "<figure>" ++ @embedFile("data/test_block_2.svg") ++ "</figure>");
+ , "<figure>" ++ @embedFile("data/block_2.svg") ++ "</figure>");
try doTest(parse, alloc,
\\$$$
\\forall x in RR, quad f(x) = x^2
\\$$$
- , "<figure>" ++ @embedFile("data/test_block_3.svg") ++ "</figure>");
+ , "<figure>" ++ @embedFile("data/block_3.svg") ++ "</figure>");
}
diff --git a/src/paragraph.zig b/src/paragraph.zig
index 5395082..82433b5 100644
--- a/src/paragraph.zig
+++ b/src/paragraph.zig
@@ -51,8 +51,8 @@ test "parse paragraphs" {
try doTest(parse, alloc, "[](bar)", "<p><a href=\"bar\">bar</a></p>");
try doTest(parse, alloc, "[foo](bar)", "<p><a href=\"bar\">foo</a></p>");
try doTest(parse, alloc, "hello [foo](bar) world", "<p>hello <a href=\"bar\">foo</a> world</p>");
- try doTestMath(parse, alloc, "$x$", "<p>" ++ @embedFile("data/test_content_1.svg") ++ "</p>");
- try doTestMath(parse, alloc, "$x^2$", "<p>" ++ @embedFile("data/test_content_2.svg") ++ "</p>");
+ try doTestMath(parse, alloc, "$x$", "<p>" ++ @embedFile("data/content_1.svg") ++ "</p>");
+ try doTestMath(parse, alloc, "$x^2$", "<p>" ++ @embedFile("data/content_2.svg") ++ "</p>");
try doTestError(parse, alloc, "hello *world", Error.ModifierNotClosed);
try doTestError(parse, alloc, "hello *wo_rld*", Error.ModifierNotClosed);
diff --git a/src/testing.zig b/src/testing.zig
index 143d6ca..5bab88b 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -53,7 +53,7 @@ pub fn doMath(comptime parse: ParserFn, parent: Allocator, t: []const u8, v: []c
if ((std.mem.startsWith(u8, g_it, "xlink:href=") and std.mem.startsWith(u8, g_it, "xlink:href")) or
(std.mem.startsWith(u8, g_it, "id=") and std.mem.startsWith(u8, v_it, "id="))) continue;
if (!std.mem.eql(u8, g_it, v_it)) {
- std.debug.print("not the same: {s} vs {s}", .{ g_it, v_it });
+ std.debug.print("not the same: {s} vs {s}\n", .{ g_it, v_it });
break :blk false;
}
}