Add themes option
This commit is contained in:
parent
003b2bc1c6
commit
9aee164a8e
3 changed files with 41 additions and 13 deletions
|
@ -132,6 +132,8 @@ pub struct SharedContext {
|
||||||
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
|
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
|
||||||
/// should be ordered alphabetically or in order of appearance (in the source code).
|
/// should be ordered alphabetically or in order of appearance (in the source code).
|
||||||
pub sort_modules_alphabetically: bool,
|
pub sort_modules_alphabetically: bool,
|
||||||
|
/// Additional themes to be added to the generated docs.
|
||||||
|
pub themes: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SharedContext {
|
impl SharedContext {
|
||||||
|
@ -500,7 +502,8 @@ pub fn run(mut krate: clean::Crate,
|
||||||
renderinfo: RenderInfo,
|
renderinfo: RenderInfo,
|
||||||
render_type: RenderType,
|
render_type: RenderType,
|
||||||
sort_modules_alphabetically: bool,
|
sort_modules_alphabetically: bool,
|
||||||
deny_render_differences: bool) -> Result<(), Error> {
|
deny_render_differences: bool,
|
||||||
|
themes: Vec<PathBuf>) -> Result<(), Error> {
|
||||||
let src_root = match krate.src {
|
let src_root = match krate.src {
|
||||||
FileName::Real(ref p) => match p.parent() {
|
FileName::Real(ref p) => match p.parent() {
|
||||||
Some(p) => p.to_path_buf(),
|
Some(p) => p.to_path_buf(),
|
||||||
|
@ -524,6 +527,7 @@ pub fn run(mut krate: clean::Crate,
|
||||||
markdown_warnings: RefCell::new(vec![]),
|
markdown_warnings: RefCell::new(vec![]),
|
||||||
created_dirs: RefCell::new(FxHashSet()),
|
created_dirs: RefCell::new(FxHashSet()),
|
||||||
sort_modules_alphabetically,
|
sort_modules_alphabetically,
|
||||||
|
themes,
|
||||||
};
|
};
|
||||||
|
|
||||||
// If user passed in `--playground-url` arg, we fill in crate name here
|
// If user passed in `--playground-url` arg, we fill in crate name here
|
||||||
|
@ -872,19 +876,28 @@ fn write_shared(cx: &Context,
|
||||||
|
|
||||||
write(cx.dst.join("rustdoc.css"),
|
write(cx.dst.join("rustdoc.css"),
|
||||||
include_bytes!("static/rustdoc.css"))?;
|
include_bytes!("static/rustdoc.css"))?;
|
||||||
let path = cx.shared.src_root.join("../librustdoc/html/static/themes");
|
|
||||||
let mut themes: Vec<String> = Vec::new();
|
// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
|
||||||
for entry in try_err!(fs::read_dir(path.clone()), &path) {
|
// then we'll run over the "official" styles.
|
||||||
let entry = try_err!(entry, &path);
|
let mut themes: HashSet<String> = HashSet::new();
|
||||||
|
|
||||||
|
for entry in &cx.shared.themes {
|
||||||
let mut content = Vec::with_capacity(100000);
|
let mut content = Vec::with_capacity(100000);
|
||||||
|
|
||||||
let mut f = try_err!(File::open(entry.path()), &entry.path());
|
let mut f = try_err!(File::open(&entry), &entry);
|
||||||
try_err!(f.read_to_end(&mut content), &entry.path());
|
try_err!(f.read_to_end(&mut content), &entry);
|
||||||
write(cx.dst.join(entry.file_name()), content.as_slice())?;
|
write(cx.dst.join(try_none!(entry.file_name(), &entry)), content.as_slice())?;
|
||||||
themes.push(try_none!(
|
themes.insert(try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry).to_owned());
|
||||||
try_none!(entry.path().file_stem(), &entry.path()).to_str(),
|
|
||||||
&entry.path()).to_owned());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write(cx.dst.join("main.css"),
|
||||||
|
include_bytes!("static/themes/main.css"))?;
|
||||||
|
themes.insert("main".to_owned());
|
||||||
|
write(cx.dst.join("dark.css"),
|
||||||
|
include_bytes!("static/themes/dark.css"))?;
|
||||||
|
themes.insert("dark".to_owned());
|
||||||
|
|
||||||
|
let mut themes: Vec<&String> = themes.iter().collect();
|
||||||
themes.sort();
|
themes.sort();
|
||||||
// To avoid theme switch latencies as much as possible, we put everything theme related
|
// To avoid theme switch latencies as much as possible, we put everything theme related
|
||||||
// at the beginning of the html files into another js file.
|
// at the beginning of the html files into another js file.
|
||||||
|
|
|
@ -264,6 +264,11 @@ pub fn opts() -> Vec<RustcOptGroup> {
|
||||||
o.optflag("", "deny-render-differences", "abort doc runs when markdown rendering \
|
o.optflag("", "deny-render-differences", "abort doc runs when markdown rendering \
|
||||||
differences are found")
|
differences are found")
|
||||||
}),
|
}),
|
||||||
|
unstable("themes", |o| {
|
||||||
|
o.optmulti("", "themes",
|
||||||
|
"additional themes which will be added to the generated docs",
|
||||||
|
"FILES")
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,6 +370,15 @@ pub fn main_args(args: &[String]) -> isize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut themes = Vec::new();
|
||||||
|
for theme in matches.opt_strs("themes").iter().map(|s| PathBuf::from(&s)) {
|
||||||
|
if !theme.is_file() {
|
||||||
|
eprintln!("rustdoc: option --themes arguments must all be files");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
themes.push(theme);
|
||||||
|
}
|
||||||
|
|
||||||
let external_html = match ExternalHtml::load(
|
let external_html = match ExternalHtml::load(
|
||||||
&matches.opt_strs("html-in-header"),
|
&matches.opt_strs("html-in-header"),
|
||||||
&matches.opt_strs("html-before-content"),
|
&matches.opt_strs("html-before-content"),
|
||||||
|
@ -413,7 +427,8 @@ pub fn main_args(args: &[String]) -> isize {
|
||||||
renderinfo,
|
renderinfo,
|
||||||
render_type,
|
render_type,
|
||||||
sort_modules_alphabetically,
|
sort_modules_alphabetically,
|
||||||
deny_render_differences)
|
deny_render_differences,
|
||||||
|
themes)
|
||||||
.expect("failed to generate documentation");
|
.expect("failed to generate documentation");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue