Skip to content

Commit

Permalink
Add --extend-css option to rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 4, 2016
1 parent 57e5d43 commit ab835a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/librustdoc/html/layout.rs
Expand Up @@ -28,11 +28,12 @@ pub struct Page<'a> {
pub ty: &'a str,
pub root_path: &'a str,
pub description: &'a str,
pub keywords: &'a str
pub keywords: &'a str,
}

pub fn render<T: fmt::Display, S: fmt::Display>(
dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T)
dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
css_file_extension: bool)
-> io::Result<()>
{
write!(dst,
Expand All @@ -49,6 +50,7 @@ r##"<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css">
<link rel="stylesheet" type="text/css" href="{root_path}main.css">
{css_extension}
{favicon}
{in_header}
Expand Down Expand Up @@ -141,6 +143,12 @@ r##"<!DOCTYPE html>
<script defer src="{root_path}search-index.js"></script>
</body>
</html>"##,
css_extension = if css_file_extension {
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
root_path = page.root_path)
} else {
"".to_owned()
},
content = *t,
root_path = page.root_path,
ty = page.ty,
Expand Down
32 changes: 27 additions & 5 deletions src/librustdoc/html/render.rs
Expand Up @@ -119,6 +119,9 @@ pub struct SharedContext {
/// The base-URL of the issue tracker for when an item has been tagged with
/// an issue number.
pub issue_tracker_base_url: Option<String>,
/// The given user css file which allow to customize the generated
/// documentation theme.
pub css_file_extension: Option<PathBuf>,
}

/// Indicates where an external crate can be found.
Expand Down Expand Up @@ -411,7 +414,8 @@ pub fn derive_id(candidate: String) -> String {
pub fn run(mut krate: clean::Crate,
external_html: &ExternalHtml,
dst: PathBuf,
passes: HashSet<String>) -> Result<(), Error> {
passes: HashSet<String>,
css_file_extension: Option<PathBuf>) -> Result<(), Error> {
let src_root = match krate.src.parent() {
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
Expand All @@ -429,6 +433,11 @@ pub fn run(mut krate: clean::Crate,
krate: krate.name.clone(),
playground_url: "".to_string(),
},
include_sources: true,
local_sources: HashMap::new(),
render_redirect_pages: false,
issue_tracker_base_url: None,
css_file_extension: css_file_extension,
};

// Crawl the crate attributes looking for attributes which control how we're
Expand Down Expand Up @@ -637,6 +646,7 @@ fn write_shared(cx: &Context,

// Add all the static files. These may already exist, but we just
// overwrite them anyway to make sure that they're fresh and up-to-date.

write(cx.dst.join("jquery.js"),
include_bytes!("static/jquery-2.1.4.min.js"))?;
write(cx.dst.join("main.js"),
Expand All @@ -647,6 +657,17 @@ fn write_shared(cx: &Context,
include_bytes!("static/rustdoc.css"))?;
write(cx.dst.join("main.css"),
include_bytes!("static/styles/main.css"))?;
if let Some(ref css) = cx.css_file_extension {
let mut content = String::new();
let css = css.as_path();
let mut f = try_err!(File::open(css), css);

try_err!(f.read_to_string(&mut content), css);
let css = cx.dst.join("theme.css");
let css = css.as_path();
let mut f = try_err!(File::create(css), css);
try_err!(write!(f, "{}", &content), css);
}
write(cx.dst.join("normalize.css"),
include_bytes!("static/normalize.css"))?;
write(cx.dst.join("FiraSans-Regular.woff"),
Expand Down Expand Up @@ -931,8 +952,9 @@ impl<'a> SourceCollector<'a> {
description: &desc,
keywords: BASIC_KEYWORDS,
};
layout::render(&mut w, &self.scx.layout,
&page, &(""), &Source(contents))?;
layout::render(&mut w, &self.cx.layout,
&page, &(""), &Source(contents),
self.cx.css_file_extension.is_some())?;
w.flush()?;
self.scx.local_sources.insert(p, href);
Ok(())
Expand Down Expand Up @@ -1294,8 +1316,8 @@ impl Context {
if !cx.render_redirect_pages {
layout::render(&mut writer, &cx.shared.layout, &page,
&Sidebar{ cx: cx, item: it },
&Item{ cx: cx, item: it })?;

&Item{ cx: cx, item: it },
cx.css_file_extension.is_some())?;
} else {
let mut url = repeat("../").take(cx.current.len())
.collect::<String>();
Expand Down
16 changes: 14 additions & 2 deletions src/librustdoc/lib.rs
Expand Up @@ -185,7 +185,10 @@ pub fn opts() -> Vec<getopts::OptGroup> {
"FILES"),
optopt("", "markdown-playground-url",
"URL to send code snippets to", "URL"),
optflag("", "markdown-no-toc", "don't include table of contents")
optflag("", "markdown-no-toc", "don't include table of contents"),
optopt("e", "extend-css",
"to redefine some css rules with a given file to generate doc with your \
own theme", "PATH"),
)
}

Expand Down Expand Up @@ -254,8 +257,16 @@ pub fn main_args(args: &[String]) -> isize {
let markdown_input = input.ends_with(".md") || input.ends_with(".markdown");

let output = matches.opt_str("o").map(|s| PathBuf::from(&s));
let css_file_extension = matches.opt_str("e").map(|s| PathBuf::from(&s));
let cfgs = matches.opt_strs("cfg");

if let Some(ref p) = css_file_extension {
if !p.is_file() {
println!("{}", "--extend-css option must take a css file as input");
return 1;
}
}

let external_html = match ExternalHtml::load(
&matches.opt_strs("html-in-header"),
&matches.opt_strs("html-before-content"),
Expand Down Expand Up @@ -291,7 +302,8 @@ pub fn main_args(args: &[String]) -> isize {
Some("html") | None => {
html::render::run(krate, &external_html,
output.unwrap_or(PathBuf::from("doc")),
passes.into_iter().collect())
passes.into_iter().collect(),
css_file_extension)
.expect("failed to generate documentation")
}
Some("json") => {
Expand Down

0 comments on commit ab835a1

Please sign in to comment.