Skip to content

Commit

Permalink
Move extra_syntax to markup.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Dec 21, 2020
1 parent 96439df commit 39870d8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
11 changes: 10 additions & 1 deletion components/config/src/config/markup.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
}
}
30 changes: 22 additions & 8 deletions components/config/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion components/config/src/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion components/rendering/src/markdown/codeblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions test_site/content/posts/extra_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
10 changes: 5 additions & 5 deletions test_site/syntaxes/test.sublime-syntax
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 39870d8

Please sign in to comment.