Skip to content

Commit 0909f7b

Browse files
Implement --source-code-external-url option
1 parent 6af4fd3 commit 0909f7b

File tree

4 files changed

+84
-42
lines changed

4 files changed

+84
-42
lines changed

src/librustdoc/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ pub struct RenderOptions {
220220
pub generate_search_filter: bool,
221221
/// Option (disabled by default) to generate files used by RLS and some other tools.
222222
pub generate_redirect_pages: bool,
223+
/// Base URL to use for source code linking.
224+
pub source_code_external_url: Option<String>,
223225
}
224226

225227
impl Options {
@@ -496,6 +498,21 @@ impl Options {
496498
let enable_per_target_ignores = matches.opt_present("enable-per-target-ignores");
497499
let document_private = matches.opt_present("document-private-items");
498500
let document_hidden = matches.opt_present("document-hidden-items");
501+
let source_code_external_url = matches.opt_str("source-code-external-url").map(|mut h| {
502+
if !h.ends_with('/') {
503+
h.push('/');
504+
}
505+
h
506+
});
507+
if let Some(ref source_code_external_url) = source_code_external_url {
508+
if !source_code_external_url.starts_with("http://")
509+
&& !source_code_external_url.starts_with("https://")
510+
{
511+
diag.struct_err("option `--source-code-external-url` argument must be an URL")
512+
.emit();
513+
return Err(1);
514+
}
515+
}
499516

500517
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
501518

@@ -552,6 +569,7 @@ impl Options {
552569
markdown_playground_url,
553570
generate_search_filter,
554571
generate_redirect_pages,
572+
source_code_external_url,
555573
},
556574
})
557575
}

src/librustdoc/html/render.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ crate struct SharedContext {
200200
/// The default edition used to parse doctests.
201201
pub edition: Edition,
202202
pub codes: ErrorCodes,
203+
pub source_code_external_url: Option<String>,
203204
playground: Option<markdown::Playground>,
204205
}
205206

@@ -409,6 +410,7 @@ pub fn run(
409410
static_root_path,
410411
generate_search_filter,
411412
generate_redirect_pages,
413+
source_code_external_url,
412414
..
413415
} = options;
414416

@@ -467,6 +469,7 @@ pub fn run(
467469
collapsed: krate.collapsed,
468470
src_root,
469471
include_sources,
472+
source_code_external_url,
470473
local_sources: Default::default(),
471474
issue_tracker_base_url,
472475
layout,
@@ -1602,13 +1605,23 @@ impl Context {
16021605
} else {
16031606
format!("{}-{}", item.source.loline, item.source.hiline)
16041607
};
1605-
Some(format!(
1606-
"{root}src/{krate}/{path}#{lines}",
1607-
root = Escape(&root),
1608-
krate = krate,
1609-
path = path,
1610-
lines = lines
1611-
))
1608+
if let Some(ref source_code_external_url) = self.shared.source_code_external_url {
1609+
Some(format!(
1610+
"{root}{krate}/{path}#{lines}",
1611+
root = source_code_external_url,
1612+
krate = krate,
1613+
path = path,
1614+
lines = lines
1615+
))
1616+
} else {
1617+
Some(format!(
1618+
"{root}src/{krate}/{path}#{lines}",
1619+
root = Escape(&root),
1620+
krate = krate,
1621+
path = path,
1622+
lines = lines
1623+
))
1624+
}
16121625
}
16131626
}
16141627

src/librustdoc/html/sources.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ impl<'a> SourceCollector<'a> {
7474
return Ok(());
7575
}
7676

77-
let contents = match fs::read_to_string(&p) {
78-
Ok(contents) => contents,
79-
Err(e) => {
80-
return Err(Error::new(e, &p));
81-
}
82-
};
83-
84-
// Remove the utf-8 BOM if any
85-
let contents =
86-
if contents.starts_with("\u{feff}") { &contents[3..] } else { &contents[..] };
87-
8877
// Create the intermediate directories
8978
let mut cur = self.dst.clone();
9079
let mut root_path = String::from("../../");
@@ -101,30 +90,44 @@ impl<'a> SourceCollector<'a> {
10190
cur.push(&fname);
10291
href.push_str(&fname.to_string_lossy());
10392

104-
let title = format!(
105-
"{} -- source",
106-
cur.file_name().expect("failed to get file name").to_string_lossy()
107-
);
108-
let desc = format!("Source to the Rust file `{}`.", filename);
109-
let page = layout::Page {
110-
title: &title,
111-
css_class: "source",
112-
root_path: &root_path,
113-
static_root_path: self.scx.static_root_path.as_deref(),
114-
description: &desc,
115-
keywords: BASIC_KEYWORDS,
116-
resource_suffix: &self.scx.resource_suffix,
117-
extra_scripts: &[&format!("source-files{}", self.scx.resource_suffix)],
118-
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
119-
};
120-
let v = layout::render(
121-
&self.scx.layout,
122-
&page,
123-
"",
124-
|buf: &mut _| print_src(buf, &contents),
125-
&self.scx.themes,
126-
);
127-
self.scx.fs.write(&cur, v.as_bytes())?;
93+
// we don't emit source if we have an external location for it
94+
if self.scx.source_code_external_url.is_none() {
95+
let contents = match fs::read_to_string(&p) {
96+
Ok(contents) => contents,
97+
Err(e) => {
98+
return Err(Error::new(e, &p));
99+
}
100+
};
101+
102+
// Remove the utf-8 BOM if any
103+
let contents =
104+
if contents.starts_with("\u{feff}") { &contents[3..] } else { &contents[..] };
105+
106+
let title = format!(
107+
"{} -- source",
108+
cur.file_name().expect("failed to get file name").to_string_lossy()
109+
);
110+
let desc = format!("Source to the Rust file `{}`.", filename);
111+
let page = layout::Page {
112+
title: &title,
113+
css_class: "source",
114+
root_path: &root_path,
115+
static_root_path: self.scx.static_root_path.as_deref(),
116+
description: &desc,
117+
keywords: BASIC_KEYWORDS,
118+
resource_suffix: &self.scx.resource_suffix,
119+
extra_scripts: &[&format!("source-files{}", self.scx.resource_suffix)],
120+
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
121+
};
122+
let v = layout::render(
123+
&self.scx.layout,
124+
&page,
125+
"",
126+
|buf: &mut _| print_src(buf, &contents),
127+
&self.scx.themes,
128+
);
129+
self.scx.fs.write(&cur, v.as_bytes())?;
130+
}
128131
self.scx.local_sources.insert(p.clone(), href);
129132
Ok(())
130133
}

src/librustdoc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ fn opts() -> Vec<RustcOptGroup> {
418418
"specified the rustc-like binary to use as the test builder",
419419
)
420420
}),
421+
unstable("source-code-external-url", |o| {
422+
o.optopt(
423+
"",
424+
"source-code-external-url",
425+
"Base URL for external source code linking",
426+
"URL",
427+
)
428+
}),
421429
]
422430
}
423431

0 commit comments

Comments
 (0)