Move extra_syntax to markup.rs

This commit is contained in:
Vincent Prouillet 2020-12-21 08:54:43 +01:00
parent 96439df0e2
commit 39870d8675
6 changed files with 50 additions and 18 deletions

View file

@ -1,8 +1,9 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use syntect::parsing::SyntaxSet;
pub const DEFAULT_HIGHLIGHT_THEME: &str = "base16-ocean-dark"; pub const DEFAULT_HIGHLIGHT_THEME: &str = "base16-ocean-dark";
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct Markdown { pub struct Markdown {
/// Whether to highlight all code blocks found in markdown files. Defaults to false /// 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, pub external_links_no_referrer: bool,
/// Whether smart punctuation is enabled (changing quotes, dashes, dots etc in their typographic form) /// Whether smart punctuation is enabled (changing quotes, dashes, dots etc in their typographic form)
pub smart_punctuation: bool, pub smart_punctuation: bool,
/// A list of directories to search for additional `.sublime-syntax` files in.
pub extra_syntaxes: Vec<String>,
/// The compiled extra syntaxes into a syntax set
#[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need
pub extra_syntax_set: Option<SyntaxSet>,
} }
impl Markdown { impl Markdown {
@ -66,6 +73,8 @@ impl Default for Markdown {
external_links_no_follow: false, external_links_no_follow: false,
external_links_no_referrer: false, external_links_no_referrer: false,
smart_punctuation: false, smart_punctuation: false,
extra_syntaxes: vec![],
extra_syntax_set: None,
} }
} }
} }

View file

@ -10,7 +10,7 @@ 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::{SyntaxSet, SyntaxSetBuilder}; use syntect::parsing::{SyntaxSetBuilder};
use toml::Value as Toml; use toml::Value as Toml;
use crate::highlighting::THEME_SET; use crate::highlighting::THEME_SET;
@ -93,9 +93,6 @@ pub struct Config {
/// A list of directories to search for additional `.sublime-syntax` files in. /// A list of directories to search for additional `.sublime-syntax` files in.
pub extra_syntaxes: Vec<String>, pub extra_syntaxes: Vec<String>,
/// The compiled extra syntaxes into a syntax set
#[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need
pub extra_syntax_set: Option<SyntaxSet>,
pub output_dir: String, pub output_dir: String,
@ -162,6 +159,9 @@ impl Config {
if config.highlight_code { 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."); 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)
} }
@ -201,17 +201,32 @@ impl Config {
} }
} }
/// 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 /// 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<()> { 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(()); return Ok(());
} }
let mut ss = SyntaxSetBuilder::new(); 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)?; 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(()) Ok(())
} }
@ -363,7 +378,6 @@ impl Default for Config {
ignored_content_globset: None, ignored_content_globset: None,
translations: HashMap::new(), translations: HashMap::new(),
extra_syntaxes: Vec::new(), extra_syntaxes: Vec::new(),
extra_syntax_set: None,
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

@ -30,10 +30,11 @@ pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLin
let syntax = SYNTAX_SET let syntax = SYNTAX_SET
.find_syntax_by_token(hacked_lang) .find_syntax_by_token(hacked_lang)
.or_else(|| { .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); let s = extra.find_syntax_by_token(hacked_lang);
if s.is_some() { if s.is_some() {
in_extra = true; in_extra = true;
println!("Found extra syntax");
} }
s s
} else { } else {

View file

@ -29,7 +29,7 @@ impl<'config> CodeBlock<'config> {
Self { Self {
highlighter, highlighter,
extra_syntax_set: match in_extra { extra_syntax_set: match in_extra {
true => config.extra_syntax_set.as_ref(), true => config.markdown.extra_syntax_set.as_ref(),
false => None, false => None,
}, },
background, background,

View file

@ -4,6 +4,14 @@ description = ""
date = 2018-08-14 date = 2018-08-14
+++ +++
```test-syntax ```mylang
This is a test code snippet. for (int i = 0; ; i++ ) {
if (i < 10)
}
```
```c
for (int i = 0; ; i++ ) {
if (i < 10)
}
``` ```

View file

@ -1,10 +1,10 @@
%YAML 1.2 %YAML 1.2
--- ---
file_extensions: name: mylang
- test-syntax file_extensions: [mylang]
scope: source.test scope: source.mylang
contexts: contexts:
main: main:
- match: "test" - match: \b(if|else|for|while)\b
scope: constant.language.test scope: keyword.control