Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sidebar links to be specified for Rustdoc #74226

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ pub struct RenderOptions {
pub extern_html_root_urls: BTreeMap<String, String>,
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
pub resource_suffix: String,
/// Links displayed on the crate root page.
pub crate_root_links: BTreeMap<String, String>,
/// Whether to run the static CSS/JavaScript through a minifier when outputting them. `true` by
/// default.
//
Expand Down Expand Up @@ -270,6 +272,36 @@ impl Options {
/// Parses the given command-line for options. If an error message or other early-return has
/// been printed, returns `Err` with the exit code.
pub fn from_matches(matches: &getopts::Matches) -> Result<Options, i32> {
fn parse_keyval_opt_internal(
matches: &getopts::Matches,
opt_name: &'static str,
empty_err: &'static str,
invalid_form_err: &'static str,
) -> Result<BTreeMap<String, String>, &'static str> {
let mut externs = BTreeMap::new();
for arg in &(matches).opt_strs(opt_name) {
let mut parts = arg.splitn(2, '=');
let name = parts.next().ok_or(empty_err)?;
let url = parts.next().ok_or(invalid_form_err)?;
externs.insert(name.to_string(), url.to_string());
}

Ok(externs)
}
/// Extracts key-value arguments (such as for `--extern-html-root-url`) from `matches` and returns
/// a map of crate names to the given URLs. If an argument was ill-formed, returns an error
/// describing the issue.
macro_rules! parse_keyval_opt {
($matches:expr, $opt_name:literal) => {
parse_keyval_opt_internal(
$matches,
$opt_name,
concat!("--", $opt_name, "must not be empty"),
concat!("--", $opt_name, "must be of the form name=url"),
)
};
}

// Check for unstable options.
nightly_options::check_nightly_options(&matches, &opts());

Expand Down Expand Up @@ -366,7 +398,15 @@ impl Options {
.map(|s| SearchPath::from_cli_opt(s, error_format))
.collect();
let externs = parse_externs(&matches, &debugging_options, error_format);
let extern_html_root_urls = match parse_extern_html_roots(&matches) {
let extern_html_root_urls = match parse_keyval_opt!(&matches, "extern-html-root-url") {
Ok(ex) => ex,
Err(err) => {
diag.struct_err(err).emit();
return Err(1);
}
};

let crate_root_links = match parse_keyval_opt!(&matches, "crate-root-link") {
Ok(ex) => ex,
Err(err) => {
diag.struct_err(err).emit();
Expand Down Expand Up @@ -609,6 +649,7 @@ impl Options {
generate_search_filter,
document_private,
document_hidden,
crate_root_links,
},
output_format,
})
Expand Down Expand Up @@ -654,20 +695,3 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han
}
}
}

/// Extracts `--extern-html-root-url` arguments from `matches` and returns a map of crate names to
/// the given URLs. If an `--extern-html-root-url` argument was ill-formed, returns an error
/// describing the issue.
fn parse_extern_html_roots(
matches: &getopts::Matches,
) -> Result<BTreeMap<String, String>, &'static str> {
let mut externs = BTreeMap::new();
for arg in &matches.opt_strs("extern-html-root-url") {
let mut parts = arg.splitn(2, '=');
let name = parts.next().ok_or("--extern-html-root-url must not be empty")?;
let url = parts.next().ok_or("--extern-html-root-url must be of the form name=url")?;
externs.insert(name.to_string(), url.to_string());
}

Ok(externs)
}
25 changes: 25 additions & 0 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ crate struct SharedContext {
pub edition: Edition,
pub codes: ErrorCodes,
playground: Option<markdown::Playground>,
/// Links displayed on the crate root page.
pub crate_root_links: BTreeMap<String, String>,
}

impl Context {
Expand Down Expand Up @@ -396,6 +398,7 @@ impl FormatRenderer for Context {
resource_suffix,
static_root_path,
generate_search_filter,
crate_root_links,
..
} = options;

Expand Down Expand Up @@ -466,6 +469,7 @@ impl FormatRenderer for Context {
edition,
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
playground,
crate_root_links,
};

// Add the default themes to the `Vec` of stylepaths
Expand Down Expand Up @@ -3895,6 +3899,27 @@ fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer, cache: &Ca
}

write!(buffer, "<div class=\"sidebar-elems\">");

if it.is_crate() && !cx.shared.crate_root_links.is_empty() {
let mut links_html = String::new();
for (name, url) in &cx.shared.crate_root_links {
links_html.push_str(&format!(
"<li><a href='{}'>{}</a></li>",
Escape(url),
Escape(name),
));
}
write!(
buffer,
"<div class='block items'>\
<ul>\
{}\
</ul>\
</div>",
links_html
);
};

if it.is_crate() {
write!(
buffer,
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ fn opts() -> Vec<RustcOptGroup> {
"specified the rustc-like binary to use as the test builder",
)
}),
unstable("crate-root-link", |o| {
o.optmulti("", "crate-root-link", "", "Specifies links to display on the crate root")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optflag and not optmulti to make sure passing it in both cargo doc and RUSTFLAGS is an error.

}),
]
}

Expand Down
1 change: 1 addition & 0 deletions src/stdarch
Submodule stdarch added at 45340c
2 changes: 1 addition & 1 deletion x.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
Copy link
Member

@jyn514 jyn514 Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change. There are systems that do not have python3 installed (#71818 (comment)). If you have trouble locally you can make a symbolic link from python3 to python.

Suggested change
#!/usr/bin/env python3
#!/usr/bin/env python


# This file is only a "symlink" to bootstrap.py, all logic should go there.

Expand Down