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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! The document tree
//!
//! Parsing creates it and layout reads it. Positions and dimensions are not stored here.
//! Only the numbers are written in afterwards, by [`crate::number`].
/// One whole document
#[derive(Debug, Clone, PartialEq)]
pub struct Document {
/// The name written in `\documentclass`
pub class: String,
/// The options written in `\documentclass`
pub option: Vec<String>,
pub title: Vec<Node>,
pub author: Vec<Node>,
pub date: Vec<Node>,
pub body: Vec<Node>,
}
/// The font style of characters
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Face {
Roman,
Italic,
Bold,
BoldItalic,
Mono,
Sans,
}
/// Alignment
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
Left,
Center,
Right,
/// Aligns both the left and right edges
Justify,
}
/// The kind of list
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListKind {
/// Puts a mark in front of each item
Bullet,
/// Numbers the items
Number,
/// Places a heading and a description side by side
Description,
}
/// The kind of float
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FloatKind {
Figure,
Table,
}
/// How a length is written
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length {
/// An absolute length (pt)
Absolute(f64),
/// A ratio to the width of the body text
Relative(f64),
/// A multiple of the character size of the body text
Em(f64),
}
/// One row of math
#[derive(Debug, Clone, PartialEq)]
pub struct MathRow {
/// The formula split at the alignment points. If there is only one part, it is not aligned
pub part: Vec<String>,
/// The name used to refer to this row
pub label: Option<String>,
/// Whether this row gets a number
pub numbered: bool,
/// The assigned number. Written by [`crate::number`]
pub number: String,
}
/// One item of a list
#[derive(Debug, Clone, PartialEq)]
pub struct Item {
/// The mark written in `\item[…]`. If absent, the default mark for the kind of list is used
pub mark: Option<Vec<Node>>,
pub body: Vec<Node>,
}
/// A column of a table
#[derive(Debug, Clone, PartialEq)]
pub struct Column {
pub align: Align,
/// The width fixed with `p{…}`
pub width: Option<Length>,
}
/// A cell of a table
#[derive(Debug, Clone, PartialEq)]
pub struct Cell {
pub body: Vec<Node>,
/// The number of columns the cell spans
pub span: usize,
/// The alignment used when the cell spans columns
pub align: Option<Align>,
}
/// A row of a table
#[derive(Debug, Clone, PartialEq)]
pub struct Row {
pub cell: Vec<Cell>,
/// Whether to draw a horizontal rule above this row
pub line_above: bool,
}
/// A table
#[derive(Debug, Clone, PartialEq)]
pub struct Table {
pub column: Vec<Column>,
/// Vertical rules between columns. Its length is the number of columns + 1
pub rule: Vec<bool>,
pub row: Vec<Row>,
/// Whether to draw a horizontal rule at the very bottom
pub line_below: bool,
}
/// One item in the references
#[derive(Debug, Clone, PartialEq)]
pub struct Reference {
pub key: String,
/// The mark written in `\bibitem[…]`. If absent, the number is used
pub mark: Option<String>,
pub body: Vec<Node>,
/// The assigned number. Written by [`crate::number`]
pub number: String,
}
/// An element that makes up a document
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
/// Plain text
Text(String),
/// A break between words
Space,
/// A non-breaking space
HardSpace,
/// A space of fixed width
Gap(Length),
/// A space that fills the rest of the line
Fill,
/// Breaks the line at this point
Newline,
/// A paragraph break
Break,
/// A group with a different font style
Face {face: Face, body: Vec<Node>},
/// A group with a different size. scale is the factor relative to the body text
Size {scale: f64, body: Vec<Node>},
/// Underline
Underline(Vec<Node>),
/// A group that is not broken in the middle. If frame is true, a line is drawn around it
Boxed {body: Vec<Node>, frame: bool},
/// A group typeset at a fixed width and placed inline. `minipage`
Fixed {width: Length, body: Vec<Node>},
/// Ruby. Places text in a small size above base
Ruby {base: Vec<Node>, text: Vec<Node>},
/// A filled rectangle of fixed size. `\rule`
Bar {width: Length, height: Length, lift: Length},
/// Places only the footnote mark
FootnoteMark,
/// Places only the contents of a footnote
FootnoteText(Vec<Node>),
/// Math
Math {row: Vec<MathRow>, display: bool},
/// A chemical structure
Chem {source: String, display: bool},
/// A string that is output unchanged
Verbatim {text: String, block: bool},
/// A heading
Heading {
/// 0 is part, 1 is chapter, 2 is section, 3 is subsection, 4 is subsubsection
level: usize,
numbered: bool,
title: Vec<Node>,
/// The assigned number. Written by [`crate::number`]
number: String,
},
/// Links a name used for references to the number just before it
Label(String),
/// Refers to a number or a page
Cross {name: String, page: bool},
/// Cites references
Citation(Vec<String>),
/// A footnote
Footnote(Vec<Node>),
/// A list
List {kind: ListKind, item: Vec<Item>},
/// A quotation
Quote(Vec<Node>),
/// A group with a different alignment. spaced says whether to add space before and after it
Aligned {align: Align, body: Vec<Node>, spaced: bool},
/// A table
Table(Table),
/// A float
Float {kind: FloatKind, placement: String, body: Vec<Node>},
/// A caption. The number is written by [`crate::number`]
Caption {body: Vec<Node>, number: String, kind: FloatKind},
/// An image
Image {path: String, width: Option<Length>, height: Option<Length>, scale: Option<f64>},
/// Places the table of contents
Contents,
/// Places the title block
Title,
/// The list of references
Bibliography(Vec<Reference>),
/// Vertical space
Skip(Length),
/// Does not indent the start of the paragraph
NoIndent,
/// Numbers the headings from here on as A, B, C
Appendix,
/// A page break
NewPage,
}
/// Walks the tree and works on each element
///
/// Used when assigning numbers and when resolving references. For elements that have children,
/// it goes down into the children too
pub fn walk(body: &mut Vec<Node>, visit: &mut dyn FnMut(&mut Node)) {
for node in body.iter_mut() {
visit(node);
walk_children(node, visit);
}
}
fn walk_children(node: &mut Node, visit: &mut dyn FnMut(&mut Node)) {
match node {
Node::Face {body, ..}
| Node::Size {body, ..}
| Node::Underline(body)
| Node::Boxed {body, ..}
| Node::Fixed {body, ..}
| Node::Quote(body)
| Node::Aligned {body, ..}
| Node::Float {body, ..}
| Node::FootnoteText(body)
| Node::Footnote(body) => walk(body, visit),
Node::Heading {title, ..} => walk(title, visit),
Node::Ruby {base, text} => {
walk(base, visit);
walk(text, visit);
}
Node::Caption {body, ..} => walk(body, visit),
Node::List {item, ..} => {
for one in item.iter_mut() {
if let Some(mark) = one.mark.as_mut() {
walk(mark, visit);
}
walk(&mut one.body, visit);
}
}
Node::Table(table) => {
for row in table.row.iter_mut() {
for cell in row.cell.iter_mut() {
walk(&mut cell.body, visit);
}
}
}
Node::Bibliography(entry) => {
for one in entry.iter_mut() {
walk(&mut one.body, visit);
}
}
_ => {}
}
}
/// Takes only the plain text out of a tree. Used for the table of contents and bookmarks
pub fn plain(body: &[Node]) -> String {
let mut out = String::new();
for node in body {
match node {
Node::Text(text) => out.push_str(text),
Node::Space | Node::HardSpace | Node::Gap(_) => out.push(' '),
Node::Verbatim {text, ..} => out.push_str(text),
Node::Face {body, ..}
| Node::Size {body, ..}
| Node::Underline(body)
| Node::Boxed {body, ..}
| Node::Fixed {body, ..} => out.push_str(&plain(body)),
// Ruby gives the reading, so only the base text goes into the table of contents
// and bookmarks
Node::Ruby {base, ..} => out.push_str(&plain(base)),
Node::Math {row, ..} => {
for one in row {
out.push_str(&one.part.join(" "));
}
}
_ => {}
}
}
out
}