1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Table parsing and PDF rendering.
//!
//! Handles `tabular` environments, including cell alignment and horizontal rules.
use serde::Serialize;
/// Horizontal alignment for a table column.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum Align {
Left,
Center,
Right,
}
/// A single row in a table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Row {
pub cells: Vec<String>,
pub is_separator: bool,
}
/// A parsed `tabular` environment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Table {
pub columns: Vec<Align>,
pub rows: Vec<Row>,
}
impl Table {
/// Parse the raw content of a `tabular` environment into a structured table.
///
/// `column_spec` is the string inside the braces after `\begin{tabular}`
/// (e.g. `"|c|c|c|"` or `"l r"`).
pub fn parse(column_spec: &str, content: &str) -> Self {
let columns = parse_column_spec(column_spec);
let mut rows = Vec::new();
for line in content.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if line.starts_with("\\toprule")
|| line.starts_with("\\midrule")
|| line.starts_with("\\bottomrule")
|| line.starts_with("\\hline")
{
rows.push(Row {
cells: vec![],
is_separator: true,
});
continue;
}
if line.starts_with("\\") && !line.contains('&') {
// Skip other commands (like \caption, \label inside table env)
continue;
}
if line.contains('&') {
let cells: Vec<String> = line
.split('&')
.map(|s| {
s.trim()
.trim_end_matches("\\\\")
.trim()
.replace("\\$", "$")
.to_string()
})
.collect();
rows.push(Row {
cells,
is_separator: false,
});
}
}
Table { columns, rows }
}
}
/// Parse a LaTeX column specification string into alignment values.
///
/// Ignores `|`, `@{}`, and `p{width}` — treats `p` as left-aligned.
fn parse_column_spec(spec: &str) -> Vec<Align> {
let mut result = Vec::new();
let mut chars = spec
.trim()
.trim_start_matches('{')
.trim_end_matches('}')
.chars();
while let Some(ch) = chars.next() {
match ch {
'l' => result.push(Align::Left),
'c' => result.push(Align::Center),
'r' => result.push(Align::Right),
'p' => {
// p{width} — consume until matching }
let mut depth = 0;
for c in chars.by_ref() {
if c == '{' {
depth += 1;
} else if c == '}' {
depth -= 1;
if depth == 0 {
break;
}
}
}
result.push(Align::Left);
}
'|' | ' ' | '\t' => {} // ignore
'@' => {
// @{} column spacing — skip until }
for c in chars.by_ref() {
if c == '}' {
break;
}
}
}
_ => {}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_column_spec() {
assert_eq!(
parse_column_spec("|l|c|r|"),
vec![Align::Left, Align::Center, Align::Right]
);
assert_eq!(
parse_column_spec("{l c r}"),
vec![Align::Left, Align::Center, Align::Right]
);
assert_eq!(parse_column_spec("cc"), vec![Align::Center, Align::Center]);
assert_eq!(
parse_column_spec("p{3cm}r"),
vec![Align::Left, Align::Right]
);
}
#[test]
fn test_table_parse_basic() {
let spec = "lcr";
let content = "A & B & C \\\\\n\\hline\n1 & 2 & 3 \\\\";
let table = Table::parse(spec, content);
assert_eq!(
table.columns,
vec![Align::Left, Align::Center, Align::Right]
);
assert_eq!(table.rows.len(), 3);
assert!(table.rows[1].is_separator);
assert_eq!(table.rows[0].cells, vec!["A", "B", "C"]);
assert_eq!(table.rows[2].cells, vec!["1", "2", "3"]);
}
#[test]
fn test_table_parse_with_dollar_escape() {
let spec = "cc";
let content = "$x$ & $y$ \\\\";
let table = Table::parse(spec, content);
assert_eq!(table.rows[0].cells, vec!["$x$", "$y$"]);
}
}