diff --git a/README.md b/README.md index c5da9bd..d6c82a1 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,14 @@ repos: ### `insert-whitespace-between-cn-and-en-char` 用于自动在中英文之间添加空格 + +## Pragma + +dochooks 支持 `dochooks: skip-next-line` 和 `dochooks: skip-line` 两种 pragma + + +```markdown + +这是一段中文and English混合的text +这是一段中文and English混合的text +``` diff --git a/dochooks/insert_whitespace_between_cn_and_en_char/check.py b/dochooks/insert_whitespace_between_cn_and_en_char/check.py index 056e5a4..b2bf5c7 100644 --- a/dochooks/insert_whitespace_between_cn_and_en_char/check.py +++ b/dochooks/insert_whitespace_between_cn_and_en_char/check.py @@ -6,6 +6,7 @@ from dochooks import __version__ from ..utils.return_code import FAIL, PASS, ReturnCode +from .pragma import PragmaManager from .regex import REGEX_CN_WITH_EN, REGEX_EN_WITH_CN @@ -17,11 +18,15 @@ def check(string: str) -> bool: def _check_file(file_path: str) -> ReturnCode: return_code = PASS + pragma_manager = PragmaManager() with open(file_path, encoding="utf8", newline="\n") as f: for lineno, line in enumerate(f, 1): - if not check(line): - print(f"No spaces between EN and CN chars detected at: {file_path}:{lineno}:\t{line}") - return_code = FAIL + with pragma_manager.scan(line) as skip_line: + if skip_line: + continue + if not check(line): + print(f"No spaces between EN and CN chars detected at: {file_path}:{lineno}:\t{line}") + return_code = FAIL return return_code diff --git a/dochooks/insert_whitespace_between_cn_and_en_char/format.py b/dochooks/insert_whitespace_between_cn_and_en_char/format.py index c12735f..007888d 100644 --- a/dochooks/insert_whitespace_between_cn_and_en_char/format.py +++ b/dochooks/insert_whitespace_between_cn_and_en_char/format.py @@ -7,6 +7,7 @@ from ..utils.return_code import FAIL, PASS, ReturnCode from .check import check +from .pragma import PragmaManager from .regex import REGEX_CN_WITH_EN, REGEX_EN_WITH_CN @@ -18,14 +19,19 @@ def format(text: str) -> str: def _format_file(file_path: str) -> ReturnCode: return_code = PASS + pragma_manager = PragmaManager() formatted_text = "" with open(file_path, encoding="utf8", newline="\n") as f: for lineno, line in enumerate(f, 1): - if not check(line): - line = format(line) - return_code = FAIL - print(f"Add spaces between EN and CN chars in: {file_path}:{lineno}:\t{line}") - formatted_text += line + with pragma_manager.scan(line) as skip_line: + if skip_line: + formatted_text += line + continue + if not check(line): + line = format(line) + return_code = FAIL + print(f"Add spaces between EN and CN chars in: {file_path}:{lineno}:\t{line}") + formatted_text += line if return_code != PASS: with open(file_path, "w", encoding="utf8", newline="\n") as f: f.write(formatted_text) diff --git a/dochooks/insert_whitespace_between_cn_and_en_char/pragma.py b/dochooks/insert_whitespace_between_cn_and_en_char/pragma.py new file mode 100644 index 0000000..cb65aac --- /dev/null +++ b/dochooks/insert_whitespace_between_cn_and_en_char/pragma.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from contextlib import contextmanager +from enum import Enum + + +class Pragma(Enum): + SKIP_NEXT_LINE = "dochooks: skip-next-line" + SKIP_LINE = "dochooks: skip-line" + + +class PragmaManager: + def __init__(self): + self.skip_line = False + + @contextmanager + def scan(self, line: str): + if Pragma.SKIP_LINE.value in line: + self.skip_line = True + yield self.skip_line + self.skip_line = False + if Pragma.SKIP_NEXT_LINE.value in line: + self.skip_line = True