@@ -0,0 +1,344 @@
1+const std = @import("std");
2+const render = @import("render.zig");
3+
4+/// The text-level half of the renderer: canonical markers, whitespace, and tables.
5+/// Styling runs afterwards, in highlight.zig, over the bytes produced here.
6+pub const Normalizer = struct {
7+ ordered_indent: ?usize = null,
8+ ordered_next: usize = 1,
9+
10+ pub fn reset(self: *Normalizer) void {
11+ self.ordered_indent = null;
12+ self.ordered_next = 1;
13+ }
14+
15+ /// Canonical form of one markdown line: even indentation, one space after every
16+ /// marker, `-` bullets, sequential ordered runs, `> ` quotes, `---` rules, no
17+ /// trailing whitespace. Fence bodies never reach this.
18+ pub fn line(self: *Normalizer, a: std.mem.Allocator, raw: []const u8) []const u8 {
19+ const text = std.mem.trimEnd(u8, raw, " \t\r");
20+ const ind = indentOf(text);
21+ var rest = text[ind.end..];
22+ if (fenceMarker(rest)) |fence_run| return self.fence(a, ind.cols, rest, fence_run);
23+ if (isRule(rest)) {
24+ self.reset();
25+ return std.fmt.allocPrint(a, "{s}---", .{spaces(a, ind.cols)}) catch "---";
26+ }
27+ var out: std.ArrayList(u8) = .empty;
28+ out.appendSlice(a, spaces(a, ind.cols)) catch {};
29+ while (rest.len > 0 and rest[0] == '>') {
30+ out.appendSlice(a, "> ") catch {};
31+ rest = std.mem.trimStart(u8, rest[1..], " \t");
32+ }
33+ return self.body(a, &out, ind.cols, rest);
34+ }
35+
36+ fn body(self: *Normalizer, a: std.mem.Allocator, out: *std.ArrayList(u8), indent: usize, rest: []const u8) []const u8 {
37+ if (heading(rest)) |h| {
38+ self.reset();
39+ out.appendNTimes(a, '#', h.level) catch {};
40+ if (h.text.len > 0) {
41+ out.append(a, ' ') catch {};
42+ out.appendSlice(a, h.text) catch {};
43+ }
44+ return out.items;
45+ }
46+ if (marker(rest)) |m| switch (m) {
47+ .bullet => {
48+ self.ordered_indent = null;
49+ out.appendSlice(a, "- ") catch {};
50+ out.appendSlice(a, std.mem.trimStart(u8, rest[1..], " \t")) catch {};
51+ },
52+ .ordered => |o| {
53+ const num = if (self.ordered_indent == indent) self.ordered_next else o.num;
54+ self.ordered_indent = indent;
55+ self.ordered_next = num + 1;
56+ out.appendSlice(a, std.fmt.allocPrint(a, "{d}.", .{num}) catch "-") catch {};
57+ out.append(a, ' ') catch {};
58+ out.appendSlice(a, std.mem.trimStart(u8, rest[o.at..], " \t")) catch {};
59+ },
60+ } else {
61+ self.ordered_indent = null;
62+ out.appendSlice(a, rest) catch {};
63+ }
64+ return trimmed(out);
65+ }
66+
67+ fn fence(self: *Normalizer, a: std.mem.Allocator, indent: usize, rest: []const u8, run: []const u8) []const u8 {
68+ self.reset();
69+ var out: std.ArrayList(u8) = .empty;
70+ out.appendSlice(a, spaces(a, indent)) catch {};
71+ out.appendSlice(a, run) catch {};
72+ out.appendSlice(a, std.mem.trim(u8, rest[run.len..], " \t")) catch {};
73+ return out.items;
74+ }
75+};
76+
77+fn spaces(a: std.mem.Allocator, n: usize) []const u8 {
78+ const out = a.alloc(u8, n) catch return "";
79+ @memset(out, ' ');
80+ return out;
81+}
82+
83+fn trimmed(out: *std.ArrayList(u8)) []const u8 {
84+ while (out.items.len > 0 and (out.items[out.items.len - 1] == ' ' or out.items[out.items.len - 1] == '\t'))
85+ out.items.len -= 1;
86+ return out.items;
87+}
88+
89+const Indent = struct { end: usize, cols: usize };
90+
91+/// Leading whitespace, with tabs worth two columns and the depth rounded down to even
92+/// so nested levels line up.
93+fn indentOf(text: []const u8) Indent {
94+ var end: usize = 0;
95+ var cols: usize = 0;
96+ while (end < text.len and (text[end] == ' ' or text[end] == '\t')) : (end += 1)
97+ cols += if (text[end] == '\t') 2 else 1;
98+ return .{ .end = end, .cols = cols & ~@as(usize, 1) };
99+}
100+
101+fn fenceMarker(rest: []const u8) ?[]const u8 {
102+ if (rest.len == 0) return null;
103+ const c = rest[0];
104+ if (c != '`' and c != '~') return null;
105+ var n: usize = 0;
106+ while (n < rest.len and rest[n] == c) n += 1;
107+ return if (n >= 3) rest[0..n] else null;
108+}
109+
110+fn isRule(rest: []const u8) bool {
111+ if (rest.len == 0) return false;
112+ const c = rest[0];
113+ if (c != '-' and c != '*' and c != '_') return false;
114+ var marks: usize = 0;
115+ for (rest) |ch| {
116+ if (ch == c) {
117+ marks += 1;
118+ } else if (ch != ' ' and ch != '\t') {
119+ return false;
120+ }
121+ }
122+ return marks >= 3;
123+}
124+
125+const Heading = struct { level: usize, text: []const u8 };
126+
127+fn heading(rest: []const u8) ?Heading {
128+ var n: usize = 0;
129+ while (n < rest.len and rest[n] == '#') n += 1;
130+ if (n == 0 or n > 6) return null;
131+ if (n < rest.len and rest[n] != ' ' and rest[n] != '\t') return null;
132+ var text = std.mem.trimStart(u8, rest[n..], " \t");
133+ // A closing run of `#`s is decoration; only one that follows a space counts.
134+ var end = text.len;
135+ while (end > 0 and text[end - 1] == '#') end -= 1;
136+ if (end < text.len and (end == 0 or text[end - 1] == ' ' or text[end - 1] == '\t'))
137+ text = std.mem.trimEnd(u8, text[0..end], " \t");
138+ return .{ .level = n, .text = text };
139+}
140+
141+const Marker = union(enum) {
142+ bullet,
143+ ordered: struct { num: usize, at: usize },
144+};
145+
146+/// Byte offset just past the list marker, or null when the line has none.
147+fn marker(rest: []const u8) ?Marker {
148+ if (rest.len == 0) return null;
149+ const c = rest[0];
150+ if (c == '-' or c == '*' or c == '+')
151+ return if (rest.len == 1 or rest[1] == ' ' or rest[1] == '\t') .bullet else null;
152+ var at: usize = 0;
153+ while (at < rest.len and std.ascii.isDigit(rest[at])) at += 1;
154+ if (at == 0 or at >= rest.len) return null;
155+ if (rest[at] != '.' and rest[at] != ')') return null;
156+ if (at + 1 < rest.len and rest[at + 1] != ' ' and rest[at + 1] != '\t') return null;
157+ return .{ .ordered = .{ .num = std.fmt.parseInt(usize, rest[0..at], 10) catch 1, .at = at + 1 } };
158+}
159+
160+const Align = enum { none, left, center, right };
161+
162+fn alignOf(cell: []const u8) Align {
163+ const left = cell.len > 0 and cell[0] == ':';
164+ const right = cell.len > 1 and cell[cell.len - 1] == ':';
165+ if (left and right) return .center;
166+ if (left) return .left;
167+ if (right) return .right;
168+ return .none;
169+}
170+
171+fn splitCells(a: std.mem.Allocator, line: []const u8) []const []const u8 {
172+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
173+ var cells: std.ArrayList([]const u8) = .empty;
174+ var start: usize = 0;
175+ var i: usize = 0;
176+ while (i < trimmed_line.len) : (i += 1) {
177+ if (trimmed_line[i] == '\\' and i + 1 < trimmed_line.len) {
178+ i += 1;
179+ continue;
180+ }
181+ if (trimmed_line[i] == '|') {
182+ cells.append(a, std.mem.trim(u8, trimmed_line[start..i], " \t")) catch {};
183+ start = i + 1;
184+ }
185+ }
186+ cells.append(a, std.mem.trim(u8, trimmed_line[start..], " \t")) catch {};
187+ if (trimmed_line.len > 0 and trimmed_line[0] == '|' and cells.items.len > 0) _ = cells.orderedRemove(0);
188+ if (trimmed_line.len > 1 and trimmed_line[trimmed_line.len - 1] == '|' and cells.items.len > 0) _ = cells.pop();
189+ return cells.items;
190+}
191+
192+/// Cell text with its inline markers removed: what the styling pass will actually paint.
193+fn visible(a: std.mem.Allocator, cell: []const u8) []const u8 {
194+ var out: std.ArrayList(u8) = .empty;
195+ var i: usize = 0;
196+ while (i < cell.len) {
197+ const c = cell[i];
198+ if (c == '\\' and i + 1 < cell.len) {
199+ out.appendSlice(a, cell[i .. i + 2]) catch {};
200+ i += 2;
201+ continue;
202+ }
203+ if (c == '`' or c == '*' or c == '_') {
204+ i += 1;
205+ continue;
206+ }
207+ if (c == '[') {
208+ if (std.mem.indexOfScalarPos(u8, cell, i, ']')) |close| {
209+ if (close + 1 < cell.len and cell[close + 1] == '(') {
210+ if (std.mem.indexOfScalarPos(u8, cell, close + 1, ')')) |end| {
211+ out.appendSlice(a, cell[i + 1 .. close]) catch {};
212+ i = end + 1;
213+ continue;
214+ }
215+ }
216+ }
217+ }
218+ out.append(a, c) catch {};
219+ i += 1;
220+ }
221+ return out.items;
222+}
223+
224+fn cellWidth(a: std.mem.Allocator, cell: []const u8) usize {
225+ return render.displayWidth(visible(a, cell));
226+}
227+
228+/// Delimiter row (`|---|:--:|`) as per-column alignments, or null when the line is not one.
229+fn delimiterRow(a: std.mem.Allocator, line: []const u8) ?[]const Align {
230+ if (std.mem.indexOfScalar(u8, line, '-') == null) return null;
231+ const cells = splitCells(a, line);
232+ if (cells.len == 0) return null;
233+ const aligns = a.alloc(Align, cells.len) catch return null;
234+ for (cells, 0..) |cell, k| {
235+ var body = cell;
236+ if (body.len > 0 and body[0] == ':') body = body[1..];
237+ if (body.len > 0 and body[body.len - 1] == ':') body = body[0 .. body.len - 1];
238+ if (body.len == 0) return null;
239+ for (body) |c| {
240+ if (c != '-') return null;
241+ }
242+ aligns[k] = alignOf(cell);
243+ }
244+ return aligns;
245+}
246+
247+fn tableCandidate(line: []const u8) bool {
248+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
249+ if (trimmed_line.len == 0) return false;
250+ // Tables nested in quotes or lists keep their marker; reflowing them would drop it.
251+ if (trimmed_line[0] == '>' or trimmed_line[0] == '-' or trimmed_line[0] == '*' or trimmed_line[0] == '+') return false;
252+ return std.mem.indexOfScalar(u8, trimmed_line, '|') != null;
253+}
254+
255+fn rowCells(line: []const u8) bool {
256+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
257+ return trimmed_line.len > 0 and std.mem.indexOfScalar(u8, trimmed_line, '|') != null;
258+}
259+
260+fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []const u8, named: usize, widths: []const usize, aligns: []const Align, delimiter: bool) void {
261+ block.append(a, '|') catch {};
262+ for (0..named) |k| {
263+ const cell = if (k < cells.len) cells[k] else "";
264+ const alignment = aligns[k];
265+ block.append(a, ' ') catch {};
266+ if (delimiter) {
267+ block.append(a, if (alignment == .left or alignment == .center) ':' else '-') catch {};
268+ block.appendNTimes(a, '-', widths[k] - 2) catch {};
269+ block.append(a, if (alignment == .right or alignment == .center) ':' else '-') catch {};
270+ } else {
271+ const pad = widths[k] - cellWidth(a, cell);
272+ const before: usize = switch (alignment) {
273+ .right => pad,
274+ .center => pad / 2,
275+ else => 0,
276+ };
277+ block.appendNTimes(a, ' ', before) catch {};
278+ block.appendSlice(a, cell) catch {};
279+ block.appendNTimes(a, ' ', pad - before) catch {};
280+ }
281+ block.appendSlice(a, " |") catch {};
282+ }
283+}
284+
285+fn formatTable(a: std.mem.Allocator, header: []const u8, aligns: []const Align, body: []const []const u8, trailing: bool) ?[]const u8 {
286+ const named = aligns.len;
287+ const rows = a.alloc([]const []const u8, body.len + 1) catch return null;
288+ rows[0] = splitCells(a, header);
289+ if (rows[0].len != named) return null;
290+ for (body, 0..) |line, i| {
291+ const cells = splitCells(a, line);
292+ if (cells.len > named) return null;
293+ rows[i + 1] = cells;
294+ }
295+
296+ const widths = a.alloc(usize, named) catch return null;
297+ @memset(widths, 3);
298+ for (rows) |cells| {
299+ for (cells, 0..) |cell, k| widths[k] = @max(widths[k], cellWidth(a, cell));
300+ }
301+
302+ var block: std.ArrayList(u8) = .empty;
303+ for (rows, 0..) |cells, r| {
304+ if (r > 0) block.append(a, '\n') catch {};
305+ emitRow(a, &block, cells, named, widths, aligns, false);
306+ if (r == 0) {
307+ block.append(a, '\n') catch {};
308+ emitRow(a, &block, &.{}, named, widths, aligns, true);
309+ }
310+ }
311+ if (trailing) block.append(a, '\n') catch {};
312+ return block.items;
313+}
314+
315+pub fn formatTables(a: std.mem.Allocator, text: []const u8) []const u8 {
316+ var lines: std.ArrayList([]const u8) = .empty;
317+ var it = std.mem.splitScalar(u8, text, '\n');
318+ while (it.next()) |line| lines.append(a, line) catch {};
319+
320+ var out: std.ArrayList(u8) = .empty;
321+ var i: usize = 0;
322+ while (i < lines.items.len) {
323+ const line = lines.items[i];
324+ if (i + 1 < lines.items.len and tableCandidate(line)) {
325+ if (delimiterRow(a, lines.items[i + 1])) |aligns| {
326+ var end = i + 2;
327+ while (end < lines.items.len and rowCells(lines.items[end])) end += 1;
328+ const block = formatTable(a, line, aligns, lines.items[i + 2 .. end], end < lines.items.len);
329+ if (block) |b| {
330+ out.appendSlice(a, b) catch {};
331+ i = end;
332+ continue;
333+ }
334+ }
335+ }
336+ out.appendSlice(a, line) catch {};
337+ out.append(a, '\n') catch {};
338+ i += 1;
339+ }
340+ // A single trailing split always leaves one empty entry; drop its newline.
341+ if (out.items.len > 0 and text.len > 0 and text[text.len - 1] != '\n' and out.items[out.items.len - 1] == '\n')
342+ out.items.len -= 1;
343+ return out.items;
344+}