Skip to content

Support additional themes in config #2594

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ preferred-dark-theme = "navy"
smart-punctuation = true
mathjax-support = false
copy-fonts = true
additional-themes = [{ name = "Midnight Blue", class = "midnight" }]
additional-css = ["custom.css", "custom2.css"]
additional-js = ["custom.js"]
no-section-label = false
Expand Down Expand Up @@ -134,6 +135,11 @@ The following configuration options are available:
create a `theme/fonts/fonts.css` file and store the fonts in the `theme/fonts/` directory.
- **google-analytics:** This field has been deprecated and will be removed in a future release.
Use the `theme/head.hbs` file to add the appropriate Google Analytics code instead.
- **additional-themes:** If you supply user-defined themes in `additional-css`, include them
here so they are listed in the theme dropdown. Each theme is specified as an object with two keys:
- **name:** The human-readable name of the theme.
- **class:** The css class to be applied to the body element while the theme is active.
This should correspond to selectors in one of the `additional-css` sheets.
- **additional-css:** If you need to slightly change the appearance of your book
without overwriting the whole style, you can specify a set of stylesheets that
will be loaded after the default ones where you can surgically change the
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ pub struct HtmlConfig {
pub copy_fonts: bool,
/// An optional google analytics code.
pub google_analytics: Option<String>,
/// Additional themes to list as options.
pub additional_themes: Vec<UserTheme>,
/// Additional CSS stylesheets to include in the rendered page's `<head>`.
pub additional_css: Vec<PathBuf>,
/// Additional JS scripts to include at the bottom of the rendered page's
Expand Down Expand Up @@ -592,6 +594,16 @@ pub struct HtmlConfig {
pub hash_files: bool,
}

/// Configuration for a custom theme.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserTheme {
/// The human-readable name of the theme.
pub name: String,
/// The class to apply to the `<body>` when this theme is active.
pub class: String,
}

impl Default for HtmlConfig {
fn default() -> HtmlConfig {
HtmlConfig {
Expand All @@ -603,6 +615,7 @@ impl Default for HtmlConfig {
mathjax_support: false,
copy_fonts: true,
google_analytics: None,
additional_themes: Vec::new(),
additional_css: Vec::new(),
additional_js: Vec::new(),
fold: Fold::default(),
Expand Down
16 changes: 15 additions & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::book::{Book, BookItem};
use crate::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition};
use crate::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition, UserTheme};
use crate::errors::*;
use crate::renderer::html_handlebars::helpers;
use crate::renderer::html_handlebars::StaticFiles;
Expand Down Expand Up @@ -554,6 +554,20 @@ fn make_data(
data.insert("copy_fonts".to_owned(), json!(true));
}

// Build a list of themes.
{
const BUILTIN_THEMES: &[&str] = &["Light", "Rust", "Coal", "Navy", "Ayu"];
let themes: Vec<_> = BUILTIN_THEMES
.into_iter()
.map(|&name| UserTheme {
name: name.to_owned(),
class: name.to_lowercase(),
})
.chain(html_config.additional_themes.iter().cloned())
.collect();
data.insert("themes".to_owned(), json!(themes));
}

// Add check to see if there is an additional style
if !html_config.additional_css.is_empty() {
let mut css = Vec::new();
Expand Down
8 changes: 3 additions & 5 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@
<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</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>
{{#each themes}}
<li role="none"><button role="menuitem" class="theme" id="{{class}}">{{name}}</button></li>
{{/each}}
</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