Skip to content

Commit f2ebb4c

Browse files
authored
feat: Add space-dollar option (#259)
1 parent b775e19 commit f2ebb4c

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ rules:
214214
space-backticks: 1
215215
# Add space between dash `-`
216216
space-dash: 0
217+
# Add space between dollar $ when near the CJK.
218+
space-dollar: 0
217219
# Convert to fullwidth.
218220
fullwidth: 1
219221
# To remove space near the fullwidth.

autocorrect-website/public/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
"default": 0,
4545
"$ref": "#/definitions/SeverityMode"
4646
},
47+
"space-dollar": {
48+
"description": "Add space between dollar $ when near the CJK.",
49+
"default": 0,
50+
"$ref": "#/definitions/SeverityMode"
51+
},
4752
"fullwidth": {
4853
"description": "Convert to fullwidth.",
4954
"default": 1,

autocorrect-website/src/docs/usage.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ rules:
139139
space-backticks: 1
140140
# Add space between dash `-`
141141
space-dash: 0
142+
# Add space between dollar $ when near the CJK.
143+
space-dollar: 0
142144
# Convert to fullwidth.
143145
fullwidth: 1
144146
# To remove space near the fullwidth.

autocorrect/.autocorrectrc.default

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ rules:
1111
space-backticks: 1
1212
# Add space between dash `-`
1313
space-dash: 1
14+
# Add space between dollar $ when near the CJK.
15+
space-dollar: 0
1416
# Convert to fullwidth.
1517
fullwidth: 1
1618
# To remove space near the fullwidth punctuations.

autocorrect/src/config/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ impl Config {
197197
}
198198

199199
// Setup config for test for load tests/.autocorrectrc.test
200-
static STEUP_ONCE: std::sync::Once = std::sync::Once::new();
200+
static SETUP_ONCE: std::sync::Once = std::sync::Once::new();
201201

202202
#[allow(unused)]
203203
pub(crate) fn setup_test() {
204-
STEUP_ONCE.call_once(|| {
204+
SETUP_ONCE.call_once(|| {
205205
let config_str = include_str!(concat!(
206206
env!("CARGO_MANIFEST_DIR"),
207207
"/tests/.autocorrectrc.test"
@@ -345,6 +345,7 @@ mod tests {
345345
match k.as_str() {
346346
"spellcheck" => assert_eq!(SeverityMode::Warning, v),
347347
"space-dash" => assert_eq!(SeverityMode::Error, v),
348+
"space-dollar" => assert_eq!(SeverityMode::Off, v),
348349
_ => assert_eq!(SeverityMode::Error, v),
349350
}
350351
}

autocorrect/src/rule/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ lazy_static! {
2727
Rule::new("space-dash", word::format_space_dash),
2828
// Rule: space-backticks
2929
Rule::new("space-backticks", word::format_space_backticks),
30+
// Rule: space-dollar
31+
Rule::new("space-dollar", word::format_space_dollar),
3032
// Rule: fullwidth
3133
Rule::new("fullwidth", fullwidth::format),
3234
];
@@ -206,6 +208,7 @@ mod tests {
206208
"space-bracket",
207209
"space-dash",
208210
"space-backticks",
211+
"space-dollar",
209212
"fullwidth",
210213
"halfwidth-word",
211214
"halfwidth-punctuation",
@@ -287,6 +290,8 @@ mod tests {
287290
"2你好[世界]" => (map!{ "space-bracket" => true }, "2 你好[世界]"),
288291
"代码`code`例子1" => (map!{}, "代码 `code` 例子 1"),
289292
"代码`code`例子2" => (map!{ "space-backticks" => true }, "代码`code`例子 2"),
293+
"变量$x$表示了" => (map!{}, "变量 $x$ 表示了"),
294+
"变量$x$表示了" => (map!{ "space-dollar" => true}, "变量$x$表示了"),
290295
"测试 ," => (map!{}, "测试,"),
291296
"测试 ," => (map!{"no-space-fullwidth"=>true}, "测试 ,"),
292297
};

autocorrect/src/rule/word.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ lazy_static! {
4646
Strategery::new(r"[\p{CJK_N}\s)】」”’》][\-]", r"[\p{CJK_N}“‘]"),
4747
];
4848

49+
static ref DOLLAR_STRATEGIES: Vec<Strategery> = vec![
50+
// Add space before and after dollar $ near the CJK
51+
Strategery::new(r"\p{CJK}", r"\$"),
52+
Strategery::new(r"\$", r"\p{CJK}"),
53+
];
54+
4955
static ref NO_SPACE_FULLWIDTH_STRATEGIES: Vec<Strategery> = vec![
5056
// FullwidthPunctuation remove space case, Fullwidth can safe to remove spaces
5157
Strategery::new(r"\w|\p{CJK}|`", r"[,。、!?:;()「」《》【】]").with_remove_space().with_reverse(),
@@ -102,6 +108,16 @@ pub fn format_space_backticks(input: &str) -> Cow<str> {
102108
})
103109
}
104110

111+
pub fn format_space_dollar(input: &str) -> Cow<str> {
112+
DOLLAR_STRATEGIES
113+
.iter()
114+
.fold(Cow::Borrowed(input), |text, strategy| match text {
115+
Cow::Borrowed(s) => strategy.format(s),
116+
Cow::Owned(s) => Cow::Owned(strategy.format(&s).into_owned()),
117+
},
118+
)
119+
}
120+
105121
pub fn format_no_space_fullwidth(input: &str) -> Cow<str> {
106122
if !CJK_RE.is_match(input) {
107123
return Cow::Borrowed(input);
@@ -130,7 +146,7 @@ pub fn format_no_space_fullwidth_quote(input: &str) -> Cow<str> {
130146

131147
#[cfg(test)]
132148
mod tests {
133-
use crate::rule::word::{format_space_backticks, format_space_bracket, format_space_dash};
149+
use crate::rule::word::{format_space_backticks, format_space_bracket, format_space_dash, format_space_dollar};
134150

135151
#[test]
136152
fn test_format_space_dash() {
@@ -167,4 +183,26 @@ mod tests {
167183
assert_eq!(format_space_backticks("`代码第1行"), "`代码第1行");
168184
assert_eq!(format_space_backticks("代码第2行`"), "代码第2行`");
169185
}
186+
187+
#[test]
188+
fn test_format_space_dollar() {
189+
assert_eq!(format_space_dollar("你好$世界"), "你好 $ 世界");
190+
assert_eq!(format_space_dollar("hello$世界"), "hello$ 世界");
191+
assert_eq!(format_space_dollar("你好$world"), "你好 $world");
192+
assert_eq!(format_space_dollar("你好$x$世界"), "你好 $x$ 世界");
193+
assert_eq!(format_space_dollar("变量 $x$ 代表"), "变量 $x$ 代表");
194+
assert_eq!(format_space_dollar("令$x^2+y^2=z^2$,可得"), "令 $x^2+y^2=z^2$,可得");
195+
assert_eq!(format_space_dollar("这是一个例子:$E=mc^2$。"), "这是一个例子:$E=mc^2$。");
196+
assert_eq!(format_space_dollar("$x+y$是方程"), "$x+y$ 是方程");
197+
assert_eq!(format_space_dollar("方程为$x+y=1$"), "方程为 $x+y=1$");
198+
assert_eq!(format_space_dollar("若$x>0$且$y<0$"), "若 $x>0$ 且 $y<0$");
199+
assert_eq!(format_space_dollar("函数$f(x)$的极值"), "函数 $f(x)$ 的极值");
200+
assert_eq!(format_space_dollar("变数$x$、$y$满足"), "变数 $x$、$y$ 满足");
201+
assert_eq!(format_space_dollar("公式$$E=mc^2$$证明了"), "公式 $$E=mc^2$$ 证明了");
202+
assert_eq!(format_space_dollar("矩阵$A=\\begin{bmatrix}1&0\\\\0&1\\end{bmatrix}$满足"), "矩阵 $A=\\begin{bmatrix}1&0\\\\0&1\\end{bmatrix}$ 满足");
203+
assert_eq!(format_space_dollar("测试$x_1,x_2$以及$x_3$"), "测试 $x_1,x_2$ 以及 $x_3$");
204+
assert_eq!(format_space_dollar("若$a>b$则有$c>d$"), "若 $a>b$ 则有 $c>d$");
205+
assert_eq!(format_space_dollar("设$a,b∈\\mathbb{R}$且$a>b$"), "设 $a,b∈\\mathbb{R}$ 且 $a>b$");
206+
assert_eq!(format_space_dollar("下式成立:$$f(x)=x^2$$"), "下式成立:$$f(x)=x^2$$");
207+
}
170208
}

0 commit comments

Comments
 (0)