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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/// Data structures describing the parsed document.

#[cfg(feature = "no_position")]
use serde::{Serialize, SerializeMap, Serializer};
use serde_derive::{Deserialize, Serialize};

/**
 * Element types used in the abstract syntax tree (AST).
 *
 * Each element must keep track of its position in the original
 * input document. After parsing, the document tree can be serialized by serde.
 */
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
pub enum Element {
    Document(Document),
    Heading(Heading),
    Text(Text),
    Formatted(Formatted),
    Paragraph(Paragraph),
    Template(Template),
    TemplateArgument(TemplateArgument),
    InternalReference(InternalReference),
    ExternalReference(ExternalReference),
    ListItem(ListItem),
    List(List),
    Table(Table),
    TableRow(TableRow),
    TableCell(TableCell),
    Comment(Comment),
    HtmlTag(HtmlTag),
    Gallery(Gallery),
    Error(Error),
}

/// The document root.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Document {
    #[serde(default)]
    pub position: Span,
    pub content: Vec<Element>,
}

/// Headings make a hierarchical document structure.
/// Headings of higher depths have other headings as parents.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Heading {
    #[serde(default)]
    pub position: Span,
    pub depth: usize,
    pub caption: Vec<Element>,
    pub content: Vec<Element>,
}

/// Simple text.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Text {
    #[serde(default)]
    pub position: Span,
    pub text: String,
}

/// A formatting wrapper, usually around text.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Formatted {
    #[serde(default)]
    pub position: Span,
    pub markup: MarkupType,
    pub content: Vec<Element>,
}

/// Paragraphs are separated by newlines in the input document.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Paragraph {
    #[serde(default)]
    pub position: Span,
    pub content: Vec<Element>,
}

/// A mediawiki template.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Template {
    #[serde(default)]
    pub position: Span,
    pub name: Vec<Element>,
    pub content: Vec<Element>,
}

/// Argument of a mediawiki template.
/// Empty name indicate anonymous arguments.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct TemplateArgument {
    #[serde(default)]
    pub position: Span,
    pub name: String,
    pub value: Vec<Element>,
}

/// A reference to internal data, such as embedded files
/// or other articles.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct InternalReference {
    #[serde(default)]
    pub position: Span,
    pub target: Vec<Element>,
    pub options: Vec<Vec<Element>>,
    pub caption: Vec<Element>,
}

/// External reference, usually hyperlinks.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct ExternalReference {
    #[serde(default)]
    pub position: Span,
    pub target: String,
    pub caption: Vec<Element>,
}

/// List item of a certain `ListItemKind`.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct ListItem {
    #[serde(default)]
    pub position: Span,
    pub depth: usize,
    pub kind: ListItemKind,
    pub content: Vec<Element>,
}

/// List of items. The `ListItemKind` of its children
/// can be heterogenous.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct List {
    #[serde(default)]
    pub position: Span,
    pub content: Vec<Element>,
}

/// A mediawiki table. `attributes` represent html
/// attributes assigned to the table.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Table {
    #[serde(default)]
    pub position: Span,
    pub attributes: Vec<TagAttribute>,
    pub caption: Vec<Element>,
    pub caption_attributes: Vec<TagAttribute>,
    pub rows: Vec<Element>,
}

/// A table row. `attributes` represent html
/// attributes assigned to the table.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct TableRow {
    #[serde(default)]
    pub position: Span,
    pub attributes: Vec<TagAttribute>,
    pub cells: Vec<Element>,
}

/// A single table cell. `attributes` represent html
/// attributes assigned to the table. `header` is true
/// if this cell is marked as a header cell.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct TableCell {
    #[serde(default)]
    pub position: Span,
    pub header: bool,
    pub attributes: Vec<TagAttribute>,
    pub content: Vec<Element>,
}

/// Comments in the input document.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Comment {
    #[serde(default)]
    pub position: Span,
    pub text: String,
}

/// Html tags not encoding formatting elements.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct HtmlTag {
    #[serde(default)]
    pub position: Span,
    pub name: String,
    pub attributes: Vec<TagAttribute>,
    pub content: Vec<Element>,
}

/// Gallery of images (or interal references in general).
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Gallery {
    #[serde(default)]
    pub position: Span,
    pub attributes: Vec<TagAttribute>,
    pub content: Vec<Element>,
}

/// Indicates an erroneous part of the document tree.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct Error {
    #[serde(default)]
    pub position: Span,
    pub message: String,
}

/// Types of markup a section of text may have.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum MarkupType {
    NoWiki,
    Bold,
    Italic,
    Math,
    StrikeThrough,
    Underline,
    Code,
    Blockquote,
    Preformatted,
}

/// Types of markup a section of text may have.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum ListItemKind {
    Unordered,
    Definition,
    DefinitionTerm,
    Ordered,
}

/**
 * Represents a position in the source document.
 *
 * The `PartialEq` implementation allows for a "any" position (all zero), which is
 * equal to any other position. This is used to reduce clutter in tests, where
 * a default Position ("{}") can be used where the actual representation is irrelevant.
 */
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(
    rename_all = "lowercase",
    default = "Position::any_position",
    deny_unknown_fields
)]
pub struct Position {
    pub offset: usize,
    pub line: usize,
    pub col: usize,
}

/// Holds position information (start and end) for one element
#[derive(Debug, Deserialize, PartialEq, Clone)]
#[cfg_attr(not(feature = "no_position"), derive(Serialize))]
#[serde(rename_all = "lowercase", default = "Span::any", deny_unknown_fields)]
pub struct Span {
    pub start: Position,
    pub end: Position,
}

/// Represents a pair of html tag attribute and value.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub struct TagAttribute {
    #[serde(default)]
    pub position: Span,
    pub key: String,
    pub value: String,
}

/// Position of a source line of code.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct SourceLine<'input> {
    pub start: usize,
    pub content: &'input str,
    pub end: usize,
}

impl<'input> SourceLine<'input> {
    /// checks if `pos` is at a line start
    pub fn starts_line(pos: usize, slocs: &[SourceLine]) -> bool {
        for sloc in slocs {
            if sloc.start == pos {
                return true;
            }
        }
        false
    }
}

impl MarkupType {
    /// Match an HTML tag name to it's markup type.
    pub fn by_tag_name(tag: &str) -> MarkupType {
        match &tag.to_lowercase()[..] {
            "math" => MarkupType::Math,
            "del" | "s" => MarkupType::StrikeThrough,
            "nowiki" => MarkupType::NoWiki,
            "u" | "ins" => MarkupType::Underline,
            "code" => MarkupType::Code,
            "blockquote" => MarkupType::Blockquote,
            "pre" => MarkupType::Preformatted,
            _ => panic!("markup type lookup not implemented for {}!", tag),
        }
    }
}

impl Element {
    /// returns the source code position of an element.
    pub fn get_position(&self) -> &Span {
        match *self {
            Element::Document(ref e) => &e.position,
            Element::Heading(ref e) => &e.position,
            Element::Text(ref e) => &e.position,
            Element::Formatted(ref e) => &e.position,
            Element::Paragraph(ref e) => &e.position,
            Element::Template(ref e) => &e.position,
            Element::TemplateArgument(ref e) => &e.position,
            Element::InternalReference(ref e) => &e.position,
            Element::ExternalReference(ref e) => &e.position,
            Element::List(ref e) => &e.position,
            Element::ListItem(ref e) => &e.position,
            Element::Table(ref e) => &e.position,
            Element::TableRow(ref e) => &e.position,
            Element::TableCell(ref e) => &e.position,
            Element::Comment(ref e) => &e.position,
            Element::HtmlTag(ref e) => &e.position,
            Element::Gallery(ref e) => &e.position,
            Element::Error(ref e) => &e.position,
        }
    }

    /// returns a mutable reference the source code position of an element.
    pub fn get_position_mut(&mut self) -> &mut Span {
        match *self {
            Element::Document(ref mut e) => &mut e.position,
            Element::Heading(ref mut e) => &mut e.position,
            Element::Text(ref mut e) => &mut e.position,
            Element::Formatted(ref mut e) => &mut e.position,
            Element::Paragraph(ref mut e) => &mut e.position,
            Element::Template(ref mut e) => &mut e.position,
            Element::TemplateArgument(ref mut e) => &mut e.position,
            Element::InternalReference(ref mut e) => &mut e.position,
            Element::ExternalReference(ref mut e) => &mut e.position,
            Element::List(ref mut e) => &mut e.position,
            Element::ListItem(ref mut e) => &mut e.position,
            Element::Table(ref mut e) => &mut e.position,
            Element::TableRow(ref mut e) => &mut e.position,
            Element::TableCell(ref mut e) => &mut e.position,
            Element::Comment(ref mut e) => &mut e.position,
            Element::HtmlTag(ref mut e) => &mut e.position,
            Element::Gallery(ref mut e) => &mut e.position,
            Element::Error(ref mut e) => &mut e.position,
        }
    }

    /// returns the variant name of an element.
    pub fn get_variant_name(&self) -> &str {
        match *self {
            Element::Document(_) => "Document",
            Element::Heading(_) => "Heading",
            Element::Text(_) => "Text",
            Element::Formatted(_) => "Formatted",
            Element::Paragraph(_) => "Paragraph",
            Element::Template(_) => "Template",
            Element::TemplateArgument(_) => "TemplateArgument",
            Element::InternalReference(_) => "InternalReference",
            Element::ExternalReference(_) => "ExternalReference",
            Element::List(_) => "List",
            Element::ListItem(_) => "ListItem",
            Element::Table(_) => "Table",
            Element::TableRow(_) => "TableRow",
            Element::TableCell(_) => "TableCell",
            Element::Comment(_) => "Comment",
            Element::HtmlTag(_) => "HtmlTag",
            Element::Gallery(_) => "Gallery",
            Element::Error(_) => "Error",
        }
    }
}

impl Position {
    pub fn new(offset: usize, slocs: &[SourceLine]) -> Self {
        for (i, sloc) in slocs.iter().enumerate() {
            if offset >= sloc.start && offset < sloc.end {
                return Position {
                    offset,
                    line: i + 1,
                    col: sloc.content[0..offset - sloc.start].chars().count() + 1,
                };
            }
        }
        Position {
            offset,
            line: slocs.len() + 1,
            col: 0,
        }
    }

    pub fn any_position() -> Self {
        Position {
            offset: 0,
            line: 0,
            col: 0,
        }
    }
}

impl Span {
    pub fn any() -> Self {
        Span {
            start: Position::any_position(),
            end: Position::any_position(),
        }
    }

    pub fn new(posl: usize, posr: usize, source_lines: &[SourceLine]) -> Self {
        Span {
            start: Position::new(posl, source_lines),
            end: Position::new(posr, source_lines),
        }
    }
}

impl Default for Span {
    fn default() -> Self {
        Self::any()
    }
}

#[cfg(feature = "no_position")]
impl Serialize for Span {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let map = serializer.serialize_map(None)?;
        map.end()
    }
}

impl PartialEq for Position {
    fn eq(&self, other: &Position) -> bool {
        // comparing with "any" position is always true
        if (other.offset == 0 && other.line == 0 && other.col == 0)
            || (self.offset == 0 && self.line == 0 && self.col == 0)
        {
            return true;
        }

        self.offset == other.offset && self.line == other.line && self.col == other.col
    }
}

impl TagAttribute {
    pub fn new(position: Span, key: String, value: String) -> Self {
        TagAttribute {
            position,
            key,
            value,
        }
    }
}