about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2018-06-28 01:55:43 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2018-06-28 01:55:43 +0200
commit6a86ee73285c6a522dce0da5fee3ed4681501b21 (patch)
treeae647bd0fb853e4388e8f6c627284664047d548e
parented0350e94569d74bdfe0e612fedfd5ab173f5242 (diff)
downloadrust-6a86ee73285c6a522dce0da5fee3ed4681501b21.tar.gz
rust-6a86ee73285c6a522dce0da5fee3ed4681501b21.zip
Prevent some markdown transformation on short docblocks
-rw-r--r--src/librustdoc/html/markdown.rs43
-rw-r--r--src/test/rustdoc/short-dockblock.rs35
2 files changed, 71 insertions, 7 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 7088104cd7a..889fa82935c 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -339,6 +339,20 @@ impl<'a, I: Iterator<Item = Event<'a>>> SummaryLine<'a, I> {
     }
 }
 
+fn check_if_allowed_tag(t: &Tag) -> bool {
+    match *t {
+        Tag::Paragraph
+        | Tag::CodeBlock(_)
+        | Tag::Item
+        | Tag::Emphasis
+        | Tag::Strong
+        | Tag::Code
+        | Tag::Link(_, _)
+        | Tag::BlockQuote => true,
+        _ => false,
+    }
+}
+
 impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
     type Item = Event<'a>;
 
@@ -350,12 +364,28 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
             self.started = true;
         }
         let event = self.inner.next();
-        match event {
-            Some(Event::Start(..)) => self.depth += 1,
-            Some(Event::End(..)) => self.depth -= 1,
-            _ => {}
+        let mut is_start = true;
+        let is_allowed_tag = match event {
+            Some(Event::Start(ref c)) => {
+                self.depth += 1;
+                check_if_allowed_tag(c)
+            }
+            Some(Event::End(ref c)) => {
+                self.depth -= 1;
+                is_start = false;
+                check_if_allowed_tag(c)
+            }
+            _ => true,
+        };
+        if is_allowed_tag == false {
+            if is_start {
+                Some(Event::Start(Tag::Paragraph))
+            } else {
+                Some(Event::End(Tag::Paragraph))
+            }
+        } else {
+            event
         }
-        event
     }
 }
 
@@ -688,8 +718,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
             }
         };
 
-        let p = Parser::new_with_broken_link_callback(md, Options::empty(),
-                                                      Some(&replacer));
+        let p = Parser::new_with_broken_link_callback(md, Options::empty(), Some(&replacer));
 
         let mut s = String::new();
 
diff --git a/src/test/rustdoc/short-dockblock.rs b/src/test/rustdoc/short-dockblock.rs
new file mode 100644
index 00000000000..cb36110b291
--- /dev/null
+++ b/src/test/rustdoc/short-dockblock.rs
@@ -0,0 +1,35 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_name = "foo"]
+
+// @has foo/index.html '//*[@class="docblock-short"]/p' 'fooo'
+// @!has foo/index.html '//*[@class="docblock-short"]/p/h1' 'fooo'
+// @has foo/fn.foo.html '//h1[@id="fooo"]/a[@href="#fooo"]' 'fooo'
+
+/// # fooo
+///
+/// foo
+pub fn foo() {}
+
+// @has foo/index.html '//*[@class="docblock-short"]/p' 'mooood'
+// @!has foo/index.html '//*[@class="docblock-short"]/p/h2' 'mooood'
+// @has foo/foo/index.html '//h2[@id="mooood"]/a[@href="#mooood"]' 'mooood'
+
+/// ## mooood
+///
+/// foo mod
+pub mod foo {}
+
+// @has foo/index.html '//*[@class="docblock-short"]/p/a[@href=\
+//                      "https://nougat.world"]/code' 'nougat'
+
+/// [`nougat`](https://nougat.world)
+pub struct Bar;