Skip to content

Commit

Permalink
Add context.codeblock config to speical enable / disable in codeblock.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 20, 2024
1 parent fd063d3 commit 30fbc5f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .autocorrectrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
rules:
# Default rules: https://github.com/huacnlee/autocorrect/raw/main/autocorrect/.autocorrectrc.default
spellcheck: 2
# Enable or disable in spatial context
context:
# Enable or disable to format codeblock in Markdown or AsciiDoc etc.
codeblock: 1
textRules:
# Config some special rule for some texts
# For example, if we wants to let "Hello你好" just warning, and "Hi你好" to ignore
Expand Down
4 changes: 4 additions & 0 deletions autocorrect/.autocorrectrc.default
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ rules:
halfwidth-punctuation: 1
# Spellcheck
spellcheck: 0
# Enable or disable in spatial context
context:
# Enable or disable to format codeblock in Markdown or AsciiDoc etc.
codeblock: 1
textRules:
# No default text rules.
spellcheck:
Expand Down
12 changes: 10 additions & 2 deletions autocorrect/src/code/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
use crate::config::toggle;
pub use crate::result::*;
use crate::rule::CJK_RE;
use crate::Config;
use pest::error::Error;
use pest::iterators::{Pair, Pairs};
use pest::RuleType;
Expand Down Expand Up @@ -44,7 +45,6 @@ fn format_pair<R: RuleType, O: Results>(results: &mut O, pair: Pair<R>) {
let rule_name = rule_name.as_str();

// println!("rule: {}, {}", rule_name, item.as_str());

match rule_name {
"string" | "link_string" | "mark_string" | "text" | "inner_text" | "comment"
| "COMMENT" => {
Expand Down Expand Up @@ -182,12 +182,19 @@ fn format_or_lint_for_inline_scripts<R: RuleType, O: Results>(
let part = pair.as_str();
let (base_line, _) = pair.line_col();

let is_enable_context =
rule_name != "codeblock" || Config::current().is_enabled_context("codeblock");

if results.is_lint() {
// Skip lint if AutoCorrect disabled
if !results.is_enabled() {
return;
}

if !is_enable_context {
return;
}

let sub_result = match rule_name {
"inline_style" => Some(lint_for(part, "css")),
"inline_javascript" => Some(lint_for(part, "js")),
Expand All @@ -213,14 +220,15 @@ fn format_or_lint_for_inline_scripts<R: RuleType, O: Results>(
let mut new_part = String::from(part);

// Skip format if AutoCorrect disabled
if results.is_enabled() {
if results.is_enabled() && is_enable_context {
let sub_result = match rule_name {
"inline_style" => Some(format_for(part, "css")),
"inline_javascript" => Some(format_for(part, "js")),
"codeblock" => {
// WARNING: nested codeblock, when call format_for again.
// Because codeblock.data has wrap chars, this make overflowed its stack.
let mut codeblock = Codeblock::from_pair(pair);

let mut result = format_for(&codeblock.code, &codeblock.lang);
codeblock.update_data(&result.out);
result.out = codeblock.data;
Expand Down
34 changes: 34 additions & 0 deletions autocorrect/src/code/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct MarkdownParser;

#[cfg(test)]
mod tests {
use crate::config::SeverityMode;

use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -356,4 +358,36 @@ mod tests {

assert_eq!(expected, format_markdown(raw).out);
}

#[test]
fn test_disable_context_codeblock() {
use std::collections::HashMap;

let last_mode = *crate::config::Config::current()
.context
.get("codeblock")
.unwrap();

crate::config::CURRENT_CONFIG.write().unwrap().context = map! {
"codeblock".to_string() => SeverityMode::Off,
};

let raw = indoc! {r###"
```rust
// 这段应该ignore掉
```
"###};

let expected = indoc! {r###"
```rust
// 这段应该ignore掉
```
"###};

assert_eq!(expected, format_for(raw, "markdown").to_string());

crate::config::CURRENT_CONFIG.write().unwrap().context = map! {
"codeblock".to_string() => last_mode,
};
}
}
16 changes: 15 additions & 1 deletion autocorrect/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ lazy_static! {
env!("CARGO_MANIFEST_DIR"),
"/.autocorrectrc.default"
));
static ref CURRENT_CONFIG: RwLock<Config> = RwLock::new(Config::from_str(&CONFIG_STR).unwrap());
pub(crate) static ref CURRENT_CONFIG: RwLock<Config> =
RwLock::new(Config::from_str(&CONFIG_STR).unwrap());
}

pub trait ConfigFileTypes {
Expand Down Expand Up @@ -59,6 +60,8 @@ pub struct Config {
// Addition file types map, high priority than default
#[serde(default)]
pub file_types: HashMap<String, String>,
#[serde(default)]
pub context: HashMap<String, SeverityMode>,
}

pub fn load_file(config_file: &str) -> Result<Config, Error> {
Expand Down Expand Up @@ -177,6 +180,15 @@ impl Config {

Ok(self.clone())
}

/// Check is enable format in context
pub fn is_enabled_context(&self, name: &str) -> bool {
if let Some(mode) = self.context.get(name) {
return *mode != SeverityMode::Off;
}

false
}
}

// Setup config for test for load tests/.autocorrectrc.test
Expand Down Expand Up @@ -356,6 +368,7 @@ mod tests {
words: vec!["foo".to_string(), "bar".to_string(), "baz".to_string()],
..Default::default()
},
..Default::default()
};

let config1 = Config {
Expand All @@ -374,6 +387,7 @@ mod tests {
words: vec!["foo1".to_string(), "bar1".to_string()],
..Default::default()
},
..Default::default()
};

config.merge(&config1).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion autocorrect/src/config/severity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize, Serializer};

#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
pub enum SeverityMode {
#[default]
Off = 0,
Error = 1,
Warning = 2,
Expand Down

0 comments on commit 30fbc5f

Please sign in to comment.