Skip to content

Commit

Permalink
Add space-backtick rule, default: on.
Browse files Browse the repository at this point in the history
```diff
- 演示`code`代码
+ 演示 `code` 代码
```

Fix #153
  • Loading branch information
huacnlee committed Nov 27, 2023
1 parent 3ab9c92 commit 42700c5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions autocorrect/.autocorrectrc.default
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ rules:
space-punctuation: 1
# Add space between brackets (), [] when near the CJK.
space-bracket: 1
# Add space between ``, when near the CJK.
space-backtick: 1
# Add space between dash `-`
space-dash: 0
# Convert to fullwidth.
Expand Down
1 change: 1 addition & 0 deletions autocorrect/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ mod tests {
"《腾讯》-发布-《新版》本微信" => "《腾讯》- 发布 -《新版》本微信",
"“腾讯”-发布-“新版”本微信" => "“腾讯” - 发布 - “新版”本微信",
"‘腾讯’-发布-‘新版’本微信" => "‘腾讯’ - 发布 - ‘新版’本微信",
"行内`code`代码" => "行内 `code` 代码",
];

assert_cases(cases);
Expand Down
3 changes: 3 additions & 0 deletions autocorrect/src/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ lazy_static! {
Rule::new("space-bracket", word::format_space_bracket),
// Rule: space-dash
Rule::new("space-dash", word::format_space_dash),
// Rule: space-backtick
Rule::new("space-backtick", word::format_space_backtick),
// Rule: fullwidth
Rule::new("fullwidth", fullwidth::format),
];
Expand Down Expand Up @@ -200,6 +202,7 @@ mod tests {
"space-punctuation",
"space-bracket",
"space-dash",
"space-backtick",
"fullwidth",
"halfwidth-word",
"halfwidth-punctuation",
Expand Down
35 changes: 34 additions & 1 deletion autocorrect/src/rule/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ lazy_static! {
Strategery::new(r"[\]\)]", r"\p{CJK}"),
];

static ref BACKTICK_STRATEGIES: Vec<Strategery> = vec![
// Add space before and after backtick ` near the CJK
Strategery::new(r"\p{CJK}", r"`.+`"),
Strategery::new(r"`.+`", r"\p{CJK}"),
];

static ref DASH_RE : regex::Regex = regexp!(r"([\p{CJK}])[\-]([\p{CJK}])");

static ref NO_SPACE_FULLWIDTH_STRATEGIES: Vec<Strategery> = vec![
Expand Down Expand Up @@ -75,6 +81,14 @@ pub fn format_space_dash(input: &str) -> String {
.to_string()
}

pub fn format_space_backtick(input: &str) -> String {
let mut out = String::from(input);
BACKTICK_STRATEGIES
.iter()
.for_each(|s| out = s.format(&out));
out
}

pub fn format_no_space_fullwidth(input: &str) -> String {
let mut out = String::from(input);

Expand Down Expand Up @@ -103,7 +117,7 @@ pub fn format_no_space_fullwidth_quote(input: &str) -> String {

#[cfg(test)]
mod tests {
use crate::rule::word::format_space_dash;
use crate::rule::word::{format_space_backtick, format_space_dash};

#[test]
fn test_format_space_bracket() {
Expand All @@ -112,4 +126,23 @@ mod tests {
assert_eq!(format_space_dash("你好-world"), "你好-world");
assert_eq!(format_space_dash("hello-world"), "hello-world");
}

#[test]
fn test_format_space_backtick() {
assert_eq!(format_space_backtick("代码`code`"), "代码 `code`");
assert_eq!(format_space_backtick("代码`code`代码"), "代码 `code` 代码");
assert_eq!(
format_space_backtick("`code`代码`code`"),
"`code` 代码 `code`"
);
assert_eq!(
format_space_backtick("`code`hello`code`"),
"`code`hello`code`"
);

assert_eq!(format_space_backtick("```rs"), "```rs");
assert_eq!(format_space_backtick("``代码第1行"), "``代码第1行");
assert_eq!(format_space_backtick("`代码第1行"), "`代码第1行");
assert_eq!(format_space_backtick("代码第2行`"), "代码第2行`");
}
}

0 comments on commit 42700c5

Please sign in to comment.