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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Copyright 2018 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

//! Parse dictionary pages from the German language edition of Wiktionary into structured data.
//!
//! For general information about Parse Wiktionary, see the readme file.
//!
//! # Examples
//!
//! This example prints all usage examples found in an article, together with the language and the first part of speech of the entry.
//!
//! ```
//! # extern crate parse_wiki_text;
//! # extern crate parse_wiktionary_de;
//! #
//! let title = "Ausstellungswohnen";
//! let wiki_text = concat!(
//!     "==Ausstellungswohnen ({{Sprache|Deutsch}})==\n",
//!     "==={{Wortart|Substantiv|Deutsch}}===\n",
//!     "{{Beispiele}}\n",
//!     r#":Das Haus sei geeignet bloß zum „Ausstellungswohnen", vermuteten Kritiker, "#,
//!     "die es meist nur in schwarz-weißen Zeitschriftenfotos betreten hatten."
//! );
//! let configuration = parse_wiktionary_de::create_configuration();
//! let parsed_wiki_text = configuration.parse(wiki_text);
//! let parsed_article = parse_wiktionary_de::parse(title, wiki_text, &parsed_wiki_text.nodes);
//! # let mut found = false;
//! for language_entry in parsed_article.language_entries {
//!     for pos_entry in language_entry.pos_entries {
//!         for example in pos_entry.examples {
//!             println!(
//!                 "The word '{title}' of language {language:?} and part of speech {pos:?} has the example: {example}",
//!                 title = title,
//!                 language = language_entry.language,
//!                 pos = pos_entry.pos,
//!                 example = &example.example.iter().map(|node| match node {
//!                     parse_wiktionary_de::Flowing::Text { value } => value,
//!                     _ => ""
//!                 }).collect::<String>()
//!             );
//! #           found = true;
//!         }
//!     }
//! }
//! # assert!(found);
//! ```
//!
//! # Limitations
//!
//! Parameters of overview templates are transferred to the output with minimal validation and processing. Due to the wide variety of overview templates that take parameters in highly complicated and inconsistent formats, fully validating and parsing these parameters is not feasible.
//!
//! The translations in the template [`Vorlage:Ü-Tabelle`](Vorlage:Ü-Tabelle) in the section [`Übersetzungen`](https://de.wiktionary.org/wiki/Vorlage:%C3%9Cbersetzungen) are not parsed. Due to the highly complicated format of translations, it's better not to even try parsing them than try and get an inconsistent result. Due to the common presence of translation tables that contain empty translations, it's not even indicated whether an entry has translations.
//!
//! The templates [`Ähnlichkeiten 1`](https://de.wiktionary.org/wiki/Vorlage:%C3%84hnlichkeiten_1) and [`Ähnlichkeiten 2`](https://de.wiktionary.org/wiki/Vorlage:%C3%84hnlichkeiten_2) are not parsed, because it's unclear what purpose they have and what format their parameters must have.

// XXX Consider going through all templates in https://de.wiktionary.org/wiki/Kategorie:Wiktionary:Markierung.

#![forbid(unsafe_code)]
#![warn(missing_docs)]

extern crate parse_wiki_text;
extern crate serde;
#[macro_use]
extern crate serde_derive;

mod configuration;
mod examples;
mod language;
mod languages;
mod list;
mod overview;
mod pos_section;
mod pos_template;
mod pronunciation;
mod util;

pub use configuration::create_configuration;
pub use languages::Language;
use parse_wiki_text::{DefinitionListItem, DefinitionListItemType::Details, Node, Parameter};
use std::{borrow::Cow, collections::HashMap};
use util::*;

/// Usage example.
#[derive(Debug, Deserialize, Serialize)]
pub struct Example<'a> {
    /// The example in the language of the entry.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub example: Vec<Flowing<'a>>,

    /// The German translation of the example.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub translation: Vec<Flowing<'a>>,
}

/// An element in a sequence that allows different kinds of elements.
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum Flowing<'a> {
    /// Audio sample.
    ///
    /// Parsed from the template [`Audio`](https://de.wiktionary.org/wiki/Vorlage:Audio).
    Audio {
        /// The file name referred to.
        file_name: Cow<'a, str>,

        /// The label to display for the audio sample.
        #[serde(skip_serializing_if = "Option::is_none")]
        label: Option<Cow<'a, str>>,

        /// The language of the audio.
        #[serde(skip_serializing_if = "Option::is_none")]
        language: Option<Cow<'a, str>>,
    },

    /// Toggle bold text.
    ///
    /// Parsed from the wiki text `'''`.
    Bold,

    /// Comment tag.
    ///
    /// Parsed code starting with `<!--`.
    Comment,

    /// Indication that something is of common gender.
    ///
    /// Parsed from the template [`u`](https://de.wiktionary.org/wiki/Vorlage:u).
    CommonGender,

    /// Indication that something is a comparative.
    ///
    /// Parsed from the template [`Komp.`](https://de.wiktionary.org/wiki/Vorlage:Komp.).
    Comparative,

    /// Placeholder for an audio sample that has not been filled in.
    ///
    /// Parsed from the template [`Audio`](https://de.wiktionary.org/wiki/Vorlage:Audio).
    EmptyAudio,

    /// Indication that something is of feminine gender.
    ///
    /// Parsed from the template [`u`](https://de.wiktionary.org/wiki/Vorlage:f).
    FeminineGender,

    /// Indication that something is a genitive.
    ///
    /// Parsed from the template [`Gen.`](https://de.wiktionary.org/wiki/Vorlage:Gen.).
    Genitive,

    /// Pronunciation written in IPA.
    ///
    /// Parsed from the template [`Lautschrift`](https://de.wiktionary.org/wiki/Vorlage:Lautschrift).
    Ipa {
        /// The pronunciation written in IPA.
        ipa: Cow<'a, str>,
    },

    /// Toggle italic text.
    ///
    /// Parsed from the wiki text `''`.
    Italic,

    /// Language as a noun.
    ///
    /// Parsed from the templates found in the category [`Sprachkürzel`](https://de.wiktionary.org/wiki/Kategorie:Wiktionary:Sprachk%C3%BCrzel).
    Language {
        /// The language referred to.
        language: Cow<'a, str>,
    },

    /// Language as an adjective.
    ///
    /// Parsed from the templates found in the category [`Sprachadjektive`](https://de.wiktionary.org/wiki/Kategorie:Wiktionary:Sprachadjektive).
    LanguageAdjective {
        /// The language referred to.
        language: Cow<'a, str>,
    },

    /// Link.
    ///
    /// Parsed from wiki text starting with `[[`.
    Link {
        /// The target the link refers to.
        target: Cow<'a, str>,

        /// The text to display for the link.
        text: Cow<'a, str>,
    },

    /// Unordered list.
    List {
        /// The items of the list.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        items: Vec<Vec<Flowing<'a>>>,
    },

    /// Indication that something is of masculine gender.
    ///
    /// Parsed from the template [`m`](https://de.wiktionary.org/wiki/Vorlage:m).
    MasculineGender,

    /// Indication that something is of neuter gender.
    ///
    /// Parsed from the template [`n`](https://de.wiktionary.org/wiki/Vorlage:n).
    NeuterGender,

    /// Indication that something has no plural.
    ///
    /// Parsed from the template [`kPl.`](https://de.wiktionary.org/wiki/Vorlage:kPl.).
    NoPlural,

    /// Indication that something is a past participle.
    ///
    /// Parsed from the template [`Part.`](https://de.wiktionary.org/wiki/Vorlage:Part.).
    PastParticiple,

    /// Indication that something is a plural.
    ///
    /// Parsed from the template [`Pl.`](https://de.wiktionary.org/wiki/Vorlage:Pl.).
    Plural,

    /// Indication that something is a plural.
    ///
    /// Parsed from the template [`Pl.1`](https://de.wiktionary.org/wiki/Vorlage:Pl.1).
    Plural1,

    /// Indication that something is a plural.
    ///
    /// Parsed from the template [`Pl.2`](https://de.wiktionary.org/wiki/Vorlage:Pl.2).
    Plural2,

    /// Indication that something is a plural.
    ///
    /// Parsed from the template [`Pl.3`](https://de.wiktionary.org/wiki/Vorlage:Pl.3).
    Plural3,

    /// Indication that something is a plural.
    ///
    /// Parsed from the template [`Pl.4`](https://de.wiktionary.org/wiki/Vorlage:Pl.4).
    Plural4,

    /// Part of speech.
    ///
    /// Parsed from the template [`Wortbildung`](https://de.wiktionary.org/wiki/Vorlage:Wortbildung).
    Pos {
        /// The part of speech.
        pos: Pos,
    },

    /// Indication that something is a preterite.
    ///
    /// Parsed from the template [`Prät.`](https://de.wiktionary.org/wiki/Vorlage:Pr%C3%A4t.).
    Preterite,

    /// Quality control marker.
    ///
    /// Parsed from the template [`QS Herkunft`](https://de.wiktionary.org/wiki/Vorlage:QS_Herkunft).
    QualityControl,

    /// Indication of a reference.
    ///
    /// Parsed from the extension tag `ref`. The content if the reference is not parsed. This element is added to the output just to indicate the existence of a reference.
    Reference,

    /// Rhyme.
    ///
    /// Parsed from the template [Reim](https://de.wiktionary.org/wiki/Vorlage:Reim).
    Rhyme {
        /// The rhyme.
        rhyme: Cow<'a, str>,
    },

    /// Indication that something is a superlative.
    ///
    /// Parsed from the template [`Sup.`](https://de.wiktionary.org/wiki/Vorlage:Sup.).
    Superlative,

    /// End of superscript.
    ///
    /// Parsed from the tag `</sup>`.
    SuperscriptEnd,

    /// Start of superscript.
    ///
    /// Parsed from the tag `<sup>`.
    SuperscriptStart,

    /// Link to a dictionary entry.
    ///
    /// Parsed from the template [`Ü`](https://de.wiktionary.org/wiki/Vorlage:%C3%9C).
    Term {
        /// The language the link refers to.
        language: Cow<'a, str>,

        /// The term the link refers to.
        term: Cow<'a, str>,

        /// Transliteration of the term.
        #[serde(skip_serializing_if = "Option::is_none")]
        transliteration: Option<Cow<'a, str>>,
    },

    /// Chunk of plain text.
    Text {
        /// The text to display.
        value: Cow<'a, str>,
    },

    /// Element that could not be recognized.
    Unknown {
        /// The wiki text of the element.
        value: Cow<'a, str>,
    },
}

/// Dictionary entry for a single language.
#[derive(Debug, Deserialize, Serialize)]
pub struct LanguageEntry<'a> {
    /// The language of the entry.
    pub language: Language,

    /// Entries for parts of speech for this language.
    ///
    /// Parsed from the sections with the template [`Wortart`](https://de.wiktionary.org/wiki/Vorlage:Wortart) in their heading.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub pos_entries: Vec<PosEntry<'a>>,
}

/// Output of parsing a page.
#[derive(Debug, Deserialize, Serialize)]
pub struct Output<'a> {
    /// The dictionary entries by language.
    ///
    /// Parsed from the section with the template [`Sprache`](https://de.wiktionary.org/wiki/Vorlage:Sprache) in its heading.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub language_entries: Vec<LanguageEntry<'a>>,

    /// Warnings from the parser telling that something is not well-formed.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<Warning>,
}

/// Information from the overview template in the POS entry.
///
/// There are many different overview templates for different languages and different patterns of inflection. These are constructed in a way that makes it difficult to parse their meaning. Therefore any parameters are accepted and included in the output.
#[derive(Debug, Deserialize, Serialize)]
pub struct Overview<'a> {
    /// The name of the overview template.
    pub name: Cow<'a, str>,

    /// The named parameters to the template by name.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub named_parameters: HashMap<Cow<'a, str>, Cow<'a, str>>,

    /// The unnamed parameters to the template in order.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unnamed_parameters: Vec<Vec<::Flowing<'a>>>,
}

/// Part of speech.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Pos {
    /// Abbreviation (“Abkürzung”)
    Abbreviation,

    /// Adjective (“Adjektiv”)
    Adjective,

    /// Adverb (“Adverb”)
    Adverb,

    /// Compound word (“Wortverbindung”)
    CompoundWord,

    /// Conjugated form (“Konjugierte Form”)
    ConjugatedForm,

    /// Conjunction (“Konjunktion”)
    Conjunction,

    /// Declined form (“Deklinierte Form”)
    DeclinedForm,

    /// First name (“Vorname”)
    FirstName,

    /// Idiom (“Redewendung”)
    Idiom,

    /// Interjection (“Interjektion”)
    Interjection,

    /// Last name (“Nachname”)
    LastName,

    /// Local adverb (“Lokaladverb”)
    LocalAdverb,

    /// Noun (“Substantiv”)
    Noun,

    /// Numeral (“Numerale”)
    Numeral,

    /// Past participle (“Partizip II”)
    PastParticiple,

    /// Postposition (“Postposition”)
    Postposition,

    /// Preposition (“Präposition”)
    Preposition,

    /// Proper noun (“Eigenname”)
    ProperNoun,

    /// Proverb (“Sprichwort”)
    Proverb,

    /// Symbol (“Symbol”)
    Symbol,

    /// Toponym (“Toponym”)
    Toponym,

    /// Verb (“Verb”)
    Verb,
}

/// The entry for a part of speech within the entry for a language.
#[derive(Debug, Deserialize, Serialize)]
pub struct PosEntry<'a> {
    /// Abbreviations, from the section [`Abkürzungen`](https://de.wiktionary.org/wiki/Vorlage:Abk%C3%BCrzungen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub abbreviations: Vec<Vec<Flowing<'a>>>,

    /// Affectionate forms, from the section [`Koseformen`](https://de.wiktionary.org/wiki/Vorlage:Koseformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub affectionate_forms: Vec<Vec<Flowing<'a>>>,

    /// Antonyms, from the section [`Gegenwörter`](https://de.wiktionary.org/wiki/Vorlage:Gegenw%C3%B6rter).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub antonyms: Vec<Vec<Flowing<'a>>>,

    /// Audio, from the subsection [`Hörbeispiele`](https://de.wiktionary.org/wiki/Vorlage:H%C3%B6rbeispiele) in the section [`Aussprache`](https://de.wiktionary.org/wiki/Vorlage:Aussprache).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub audio: Vec<Flowing<'a>>,

    /// Compound words, from the section [`Wortbildungen`](https://de.wiktionary.org/wiki/Vorlage:Wortbildungen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub compound_words: Vec<Vec<Flowing<'a>>>,

    /// Definition, from the section [`Bedeutungen`](https://de.wiktionary.org/wiki/Vorlage:Bedeutungen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub definitions: Vec<Vec<Flowing<'a>>>,

    /// Various details from the POS heading.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub details: Vec<Flowing<'a>>,

    /// Diminutives, from the section [`Verkleinerungsformen`](https://de.wiktionary.org/wiki/Vorlage:Verkleinerungsformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diminutives: Vec<Vec<Flowing<'a>>>,

    /// Etymology, from the section [`Herkunft`](https://de.wiktionary.org/wiki/Vorlage:Herkunft).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub etymology: Vec<Vec<Flowing<'a>>>,

    /// Examples, from the section [`Beispiele`](https://de.wiktionary.org/wiki/Vorlage:Beispiele).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub examples: Vec<Example<'a>>,

    /// Feminine forms, from the section [`Weibliche Wortformen`](https://de.wiktionary.org/wiki/Vorlage:Weibliche_Wortformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub feminine_forms: Vec<Vec<Flowing<'a>>>,

    /// Hypernyms, from the section [`Oberbegriffe`](https://de.wiktionary.org/wiki/Vorlage:Oberbegriffe).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hypernyms: Vec<Vec<Flowing<'a>>>,

    /// Hyphenation, from the section [`Worttrennung`](https://de.wiktionary.org/wiki/Vorlage:Worttrennung).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hyphenation: Vec<Vec<Flowing<'a>>>,

    /// Hyponyms, from the section [`Unterbegriffe`](https://de.wiktionary.org/wiki/Vorlage:Unterbegriffe).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hyponyms: Vec<Vec<Flowing<'a>>>,

    /// Idioms, from the section [`Redewendungen`](https://de.wiktionary.org/wiki/Vorlage:Redewendungen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub idioms: Vec<Vec<Flowing<'a>>>,

    /// IPA, from the subsection [`IPA`](https://de.wiktionary.org/wiki/Vorlage:IPA) in the section [`Aussprache`](https://de.wiktionary.org/wiki/Vorlage:Aussprache).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub ipa: Vec<Flowing<'a>>,

    /// Masculine forms, from the section [`Männliche Wortformen`](https://de.wiktionary.org/wiki/Vorlage:M%C3%A4nnliche_Wortformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub masculine_forms: Vec<Vec<Flowing<'a>>>,

    /// No longer valid spellings, from the section [`Nicht mehr gültige Schreibweisen`](https://de.wiktionary.org/wiki/Vorlage:Nicht_mehr_g%C3%BCltige_Schreibweisen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub no_longer_valid_spellings: Vec<Vec<Flowing<'a>>>,

    /// Various information about the entry, from any of the many overview templates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub overview: Option<Overview<'a>>,

    /// The first part of speech of the entry, from the heading of the POS entry.
    pub pos: Pos,

    /// Proverbs, fro mthe section [`Sprichwörter`](https://de.wiktionary.org/wiki/Vorlage:Sprichw%C3%B6rter).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub proverbs: Vec<Vec<Flowing<'a>>>,

    /// Related words, from the section [`Sinnverwandte Wörter`](https://de.wiktionary.org/wiki/Vorlage:Sinnverwandte_W%C3%B6rter).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub related_words: Vec<Vec<Flowing<'a>>>,

    /// Rhymes, from the subsection [`Reime`](https://de.wiktionary.org/wiki/Vorlage:Reime) in the section [`Aussprache`](https://de.wiktionary.org/wiki/Vorlage:Aussprache).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rhymes: Vec<Flowing<'a>>,

    /// Short forms, from the section [`Kurzformen`](https://de.wiktionary.org/wiki/Vorlage:Kurzformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub short_forms: Vec<Vec<Flowing<'a>>>,

    /// Similar words, from the section [`Ähnlichkeiten`](https://de.wiktionary.org/wiki/Vorlage:%C3%84hnlichkeiten).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub similar_words: Vec<Vec<Flowing<'a>>>,

    /// Symbols, from the section [`Symbole`](https://de.wiktionary.org/wiki/Vorlage:Symbole).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub symbols: Vec<Vec<Flowing<'a>>>,

    /// Synonyms, from the section [`Synonyme`](https://de.wiktionary.org/wiki/Vorlage:Synonyme).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub synonyms: Vec<Vec<Flowing<'a>>>,

    /// Typical word combinations, from the section [`Charakteristische Wortkombinationen`](https://de.wiktionary.org/wiki/Vorlage:Charakteristische_Wortkombinationen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub typical_word_combinations: Vec<Vec<Flowing<'a>>>,

    /// Variants, from the section [`Nebenformen`](https://de.wiktionary.org/wiki/Vorlage:Nebenformen).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub variants: Vec<Vec<Flowing<'a>>>,
}

/// Warning from the parser telling that something is not well-formed.
///
/// When a warning occurs, it's not guaranteed that the text near the warning is parsed correctly. Usually the data that could not be unambiguously parsed due to the warning is excluded from the output, to make sure the output doesn't contain incorrectly parsed data.
#[derive(Debug, Deserialize, Serialize)]
pub struct Warning {
    /// The byte position in the wiki text where the warning ends.
    pub end: usize,

    /// The language of the language section in which the warning occurred, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<Language>,

    /// An identifier for the kind of warning.
    pub message: WarningMessage,

    /// The byte position in the wiki text where the warning starts.
    pub start: usize,
}

/// Identifier for a kind of warning from the parser.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum WarningMessage {
    /// The element is a duplicate of something that comes before it.
    ///
    /// This may be a heading that contains the same text as a previous heading in the same section, or a parameter that has the same name as a previous parameter to the same template.
    Duplicate,

    /// The element is missing some required content.
    Empty,

    /// The section following the heading is missing some required content.
    SectionEmpty,

    /// The element is recognized but not represented in the output.
    ///
    /// The element conveys meaningful information, but this information has not been parsed and is not represented in the output. In contrast to other warnings, this warning does not indicate there is anything wrong with the wiki text. It just indicates that the wiki text contains additional information that is not represented in the output. The element is recognized as valid in the position it occurs, but its content is not parsed, and nothing can be said about whether the content is valid.
    ///
    /// This applies for example to the section [`Referenzen`](https://de.wiktionary.org/wiki/Vorlage:Referenzen), the templates [`Ü-Tabelle`](https://de.wiktionary.org/wiki/Vorlage:%C3%9C-Tabelle) and [`erweitern`](https://de.wiktionary.org/wiki/Vorlage:erweitern) and the extension tag `ref`.
    Supplementary,

    /// The element is not recognized.
    ///
    /// This may be because of the type of the element itself or because of anything inside it.
    Unrecognized,

    /// The value of the element conflicts with information occurring before it.
    ///
    /// This can mean for example that a specified language within a section doesn't match the language specified for the section as a whole.
    ValueConflicting,

    /// The element is recognized, but its value is not.
    ///
    /// On lists it means that a list with this kind is valid in this position, but something about the list items contained in the list is not recognized.
    ///
    /// On templates it means that a template with this name is valid in this position, but something about the parameters of the template is not recognized.
    ///
    /// On template parameters it means that a parameter with this name (or lack of name) is valid in this position, but something about the value of the parameter is not recognized.
    ValueUnrecognized,
}

/// Parses an article from the German language version of Wiktionary into structured data.
///
/// `title` is the title of the article. `wiki_text` is the wiki text of the article. `nodes` is the sequence of nodes obtained by parsing the wiki text with the crate [Parse Wiki Text](https://github.com/portstrom/parse_wiki_text).
#[must_use]
pub fn parse<'a>(title: &str, wiki_text: &'a str, nodes: &[Node<'a>]) -> Output<'a> {
    let mut context = Context {
        language: None,
        warnings: vec![],
        wiki_text,
    };
    let mut language_entries = vec![];
    let mut node_index = 0;
    while let Some(node) = nodes.get(node_index) {
        if let Node::Heading {
            level,
            nodes: heading_child_nodes,
            ..
        } = node
        {
            if *level < 2 {
                add_warning(&mut context, node, WarningMessage::Unrecognized);
                break;
            }
            if *level == 2 {
                node_index += 1;
                if let [Node::Text { value, .. }, Node::Template {
                    name, parameters, ..
                }, Node::Text { value: ")", .. }] = heading_child_nodes.as_slice()
                {
                    if value.len() == title.len() + 2
                        && value.starts_with(title)
                        && value.ends_with(" (")
                        && text_equals(name, "Sprache")
                    {
                        if let [parameter @ Parameter { name: None, .. }] = parameters.as_slice() {
                            if let Some(language) = parse_text(&parameter.value) {
                                match Language::from_name(&language) {
                                    None => add_warning(
                                        &mut context,
                                        parameter,
                                        WarningMessage::ValueUnrecognized,
                                    ),
                                    Some(language) => {
                                        context.language = Some(language);
                                        node_index += language::parse_language(
                                            &mut context,
                                            node,
                                            &nodes[node_index..],
                                            &mut language_entries,
                                        );
                                        context.language = None;
                                    }
                                }
                                continue;
                            }
                        }
                    }
                }
                add_warning(&mut context, node, WarningMessage::ValueUnrecognized);
                continue;
            }
        }
        node_index += 1;
        add_warning(&mut context, node, WarningMessage::Unrecognized);
    }
    Output {
        language_entries,
        warnings: context.warnings,
    }
}