Remove deprecated config options

This commit is contained in:
Vincent Prouillet 2021-03-12 22:30:33 +01:00
parent 86b42fc1ab
commit 0812b603a6
7 changed files with 33 additions and 95 deletions

View file

@ -1,5 +1,9 @@
use std::path::Path;
use serde_derive::{Deserialize, Serialize}; 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"; pub const DEFAULT_HIGHLIGHT_THEME: &str = "base16-ocean-dark";
@ -31,6 +35,21 @@ pub struct Markdown {
} }
impl 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 { pub fn has_external_link_tweaks(&self) -> bool {
self.external_links_target_blank self.external_links_target_blank
|| self.external_links_no_follow || self.external_links_no_follow

View file

@ -10,7 +10,6 @@ use std::path::{Path, PathBuf};
use globset::{Glob, GlobSet, GlobSetBuilder}; use globset::{Glob, GlobSet, GlobSetBuilder};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use syntect::parsing::SyntaxSetBuilder;
use toml::Value as Toml; use toml::Value as Toml;
use crate::highlighting::THEME_SET; use crate::highlighting::THEME_SET;
@ -55,12 +54,6 @@ pub struct Config {
/// key into different language. /// key into different language.
translations: HashMap<String, languages::TranslateTerm>, translations: HashMap<String, languages::TranslateTerm>,
/// 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. /// Whether to generate a feed. Defaults to false.
pub generate_feed: bool, pub generate_feed: bool,
/// The number of articles to include in the feed. Defaults to including all items. /// The number of articles to include in the feed. Defaults to including all items.
@ -91,9 +84,6 @@ pub struct Config {
#[serde(skip_serializing)] #[serde(skip_serializing)]
pub mode: Mode, pub mode: Mode,
/// A list of directories to search for additional `.sublime-syntax` files in.
pub extra_syntaxes: Vec<String>,
pub output_dir: String, pub output_dir: String,
pub link_checker: link_checker::LinkChecker, 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`"); 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(&config.markdown.highlight_theme) {
if !THEME_SET.themes.contains_key(highlight_theme) { bail!("Highlight theme {} defined in config does not exist.", config.markdown.highlight_theme);
bail!("Highlight theme {} defined in config does not exist.", highlight_theme);
} }
languages::validate_code(&config.default_language)?; languages::validate_code(&config.default_language)?;
@ -168,13 +157,6 @@ impl Config {
Some(glob_set_builder.build().expect("Bad ignored_content in config file.")); 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) Ok(config)
} }
@ -186,60 +168,6 @@ impl Config {
Config::parse(&content) 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<String> {
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 /// Makes a url, taking into account that the base url might have a trailing slash
pub fn make_permalink(&self, path: &str) -> String { pub fn make_permalink(&self, path: &str) -> String {
let trailing_bit = let trailing_bit =
@ -284,6 +212,7 @@ impl Config {
self.add_theme_extra(&theme) 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> { pub fn other_languages(&self) -> HashMap<&str, &languages::LanguageOptions> {
let mut others = HashMap::new(); let mut others = HashMap::new();
for (k, v) in &self.languages { for (k, v) in &self.languages {
@ -300,14 +229,6 @@ impl Config {
!self.other_languages().is_empty() !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 { pub fn is_in_check_mode(&self) -> bool {
self.mode == Mode::Check self.mode == Mode::Check
} }
@ -318,9 +239,8 @@ impl Config {
pub fn enable_check_mode(&mut self) { pub fn enable_check_mode(&mut self) {
self.mode = Mode::Check; self.mode = Mode::Check;
// Disable syntax highlighting since the results won't be used // Disable syntax highlighting since the results won't be used and it is slow
// and this operation can be expensive. self.markdown.highlight_code = false;
self.highlight_code = false;
} }
pub fn get_translation<S: AsRef<str>>(&self, lang: S, key: S) -> Result<String> { pub fn get_translation<S: AsRef<str>>(&self, lang: S, key: S) -> Result<String> {
@ -376,8 +296,6 @@ impl Default for Config {
title: None, title: None,
description: None, description: None,
theme: None, theme: None,
highlight_code: false,
highlight_theme: "base16-ocean-dark".to_string(),
default_language: "en".to_string(), default_language: "en".to_string(),
languages: HashMap::new(), languages: HashMap::new(),
generate_feed: false, generate_feed: false,
@ -392,7 +310,6 @@ impl Default for Config {
ignored_content: Vec::new(), ignored_content: Vec::new(),
ignored_content_globset: None, ignored_content_globset: None,
translations: HashMap::new(), translations: HashMap::new(),
extra_syntaxes: Vec::new(),
output_dir: "public".to_string(), output_dir: "public".to_string(),
link_checker: link_checker::LinkChecker::default(), link_checker: link_checker::LinkChecker::default(),
slugify: slugify::Slugify::default(), slugify: slugify::Slugify::default(),

View file

@ -28,7 +28,7 @@ pub fn get_highlighter(
language: Option<&str>, language: Option<&str>,
config: &Config, config: &Config,
) -> (HighlightLines<'static>, HighlightSource) { ) -> (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 lang) = language {
if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set { if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set {

View file

@ -1,6 +1,8 @@
mod config; mod config;
pub mod highlighting; pub mod highlighting;
mod theme; mod theme;
pub use crate::config::{ pub use crate::config::{
languages::LanguageOptions, link_checker::LinkChecker, slugify::Slugify, taxonomies::Taxonomy, languages::LanguageOptions, link_checker::LinkChecker, slugify::Slugify, taxonomies::Taxonomy,
Config, Config,

View file

@ -216,7 +216,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
_ => None, _ => None,
}; };
if !context.config.highlight_code() { if !context.config.markdown.highlight_code {
if let Some(lang) = language { if let Some(lang) = language {
let html = format!( let html = format!(
r#"<pre><code class="language-{}" data-lang="{}">"#, r#"<pre><code class="language-{}" data-lang="{}">"#,
@ -227,7 +227,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
return Event::Html("<pre><code>".into()); return Event::Html("<pre><code>".into());
} }
let theme = &THEME_SET.themes[context.config.highlight_theme()]; let theme = &THEME_SET.themes[&context.config.markdown.highlight_theme];
match kind { match kind {
cmark::CodeBlockKind::Indented => (), cmark::CodeBlockKind::Indented => (),
cmark::CodeBlockKind::Fenced(fence_info) => { cmark::CodeBlockKind::Fenced(fence_info) => {
@ -270,7 +270,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
Event::Html(html.into()) Event::Html(html.into())
} }
Event::End(Tag::CodeBlock(_)) => { Event::End(Tag::CodeBlock(_)) => {
if !context.config.highlight_code() { if !context.config.markdown.highlight_code {
return Event::Html("</code></pre>\n".into()); return Event::Html("</code></pre>\n".into());
} }
// reset highlight and close the code block // reset highlight and close the code block

View file

@ -29,7 +29,7 @@ impl<'config> CodeBlock<'config> {
path: Option<&'config str>, path: Option<&'config str>,
) -> Self { ) -> Self {
let fence_info = FenceSettings::new(fence_info); 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 (highlighter, highlight_source) = get_highlighter(fence_info.language, config);
let extra_syntax_set = match highlight_source { let extra_syntax_set = match highlight_source {
HighlightSource::Extra => config.markdown.extra_syntax_set.as_ref(), HighlightSource::Extra => config.markdown.extra_syntax_set.as_ref(),

View file

@ -73,7 +73,7 @@ impl Site {
let path = path.as_ref(); let path = path.as_ref();
let config_file = config_file.as_ref(); let config_file = config_file.as_ref();
let mut config = get_config(config_file)?; 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() { if let Some(theme) = config.theme.clone() {
// Grab data from the extra section of the theme // Grab data from the extra section of the theme