]> git.proxmox.com Git - rustc.git/blob - src/vendor/mdbook/src/theme/mod.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / mdbook / src / theme / mod.rs
1 use std::path::Path;
2 use std::fs::File;
3 use std::io::Read;
4
5
6 pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
7 pub static CSS: &'static [u8] = include_bytes!("book.css");
8 pub static FAVICON: &'static [u8] = include_bytes!("favicon.png");
9 pub static JS: &'static [u8] = include_bytes!("book.js");
10 pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js");
11 pub static TOMORROW_NIGHT_CSS: &'static [u8] = include_bytes!("tomorrow-night.css");
12 pub static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css");
13 pub static JQUERY: &'static [u8] = include_bytes!("jquery-2.1.4.min.js");
14 pub static FONT_AWESOME: &'static [u8] = include_bytes!("_FontAwesome/css/font-awesome.min.css");
15 pub static FONT_AWESOME_EOT: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.eot");
16 pub static FONT_AWESOME_SVG: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.svg");
17 pub static FONT_AWESOME_TTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.ttf");
18 pub static FONT_AWESOME_WOFF: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.woff");
19 pub static FONT_AWESOME_WOFF2: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.woff2");
20 pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/FontAwesome.otf");
21
22 /// The `Theme` struct should be used instead of the static variables because the `new()` method
23 /// will look if the user has a theme directory in his source folder and use the users theme instead
24 /// of the default.
25 ///
26 /// You should exceptionnaly use the static variables only if you need the default theme even if the
27 /// user has specified another theme.
28 pub struct Theme {
29 pub index: Vec<u8>,
30 pub css: Vec<u8>,
31 pub favicon: Vec<u8>,
32 pub js: Vec<u8>,
33 pub highlight_css: Vec<u8>,
34 pub tomorrow_night_css: Vec<u8>,
35 pub highlight_js: Vec<u8>,
36 pub jquery: Vec<u8>,
37 }
38
39 impl Theme {
40 pub fn new(src: &Path) -> Self {
41
42 // Default theme
43 let mut theme = Theme {
44 index: INDEX.to_owned(),
45 css: CSS.to_owned(),
46 favicon: FAVICON.to_owned(),
47 js: JS.to_owned(),
48 highlight_css: HIGHLIGHT_CSS.to_owned(),
49 tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(),
50 highlight_js: HIGHLIGHT_JS.to_owned(),
51 jquery: JQUERY.to_owned(),
52 };
53
54 // Check if the given path exists
55 if !src.exists() || !src.is_dir() {
56 return theme;
57 }
58
59 // Check for individual files if they exist
60
61 // index.hbs
62 if let Ok(mut f) = File::open(&src.join("index.hbs")) {
63 theme.index.clear(); // Reset the value, because read_to_string appends...
64 let _ = f.read_to_end(&mut theme.index);
65 }
66
67 // book.js
68 if let Ok(mut f) = File::open(&src.join("book.js")) {
69 theme.js.clear();
70 let _ = f.read_to_end(&mut theme.js);
71 }
72
73 // book.css
74 if let Ok(mut f) = File::open(&src.join("book.css")) {
75 theme.css.clear();
76 let _ = f.read_to_end(&mut theme.css);
77 }
78
79 // favicon.png
80 if let Ok(mut f) = File::open(&src.join("favicon.png")) {
81 theme.favicon.clear();
82 let _ = f.read_to_end(&mut theme.favicon);
83 }
84
85 // highlight.js
86 if let Ok(mut f) = File::open(&src.join("highlight.js")) {
87 theme.highlight_js.clear();
88 let _ = f.read_to_end(&mut theme.highlight_js);
89 }
90
91 // highlight.css
92 if let Ok(mut f) = File::open(&src.join("highlight.css")) {
93 theme.highlight_css.clear();
94 let _ = f.read_to_end(&mut theme.highlight_css);
95 }
96
97 // tomorrow-night.css
98 if let Ok(mut f) = File::open(&src.join("tomorrow-night.css")) {
99 theme.tomorrow_night_css.clear();
100 let _ = f.read_to_end(&mut theme.tomorrow_night_css);
101 }
102
103 theme
104 }
105 }