cargo-rdme 2.0.0

Cargo command to create your `README.md` from your crate's documentation
Documentation
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
use crate::markdown::Markdown;
use crate::utils::{MarkdownItemIterator, Span};
use itertools::Itertools;
use pulldown_cmark::{CowStr, TagEnd};
use std::fmt;
use std::fmt::Display;
use unicase::UniCase;

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct MarkdownReferenceLinkDefinition {
    pub label: UniCase<String>,
    pub link: Link,
    pub raw_title: Option<String>,
}

impl MarkdownReferenceLinkDefinition {
    fn new(
        label: String,
        link: Link,
        raw_title: Option<String>,
    ) -> MarkdownReferenceLinkDefinition {
        MarkdownReferenceLinkDefinition { label: UniCase::unicode(label), link, raw_title }
    }

    pub fn with_link(&self, link: Link) -> MarkdownReferenceLinkDefinition {
        MarkdownReferenceLinkDefinition {
            label: self.label.clone(),
            link,
            raw_title: self.raw_title.clone(),
        }
    }
}

impl Display for MarkdownReferenceLinkDefinition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let MarkdownReferenceLinkDefinition { label: key, link, raw_title } = self;

        match raw_title {
            None => write!(f, "[{key}]: {link}"),
            Some(title) => write!(f, "[{key}]: {link} {title}"),
        }
    }
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub enum MarkdownLink {
    Inline { link: MarkdownInlineLink },
    Reference { link: MarkdownReferenceLink },
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct MarkdownInlineLink {
    pub text: String,
    pub link: Link,
}

impl MarkdownInlineLink {
    pub fn with_link(&self, link: Link) -> MarkdownInlineLink {
        MarkdownInlineLink { text: self.text.clone(), link }
    }
}

impl Display for MarkdownInlineLink {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}]({})", self.text, self.link)
    }
}

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct Link {
    pub raw_link: String,
}

impl Link {
    pub fn new(raw_link: String) -> Link {
        Link { raw_link }
    }

    fn split_link_fragment(&self) -> (&str, &str) {
        fn strip_last_backtick(strip_backtick_end: bool, s: &str) -> &str {
            match strip_backtick_end {
                true => s.strip_suffix('`').unwrap_or(s),
                false => s,
            }
        }

        let strip_backtick_end: bool = self.raw_link.starts_with('`');
        let link = self.raw_link.strip_prefix('`').unwrap_or(&self.raw_link);

        match link.find('#') {
            None => (strip_last_backtick(strip_backtick_end, link), ""),
            Some(i) => {
                let (l, f) = link.split_at(i);
                (
                    strip_last_backtick(strip_backtick_end, l),
                    strip_last_backtick(strip_backtick_end, &f[1..]),
                )
            }
        }
    }

    pub fn symbol(&self) -> &str {
        self.split_link_fragment().0
    }

    pub fn link_fragment(&self) -> Option<&str> {
        match self.split_link_fragment().1 {
            "" => None,
            f => Some(f),
        }
    }
}

impl From<String> for Link {
    fn from(raw_link: String) -> Link {
        Link::new(raw_link)
    }
}

impl From<&str> for Link {
    fn from(raw_link: &str) -> Link {
        Link::new(raw_link.to_owned())
    }
}

impl Display for Link {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.raw_link.fmt(f)
    }
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub enum MarkdownReferenceLink {
    /// A reference link like [text][label].
    Normal { text: String, label: UniCase<String> },
    /// A reference link like [label]. Note that the label is also the text.
    Shortcut { text: UniCase<String> },
}

impl MarkdownReferenceLink {
    fn new(text: String, label: String) -> MarkdownReferenceLink {
        MarkdownReferenceLink::Normal { text, label: UniCase::unicode(label) }
    }

    fn new_shortcut(text: String) -> MarkdownReferenceLink {
        MarkdownReferenceLink::Shortcut { text: UniCase::unicode(text) }
    }

    pub fn text(&self) -> &str {
        match self {
            MarkdownReferenceLink::Normal { text, .. } => text,
            MarkdownReferenceLink::Shortcut { text } => text.as_str(),
        }
    }

    pub fn label(&self) -> &UniCase<String> {
        match self {
            MarkdownReferenceLink::Normal { label, .. } => label,
            MarkdownReferenceLink::Shortcut { text } => text,
        }
    }
}

impl Display for MarkdownReferenceLink {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MarkdownReferenceLink::Normal { text, label } => write!(f, "[{text}][{label}]"),
            MarkdownReferenceLink::Shortcut { text: label } => write!(f, "[{label}]"),
        }
    }
}

pub fn markdown_link_iterator(markdown: &Markdown) -> MarkdownItemIterator<'_, MarkdownLink> {
    use pulldown_cmark::{Event, LinkType, Options, Parser, Tag};

    let source = markdown.as_string();

    // We need to define a callback for broken links so that we can see them and strip them.
    let mut broken_link_callback =
        |_| Some((CowStr::Borrowed("fake://link"), CowStr::Borrowed("fake title")));
    let parser = Parser::new_with_broken_link_callback(
        source,
        Options::all(),
        Some(&mut broken_link_callback),
    );

    let mut in_link: Option<LinkType> = None;
    let mut start_text = 0;
    let mut end_text = 0;

    let iter = parser.into_offset_iter().filter_map(move |(event, range)| match event {
        Event::Start(Tag::Link {
            link_type:
                link_type @ (LinkType::Inline
                | LinkType::Reference
                | LinkType::Shortcut
                | LinkType::ReferenceUnknown
                | LinkType::ShortcutUnknown),
            ..
        }) => {
            in_link = Some(link_type);
            start_text = range.start + 1;
            end_text = range.end;
            None
        }
        Event::End(TagEnd::Link) => match in_link {
            Some(LinkType::Inline) => {
                in_link = None;

                let text = source[start_text..end_text].to_owned();
                let link = source[(end_text + 2)..(range.end - 1)].to_owned().into();

                let link = MarkdownLink::Inline { link: MarkdownInlineLink { text, link } };

                Some((range.into(), link))
            }
            Some(LinkType::Reference | LinkType::ReferenceUnknown) => {
                in_link = None;

                let text = source[start_text..end_text].to_owned();
                let label = source[(end_text + 2)..(range.end - 1)].to_owned();

                let link =
                    MarkdownLink::Reference { link: MarkdownReferenceLink::new(text, label) };

                Some((range.into(), link))
            }
            Some(LinkType::Shortcut | LinkType::ShortcutUnknown) => {
                in_link = None;

                let text = source[start_text..end_text].to_owned();

                let link =
                    MarkdownLink::Reference { link: MarkdownReferenceLink::new_shortcut(text) };

                Some((range.into(), link))
            }
            Some(_) | None => None,
        },
        _ => {
            if in_link.is_some() {
                end_text = range.end;
            }

            None
        }
    });

    // Unfortunately we need to collect the iterator here, because the parser references
    // `broken_link_callback` which is in the local stack.
    let collected = iter.collect_vec();

    MarkdownItemIterator::new(source, collected.into_iter())
}

fn parse_raw_reference_link_definition(
    label: &str,
    raw_ref_def: &str,
) -> Option<MarkdownReferenceLinkDefinition> {
    // We need to parse things manually here, because the pulldown-cmark parser escapes the title
    // and the link.  We need the raw version to emit them later.

    let link_and_title = raw_ref_def.get(label.len() + 3..).map(str::trim)?;

    assert_eq!(
        raw_ref_def.get(label.len() + 1..label.len() + 3),
        Some("]:"),
        "This should never happen, but if it does please report it as a bug.",
    );

    let (link, title) = match link_and_title.find(char::is_whitespace) {
        None => (link_and_title, None),
        Some(i) => {
            let (link, title) = link_and_title.split_at(i);
            let title = match title.trim() {
                "" => None,
                title => Some(title),
            };

            (link.trim(), title)
        }
    };

    let link = MarkdownReferenceLinkDefinition::new(
        label.to_owned(),
        link.into(),
        title.map(ToOwned::to_owned),
    );

    Some(link)
}

pub fn markdown_reference_link_definition_iterator(
    markdown: &Markdown,
) -> MarkdownItemIterator<'_, MarkdownReferenceLinkDefinition> {
    use pulldown_cmark::{Options, Parser};

    let source = markdown.as_string();
    let parser = Parser::new_ext(source, Options::all());

    let mut link_defs: Vec<(Span, MarkdownReferenceLinkDefinition)> = parser
        .reference_definitions()
        .iter()
        .filter_map(|(label, ref_def)| {
            let raw_ref_def = &source[ref_def.span.clone()];

            parse_raw_reference_link_definition(label, raw_ref_def)
                .map(|link| (Span::from(ref_def.span.clone()), link))
        })
        .collect_vec();

    // `Parser::reference_definitions()` does not traverse the definitions in order. But
    // `MarkdownItemIterator::complete()` expects the iterated-over spans to be in order.
    link_defs.sort_by_key(|(span, _)| span.clone());

    MarkdownItemIterator::new(source, link_defs.into_iter())
}

#[cfg(test)]
mod tests {
    use super::*;
    use indoc::indoc;

    #[allow(clippy::too_many_lines)]
    #[test]
    fn test_markdown_link_iterator() {
        let markdown =
            Markdown::from_str("A [some text] [another](http://foo.com), [another][one]");

        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new_shortcut("some text".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[some text]");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Inline {
                link: MarkdownInlineLink {
                    text: "another".to_owned(),
                    link: "http://foo.com".into(),
                }
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[another](http://foo.com)");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new("another".to_owned(), "one".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[another][one]");

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str("[another](http://foo.com)[another][one]");
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Inline {
                link: MarkdownInlineLink {
                    text: "another".to_owned(),
                    link: "http://foo.com".into()
                }
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[another](http://foo.com)");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new("another".to_owned(), "one".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[another][one]");

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str(
            "A [some [text]], [another [text2] (foo)](http://foo.com/foo(bar)), [another [] one][foo[]bar]",
        );
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new_shortcut("text".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[text]");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new_shortcut("text2".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[text2]");

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str(
            "A [some \\]text], [another](http://foo.\\(com\\)), [another\\]][one\\]]",
        );
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new_shortcut(r"some \]text".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], r"[some \]text]");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Inline {
                link: MarkdownInlineLink {
                    text: "another".to_owned(),
                    link: r"http://foo.\(com\)".into(),
                }
            }
        );
        assert_eq!(&markdown.as_string()[start..end], r"[another](http://foo.\(com\))");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new(r"another\]".to_owned(), r"one\]".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], r"[another\]][one\]]");

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str("A `this is no link [link](http://foo.com)`");
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str("A\n```\nthis is no link [link](http://foo.com)\n```");
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        assert_eq!(iter.next(), None);

        let markdown = Markdown::from_str("A [link with `code`!](http://foo.com)!");
        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Inline {
                link: MarkdownInlineLink {
                    text: "link with `code`!".to_owned(),
                    link: "http://foo.com".into(),
                }
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[link with `code`!](http://foo.com)");

        assert_eq!(iter.next(), None);

        let doc = indoc! { r#"
            Here a link with [some text] and [another][one].

            [some text]: https://en.wikipedia.org/wiki/Markdown
            [one]: https://en.wikipedia.org/wiki/Markdown
            "#
        };
        let markdown = Markdown::from_str(doc);

        let mut iter = markdown_link_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new_shortcut("some text".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[some text]");

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownLink::Reference {
                link: MarkdownReferenceLink::new("another".to_owned(), "one".to_owned()),
            }
        );
        assert_eq!(&markdown.as_string()[start..end], "[another][one]");

        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_markdown_reference_link_definition_iterator() {
        let doc = indoc! { r#"
            [ref]: https://en.wikipedia.org/wiki/Markdown "This is the title"
            [Another ref]: https://en.wikipedia.org/wiki/Markdown
            [And another one]: https://en.wikipedia.org/wiki/Markdown "Title with &amp;"
            [spaces]:    https://en.wikipedia.org/wiki/Markdown    " Title  with  spaces  "
            "#
        };
        let markdown = Markdown::from_str(doc);

        let mut iter = markdown_reference_link_definition_iterator(&markdown).items_with_spans();

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownReferenceLinkDefinition::new(
                "ref".to_owned(),
                "https://en.wikipedia.org/wiki/Markdown".into(),
                Some("\"This is the title\"".to_owned()),
            )
        );
        assert_eq!(
            &markdown.as_string()[start..end],
            "[ref]: https://en.wikipedia.org/wiki/Markdown \"This is the title\""
        );

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownReferenceLinkDefinition::new(
                "Another ref".to_owned(),
                "https://en.wikipedia.org/wiki/Markdown".into(),
                None,
            )
        );
        assert_eq!(
            &markdown.as_string()[start..end],
            "[Another ref]: https://en.wikipedia.org/wiki/Markdown"
        );

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownReferenceLinkDefinition::new(
                "And another one".to_owned(),
                "https://en.wikipedia.org/wiki/Markdown".into(),
                Some("\"Title with &amp;\"".to_owned()),
            )
        );
        assert_eq!(
            &markdown.as_string()[start..end],
            "[And another one]: https://en.wikipedia.org/wiki/Markdown \"Title with &amp;\""
        );

        let (Span { start, end }, link) = iter.next().unwrap();
        assert_eq!(
            link,
            MarkdownReferenceLinkDefinition::new(
                "spaces".to_owned(),
                "https://en.wikipedia.org/wiki/Markdown".into(),
                Some("\" Title  with  spaces  \"".to_owned()),
            )
        );
        assert_eq!(
            &markdown.as_string()[start..end],
            "[spaces]:    https://en.wikipedia.org/wiki/Markdown    \" Title  with  spaces  \""
        );

        assert_eq!(iter.next(), None);
    }
}