Rollup merge of #48335 - Manishearth:shortcut-links, r=QuietMisdreavus

Implement implied shortcut links for intra-rustdoc-links

cc https://github.com/rust-lang/rust/issues/43466

Needs https://github.com/google/pulldown-cmark/pull/126

r? @QuietMisdreavus
This commit is contained in:
Guillaume Gomez 2018-02-21 16:29:52 +01:00 committed by GitHub
commit fe1293f8a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 16 deletions

8
src/Cargo.lock generated
View File

@ -1086,7 +1086,7 @@ dependencies = [
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1383,7 +1383,7 @@ dependencies = [
[[package]] [[package]]
name = "pulldown-cmark" name = "pulldown-cmark"
version = "0.1.0" version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2104,7 +2104,7 @@ dependencies = [
name = "rustdoc" name = "rustdoc"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"pulldown-cmark 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -2886,7 +2886,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a6dda33d67c26f0aac90d324ab2eb7239c819fc7b2552fe9faa4fe88441edc8" "checksum pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a6dda33d67c26f0aac90d324ab2eb7239c819fc7b2552fe9faa4fe88441edc8"
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
"checksum pulldown-cmark 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "378e941dbd392c101f2cb88097fa4d7167bc421d4b88de3ff7dbee503bc3233b" "checksum pulldown-cmark 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "378e941dbd392c101f2cb88097fa4d7167bc421d4b88de3ff7dbee503bc3233b"
"checksum pulldown-cmark 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a656fdb8b6848f896df5e478a0eb9083681663e37dcb77dd16981ff65329fe8b" "checksum pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fdf85cda6cadfae5428a54661d431330b312bc767ddbc57adbedc24da66e32"
"checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4" "checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4"
"checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45" "checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"

View File

@ -10,5 +10,5 @@ path = "lib.rs"
doctest = false doctest = false
[dependencies] [dependencies]
pulldown-cmark = { version = "0.1.0", default-features = false } pulldown-cmark = { version = "0.1.2", default-features = false }
tempdir = "0.3" tempdir = "0.3"

View File

@ -1044,7 +1044,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
/// Resolve a string as a macro /// Resolve a string as a macro
fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> { fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
use syntax::ext::base::MacroKind; use syntax::ext::base::{MacroKind, SyntaxExtension};
use syntax::ext::hygiene::Mark; use syntax::ext::hygiene::Mark;
let segment = ast::PathSegment { let segment = ast::PathSegment {
identifier: ast::Ident::from_str(path_str), identifier: ast::Ident::from_str(path_str),
@ -1061,7 +1061,11 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
let res = resolver let res = resolver
.resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false); .resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
if let Ok(def) = res { if let Ok(def) = res {
Some(def) if let SyntaxExtension::DeclMacro(..) = *resolver.get_macro(def) {
Some(def)
} else {
None
}
} else if let Some(def) = resolver.all_macros.get(&path_str.into()) { } else if let Some(def) = resolver.all_macros.get(&path_str.into()) {
Some(*def) Some(*def)
} else { } else {

View File

@ -591,7 +591,15 @@ impl<'a> fmt::Display for Markdown<'a> {
opts.insert(OPTION_ENABLE_TABLES); opts.insert(OPTION_ENABLE_TABLES);
opts.insert(OPTION_ENABLE_FOOTNOTES); opts.insert(OPTION_ENABLE_FOOTNOTES);
let p = Parser::new_ext(md, opts); let replacer = |_: &str, s: &str| {
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
Some((replace.clone(), s.to_owned()))
} else {
None
}
};
let p = Parser::new_with_broken_link_callback(md, opts, Some(&replacer));
let mut s = String::with_capacity(md.len() * 3 / 2); let mut s = String::with_capacity(md.len() * 3 / 2);
@ -662,7 +670,16 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
// This is actually common enough to special-case // This is actually common enough to special-case
if md.is_empty() { return Ok(()) } if md.is_empty() { return Ok(()) }
let p = Parser::new(md); let replacer = |_: &str, s: &str| {
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
Some((replace.clone(), s.to_owned()))
} else {
None
}
};
let p = Parser::new_with_broken_link_callback(md, Options::empty(),
Some(&replacer));
let mut s = String::new(); let mut s = String::new();
@ -731,18 +748,30 @@ pub fn markdown_links(md: &str) -> Vec<String> {
opts.insert(OPTION_ENABLE_TABLES); opts.insert(OPTION_ENABLE_TABLES);
opts.insert(OPTION_ENABLE_FOOTNOTES); opts.insert(OPTION_ENABLE_FOOTNOTES);
let p = Parser::new_ext(md, opts);
let iter = Footnotes::new(HeadingLinks::new(p, None));
let mut links = vec![]; let mut links = vec![];
let shortcut_links = RefCell::new(vec![]);
for ev in iter { {
if let Event::Start(Tag::Link(dest, _)) = ev { let push = |_: &str, s: &str| {
debug!("found link: {}", dest); shortcut_links.borrow_mut().push(s.to_owned());
links.push(dest.into_owned()); None
};
let p = Parser::new_with_broken_link_callback(md, opts,
Some(&push));
let iter = Footnotes::new(HeadingLinks::new(p, None));
for ev in iter {
if let Event::Start(Tag::Link(dest, _)) = ev {
debug!("found link: {}", dest);
links.push(dest.into_owned());
}
} }
} }
let mut shortcut_links = shortcut_links.into_inner();
links.extend(shortcut_links.drain(..));
links links
} }

View File

@ -77,3 +77,15 @@ pub trait SoAmbiguous {}
#[allow(bad_style)] #[allow(bad_style)]
pub fn SoAmbiguous() {} pub fn SoAmbiguous() {}
// @has - '//a/@href' '../intra_links/struct.ThisType.html'
// @has - '//a/@href' '../intra_links/struct.ThisType.html#method.this_method'
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html'
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html#ThisVariant.v'
/// Shortcut links for:
/// * [`ThisType`]
/// * [`ThisType::this_method`]
/// * [ThisEnum]
/// * [ThisEnum::ThisVariant]
pub struct SomeOtherType;