diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0d2c24b..109cee1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,8 +48,8 @@ repos: args: [--fix] - id: ruff-format - - repo: https://github.com/ulgens/ruffen-docs - rev: 1def1ebbc1bd7f6b9934121948d0d06515f71e3f + - repo: . + rev: 7ed7e55de4a6515a97ee8c2202db2842d00b2b6c hooks: - id: blacken-docs language: python @@ -57,6 +57,18 @@ repos: - black==25.12.0 files: \.rst$ + - id: ruffen-docs-check + language: python + additional_dependencies: + - ruff==0.14.10 + files: \.rst$ + + - id: ruffen-docs-format + language: python + additional_dependencies: + - ruff==0.14.10 + files: \.rst$ + - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.19.1 hooks: diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 03d025a..56d2c31 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -7,14 +7,14 @@ files: \.(rst|md|markdown|py|tex)$ - id: ruffen-docs-check - name: ruffen-docs-check + name: ruffen-docs check description: Run `ruff check` on Python code blocks in documentation files entry: ruffen-docs-check language: python files: \.(rst|md|markdown|py|tex)$ - id: ruffen-docs-format - name: ruffen-docs-format + name: ruffen-docs format description: Run `ruff format` on Python code blocks in documentation files entry: ruffen-docs-format language: python diff --git a/src/ruffen_docs/__init__.py b/src/ruffen_docs/__init__.py index db18a40..9d5c778 100644 --- a/src/ruffen_docs/__init__.py +++ b/src/ruffen_docs/__init__.py @@ -6,7 +6,7 @@ from black.const import DEFAULT_LINE_LENGTH from black.mode import TargetVersion -from .formatters import BlackFormatter +from .processors import BlackFormatter, RuffChecker, RuffFormatter def run_black(argv: Sequence[str] | None = None) -> int: @@ -68,6 +68,7 @@ def run_black(argv: Sequence[str] | None = None) -> int: retv = 0 for filename in args.filenames: + print(filename) formatter = BlackFormatter(**formatter_kwargs) retv |= formatter.format_file( filename, @@ -79,10 +80,64 @@ def run_black(argv: Sequence[str] | None = None) -> int: def run_check(argv: Sequence[str] | None = None) -> int: - ... - return 1 + parser = argparse.ArgumentParser() + parser.add_argument( + "-l", + "--line-length", + type=int, + default=DEFAULT_LINE_LENGTH, + ) + parser.add_argument( + "-S", + "--skip-string-normalization", + action="store_true", + ) + parser.add_argument("-E", "--skip-errors", action="store_true") + parser.add_argument("filenames", nargs="*") + args = parser.parse_args(argv) + + retv = 0 + + for filename in args.filenames: + formatter = RuffChecker() + retv |= formatter.format_file( + filename, + skip_errors=args.skip_errors, + # rst_literal_blocks=args.rst_literal_blocks, + # check_only=args.check, + ) + + return retv def run_format(argv: Sequence[str] | None = None) -> int: - ... - return 1 + parser = argparse.ArgumentParser() + parser.add_argument( + "-l", + "--line-length", + type=int, + default=DEFAULT_LINE_LENGTH, + ) + parser.add_argument( + "-S", + "--skip-string-normalization", + action="store_true", + ) + parser.add_argument("-E", "--skip-errors", action="store_true") + parser.add_argument("filenames", nargs="*") + args = parser.parse_args(argv) + + filenames = [] + + retv = 0 + + for filename in filenames: + formatter = RuffFormatter() + retv |= formatter.format_file( + filename, + skip_errors=args.skip_errors, + # rst_literal_blocks=args.rst_literal_blocks, + # check_only=args.check, + ) + + return retv diff --git a/src/ruffen_docs/formatters.py b/src/ruffen_docs/processors.py similarity index 88% rename from src/ruffen_docs/formatters.py rename to src/ruffen_docs/processors.py index ec00082..d39d948 100644 --- a/src/ruffen_docs/formatters.py +++ b/src/ruffen_docs/processors.py @@ -2,6 +2,7 @@ import contextlib import re +import subprocess import textwrap from abc import ABC, abstractmethod from bisect import bisect @@ -13,6 +14,7 @@ from black import Mode from black.const import DEFAULT_LINE_LENGTH from black.mode import TargetVersion +from ruff.__main__ import find_ruff_bin from .constants import PYGMENTS_PY_LANGS from .errors import CodeBlockError @@ -33,7 +35,11 @@ TRAILING_NL_RE, ) -__all__ = ("BlackFormatter",) +__all__ = ( + "BlackFormatter", + "RuffChecker", + "RuffFormatter", +) class BaseProcessor(ABC): @@ -264,8 +270,8 @@ def format_file( self, filename: str, skip_errors: bool, - rst_literal_blocks: bool, - check_only: bool, + rst_literal_blocks: bool = True, + check_only: bool = False, ) -> int: with Path(filename).open(encoding="UTF-8") as f: contents = f.read() @@ -324,3 +330,39 @@ def __init__( def process_code_block(self, code_block: str) -> str: return black.format_str(code_block, mode=self.mode) + + +class RuffChecker(BaseProcessor): + def __init__(self) -> None: + super().__init__() + + def process_code_block(self, code_block: str) -> str: + # https://gist.github.com/jorenham/63942278b01515ffdeb0c7d4d1895684 + result = subprocess.run( + [find_ruff_bin(), "check", "-"], + input=code_block, + text=True, + capture_output=True, + # check=True, + cwd=Path.cwd(), + ) + result.check_returncode() + return result.stdout + + +class RuffFormatter(BaseProcessor): + def __init__(self) -> None: + super().__init__() + + def process_code_block(self, code_block: str) -> str: + # https://gist.github.com/jorenham/63942278b01515ffdeb0c7d4d1895684 + result = subprocess.run( + [find_ruff_bin(), "format", "-"], + input=code_block, + text=True, + capture_output=True, + # check=True, + cwd=Path.cwd(), + ) + result.check_returncode() + return result.stdout diff --git a/tests/test_ruffen_docs.py b/tests/test_ruffen_docs.py index 95378e8..57cb062 100644 --- a/tests/test_ruffen_docs.py +++ b/tests/test_ruffen_docs.py @@ -8,7 +8,7 @@ __main__, # noqa: F401 run_black, ) -from ruffen_docs.formatters import BlackFormatter as Formatter +from ruffen_docs.processors import BlackFormatter as Formatter BLACK_MODE = Mode()