diff --git a/components/config/src/config/markup.rs b/components/config/src/config/markup.rs index 3feb5cfc..39c686de 100644 --- a/components/config/src/config/markup.rs +++ b/components/config/src/config/markup.rs @@ -1,5 +1,9 @@ +use std::path::Path; + use serde_derive::{Deserialize, Serialize}; -use syntect::parsing::SyntaxSet; +use syntect::parsing::{SyntaxSet, SyntaxSetBuilder}; + +use errors::Result; pub const DEFAULT_HIGHLIGHT_THEME: &str = "base16-ocean-dark"; @@ -31,6 +35,21 @@ pub struct Markdown { } impl Markdown { + /// Attempt to load any extra syntax found in the extra syntaxes of the config + pub fn load_extra_syntaxes(&mut self, base_path: &Path) -> Result<()> { + if self.extra_syntaxes.is_empty() { + return Ok(()); + } + + let mut ss = SyntaxSetBuilder::new(); + for dir in &self.extra_syntaxes { + ss.add_from_folder(base_path.join(dir), true)?; + } + self.extra_syntax_set = Some(ss.build()); + + Ok(()) + } + pub fn has_external_link_tweaks(&self) -> bool { self.external_links_target_blank || self.external_links_no_follow diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index 0f3c26e8..8fba9079 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -10,7 +10,6 @@ use std::path::{Path, PathBuf}; use globset::{Glob, GlobSet, GlobSetBuilder}; use serde_derive::{Deserialize, Serialize}; -use syntect::parsing::SyntaxSetBuilder; use toml::Value as Toml; use crate::highlighting::THEME_SET; @@ -55,12 +54,6 @@ pub struct Config { /// key into different language. translations: HashMap, - /// Whether to highlight all code blocks found in markdown files. Defaults to false - highlight_code: bool, - /// Which themes to use for code highlighting. See Readme for supported themes - /// Defaults to "base16-ocean-dark" - highlight_theme: String, - /// Whether to generate a feed. Defaults to false. pub generate_feed: bool, /// The number of articles to include in the feed. Defaults to including all items. @@ -91,9 +84,6 @@ pub struct Config { #[serde(skip_serializing)] pub mode: Mode, - /// A list of directories to search for additional `.sublime-syntax` files in. - pub extra_syntaxes: Vec, - pub output_dir: String, pub link_checker: link_checker::LinkChecker, @@ -124,9 +114,8 @@ impl Config { bail!("A base URL is required in config.toml with key `base_url`"); } - let highlight_theme = config.highlight_theme(); - if !THEME_SET.themes.contains_key(highlight_theme) { - bail!("Highlight theme {} defined in config does not exist.", highlight_theme); + if !THEME_SET.themes.contains_key(&config.markdown.highlight_theme) { + bail!("Highlight theme {} defined in config does not exist.", config.markdown.highlight_theme); } languages::validate_code(&config.default_language)?; @@ -168,13 +157,6 @@ impl Config { Some(glob_set_builder.build().expect("Bad ignored_content in config file.")); } - if config.highlight_code { - println!("`highlight_code` has been moved to a [markdown] section. Top level `highlight_code` and `highlight_theme` will stop working in 0.14."); - } - if !config.extra_syntaxes.is_empty() { - println!("`extra_syntaxes` has been moved to a [markdown] section. Top level `extra_syntaxes` will stop working in 0.14."); - } - Ok(config) } @@ -186,60 +168,6 @@ impl Config { Config::parse(&content) } - /// Temporary, while we have the settings in 2 places - /// TODO: remove me in 0.14 - pub fn highlight_code(&self) -> bool { - if !self.highlight_code && !self.markdown.highlight_code { - return false; - } - - if self.highlight_code { - true - } else { - self.markdown.highlight_code - } - } - - /// Temporary, while we have the settings in 2 places - /// TODO: remove me in 0.14 - pub fn highlight_theme(&self) -> &str { - if self.highlight_theme != markup::DEFAULT_HIGHLIGHT_THEME { - &self.highlight_theme - } else { - &self.markdown.highlight_theme - } - } - - /// TODO: remove me in 0.14 - pub fn extra_syntaxes(&self) -> Vec { - if !self.markdown.extra_syntaxes.is_empty() { - return self.markdown.extra_syntaxes.clone(); - } - - if !self.extra_syntaxes.is_empty() { - return self.extra_syntaxes.clone(); - } - - Vec::new() - } - - /// Attempt to load any extra syntax found in the extra syntaxes of the config - /// TODO: move to markup.rs in 0.14 - pub fn load_extra_syntaxes(&mut self, base_path: &Path) -> Result<()> { - let extra_syntaxes = self.extra_syntaxes(); - if extra_syntaxes.is_empty() { - return Ok(()); - } - - let mut ss = SyntaxSetBuilder::new(); - for dir in &extra_syntaxes { - ss.add_from_folder(base_path.join(dir), true)?; - } - self.markdown.extra_syntax_set = Some(ss.build()); - - Ok(()) - } - /// Makes a url, taking into account that the base url might have a trailing slash pub fn make_permalink(&self, path: &str) -> String { let trailing_bit = @@ -284,6 +212,7 @@ impl Config { self.add_theme_extra(&theme) } + /// Returns all the languages settings for languages other than the default one pub fn other_languages(&self) -> HashMap<&str, &languages::LanguageOptions> { let mut others = HashMap::new(); for (k, v) in &self.languages { @@ -300,14 +229,6 @@ impl Config { !self.other_languages().is_empty() } - pub fn is_in_build_mode(&self) -> bool { - self.mode == Mode::Build - } - - pub fn is_in_serve_mode(&self) -> bool { - self.mode == Mode::Serve - } - pub fn is_in_check_mode(&self) -> bool { self.mode == Mode::Check } @@ -318,9 +239,8 @@ impl Config { pub fn enable_check_mode(&mut self) { self.mode = Mode::Check; - // Disable syntax highlighting since the results won't be used - // and this operation can be expensive. - self.highlight_code = false; + // Disable syntax highlighting since the results won't be used and it is slow + self.markdown.highlight_code = false; } pub fn get_translation>(&self, lang: S, key: S) -> Result { @@ -376,8 +296,6 @@ impl Default for Config { title: None, description: None, theme: None, - highlight_code: false, - highlight_theme: "base16-ocean-dark".to_string(), default_language: "en".to_string(), languages: HashMap::new(), generate_feed: false, @@ -392,7 +310,6 @@ impl Default for Config { ignored_content: Vec::new(), ignored_content_globset: None, translations: HashMap::new(), - extra_syntaxes: Vec::new(), output_dir: "public".to_string(), link_checker: link_checker::LinkChecker::default(), slugify: slugify::Slugify::default(), diff --git a/components/config/src/highlighting.rs b/components/config/src/highlighting.rs index 54f6b8fa..43b0f5ba 100644 --- a/components/config/src/highlighting.rs +++ b/components/config/src/highlighting.rs @@ -28,7 +28,7 @@ pub fn get_highlighter( language: Option<&str>, config: &Config, ) -> (HighlightLines<'static>, HighlightSource) { - let theme = &THEME_SET.themes[config.highlight_theme()]; + let theme = &THEME_SET.themes[&config.markdown.highlight_theme]; if let Some(ref lang) = language { if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set { diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index 4c211540..130b88bf 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -1,6 +1,8 @@ mod config; pub mod highlighting; mod theme; + + pub use crate::config::{ languages::LanguageOptions, link_checker::LinkChecker, slugify::Slugify, taxonomies::Taxonomy, Config, diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 1b877beb..731b2c7b 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -216,7 +216,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result None, }; - if !context.config.highlight_code() { + if !context.config.markdown.highlight_code { if let Some(lang) = language { let html = format!( r#"
"#,
@@ -227,7 +227,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result".into());
                         }
 
-                        let theme = &THEME_SET.themes[context.config.highlight_theme()];
+                        let theme = &THEME_SET.themes[&context.config.markdown.highlight_theme];
                         match kind {
                             cmark::CodeBlockKind::Indented => (),
                             cmark::CodeBlockKind::Fenced(fence_info) => {
@@ -270,7 +270,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result {
-                        if !context.config.highlight_code() {
+                        if !context.config.markdown.highlight_code {
                             return Event::Html("
\n".into()); } // reset highlight and close the code block diff --git a/components/rendering/src/markdown/codeblock.rs b/components/rendering/src/markdown/codeblock.rs index c681a830..3c3a194c 100644 --- a/components/rendering/src/markdown/codeblock.rs +++ b/components/rendering/src/markdown/codeblock.rs @@ -29,7 +29,7 @@ impl<'config> CodeBlock<'config> { path: Option<&'config str>, ) -> Self { let fence_info = FenceSettings::new(fence_info); - let theme = &THEME_SET.themes[config.highlight_theme()]; + let theme = &THEME_SET.themes[&config.markdown.highlight_theme]; let (highlighter, highlight_source) = get_highlighter(fence_info.language, config); let extra_syntax_set = match highlight_source { HighlightSource::Extra => config.markdown.extra_syntax_set.as_ref(), diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index a3c4c844..42ebd687 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -73,7 +73,7 @@ impl Site { let path = path.as_ref(); let config_file = config_file.as_ref(); let mut config = get_config(config_file)?; - config.load_extra_syntaxes(path)?; + config.markdown.load_extra_syntaxes(path)?; if let Some(theme) = config.theme.clone() { // Grab data from the extra section of the theme