Skip to content

Commit

Permalink
✨ feat: add pragma dochooks: skip-line and `dochooks: skip-next-lin…
Browse files Browse the repository at this point in the history
…e` to suppressing lint error (#46)

* ✨ feat: add pragma `dochooks: skip-line` and `dochooks: skip-next-line` to suppressing lint error

* Update README.md

Co-authored-by: ooo oo <[email protected]>

---------

Co-authored-by: ooo oo <[email protected]>
  • Loading branch information
SigureMo and ooooo-create authored Oct 13, 2024
1 parent 15d9ceb commit b3cea43
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ repos:
### `insert-whitespace-between-cn-and-en-char`

用于自动在中英文之间添加空格

## Pragma

dochooks 支持 `dochooks: skip-next-line` 和 `dochooks: skip-line` 两种 pragma

<!-- prettier-ignore -->
```markdown
<!-- dochooks: skip-next-line -->
这是一段中文and English混合的text
这是一段中文and English混合的text <!-- dochooks: skip-line -->
```
11 changes: 8 additions & 3 deletions dochooks/insert_whitespace_between_cn_and_en_char/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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


Expand Down
16 changes: 11 additions & 5 deletions dochooks/insert_whitespace_between_cn_and_en_char/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions dochooks/insert_whitespace_between_cn_and_en_char/pragma.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3cea43

Please sign in to comment.