Skip to content

Commit

Permalink
Add themes option
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jan 21, 2018
1 parent 003b2bc commit 9aee164
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/externalfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::str;
use html::markdown::{Markdown, RenderType};

#[derive(Clone)]
pub struct ExternalHtml{
pub struct ExternalHtml {
/// Content that will be included inline in the <head> section of a
/// rendered Markdown file or generated documentation
pub in_header: String,
Expand Down
35 changes: 24 additions & 11 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub struct SharedContext {
/// 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).
pub sort_modules_alphabetically: bool,
/// Additional themes to be added to the generated docs.
pub themes: Vec<PathBuf>,
}

impl SharedContext {
Expand Down Expand Up @@ -500,7 +502,8 @@ pub fn run(mut krate: clean::Crate,
renderinfo: RenderInfo,
render_type: RenderType,
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 {
FileName::Real(ref p) => match p.parent() {
Some(p) => p.to_path_buf(),
Expand All @@ -524,6 +527,7 @@ pub fn run(mut krate: clean::Crate,
markdown_warnings: RefCell::new(vec![]),
created_dirs: RefCell::new(FxHashSet()),
sort_modules_alphabetically,
themes,
};

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

write(cx.dst.join("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();
for entry in try_err!(fs::read_dir(path.clone()), &path) {
let entry = try_err!(entry, &path);

// To avoid "main.css" to be overwritten, we'll first run over the received themes and only
// then we'll run over the "official" styles.
let mut themes: HashSet<String> = HashSet::new();

for entry in &cx.shared.themes {
let mut content = Vec::with_capacity(100000);

let mut f = try_err!(File::open(entry.path()), &entry.path());
try_err!(f.read_to_end(&mut content), &entry.path());
write(cx.dst.join(entry.file_name()), content.as_slice())?;
themes.push(try_none!(
try_none!(entry.path().file_stem(), &entry.path()).to_str(),
&entry.path()).to_owned());
let mut f = try_err!(File::open(&entry), &entry);
try_err!(f.read_to_end(&mut content), &entry);
write(cx.dst.join(try_none!(entry.file_name(), &entry)), content.as_slice())?;
themes.insert(try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry).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();
// 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.
Expand Down
17 changes: 16 additions & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ pub fn opts() -> Vec<RustcOptGroup> {
o.optflag("", "deny-render-differences", "abort doc runs when markdown rendering \
differences are found")
}),
unstable("themes", |o| {
o.optmulti("", "themes",
"additional themes which will be added to the generated docs",
"FILES")
}),
]
}

Expand Down Expand Up @@ -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(
&matches.opt_strs("html-in-header"),
&matches.opt_strs("html-before-content"),
Expand Down Expand Up @@ -413,7 +427,8 @@ pub fn main_args(args: &[String]) -> isize {
renderinfo,
render_type,
sort_modules_alphabetically,
deny_render_differences)
deny_render_differences,
themes)
.expect("failed to generate documentation");
0
}
Expand Down

0 comments on commit 9aee164

Please sign in to comment.