From b3cea43c786b486ac83c4b59ac0ace8c6b1d6609 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Sun, 13 Oct 2024 21:22:01 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20pragma=20`dochooks:?= =?UTF-8?q?=20skip-line`=20and=20`dochooks:=20skip-next-line`=20to=20suppr?= =?UTF-8?q?essing=20lint=20error=20(#46)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat: add pragma `dochooks: skip-line` and `dochooks: skip-next-line` to suppressing lint error * Update README.md Co-authored-by: ooo oo <106524776+ooooo-create@users.noreply.github.com> --------- Co-authored-by: ooo oo <106524776+ooooo-create@users.noreply.github.com> --- README.md | 11 +++++++++ .../check.py | 11 ++++++--- .../format.py | 16 +++++++++---- .../pragma.py | 23 +++++++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 dochooks/insert_whitespace_between_cn_and_en_char/pragma.py 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