]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/markdown.rs
End of pulldown switch and remove completely hoedown
[rust.git] / src / librustdoc / html / markdown.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Markdown formatting for rustdoc
12 //!
13 //! This module implements markdown formatting through the hoedown C-library
14 //! (bundled into the rust runtime). This module self-contains the C bindings
15 //! and necessary legwork to render markdown, and exposes all of the
16 //! functionality through a unit-struct, `Markdown`, which has an implementation
17 //! of `fmt::Display`. Example usage:
18 //!
19 //! ```rust,ignore
20 //! use rustdoc::html::markdown::Markdown;
21 //!
22 //! let s = "My *markdown* _text_";
23 //! let html = format!("{}", Markdown(s));
24 //! // ... something using html
25 //! ```
26
27 #![allow(non_camel_case_types)]
28
29 //use libc;
30 use std::ascii::AsciiExt;
31 use std::cell::RefCell;
32 use std::default::Default;
33 //use std::ffi::CString;
34 use std::fmt::{self, Write};
35 //use std::slice;
36 use std::str;
37 use syntax::feature_gate::UnstableFeatures;
38 use syntax::codemap::Span;
39
40 use html::render::derive_id;
41 use html::toc::TocBuilder;
42 use html::highlight;
43 use html::escape::Escape;
44 use test;
45
46 use pulldown_cmark::{self, Event, Parser, Tag};
47
48 /// A unit struct which has the `fmt::Display` trait implemented. When
49 /// formatted, this struct will emit the HTML corresponding to the rendered
50 /// version of the contained markdown string.
51 // The second parameter is whether we need a shorter version or not.
52 pub struct Markdown<'a>(pub &'a str, pub bool);
53 /// A unit struct like `Markdown`, that renders the markdown with a
54 /// table of contents.
55 pub struct MarkdownWithToc<'a>(pub &'a str);
56 /// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
57 pub struct MarkdownHtml<'a>(pub &'a str);
58
59 /// Returns Some(code) if `s` is a line that should be stripped from
60 /// documentation but used in example code. `code` is the portion of
61 /// `s` that should be used in tests. (None for lines that should be
62 /// left as-is.)
63 fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
64     let trimmed = s.trim();
65     if trimmed == "#" {
66         Some("")
67     } else if trimmed.starts_with("# ") {
68         Some(&trimmed[2..])
69     } else {
70         None
71     }
72 }
73
74 /// Returns a new string with all consecutive whitespace collapsed into
75 /// single spaces.
76 ///
77 /// Any leading or trailing whitespace will be trimmed.
78 fn collapse_whitespace(s: &str) -> String {
79     s.split_whitespace().collect::<Vec<_>>().join(" ")
80 }
81
82 // Information about the playground if a URL has been specified, containing an
83 // optional crate name and the URL.
84 thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> = {
85     RefCell::new(None)
86 });
87
88
89 pub fn render(w: &mut fmt::Formatter,
90               s: &str,
91               print_toc: bool,
92               shorter: bool) -> fmt::Result {
93     fn block(parser: &mut Parser, buffer: &mut String, lang: &str) {
94         let mut origtext = String::new();
95         loop {
96             let event = parser.next();
97             if let Some(event) = event {
98                 match event {
99                     Event::End(Tag::CodeBlock(_)) => break,
100                     Event::Text(ref s) => {
101                         origtext.push_str(s);
102                     }
103                     _ => {}
104                 }
105             } else {
106                 break
107             }
108         }
109         let origtext = origtext.trim_left();
110         debug!("docblock: ==============\n{:?}\n=======", origtext);
111
112         let lines = origtext.lines().filter(|l| {
113             stripped_filtered_line(*l).is_none()
114         });
115         let text = lines.collect::<Vec<&str>>().join("\n");
116         let block_info = if lang.is_empty() {
117             LangString::all_false()
118         } else {
119             LangString::parse(lang)
120         };
121         if !block_info.rust {
122             buffer.push_str(&format!("<pre><code class=\"language-{}\">{}</code></pre>",
123                             lang, text));
124             return
125         }
126         PLAYGROUND.with(|play| {
127             // insert newline to clearly separate it from the
128             // previous block so we can shorten the html output
129             buffer.push('\n');
130             let playground_button = play.borrow().as_ref().and_then(|&(ref krate, ref url)| {
131                 if url.is_empty() {
132                     return None;
133                 }
134                 let test = origtext.lines().map(|l| {
135                     stripped_filtered_line(l).unwrap_or(l)
136                 }).collect::<Vec<&str>>().join("\n");
137                 let krate = krate.as_ref().map(|s| &**s);
138                 let test = test::maketest(&test, krate, false,
139                                           &Default::default());
140                 let channel = if test.contains("#![feature(") {
141                     "&amp;version=nightly"
142                 } else {
143                     ""
144                 };
145                 // These characters don't need to be escaped in a URI.
146                 // FIXME: use a library function for percent encoding.
147                 fn dont_escape(c: u8) -> bool {
148                     (b'a' <= c && c <= b'z') ||
149                     (b'A' <= c && c <= b'Z') ||
150                     (b'0' <= c && c <= b'9') ||
151                     c == b'-' || c == b'_' || c == b'.' ||
152                     c == b'~' || c == b'!' || c == b'\'' ||
153                     c == b'(' || c == b')' || c == b'*'
154                 }
155                 let mut test_escaped = String::new();
156                 for b in test.bytes() {
157                     if dont_escape(b) {
158                         test_escaped.push(char::from(b));
159                     } else {
160                         write!(test_escaped, "%{:02X}", b).unwrap();
161                     }
162                 }
163                 Some(format!(
164                     r#"<a class="test-arrow" target="_blank" href="{}?code={}{}">Run</a>"#,
165                     url, test_escaped, channel
166                 ))
167             });
168             buffer.push_str(&highlight::render_with_highlighting(
169                             &text,
170                             Some("rust-example-rendered"),
171                             None,
172                             playground_button.as_ref().map(String::as_str)));
173         });
174     }
175
176     fn header(parser: &mut Parser, level: i32, toc_builder: &mut Option<TocBuilder>,
177               buffer: &mut String) {
178         let mut ret = String::new();
179         loop {
180             let event = parser.next();
181             if let Some(event) = event {
182                 match event {
183                     Event::End(Tag::Header(_)) => break,
184                     Event::Text(ref s) => {
185                         ret.push_str(s);
186                     }
187                     _ => {}
188                 }
189             } else {
190                 break
191             }
192         }
193
194         let id = ret.clone();
195         // Discard '<em>', '<code>' tags and some escaped characters,
196         // transform the contents of the header into a hyphenated string
197         // without non-alphanumeric characters other than '-' and '_'.
198         //
199         // This is a terrible hack working around how hoedown gives us rendered
200         // html for text rather than the raw text.
201         let id = id.chars().filter_map(|c| {
202             if c.is_alphanumeric() || c == '-' || c == '_' {
203                 if c.is_ascii() {
204                     Some(c.to_ascii_lowercase())
205                 } else {
206                     Some(c)
207                 }
208             } else if c.is_whitespace() && c.is_ascii() {
209                 Some('-')
210             } else {
211                 None
212             }
213         }).collect::<String>();
214
215         let id = derive_id(id);
216
217         let sec = toc_builder.as_mut().map_or("".to_owned(), |builder| {
218             format!("{} ", builder.push(level as u32, ret.clone(), id.clone()))
219         });
220
221         // Render the HTML
222         buffer.push_str(&format!("<h{lvl} id=\"{id}\" class=\"section-header\">\
223                                   <a href=\"#{id}\">{sec}{}</a></h{lvl}>",
224                                  ret, lvl = level, id = id, sec = sec));
225     }
226
227     fn codespan(parser: &mut Parser, buffer: &mut String) {
228         let mut content = String::new();
229         loop {
230             let event = parser.next();
231             if let Some(event) = event {
232                 match event {
233                     Event::End(Tag::Code) => break,
234                     Event::Text(ref s) => {
235                         content.push_str(s);
236                     }
237                     _ => {}
238                 }
239             } else {
240                 break
241             }
242         }
243         buffer.push_str(&format!("<code>{}</code>", Escape(&collapse_whitespace(&content))));
244     }
245
246     fn link(parser: &mut Parser, buffer: &mut String, url: &str, mut title: String) {
247         loop {
248             let event = parser.next();
249             if let Some(event) = event {
250                 match event {
251                     Event::End(Tag::Link(_, _)) => break,
252                     Event::Text(ref s) => {
253                         title.push_str(s);
254                     }
255                     _ => {}
256                 }
257             } else {
258                 break
259             }
260         }
261         buffer.push_str(&format!("<a href=\"{}\">{}</a>", url, title));
262     }
263
264     fn paragraph(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
265                  shorter: bool) {
266         let mut content = String::new();
267         loop {
268             let event = parser.next();
269             if let Some(event) = event {
270                 match event {
271                     Event::End(Tag::Paragraph) => break,
272                     Event::Text(ref s) => {
273                         content.push_str(s);
274                     }
275                     x => {
276                         looper(parser, &mut content, Some(x), toc_builder, shorter);
277                     }
278                 }
279             } else {
280                 break
281             }
282         }
283         buffer.push_str(&format!("<p>{}</p>", content));
284     }
285
286     fn cell(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
287             shorter: bool) {
288         let mut content = String::new();
289         loop {
290             let event = parser.next();
291             if let Some(event) = event {
292                 match event {
293                     Event::End(Tag::TableHead) |
294                         Event::End(Tag::Table(_)) |
295                         Event::End(Tag::TableRow) |
296                         Event::End(Tag::TableCell) => break,
297                     Event::Text(ref s) => {
298                         content.push_str(s);
299                     }
300                     x => {
301                         looper(parser, &mut content, Some(x), toc_builder, shorter);
302                     }
303                 }
304             } else {
305                 break
306             }
307         }
308         buffer.push_str(&format!("<td>{}</td>", content.trim()));
309     }
310
311     fn row(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
312            shorter: bool) {
313         let mut content = String::new();
314         loop {
315             let event = parser.next();
316             if let Some(event) = event {
317                 match event {
318                     Event::End(Tag::TableHead) |
319                         Event::End(Tag::Table(_)) |
320                         Event::End(Tag::TableRow) => break,
321                     Event::Start(Tag::TableCell) => {
322                         cell(parser, &mut content, toc_builder, shorter);
323                     }
324                     x => {
325                         looper(parser, &mut content, Some(x), toc_builder, shorter);
326                     }
327                 }
328             } else {
329                 break
330             }
331         }
332         buffer.push_str(&format!("<tr>{}</tr>", content));
333     }
334
335     fn head(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
336             shorter: bool) {
337         let mut content = String::new();
338         loop {
339             let event = parser.next();
340             if let Some(event) = event {
341                 match event {
342                     Event::End(Tag::TableHead) | Event::End(Tag::Table(_)) => break,
343                     Event::Start(Tag::TableCell) => {
344                         cell(parser, &mut content, toc_builder, shorter);
345                     }
346                     x => {
347                         looper(parser, &mut content, Some(x), toc_builder, shorter);
348                     }
349                 }
350             } else {
351                 break
352             }
353         }
354         if content.is_empty() {
355             return
356         }
357         buffer.push_str(&format!("<thead><tr>{}</tr></thead>", content.replace("td>", "th>")));
358     }
359
360     fn table(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
361              shorter: bool) {
362         let mut content = String::new();
363         let mut rows = String::new();
364         loop {
365             let event = parser.next();
366             if let Some(event) = event {
367                 match event {
368                     Event::End(Tag::Table(_)) => break,
369                     Event::Start(Tag::TableHead) => {
370                         head(parser, &mut content, toc_builder, shorter);
371                     }
372                     Event::Start(Tag::TableRow) => {
373                         row(parser, &mut rows, toc_builder, shorter);
374                     }
375                     _ => {}
376                 }
377             } else {
378                 break
379             }
380         }
381         buffer.push_str(&format!("<table>{}{}</table>",
382                                  content,
383                                  if shorter || rows.is_empty() {
384                                      String::new()
385                                  } else {
386                                      format!("<tbody>{}</tbody>", rows)
387                                  }));
388     }
389
390     fn looper<'a>(parser: &'a mut Parser, buffer: &mut String, next_event: Option<Event<'a>>,
391                   toc_builder: &mut Option<TocBuilder>, shorter: bool) -> bool {
392         if let Some(event) = next_event {
393             match event {
394                 Event::Start(Tag::CodeBlock(lang)) => {
395                     block(parser, buffer, &*lang);
396                 }
397                 Event::Start(Tag::Header(level)) => {
398                     header(parser, level, toc_builder, buffer);
399                 }
400                 Event::Start(Tag::Code) => {
401                     codespan(parser, buffer);
402                 }
403                 Event::Start(Tag::Paragraph) => {
404                     paragraph(parser, buffer, toc_builder, shorter);
405                 }
406                 Event::Start(Tag::Link(ref url, ref t)) => {
407                     link(parser, buffer, url, t.as_ref().to_owned());
408                 }
409                 Event::Start(Tag::Table(_)) => {
410                     table(parser, buffer, toc_builder, shorter);
411                 }
412                 _ => {}
413             }
414             shorter == false
415         } else {
416             false
417         }
418     }
419
420     let mut toc_builder = if print_toc {
421         Some(TocBuilder::new())
422     } else {
423         None
424     };
425     let mut buffer = String::new();
426     let mut parser = Parser::new_ext(s, pulldown_cmark::OPTION_ENABLE_TABLES);
427     loop {
428         let next_event = parser.next();
429         if !looper(&mut parser, &mut buffer, next_event, &mut toc_builder, shorter) {
430             break
431         }
432     }
433     let mut ret = toc_builder.map_or(Ok(()), |builder| {
434         write!(w, "<nav id=\"TOC\">{}</nav>", builder.into_toc())
435     });
436
437     if ret.is_ok() {
438         ret = w.write_str(&buffer);
439     }
440     ret
441 }
442
443 pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
444     tests.set_position(position);
445
446     let mut parser = Parser::new(doc);
447     let mut prev_offset = 0;
448     let mut nb_lines = 0;
449     let mut register_header = None;
450     'main: while let Some(event) = parser.next() {
451         match event {
452             Event::Start(Tag::CodeBlock(s)) => {
453                 let block_info = if s.is_empty() {
454                     LangString::all_false()
455                 } else {
456                     LangString::parse(&*s)
457                 };
458                 if !block_info.rust {
459                     continue
460                 }
461                 let mut test_s = String::new();
462                 let mut offset = None;
463                 loop {
464                     let event = parser.next();
465                     if let Some(event) = event {
466                         match event {
467                             Event::End(Tag::CodeBlock(_)) => break,
468                             Event::Text(ref s) => {
469                                 test_s.push_str(s);
470                                 if offset.is_none() {
471                                     offset = Some(parser.get_offset());
472                                 }
473                             }
474                             _ => {}
475                         }
476                     } else {
477                         break 'main;
478                     }
479                 }
480                 let offset = offset.unwrap_or(0);
481                 let lines = test_s.lines().map(|l| {
482                     stripped_filtered_line(l).unwrap_or(l)
483                 });
484                 let text = lines.collect::<Vec<&str>>().join("\n");
485                 nb_lines += doc[prev_offset..offset].lines().count();
486                 let line = tests.get_line() + (nb_lines - 1);
487                 let filename = tests.get_filename();
488                 tests.add_test(text.to_owned(),
489                                block_info.should_panic, block_info.no_run,
490                                block_info.ignore, block_info.test_harness,
491                                block_info.compile_fail, block_info.error_codes,
492                                line, filename);
493                 prev_offset = offset;
494             }
495             Event::Start(Tag::Header(level)) => {
496                 register_header = Some(level as u32);
497             }
498             Event::Text(ref s) if register_header.is_some() => {
499                 let level = register_header.unwrap();
500                 if s.is_empty() {
501                     tests.register_header("", level);
502                 } else {
503                     tests.register_header(s, level);
504                 }
505                 register_header = None;
506             }
507             _ => {}
508         }
509     }
510 }
511
512 #[derive(Eq, PartialEq, Clone, Debug)]
513 struct LangString {
514     original: String,
515     should_panic: bool,
516     no_run: bool,
517     ignore: bool,
518     rust: bool,
519     test_harness: bool,
520     compile_fail: bool,
521     error_codes: Vec<String>,
522 }
523
524 impl LangString {
525     fn all_false() -> LangString {
526         LangString {
527             original: String::new(),
528             should_panic: false,
529             no_run: false,
530             ignore: false,
531             rust: true,  // NB This used to be `notrust = false`
532             test_harness: false,
533             compile_fail: false,
534             error_codes: Vec::new(),
535         }
536     }
537
538     fn parse(string: &str) -> LangString {
539         let mut seen_rust_tags = false;
540         let mut seen_other_tags = false;
541         let mut data = LangString::all_false();
542         let mut allow_compile_fail = false;
543         let mut allow_error_code_check = false;
544         if UnstableFeatures::from_environment().is_nightly_build() {
545             allow_compile_fail = true;
546             allow_error_code_check = true;
547         }
548
549         data.original = string.to_owned();
550         let tokens = string.split(|c: char|
551             !(c == '_' || c == '-' || c.is_alphanumeric())
552         );
553
554         for token in tokens {
555             match token {
556                 "" => {},
557                 "should_panic" => { data.should_panic = true; seen_rust_tags = true; },
558                 "no_run" => { data.no_run = true; seen_rust_tags = true; },
559                 "ignore" => { data.ignore = true; seen_rust_tags = true; },
560                 "rust" => { data.rust = true; seen_rust_tags = true; },
561                 "test_harness" => { data.test_harness = true; seen_rust_tags = true; },
562                 "compile_fail" if allow_compile_fail => {
563                     data.compile_fail = true;
564                     seen_rust_tags = true;
565                     data.no_run = true;
566                 }
567                 x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
568                     if let Ok(_) = x[1..].parse::<u32>() {
569                         data.error_codes.push(x.to_owned());
570                         seen_rust_tags = true;
571                     } else {
572                         seen_other_tags = true;
573                     }
574                 }
575                 _ => { seen_other_tags = true }
576             }
577         }
578
579         data.rust &= !seen_other_tags || seen_rust_tags;
580
581         data
582     }
583 }
584
585 impl<'a> fmt::Display for Markdown<'a> {
586     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
587         let Markdown(md, shorter) = *self;
588         // This is actually common enough to special-case
589         if md.is_empty() { return Ok(()) }
590         render(fmt, md, false, shorter)
591     }
592 }
593
594 impl<'a> fmt::Display for MarkdownWithToc<'a> {
595     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
596         let MarkdownWithToc(md) = *self;
597         render(fmt, md, true, false)
598     }
599 }
600
601 impl<'a> fmt::Display for MarkdownHtml<'a> {
602     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
603         let MarkdownHtml(md) = *self;
604         // This is actually common enough to special-case
605         if md.is_empty() { return Ok(()) }
606         render(fmt, md, false, false)
607     }
608 }
609
610 pub fn plain_summary_line(md: &str) -> String {
611     struct ParserWrapper<'a> {
612         inner: Parser<'a>,
613         is_in: isize,
614         is_first: bool,
615     }
616
617     impl<'a> Iterator for ParserWrapper<'a> {
618         type Item = String;
619
620         fn next(&mut self) -> Option<String> {
621             let next_event = self.inner.next();
622             if next_event.is_none() {
623                 return None
624             }
625             let next_event = next_event.unwrap();
626             let (ret, is_in) = match next_event {
627                 Event::Start(Tag::Paragraph) => (None, 1),
628                 Event::Start(Tag::Link(_, ref t)) if !self.is_first => {
629                     (Some(t.as_ref().to_owned()), 1)
630                 }
631                 Event::Text(ref s) if self.is_in > 0 => (Some(s.as_ref().to_owned()), 0),
632                 Event::End(Tag::Link(_, ref t)) => (Some(t.as_ref().to_owned()), -1),
633                 Event::End(Tag::Paragraph) => (None, -1),
634                 _ => (None, 0),
635             };
636             if is_in > 0 || (is_in < 0 && self.is_in > 0) {
637                 self.is_in += is_in;
638             }
639             if ret.is_some() {
640                 self.is_first = false;
641                 ret
642             } else {
643                 Some(String::new())
644             }
645         }
646     }
647     let mut s = String::with_capacity(md.len() * 3 / 2);
648     let mut p = ParserWrapper {
649         inner: Parser::new(md),
650         is_in: 0,
651         is_first: true,
652     };
653     while let Some(t) = p.next() {
654         if !t.is_empty() {
655             s.push_str(&t);
656         }
657     }
658     s
659 }
660
661 #[cfg(test)]
662 mod tests {
663     use super::{LangString, Markdown, MarkdownHtml};
664     use super::plain_summary_line;
665     use html::render::reset_ids;
666
667     #[test]
668     fn test_lang_string_parse() {
669         fn t(s: &str,
670             should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
671             compile_fail: bool, error_codes: Vec<String>) {
672             assert_eq!(LangString::parse(s), LangString {
673                 should_panic: should_panic,
674                 no_run: no_run,
675                 ignore: ignore,
676                 rust: rust,
677                 test_harness: test_harness,
678                 compile_fail: compile_fail,
679                 error_codes: error_codes,
680                 original: s.to_owned(),
681             })
682         }
683
684         // marker                | should_panic| no_run| ignore| rust | test_harness| compile_fail
685         //                       | error_codes
686         t("",                      false,        false,  false,  true,  false, false, Vec::new());
687         t("rust",                  false,        false,  false,  true,  false, false, Vec::new());
688         t("sh",                    false,        false,  false,  false, false, false, Vec::new());
689         t("ignore",                false,        false,  true,   true,  false, false, Vec::new());
690         t("should_panic",          true,         false,  false,  true,  false, false, Vec::new());
691         t("no_run",                false,        true,   false,  true,  false, false, Vec::new());
692         t("test_harness",          false,        false,  false,  true,  true,  false, Vec::new());
693         t("compile_fail",          false,        true,   false,  true,  false, true,  Vec::new());
694         t("{.no_run .example}",    false,        true,   false,  true,  false, false, Vec::new());
695         t("{.sh .should_panic}",   true,         false,  false,  true,  false, false, Vec::new());
696         t("{.example .rust}",      false,        false,  false,  true,  false, false, Vec::new());
697         t("{.test_harness .rust}", false,        false,  false,  true,  true,  false, Vec::new());
698     }
699
700     #[test]
701     fn issue_17736() {
702         let markdown = "# title";
703         format!("{}", Markdown(markdown));
704         reset_ids(true);
705     }
706
707     #[test]
708     fn test_header() {
709         fn t(input: &str, expect: &str) {
710             let output = format!("{}", Markdown(input));
711             assert_eq!(output, expect);
712             reset_ids(true);
713         }
714
715         t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\
716           <a href='#foo-bar'>Foo bar</a></h1>");
717         t("## Foo-bar_baz qux", "\n<h2 id='foo-bar_baz-qux' class=\'section-\
718           header'><a href='#foo-bar_baz-qux'>Foo-bar_baz qux</a></h2>");
719         t("### **Foo** *bar* baz!?!& -_qux_-%",
720           "\n<h3 id='foo-bar-baz--_qux_-' class='section-header'>\
721           <a href='#foo-bar-baz--_qux_-'><strong>Foo</strong> \
722           <em>bar</em> baz!?!&amp; -_qux_-%</a></h3>");
723         t("####**Foo?** & \\*bar?!*  _`baz`_ ❤ #qux",
724           "\n<h4 id='foo--bar--baz--qux' class='section-header'>\
725           <a href='#foo--bar--baz--qux'><strong>Foo?</strong> &amp; *bar?!*  \
726           <em><code>baz</code></em> ❤ #qux</a></h4>");
727     }
728
729     #[test]
730     fn test_header_ids_multiple_blocks() {
731         fn t(input: &str, expect: &str) {
732             let output = format!("{}", Markdown(input));
733             assert_eq!(output, expect);
734         }
735
736         let test = || {
737             t("# Example", "\n<h1 id='example' class='section-header'>\
738               <a href='#example'>Example</a></h1>");
739             t("# Panics", "\n<h1 id='panics' class='section-header'>\
740               <a href='#panics'>Panics</a></h1>");
741             t("# Example", "\n<h1 id='example-1' class='section-header'>\
742               <a href='#example-1'>Example</a></h1>");
743             t("# Main", "\n<h1 id='main-1' class='section-header'>\
744               <a href='#main-1'>Main</a></h1>");
745             t("# Example", "\n<h1 id='example-2' class='section-header'>\
746               <a href='#example-2'>Example</a></h1>");
747             t("# Panics", "\n<h1 id='panics-1' class='section-header'>\
748               <a href='#panics-1'>Panics</a></h1>");
749         };
750         test();
751         reset_ids(true);
752         test();
753     }
754
755     #[test]
756     fn test_plain_summary_line() {
757         fn t(input: &str, expect: &str) {
758             let output = plain_summary_line(input);
759             assert_eq!(output, expect);
760         }
761
762         t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
763         t("code `let x = i32;` ...", "code `let x = i32;` ...");
764         t("type `Type<'static>` ...", "type `Type<'static>` ...");
765         t("# top header", "top header");
766         t("## header", "header");
767     }
768
769     #[test]
770     fn test_markdown_html_escape() {
771         fn t(input: &str, expect: &str) {
772             let output = format!("{}", MarkdownHtml(input));
773             assert_eq!(output, expect);
774         }
775
776         t("`Struct<'a, T>`", "<p><code>Struct&lt;&#39;a, T&gt;</code></p>\n");
777         t("Struct<'a, T>", "<p>Struct&lt;&#39;a, T&gt;</p>\n");
778     }
779 }