Initial implementation of default theme option

This commit is contained in:
Jason Liquorish 2018-10-12 19:57:59 +01:00
parent 0c926b3e88
commit 0c31ab2953
4 changed files with 17 additions and 6 deletions

View File

@ -415,6 +415,8 @@ impl Default for BuildConfig {
pub struct HtmlConfig { pub struct HtmlConfig {
/// The theme directory, if specified. /// The theme directory, if specified.
pub theme: Option<PathBuf>, 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. /// Use "smart quotes" instead of the usual `"` character.
pub curly_quotes: bool, pub curly_quotes: bool,
/// Should mathjax be enabled? /// Should mathjax be enabled?

View File

@ -395,6 +395,12 @@ fn make_data(
data.insert("livereload".to_owned(), json!(livereload)); data.insert("livereload".to_owned(), json!(livereload));
} }
let default_theme = match html_config.default_theme {
Some(ref theme) => theme,
None => "light",
};
data.insert("default_theme".to_owned(), json!(default_theme));
// Add google analytics tag // Add google analytics tag
if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) { if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) {
data.insert("google_analytics".to_owned(), json!(ga)); data.insert("google_analytics".to_owned(), json!(ga));

View File

@ -352,7 +352,7 @@ function playpen_text(playpen) {
var previousTheme; var previousTheme;
try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { } 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) { } try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
@ -364,7 +364,7 @@ function playpen_text(playpen) {
// Set theme // Set theme
var theme; var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 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); set_theme(theme);

View File

@ -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> <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}} {{/if}}
</head> </head>
<body class="light"> <body class="{{ default_theme }}">
<!-- Provide site root to javascript --> <!-- 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 --> <!-- Work around some values being stored in localStorage wrapped in quotes -->
<script type="text/javascript"> <script type="text/javascript">
@ -59,7 +62,7 @@
<script type="text/javascript"> <script type="text/javascript">
var theme; var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 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.body.className = theme;
document.querySelector('html').className = theme + ' js'; document.querySelector('html').className = theme + ' js';
</script> </script>
@ -94,7 +97,7 @@
<i class="fa fa-paint-brush"></i> <i class="fa fa-paint-brush"></i>
</button> </button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> <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="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</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="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="navy">Navy</button></li>