Skip to content

Commit ab835a1

Browse files
Add --extend-css option to rustdoc
1 parent 57e5d43 commit ab835a1

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/librustdoc/html/layout.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ pub struct Page<'a> {
2828
pub ty: &'a str,
2929
pub root_path: &'a str,
3030
pub description: &'a str,
31-
pub keywords: &'a str
31+
pub keywords: &'a str,
3232
}
3333

3434
pub fn render<T: fmt::Display, S: fmt::Display>(
35-
dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T)
35+
dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
36+
css_file_extension: bool)
3637
-> io::Result<()>
3738
{
3839
write!(dst,
@@ -49,6 +50,7 @@ r##"<!DOCTYPE html>
4950
5051
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css">
5152
<link rel="stylesheet" type="text/css" href="{root_path}main.css">
53+
{css_extension}
5254
5355
{favicon}
5456
{in_header}
@@ -141,6 +143,12 @@ r##"<!DOCTYPE html>
141143
<script defer src="{root_path}search-index.js"></script>
142144
</body>
143145
</html>"##,
146+
css_extension = if css_file_extension {
147+
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
148+
root_path = page.root_path)
149+
} else {
150+
"".to_owned()
151+
},
144152
content = *t,
145153
root_path = page.root_path,
146154
ty = page.ty,

src/librustdoc/html/render.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub struct SharedContext {
119119
/// The base-URL of the issue tracker for when an item has been tagged with
120120
/// an issue number.
121121
pub issue_tracker_base_url: Option<String>,
122+
/// The given user css file which allow to customize the generated
123+
/// documentation theme.
124+
pub css_file_extension: Option<PathBuf>,
122125
}
123126

124127
/// Indicates where an external crate can be found.
@@ -411,7 +414,8 @@ pub fn derive_id(candidate: String) -> String {
411414
pub fn run(mut krate: clean::Crate,
412415
external_html: &ExternalHtml,
413416
dst: PathBuf,
414-
passes: HashSet<String>) -> Result<(), Error> {
417+
passes: HashSet<String>,
418+
css_file_extension: Option<PathBuf>) -> Result<(), Error> {
415419
let src_root = match krate.src.parent() {
416420
Some(p) => p.to_path_buf(),
417421
None => PathBuf::new(),
@@ -429,6 +433,11 @@ pub fn run(mut krate: clean::Crate,
429433
krate: krate.name.clone(),
430434
playground_url: "".to_string(),
431435
},
436+
include_sources: true,
437+
local_sources: HashMap::new(),
438+
render_redirect_pages: false,
439+
issue_tracker_base_url: None,
440+
css_file_extension: css_file_extension,
432441
};
433442

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

638647
// Add all the static files. These may already exist, but we just
639648
// overwrite them anyway to make sure that they're fresh and up-to-date.
649+
640650
write(cx.dst.join("jquery.js"),
641651
include_bytes!("static/jquery-2.1.4.min.js"))?;
642652
write(cx.dst.join("main.js"),
@@ -647,6 +657,17 @@ fn write_shared(cx: &Context,
647657
include_bytes!("static/rustdoc.css"))?;
648658
write(cx.dst.join("main.css"),
649659
include_bytes!("static/styles/main.css"))?;
660+
if let Some(ref css) = cx.css_file_extension {
661+
let mut content = String::new();
662+
let css = css.as_path();
663+
let mut f = try_err!(File::open(css), css);
664+
665+
try_err!(f.read_to_string(&mut content), css);
666+
let css = cx.dst.join("theme.css");
667+
let css = css.as_path();
668+
let mut f = try_err!(File::create(css), css);
669+
try_err!(write!(f, "{}", &content), css);
670+
}
650671
write(cx.dst.join("normalize.css"),
651672
include_bytes!("static/normalize.css"))?;
652673
write(cx.dst.join("FiraSans-Regular.woff"),
@@ -931,8 +952,9 @@ impl<'a> SourceCollector<'a> {
931952
description: &desc,
932953
keywords: BASIC_KEYWORDS,
933954
};
934-
layout::render(&mut w, &self.scx.layout,
935-
&page, &(""), &Source(contents))?;
955+
layout::render(&mut w, &self.cx.layout,
956+
&page, &(""), &Source(contents),
957+
self.cx.css_file_extension.is_some())?;
936958
w.flush()?;
937959
self.scx.local_sources.insert(p, href);
938960
Ok(())
@@ -1294,8 +1316,8 @@ impl Context {
12941316
if !cx.render_redirect_pages {
12951317
layout::render(&mut writer, &cx.shared.layout, &page,
12961318
&Sidebar{ cx: cx, item: it },
1297-
&Item{ cx: cx, item: it })?;
1298-
1319+
&Item{ cx: cx, item: it },
1320+
cx.css_file_extension.is_some())?;
12991321
} else {
13001322
let mut url = repeat("../").take(cx.current.len())
13011323
.collect::<String>();

src/librustdoc/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ pub fn opts() -> Vec<getopts::OptGroup> {
185185
"FILES"),
186186
optopt("", "markdown-playground-url",
187187
"URL to send code snippets to", "URL"),
188-
optflag("", "markdown-no-toc", "don't include table of contents")
188+
optflag("", "markdown-no-toc", "don't include table of contents"),
189+
optopt("e", "extend-css",
190+
"to redefine some css rules with a given file to generate doc with your \
191+
own theme", "PATH"),
189192
)
190193
}
191194

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

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

263+
if let Some(ref p) = css_file_extension {
264+
if !p.is_file() {
265+
println!("{}", "--extend-css option must take a css file as input");
266+
return 1;
267+
}
268+
}
269+
259270
let external_html = match ExternalHtml::load(
260271
&matches.opt_strs("html-in-header"),
261272
&matches.opt_strs("html-before-content"),
@@ -291,7 +302,8 @@ pub fn main_args(args: &[String]) -> isize {
291302
Some("html") | None => {
292303
html::render::run(krate, &external_html,
293304
output.unwrap_or(PathBuf::from("doc")),
294-
passes.into_iter().collect())
305+
passes.into_iter().collect(),
306+
css_file_extension)
295307
.expect("failed to generate documentation")
296308
}
297309
Some("json") => {

0 commit comments

Comments
 (0)