Skip to content

Commit 9aee164

Browse files
Add themes option
1 parent 003b2bc commit 9aee164

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/librustdoc/externalfiles.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::str;
1414
use html::markdown::{Markdown, RenderType};
1515

1616
#[derive(Clone)]
17-
pub struct ExternalHtml{
17+
pub struct ExternalHtml {
1818
/// Content that will be included inline in the <head> section of a
1919
/// rendered Markdown file or generated documentation
2020
pub in_header: String,

src/librustdoc/html/render.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ pub struct SharedContext {
132132
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
133133
/// should be ordered alphabetically or in order of appearance (in the source code).
134134
pub sort_modules_alphabetically: bool,
135+
/// Additional themes to be added to the generated docs.
136+
pub themes: Vec<PathBuf>,
135137
}
136138

137139
impl SharedContext {
@@ -500,7 +502,8 @@ pub fn run(mut krate: clean::Crate,
500502
renderinfo: RenderInfo,
501503
render_type: RenderType,
502504
sort_modules_alphabetically: bool,
503-
deny_render_differences: bool) -> Result<(), Error> {
505+
deny_render_differences: bool,
506+
themes: Vec<PathBuf>) -> Result<(), Error> {
504507
let src_root = match krate.src {
505508
FileName::Real(ref p) => match p.parent() {
506509
Some(p) => p.to_path_buf(),
@@ -524,6 +527,7 @@ pub fn run(mut krate: clean::Crate,
524527
markdown_warnings: RefCell::new(vec![]),
525528
created_dirs: RefCell::new(FxHashSet()),
526529
sort_modules_alphabetically,
530+
themes,
527531
};
528532

529533
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -872,19 +876,28 @@ fn write_shared(cx: &Context,
872876

873877
write(cx.dst.join("rustdoc.css"),
874878
include_bytes!("static/rustdoc.css"))?;
875-
let path = cx.shared.src_root.join("../librustdoc/html/static/themes");
876-
let mut themes: Vec<String> = Vec::new();
877-
for entry in try_err!(fs::read_dir(path.clone()), &path) {
878-
let entry = try_err!(entry, &path);
879+
880+
// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
881+
// then we'll run over the "official" styles.
882+
let mut themes: HashSet<String> = HashSet::new();
883+
884+
for entry in &cx.shared.themes {
879885
let mut content = Vec::with_capacity(100000);
880886

881-
let mut f = try_err!(File::open(entry.path()), &entry.path());
882-
try_err!(f.read_to_end(&mut content), &entry.path());
883-
write(cx.dst.join(entry.file_name()), content.as_slice())?;
884-
themes.push(try_none!(
885-
try_none!(entry.path().file_stem(), &entry.path()).to_str(),
886-
&entry.path()).to_owned());
887+
let mut f = try_err!(File::open(&entry), &entry);
888+
try_err!(f.read_to_end(&mut content), &entry);
889+
write(cx.dst.join(try_none!(entry.file_name(), &entry)), content.as_slice())?;
890+
themes.insert(try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry).to_owned());
887891
}
892+
893+
write(cx.dst.join("main.css"),
894+
include_bytes!("static/themes/main.css"))?;
895+
themes.insert("main".to_owned());
896+
write(cx.dst.join("dark.css"),
897+
include_bytes!("static/themes/dark.css"))?;
898+
themes.insert("dark".to_owned());
899+
900+
let mut themes: Vec<&String> = themes.iter().collect();
888901
themes.sort();
889902
// To avoid theme switch latencies as much as possible, we put everything theme related
890903
// at the beginning of the html files into another js file.

src/librustdoc/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ pub fn opts() -> Vec<RustcOptGroup> {
264264
o.optflag("", "deny-render-differences", "abort doc runs when markdown rendering \
265265
differences are found")
266266
}),
267+
unstable("themes", |o| {
268+
o.optmulti("", "themes",
269+
"additional themes which will be added to the generated docs",
270+
"FILES")
271+
}),
267272
]
268273
}
269274

@@ -365,6 +370,15 @@ pub fn main_args(args: &[String]) -> isize {
365370
}
366371
}
367372

373+
let mut themes = Vec::new();
374+
for theme in matches.opt_strs("themes").iter().map(|s| PathBuf::from(&s)) {
375+
if !theme.is_file() {
376+
eprintln!("rustdoc: option --themes arguments must all be files");
377+
return 1;
378+
}
379+
themes.push(theme);
380+
}
381+
368382
let external_html = match ExternalHtml::load(
369383
&matches.opt_strs("html-in-header"),
370384
&matches.opt_strs("html-before-content"),
@@ -413,7 +427,8 @@ pub fn main_args(args: &[String]) -> isize {
413427
renderinfo,
414428
render_type,
415429
sort_modules_alphabetically,
416-
deny_render_differences)
430+
deny_render_differences,
431+
themes)
417432
.expect("failed to generate documentation");
418433
0
419434
}

0 commit comments

Comments
 (0)