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.
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.
11 //! Markdown formatting for rustdoc
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:
20 //! use rustdoc::html::markdown::Markdown;
22 //! let s = "My *markdown* _text_";
23 //! let html = format!("{}", Markdown(s));
24 //! // ... something using html
27 #![allow(non_camel_case_types)]
29 use std::ascii::AsciiExt;
30 use std::cell::RefCell;
31 use std::default::Default;
32 use std::fmt::{self, Write};
34 use syntax::feature_gate::UnstableFeatures;
35 use syntax::codemap::Span;
37 use html::render::derive_id;
38 use html::toc::TocBuilder;
40 use html::escape::Escape;
43 use pulldown_cmark::{self, Event, Parser, Tag};
45 #[derive(Copy, Clone)]
46 pub enum MarkdownOutputStyle {
51 impl MarkdownOutputStyle {
52 pub fn is_compact(&self) -> bool {
54 MarkdownOutputStyle::Compact => true,
59 pub fn is_fancy(&self) -> bool {
61 MarkdownOutputStyle::Fancy => true,
67 /// A unit struct which has the `fmt::Display` trait implemented. When
68 /// formatted, this struct will emit the HTML corresponding to the rendered
69 /// version of the contained markdown string.
70 // The second parameter is whether we need a shorter version or not.
71 pub struct Markdown<'a>(pub &'a str, pub MarkdownOutputStyle);
72 /// A unit struct like `Markdown`, that renders the markdown with a
73 /// table of contents.
74 pub struct MarkdownWithToc<'a>(pub &'a str);
75 /// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
76 pub struct MarkdownHtml<'a>(pub &'a str);
78 /// Returns Some(code) if `s` is a line that should be stripped from
79 /// documentation but used in example code. `code` is the portion of
80 /// `s` that should be used in tests. (None for lines that should be
82 fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
83 let trimmed = s.trim();
86 } else if trimmed.starts_with("# ") {
93 /// Returns a new string with all consecutive whitespace collapsed into
96 /// Any leading or trailing whitespace will be trimmed.
97 fn collapse_whitespace(s: &str) -> String {
98 s.split_whitespace().collect::<Vec<_>>().join(" ")
101 // Information about the playground if a URL has been specified, containing an
102 // optional crate name and the URL.
103 thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> = {
107 macro_rules! event_loop_break {
108 ($parser:expr, $toc_builder:expr, $shorter:expr, $buf:expr, $escape:expr, $id:expr,
109 $($end_event:pat)|*) => {{
110 fn inner(id: &mut Option<&mut String>, s: &str) {
111 if let Some(ref mut id) = *id {
115 while let Some(event) = $parser.next() {
117 $($end_event)|* => break,
118 Event::Text(ref s) => {
121 $buf.push_str(&format!("{}", Escape(s)));
126 Event::SoftBreak | Event::HardBreak if !$buf.is_empty() => {
130 looper($parser, &mut $buf, Some(x), $toc_builder, $shorter, $id);
137 pub fn render(w: &mut fmt::Formatter,
140 shorter: MarkdownOutputStyle) -> fmt::Result {
141 fn block(parser: &mut Parser, buffer: &mut String, lang: &str) {
142 let mut origtext = String::new();
143 while let Some(event) = parser.next() {
145 Event::End(Tag::CodeBlock(_)) => break,
146 Event::Text(ref s) => {
147 origtext.push_str(s);
152 let origtext = origtext.trim_left();
153 debug!("docblock: ==============\n{:?}\n=======", origtext);
155 let lines = origtext.lines().filter(|l| {
156 stripped_filtered_line(*l).is_none()
158 let text = lines.collect::<Vec<&str>>().join("\n");
159 let block_info = if lang.is_empty() {
160 LangString::all_false()
162 LangString::parse(lang)
164 if !block_info.rust {
165 buffer.push_str(&format!("<pre><code class=\"language-{}\">{}</code></pre>",
169 PLAYGROUND.with(|play| {
170 // insert newline to clearly separate it from the
171 // previous block so we can shorten the html output
173 let playground_button = play.borrow().as_ref().and_then(|&(ref krate, ref url)| {
177 let test = origtext.lines().map(|l| {
178 stripped_filtered_line(l).unwrap_or(l)
179 }).collect::<Vec<&str>>().join("\n");
180 let krate = krate.as_ref().map(|s| &**s);
181 let test = test::maketest(&test, krate, false,
182 &Default::default());
183 let channel = if test.contains("#![feature(") {
184 "&version=nightly"
188 // These characters don't need to be escaped in a URI.
189 // FIXME: use a library function for percent encoding.
190 fn dont_escape(c: u8) -> bool {
191 (b'a' <= c && c <= b'z') ||
192 (b'A' <= c && c <= b'Z') ||
193 (b'0' <= c && c <= b'9') ||
194 c == b'-' || c == b'_' || c == b'.' ||
195 c == b'~' || c == b'!' || c == b'\'' ||
196 c == b'(' || c == b')' || c == b'*'
198 let mut test_escaped = String::new();
199 for b in test.bytes() {
201 test_escaped.push(char::from(b));
203 write!(test_escaped, "%{:02X}", b).unwrap();
207 r#"<a class="test-arrow" target="_blank" href="{}?code={}{}">Run</a>"#,
208 url, test_escaped, channel
211 buffer.push_str(&highlight::render_with_highlighting(
213 Some("rust-example-rendered"),
215 playground_button.as_ref().map(String::as_str)));
219 fn header(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
220 shorter: MarkdownOutputStyle, level: i32) {
221 let mut ret = String::new();
222 let mut id = String::new();
223 event_loop_break!(parser, toc_builder, shorter, ret, true, &mut Some(&mut id),
224 Event::End(Tag::Header(_)));
225 ret = ret.trim_right().to_owned();
227 let id = id.chars().filter_map(|c| {
228 if c.is_alphanumeric() || c == '-' || c == '_' {
230 Some(c.to_ascii_lowercase())
234 } else if c.is_whitespace() && c.is_ascii() {
239 }).collect::<String>();
241 let id = derive_id(id);
243 let sec = toc_builder.as_mut().map_or("".to_owned(), |builder| {
244 format!("{} ", builder.push(level as u32, ret.clone(), id.clone()))
248 buffer.push_str(&format!("<h{lvl} id=\"{id}\" class=\"section-header\">\
249 <a href=\"#{id}\">{sec}{}</a></h{lvl}>",
250 ret, lvl = level, id = id, sec = sec));
253 fn codespan(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
254 shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) {
255 let mut content = String::new();
256 event_loop_break!(parser, toc_builder, shorter, content, false, id, Event::End(Tag::Code));
257 buffer.push_str(&format!("<code>{}</code>",
258 Escape(&collapse_whitespace(content.trim_right()))));
261 fn link(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
262 shorter: MarkdownOutputStyle, url: &str, mut title: String,
263 id: &mut Option<&mut String>) {
264 event_loop_break!(parser, toc_builder, shorter, title, true, id,
265 Event::End(Tag::Link(_, _)));
266 buffer.push_str(&format!("<a href=\"{}\">{}</a>", url, title));
269 fn paragraph(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
270 shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) {
271 let mut content = String::new();
272 event_loop_break!(parser, toc_builder, shorter, content, true, id,
273 Event::End(Tag::Paragraph));
274 buffer.push_str(&format!("<p>{}</p>", content.trim_right()));
277 fn cell(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
278 shorter: MarkdownOutputStyle) {
279 let mut content = String::new();
280 event_loop_break!(parser, toc_builder, shorter, content, true, &mut None,
281 Event::End(Tag::TableHead) |
282 Event::End(Tag::Table(_)) |
283 Event::End(Tag::TableRow) |
284 Event::End(Tag::TableCell));
285 buffer.push_str(&format!("<td>{}</td>", content.trim()));
288 fn row(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
289 shorter: MarkdownOutputStyle) {
290 let mut content = String::new();
291 while let Some(event) = parser.next() {
293 Event::End(Tag::TableHead) |
294 Event::End(Tag::Table(_)) |
295 Event::End(Tag::TableRow) => break,
296 Event::Start(Tag::TableCell) => {
297 cell(parser, &mut content, toc_builder, shorter);
300 looper(parser, &mut content, Some(x), toc_builder, shorter, &mut None);
304 buffer.push_str(&format!("<tr>{}</tr>", content));
307 fn head(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
308 shorter: MarkdownOutputStyle) {
309 let mut content = String::new();
310 while let Some(event) = parser.next() {
312 Event::End(Tag::TableHead) | Event::End(Tag::Table(_)) => break,
313 Event::Start(Tag::TableCell) => {
314 cell(parser, &mut content, toc_builder, shorter);
317 looper(parser, &mut content, Some(x), toc_builder, shorter, &mut None);
321 if !content.is_empty() {
322 buffer.push_str(&format!("<thead><tr>{}</tr></thead>", content.replace("td>", "th>")));
326 fn table(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
327 shorter: MarkdownOutputStyle) {
328 let mut content = String::new();
329 let mut rows = String::new();
330 while let Some(event) = parser.next() {
332 Event::End(Tag::Table(_)) => break,
333 Event::Start(Tag::TableHead) => {
334 head(parser, &mut content, toc_builder, shorter);
336 Event::Start(Tag::TableRow) => {
337 row(parser, &mut rows, toc_builder, shorter);
342 buffer.push_str(&format!("<table>{}{}</table>",
344 if shorter.is_compact() || rows.is_empty() {
347 format!("<tbody>{}</tbody>", rows)
351 fn blockquote(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
352 shorter: MarkdownOutputStyle) {
353 let mut content = String::new();
354 event_loop_break!(parser, toc_builder, shorter, content, true, &mut None,
355 Event::End(Tag::BlockQuote));
356 buffer.push_str(&format!("<blockquote>{}</blockquote>", content.trim_right()));
359 fn list_item(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
360 shorter: MarkdownOutputStyle) {
361 let mut content = String::new();
362 while let Some(event) = parser.next() {
364 Event::End(Tag::Item) => break,
365 Event::Text(ref s) => {
366 content.push_str(&format!("{}", Escape(s)));
369 looper(parser, &mut content, Some(x), toc_builder, shorter, &mut None);
373 buffer.push_str(&format!("<li>{}</li>", content));
376 fn list(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
377 shorter: MarkdownOutputStyle) {
378 let mut content = String::new();
379 while let Some(event) = parser.next() {
381 Event::End(Tag::List(_)) => break,
382 Event::Start(Tag::Item) => {
383 list_item(parser, &mut content, toc_builder, shorter);
386 looper(parser, &mut content, Some(x), toc_builder, shorter, &mut None);
390 buffer.push_str(&format!("<ul>{}</ul>", content));
393 fn emphasis(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
394 shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) {
395 let mut content = String::new();
396 event_loop_break!(parser, toc_builder, shorter, content, false, id,
397 Event::End(Tag::Emphasis));
398 buffer.push_str(&format!("<em>{}</em>", content));
401 fn strong(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
402 shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) {
403 let mut content = String::new();
404 event_loop_break!(parser, toc_builder, shorter, content, false, id,
405 Event::End(Tag::Strong));
406 buffer.push_str(&format!("<strong>{}</strong>", content));
409 fn looper<'a>(parser: &'a mut Parser, buffer: &mut String, next_event: Option<Event<'a>>,
410 toc_builder: &mut Option<TocBuilder>, shorter: MarkdownOutputStyle,
411 id: &mut Option<&mut String>) -> bool {
412 if let Some(event) = next_event {
414 Event::Start(Tag::CodeBlock(lang)) => {
415 block(parser, buffer, &*lang);
417 Event::Start(Tag::Header(level)) => {
418 header(parser, buffer, toc_builder, shorter, level);
420 Event::Start(Tag::Code) => {
421 codespan(parser, buffer, toc_builder, shorter, id);
423 Event::Start(Tag::Paragraph) => {
424 paragraph(parser, buffer, toc_builder, shorter, id);
426 Event::Start(Tag::Link(ref url, ref t)) => {
427 link(parser, buffer, toc_builder, shorter, url, t.as_ref().to_owned(), id);
429 Event::Start(Tag::Table(_)) => {
430 table(parser, buffer, toc_builder, shorter);
432 Event::Start(Tag::BlockQuote) => {
433 blockquote(parser, buffer, toc_builder, shorter);
435 Event::Start(Tag::List(_)) => {
436 list(parser, buffer, toc_builder, shorter);
438 Event::Start(Tag::Emphasis) => {
439 emphasis(parser, buffer, toc_builder, shorter, id);
441 Event::Start(Tag::Strong) => {
442 strong(parser, buffer, toc_builder, shorter, id);
444 Event::Html(h) | Event::InlineHtml(h) => {
445 buffer.push_str(&*h);
455 let mut toc_builder = if print_toc {
456 Some(TocBuilder::new())
460 let mut buffer = String::new();
461 let mut parser = Parser::new_ext(s, pulldown_cmark::OPTION_ENABLE_TABLES);
463 let next_event = parser.next();
464 if !looper(&mut parser, &mut buffer, next_event, &mut toc_builder, shorter, &mut None) {
468 let mut ret = toc_builder.map_or(Ok(()), |builder| {
469 write!(w, "<nav id=\"TOC\">{}</nav>", builder.into_toc())
473 ret = w.write_str(&buffer);
478 pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
479 tests.set_position(position);
481 let mut parser = Parser::new(doc);
482 let mut prev_offset = 0;
483 let mut nb_lines = 0;
484 let mut register_header = None;
485 'main: while let Some(event) = parser.next() {
487 Event::Start(Tag::CodeBlock(s)) => {
488 let block_info = if s.is_empty() {
489 LangString::all_false()
491 LangString::parse(&*s)
493 if !block_info.rust {
496 let mut test_s = String::new();
497 let mut offset = None;
499 let event = parser.next();
500 if let Some(event) = event {
502 Event::End(Tag::CodeBlock(_)) => break,
503 Event::Text(ref s) => {
505 if offset.is_none() {
506 offset = Some(parser.get_offset());
515 let offset = offset.unwrap_or(0);
516 let lines = test_s.lines().map(|l| {
517 stripped_filtered_line(l).unwrap_or(l)
519 let text = lines.collect::<Vec<&str>>().join("\n");
520 nb_lines += doc[prev_offset..offset].lines().count();
521 let line = tests.get_line() + (nb_lines - 1);
522 let filename = tests.get_filename();
523 tests.add_test(text.to_owned(),
524 block_info.should_panic, block_info.no_run,
525 block_info.ignore, block_info.test_harness,
526 block_info.compile_fail, block_info.error_codes,
528 prev_offset = offset;
530 Event::Start(Tag::Header(level)) => {
531 register_header = Some(level as u32);
533 Event::Text(ref s) if register_header.is_some() => {
534 let level = register_header.unwrap();
536 tests.register_header("", level);
538 tests.register_header(s, level);
540 register_header = None;
547 #[derive(Eq, PartialEq, Clone, Debug)]
556 error_codes: Vec<String>,
560 fn all_false() -> LangString {
562 original: String::new(),
566 rust: true, // NB This used to be `notrust = false`
569 error_codes: Vec::new(),
573 fn parse(string: &str) -> LangString {
574 let mut seen_rust_tags = false;
575 let mut seen_other_tags = false;
576 let mut data = LangString::all_false();
577 let mut allow_compile_fail = false;
578 let mut allow_error_code_check = false;
579 if UnstableFeatures::from_environment().is_nightly_build() {
580 allow_compile_fail = true;
581 allow_error_code_check = true;
584 data.original = string.to_owned();
585 let tokens = string.split(|c: char|
586 !(c == '_' || c == '-' || c.is_alphanumeric())
589 for token in tokens {
592 "should_panic" => { data.should_panic = true; seen_rust_tags = true; },
593 "no_run" => { data.no_run = true; seen_rust_tags = true; },
594 "ignore" => { data.ignore = true; seen_rust_tags = true; },
595 "rust" => { data.rust = true; seen_rust_tags = true; },
596 "test_harness" => { data.test_harness = true; seen_rust_tags = true; },
597 "compile_fail" if allow_compile_fail => {
598 data.compile_fail = true;
599 seen_rust_tags = true;
602 x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
603 if let Ok(_) = x[1..].parse::<u32>() {
604 data.error_codes.push(x.to_owned());
605 seen_rust_tags = true;
607 seen_other_tags = true;
610 _ => { seen_other_tags = true }
614 data.rust &= !seen_other_tags || seen_rust_tags;
620 impl<'a> fmt::Display for Markdown<'a> {
621 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
622 let Markdown(md, shorter) = *self;
623 // This is actually common enough to special-case
624 if md.is_empty() { return Ok(()) }
625 render(fmt, md, false, shorter)
629 impl<'a> fmt::Display for MarkdownWithToc<'a> {
630 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
631 let MarkdownWithToc(md) = *self;
632 render(fmt, md, true, MarkdownOutputStyle::Fancy)
636 impl<'a> fmt::Display for MarkdownHtml<'a> {
637 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
638 let MarkdownHtml(md) = *self;
639 // This is actually common enough to special-case
640 if md.is_empty() { return Ok(()) }
641 render(fmt, md, false, MarkdownOutputStyle::Fancy)
645 pub fn plain_summary_line(md: &str) -> String {
646 struct ParserWrapper<'a> {
652 impl<'a> Iterator for ParserWrapper<'a> {
655 fn next(&mut self) -> Option<String> {
656 let next_event = self.inner.next();
657 if next_event.is_none() {
660 let next_event = next_event.unwrap();
661 let (ret, is_in) = match next_event {
662 Event::Start(Tag::Paragraph) => (None, 1),
663 Event::Start(Tag::Link(_, ref t)) if !self.is_first => {
664 (Some(t.as_ref().to_owned()), 1)
666 Event::Start(Tag::Code) => (Some("`".to_owned()), 1),
667 Event::End(Tag::Code) => (Some("`".to_owned()), -1),
668 Event::Start(Tag::Header(_)) => (None, 1),
669 Event::Text(ref s) if self.is_in > 0 => (Some(s.as_ref().to_owned()), 0),
670 Event::End(Tag::Link(_, ref t)) => (Some(t.as_ref().to_owned()), -1),
671 Event::End(Tag::Paragraph) | Event::End(Tag::Header(_)) => (None, -1),
674 if is_in > 0 || (is_in < 0 && self.is_in > 0) {
678 self.is_first = false;
685 let mut s = String::with_capacity(md.len() * 3 / 2);
686 let mut p = ParserWrapper {
687 inner: Parser::new(md),
691 while let Some(t) = p.next() {
701 use super::{LangString, Markdown, MarkdownHtml, MarkdownOutputStyle};
702 use super::plain_summary_line;
703 use html::render::reset_ids;
706 fn test_lang_string_parse() {
708 should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
709 compile_fail: bool, error_codes: Vec<String>) {
710 assert_eq!(LangString::parse(s), LangString {
711 should_panic: should_panic,
715 test_harness: test_harness,
716 compile_fail: compile_fail,
717 error_codes: error_codes,
718 original: s.to_owned(),
722 // marker | should_panic| no_run| ignore| rust | test_harness| compile_fail
724 t("", false, false, false, true, false, false, Vec::new());
725 t("rust", false, false, false, true, false, false, Vec::new());
726 t("sh", false, false, false, false, false, false, Vec::new());
727 t("ignore", false, false, true, true, false, false, Vec::new());
728 t("should_panic", true, false, false, true, false, false, Vec::new());
729 t("no_run", false, true, false, true, false, false, Vec::new());
730 t("test_harness", false, false, false, true, true, false, Vec::new());
731 t("compile_fail", false, true, false, true, false, true, Vec::new());
732 t("{.no_run .example}", false, true, false, true, false, false, Vec::new());
733 t("{.sh .should_panic}", true, false, false, true, false, false, Vec::new());
734 t("{.example .rust}", false, false, false, true, false, false, Vec::new());
735 t("{.test_harness .rust}", false, false, false, true, true, false, Vec::new());
740 let markdown = "# title";
741 format!("{}", Markdown(markdown, MarkdownOutputStyle::Fancy));
747 fn t(input: &str, expect: &str) {
748 let output = format!("{}", Markdown(input, MarkdownOutputStyle::Fancy));
749 assert_eq!(output, expect, "original: {}", input);
753 t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
754 <a href=\"#foo-bar\">Foo bar</a></h1>");
755 t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
756 header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>");
757 t("### **Foo** *bar* baz!?!& -_qux_-%",
758 "<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\
759 <a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
760 <em>bar</em> baz!?!& -<em>qux</em>-%</a></h3>");
761 t("#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
762 "<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
763 <a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> & *bar?!* \
764 <em><code>baz</code></em> ❤ #qux</a></h4>");
768 fn test_header_ids_multiple_blocks() {
769 fn t(input: &str, expect: &str) {
770 let output = format!("{}", Markdown(input, MarkdownOutputStyle::Fancy));
771 assert_eq!(output, expect, "original: {}", input);
775 t("# Example", "<h1 id=\"example\" class=\"section-header\">\
776 <a href=\"#example\">Example</a></h1>");
777 t("# Panics", "<h1 id=\"panics\" class=\"section-header\">\
778 <a href=\"#panics\">Panics</a></h1>");
779 t("# Example", "<h1 id=\"example-1\" class=\"section-header\">\
780 <a href=\"#example-1\">Example</a></h1>");
781 t("# Main", "<h1 id=\"main-1\" class=\"section-header\">\
782 <a href=\"#main-1\">Main</a></h1>");
783 t("# Example", "<h1 id=\"example-2\" class=\"section-header\">\
784 <a href=\"#example-2\">Example</a></h1>");
785 t("# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
786 <a href=\"#panics-1\">Panics</a></h1>");
794 fn test_plain_summary_line() {
795 fn t(input: &str, expect: &str) {
796 let output = plain_summary_line(input);
797 assert_eq!(output, expect, "original: {}", input);
800 t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
801 t("code `let x = i32;` ...", "code `let x = i32;` ...");
802 t("type `Type<'static>` ...", "type `Type<'static>` ...");
803 t("# top header", "top header");
804 t("## header", "header");
808 fn test_markdown_html_escape() {
809 fn t(input: &str, expect: &str) {
810 let output = format!("{}", MarkdownHtml(input));
811 assert_eq!(output, expect, "original: {}", input);
814 t("`Struct<'a, T>`", "<p><code>Struct<'a, T></code></p>");
815 t("Struct<'a, T>", "<p>Struct<'a, T></p>");