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
//! Store information needed to build a table of contents for a
//! document.

use std::cmp::Ordering;
use std::collections::HashSet;

use slug::slugify;

use crate::html::{as_plain_text, Content};

// Compute a short unique name ("slug") for a heading in a document.
#[derive(Debug, Default)]
struct SlugFactory {
    known: HashSet<String>,
}

impl SlugFactory {
    /// Compute a slug for a given heading text.
    fn slug(&mut self, text: &str) -> Result<String, SlugError> {
        let simple = slugify(text);
        let slug = self.unique_slug(&simple)?;
        self.known.insert(slug.clone());
        Ok(slug)
    }

    fn given(&mut self, slug: &str) {
        self.known.insert(slug.into());
    }

    fn unique_slug(&mut self, simple: &str) -> Result<String, SlugError> {
        const MAX_TRIES: usize = 1000;

        if self.known.contains(simple) {
            for i in 2..MAX_TRIES {
                let slug = format!("{simple}{i}");
                if !self.known.contains(&slug) {
                    return Ok(slug);
                }
            }
            Err(SlugError::Unique(MAX_TRIES, simple.into()))
        } else {
            Ok(simple.into())
        }
    }
}

/// Possible errors from creating a slug.
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
pub enum SlugError {
    /// Can't create a slug. This is probably due to too many headings
    /// that start with the same words.
    #[error("failed to generate a unique slug in {0} tries for {1:?}")]
    Unique(usize, String),
}

#[cfg(test)]
mod test_slugs {
    use super::SlugFactory;

    #[test]
    fn short_and_simple() {
        let mut slugs = SlugFactory::default();
        assert_eq!(slugs.slug("Foo"), Ok("foo".into()));
    }

    #[test]
    fn unique_for_identical_simple_headings() {
        let mut slugs = SlugFactory::default();
        assert_ne!(slugs.slug("Foo"), slugs.slug("Foo"));
    }
}

// Number all headings in a document.
//
// Headings are numbered sequentially. The first heading is 1. A new
// heading increments the least significant component of a heading one
// the same level as the new one. Thus, the second heading is 2, or
// 1.1, if it's a subheading.
#[derive(Debug, Default)]
struct HeadingNumberer {
    prev: Vec<usize>,
}

impl HeadingNumberer {
    // Pick number a new heading: 1, 1.1, 1.2, 1.3, 2, 2.1, etc.
    // There can be any number of levels..
    fn number(&mut self, level: usize) -> String {
        match level.cmp(&self.prev.len()) {
            Ordering::Equal => {
                if let Some(n) = self.prev.pop() {
                    self.prev.push(n + 1);
                } else {
                    self.prev.push(1);
                }
            }
            Ordering::Greater => {
                self.prev.push(1);
            }
            Ordering::Less => {
                assert!(!self.prev.is_empty());
                self.prev.pop();
                if let Some(n) = self.prev.pop() {
                    self.prev.push(n + 1);
                } else {
                    self.prev.push(1);
                }
            }
        }

        let mut s = String::new();
        for i in self.prev.iter() {
            if !s.is_empty() {
                s.push('.');
            }
            s.push_str(&i.to_string());
        }
        s
    }
}

#[cfg(test)]
mod test_numberer {
    use super::HeadingNumberer;

    #[test]
    fn numbering() {
        let mut n = HeadingNumberer::default();
        assert_eq!(n.number(1), "1");
        assert_eq!(n.number(2), "1.1");
        assert_eq!(n.number(1), "2");
        assert_eq!(n.number(2), "2.1");
    }
}

/// A table of contents.
#[derive(Debug, Default)]
pub struct TableOfContents {
    slugs: SlugFactory,
    numbers: HeadingNumberer,
    headings: Vec<Heading>,
}

impl TableOfContents {
    /// Push a new heading, returning its unique slug. Use the slug
    /// given, if any.
    pub fn push_heading(
        &mut self,
        level: usize,
        content: &Content,
        slug: Option<&str>,
    ) -> Result<Heading, ToCError> {
        let text = as_plain_text(&[content.clone()]);
        let slug = if let Some(slug) = slug {
            self.slugs.given(slug);
            slug.to_string()
        } else {
            self.slugs.slug(&text)?
        };

        let number = self.numbers.number(level);
        let heading = Heading {
            level,
            slug: slug.clone(),
            number,
            content: content.clone(),
        };
        self.headings.push(heading.clone());
        Ok(heading)
    }

    /// Iterate over slugs for all headings.
    pub fn iter(&self) -> impl Iterator<Item = &Heading> {
        self.headings.iter()
    }
}

/// Possible errors for table of contents.
#[derive(Debug, thiserror::Error)]
pub enum ToCError {
    /// Error from generating a slug.
    #[error(transparent)]
    Slug(#[from] SlugError),
}

/// Represent one heading in a table of contents.
#[derive(Debug, Clone)]
pub struct Heading {
    /// The level of the heading.
    pub level: usize,

    /// The slug identifier for the heading.
    pub slug: String,

    /// The number of the heading.
    pub number: String,

    /// The HTML content of the heading.
    pub content: Content,
}

impl PartialEq for Heading {
    fn eq(&self, other: &Self) -> bool {
        self.level == other.level && self.slug == other.slug && self.number == other.number
    }
}

impl Eq for &Heading {}

#[cfg(test)]
mod test_toc {
    use crate::html::Content;

    use super::{Heading, TableOfContents};

    #[test]
    fn iterate() {
        let mut toc = TableOfContents::default();
        toc.push_heading(1, &Content::Text("Foo".into()), None)
            .unwrap();
        toc.push_heading(1, &Content::Text("Bar".into()), None)
            .unwrap();
        let expected_foo = Heading {
            level: 1,
            slug: "foo".into(),
            number: "1".into(),
            content: Content::Text("Foo".into()),
        };
        let expected_bar = Heading {
            level: 1,
            slug: "bar".into(),
            number: "2".into(),
            content: Content::Text("Bar".into()),
        };
        let actual: Vec<&Heading> = toc.iter().collect();
        assert_eq!(actual, vec![&expected_foo, &expected_bar]);
    }

    #[test]
    fn uses_given_slug() {
        let mut toc = TableOfContents::default();
        toc.push_heading(1, &Content::Text("Foo".into()), Some("foo"))
            .unwrap();
        toc.push_heading(1, &Content::Text("Foo".into()), None)
            .unwrap();
        let expected_1 = Heading {
            level: 1,
            slug: "foo".into(),
            number: "1".into(),
            content: Content::Text("Foo".into()),
        };
        let expected_2 = Heading {
            level: 1,
            slug: "foo2".into(),
            number: "2".into(),
            content: Content::Text("Foo".into()),
        };
        let actual: Vec<&Heading> = toc.iter().collect();
        assert_eq!(actual, vec![&expected_1, &expected_2]);
    }
}