Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default theme option #804

Merged
merged 4 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ The following configuration options are available:
- **theme:** mdBook comes with a default theme and all the resource files needed
for it. But if this option is set, mdBook will selectively overwrite the theme
files with the ones found in the specified folder.
- **default-theme:** The theme to be used by default. Defaults to `light`.
- **curly-quotes:** Convert straight quotes to curly quotes, except for those
that occur in code blocks and code spans. Defaults to `false`.
- **google-analytics:** If you use Google Analytics, this option lets you enable
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ impl Default for BuildConfig {
pub struct HtmlConfig {
/// The theme directory, if specified.
pub theme: Option<PathBuf>,
/// The default theme to use, defaults to 'light'
pub default_theme: Option<String>,
/// Use "smart quotes" instead of the usual `"` character.
pub curly_quotes: bool,
/// Should mathjax be enabled?
Expand Down Expand Up @@ -568,6 +570,7 @@ mod tests {

[output.html]
theme = "./themedir"
default-theme = "rust"
curly-quotes = true
google-analytics = "123456"
additional-css = ["./foo/bar/baz.css"]
Expand Down Expand Up @@ -607,6 +610,7 @@ mod tests {
google_analytics: Some(String::from("123456")),
additional_css: vec![PathBuf::from("./foo/bar/baz.css")],
theme: Some(PathBuf::from("./themedir")),
default_theme: Some(String::from("rust")),
playpen: playpen_should_be,
..Default::default()
};
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl HtmlHandlebars {
);
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
handlebars.register_helper("next", Box::new(helpers::navigation::next));
handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option));
}

/// Copy across any additional CSS and JavaScript files which the book
Expand Down Expand Up @@ -395,6 +396,12 @@ fn make_data(
data.insert("livereload".to_owned(), json!(livereload));
}

let default_theme = match html_config.default_theme {
Bassetts marked this conversation as resolved.
Show resolved Hide resolved
Some(ref theme) => theme,
None => "light",
};
data.insert("default_theme".to_owned(), json!(default_theme));

// Add google analytics tag
if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) {
data.insert("google_analytics".to_owned(), json!(ga));
Expand Down
1 change: 1 addition & 0 deletions src/renderer/html_handlebars/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod navigation;
pub mod toc;
pub mod theme;
30 changes: 30 additions & 0 deletions src/renderer/html_handlebars/helpers/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError};

pub fn theme_option(
Bassetts marked this conversation as resolved.
Show resolved Hide resolved
h: &Helper,
_r: &Handlebars,
ctx: &Context,
rc: &mut RenderContext,
out: &mut Output,
) -> Result<(), RenderError> {
trace!("theme_option (handlebars helper)");

let param = h
.param(0)
.and_then(|v| v.value().as_str())
.ok_or(RenderError::new(
"Param 0 with String type is required for theme_option helper.",
))?;

let theme_name = rc
.evaluate_absolute(ctx, "default_theme", true)?
.as_str()
.ok_or_else(|| RenderError::new("Type error for `default_theme`, string expected"))?;

out.write(param)?;
if param.to_lowercase() == theme_name.to_lowercase() {
out.write(" (default)")?;
}

Ok(())
}
4 changes: 2 additions & 2 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function playpen_text(playpen) {

var previousTheme;
try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { }
if (previousTheme === null || previousTheme === undefined) { previousTheme = 'light'; }
if (previousTheme === null || previousTheme === undefined) { previousTheme = default_theme; }

try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }

Expand All @@ -364,7 +364,7 @@ function playpen_text(playpen) {
// Set theme
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = 'light'; }
if (theme === null || theme === undefined) { theme = default_theme; }

set_theme(theme);

Expand Down
19 changes: 11 additions & 8 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{/if}}
</head>
<body class="light">
<body class="{{ default_theme }}">
<!-- Provide site root to javascript -->
<script type="text/javascript">var path_to_root = "{{ path_to_root }}";</script>
<script type="text/javascript">
var path_to_root = "{{ path_to_root }}";
var default_theme = "{{ default_theme }}";
</script>

<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script type="text/javascript">
Expand All @@ -59,7 +62,7 @@
<script type="text/javascript">
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = 'light'; }
if (theme === null || theme === undefined) { theme = default_theme; }
document.body.className = theme;
document.querySelector('html').className = theme + ' js';
</script>
Expand Down Expand Up @@ -94,11 +97,11 @@
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">{{ theme_option "Light" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">{{ theme_option "Rust" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">{{ theme_option "Coal" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">{{ theme_option "Navy" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">{{ theme_option "Ayu" }}</button></li>
</ul>
{{#if search_enabled}}
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
Expand Down