Skip to content

Commit a8403e1

Browse files
committed
Auto merge of #51861 - GuillaumeGomez:prevent-some-markdown-short-doc, r=QuietMisdreavus
Prevent some markdown transformation on short docblocks Before: <img width="1440" alt="screen shot 2018-06-28 at 01 46 01" src="https://user-images.githubusercontent.com/3050060/42005762-7d533bbe-7a76-11e8-8361-027886803399.png"> After: <img width="1440" alt="screen shot 2018-06-28 at 01 46 02" src="https://user-images.githubusercontent.com/3050060/42005768-81bd59a0-7a76-11e8-819b-9b4be72579d6.png"> This is only performed on short doc blocks, not on plain ones. Not all transformations are prevented (you still have a few like urls, code blocks, etc...). cc @nical r? @QuietMisdreavus
2 parents 00f4972 + 6a86ee7 commit a8403e1

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

src/librustdoc/html/markdown.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ impl<'a, I: Iterator<Item = Event<'a>>> SummaryLine<'a, I> {
339339
}
340340
}
341341

342+
fn check_if_allowed_tag(t: &Tag) -> bool {
343+
match *t {
344+
Tag::Paragraph
345+
| Tag::CodeBlock(_)
346+
| Tag::Item
347+
| Tag::Emphasis
348+
| Tag::Strong
349+
| Tag::Code
350+
| Tag::Link(_, _)
351+
| Tag::BlockQuote => true,
352+
_ => false,
353+
}
354+
}
355+
342356
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
343357
type Item = Event<'a>;
344358

@@ -350,12 +364,28 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
350364
self.started = true;
351365
}
352366
let event = self.inner.next();
353-
match event {
354-
Some(Event::Start(..)) => self.depth += 1,
355-
Some(Event::End(..)) => self.depth -= 1,
356-
_ => {}
367+
let mut is_start = true;
368+
let is_allowed_tag = match event {
369+
Some(Event::Start(ref c)) => {
370+
self.depth += 1;
371+
check_if_allowed_tag(c)
372+
}
373+
Some(Event::End(ref c)) => {
374+
self.depth -= 1;
375+
is_start = false;
376+
check_if_allowed_tag(c)
377+
}
378+
_ => true,
379+
};
380+
if is_allowed_tag == false {
381+
if is_start {
382+
Some(Event::Start(Tag::Paragraph))
383+
} else {
384+
Some(Event::End(Tag::Paragraph))
385+
}
386+
} else {
387+
event
357388
}
358-
event
359389
}
360390
}
361391

@@ -688,8 +718,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
688718
}
689719
};
690720

691-
let p = Parser::new_with_broken_link_callback(md, Options::empty(),
692-
Some(&replacer));
721+
let p = Parser::new_with_broken_link_callback(md, Options::empty(), Some(&replacer));
693722

694723
let mut s = String::new();
695724

src/test/rustdoc/short-dockblock.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 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+
#![crate_name = "foo"]
12+
13+
// @has foo/index.html '//*[@class="docblock-short"]/p' 'fooo'
14+
// @!has foo/index.html '//*[@class="docblock-short"]/p/h1' 'fooo'
15+
// @has foo/fn.foo.html '//h1[@id="fooo"]/a[@href="#fooo"]' 'fooo'
16+
17+
/// # fooo
18+
///
19+
/// foo
20+
pub fn foo() {}
21+
22+
// @has foo/index.html '//*[@class="docblock-short"]/p' 'mooood'
23+
// @!has foo/index.html '//*[@class="docblock-short"]/p/h2' 'mooood'
24+
// @has foo/foo/index.html '//h2[@id="mooood"]/a[@href="#mooood"]' 'mooood'
25+
26+
/// ## mooood
27+
///
28+
/// foo mod
29+
pub mod foo {}
30+
31+
// @has foo/index.html '//*[@class="docblock-short"]/p/a[@href=\
32+
// "https://nougat.world"]/code' 'nougat'
33+
34+
/// [`nougat`](https://nougat.world)
35+
pub struct Bar;

0 commit comments

Comments
 (0)