From 39870d8675f482f63360bafae72d2839c0f0e308 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 21 Dec 2020 08:54:43 +0100 Subject: [PATCH] Move extra_syntax to markup.rs --- components/config/src/config/markup.rs | 11 ++++++- components/config/src/config/mod.rs | 30 ++++++++++++++----- components/config/src/highlighting.rs | 3 +- .../rendering/src/markdown/codeblock.rs | 2 +- test_site/content/posts/extra_syntax.md | 12 ++++++-- test_site/syntaxes/test.sublime-syntax | 10 +++---- 6 files changed, 50 insertions(+), 18 deletions(-) diff --git a/components/config/src/config/markup.rs b/components/config/src/config/markup.rs index 035aad7d..3feb5cfc 100644 --- a/components/config/src/config/markup.rs +++ b/components/config/src/config/markup.rs @@ -1,8 +1,9 @@ use serde_derive::{Deserialize, Serialize}; +use syntect::parsing::SyntaxSet; pub const DEFAULT_HIGHLIGHT_THEME: &str = "base16-ocean-dark"; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct Markdown { /// Whether to highlight all code blocks found in markdown files. Defaults to false @@ -21,6 +22,12 @@ pub struct Markdown { pub external_links_no_referrer: bool, /// Whether smart punctuation is enabled (changing quotes, dashes, dots etc in their typographic form) pub smart_punctuation: bool, + + /// A list of directories to search for additional `.sublime-syntax` files in. + pub extra_syntaxes: Vec, + /// The compiled extra syntaxes into a syntax set + #[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need + pub extra_syntax_set: Option, } impl Markdown { @@ -66,6 +73,8 @@ impl Default for Markdown { external_links_no_follow: false, external_links_no_referrer: false, smart_punctuation: false, + extra_syntaxes: vec![], + extra_syntax_set: None, } } } diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index fc928ee3..81345136 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; use globset::{Glob, GlobSet, GlobSetBuilder}; use serde_derive::{Deserialize, Serialize}; -use syntect::parsing::{SyntaxSet, SyntaxSetBuilder}; +use syntect::parsing::{SyntaxSetBuilder}; use toml::Value as Toml; use crate::highlighting::THEME_SET; @@ -93,9 +93,6 @@ pub struct Config { /// A list of directories to search for additional `.sublime-syntax` files in. pub extra_syntaxes: Vec, - /// The compiled extra syntaxes into a syntax set - #[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need - pub extra_syntax_set: Option, pub output_dir: String, @@ -162,6 +159,9 @@ impl Config { 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) } @@ -201,17 +201,32 @@ impl Config { } } + /// 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<()> { - if self.extra_syntaxes.is_empty() { + let extra_syntaxes = self.extra_syntaxes(); + if extra_syntaxes.is_empty() { return Ok(()); } let mut ss = SyntaxSetBuilder::new(); - for dir in &self.extra_syntaxes { + for dir in &extra_syntaxes { ss.add_from_folder(base_path.join(dir), true)?; } - self.extra_syntax_set = Some(ss.build()); + self.markdown.extra_syntax_set = Some(ss.build()); Ok(()) } @@ -363,7 +378,6 @@ impl Default for Config { ignored_content_globset: None, translations: HashMap::new(), extra_syntaxes: Vec::new(), - extra_syntax_set: None, 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 8b741ff7..9d73d4da 100644 --- a/components/config/src/highlighting.rs +++ b/components/config/src/highlighting.rs @@ -30,10 +30,11 @@ pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLin let syntax = SYNTAX_SET .find_syntax_by_token(hacked_lang) .or_else(|| { - if let Some(ref extra) = config.extra_syntax_set { + if let Some(ref extra) = config.markdown.extra_syntax_set { let s = extra.find_syntax_by_token(hacked_lang); if s.is_some() { in_extra = true; + println!("Found extra syntax"); } s } else { diff --git a/components/rendering/src/markdown/codeblock.rs b/components/rendering/src/markdown/codeblock.rs index f27e592d..b273664f 100644 --- a/components/rendering/src/markdown/codeblock.rs +++ b/components/rendering/src/markdown/codeblock.rs @@ -29,7 +29,7 @@ impl<'config> CodeBlock<'config> { Self { highlighter, extra_syntax_set: match in_extra { - true => config.extra_syntax_set.as_ref(), + true => config.markdown.extra_syntax_set.as_ref(), false => None, }, background, diff --git a/test_site/content/posts/extra_syntax.md b/test_site/content/posts/extra_syntax.md index b194c19c..dfdc5afe 100644 --- a/test_site/content/posts/extra_syntax.md +++ b/test_site/content/posts/extra_syntax.md @@ -4,6 +4,14 @@ description = "" date = 2018-08-14 +++ -```test-syntax -This is a test code snippet. +```mylang +for (int i = 0; ; i++ ) { + if (i < 10) +} +``` + +```c +for (int i = 0; ; i++ ) { + if (i < 10) +} ``` diff --git a/test_site/syntaxes/test.sublime-syntax b/test_site/syntaxes/test.sublime-syntax index bd13a8e9..0215d7ec 100644 --- a/test_site/syntaxes/test.sublime-syntax +++ b/test_site/syntaxes/test.sublime-syntax @@ -1,10 +1,10 @@ %YAML 1.2 --- -file_extensions: - - test-syntax -scope: source.test +name: mylang +file_extensions: [mylang] +scope: source.mylang contexts: main: - - match: "test" - scope: constant.language.test + - match: \b(if|else|for|while)\b + scope: keyword.control \ No newline at end of file