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 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<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 {
@ -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,
}
}
}

View file

@ -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<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,
@ -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<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<()> {
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(),

View file

@ -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 {

View file

@ -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,

View file

@ -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)
}
```

View file

@ -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