c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
"""
|
|
Pre-commit hook that enforces reStructuredText inline-code syntax in reno release notes.
|
|
|
|
Release notes under ``releasenotes/notes/*.yaml`` are rendered as reStructuredText, where inline
|
|
code must use double backticks (``like_this``). A common mistake is to use Markdown-style single
|
|
backticks (`like_this`), which RST renders as italic "interpreted text" instead of code.
|
|
|
|
By default the hook rewrites single backticks to double backticks and exits non-zero when it
|
|
changes a file, so the rewrite can be reviewed before re-staging; ``--check`` only reports.
|
|
It leaves existing double backticks ``code`` untouched and skips RST roles (:func:`x`) and
|
|
hyperlink references (`text <url>`_).
|
|
"""
|
|
|
|
import argparse
|
|
import re
|
|
import sys
|
|
|
|
# A single-backtick inline-code span that should use double backticks. The look-arounds skip
|
|
# valid RST: ``...`` pairs (the backtick-adjacent guards), roles like :func:`x` (no ':' before
|
|
# the opening backtick), and hyperlink refs like `text <url>`_ (no '_' after the closing one).
|
|
SINGLE_BACKTICK_RE = re.compile(r"(?<![`:])`(?!`)([^`\n]+?)`(?![`_])")
|
|
|
|
|
|
def fix_text(text: str) -> str:
|
|
"""Return ``text`` with single-backtick inline code rewritten to double backticks."""
|
|
return SINGLE_BACKTICK_RE.sub(r"``\1``", text)
|
|
|
|
|
|
def main() -> int:
|
|
"""Entry point for the pre-commit hook."""
|
|
parser = argparse.ArgumentParser(description="Enforce double backticks in reno release notes.")
|
|
parser.add_argument("--check", action="store_true", help="Report problems without modifying files.")
|
|
parser.add_argument("files", nargs="*")
|
|
args = parser.parse_args()
|
|
|
|
ret = 0
|
|
for path in args.files:
|
|
with open(path, encoding="utf-8") as f:
|
|
original = f.read()
|
|
fixed = fix_text(original)
|
|
if fixed == original:
|
|
continue
|
|
ret = 1
|
|
print(f"{'single backtick in' if args.check else 'fixed'}: {path}", file=sys.stderr)
|
|
if not args.check:
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(fixed)
|
|
|
|
if ret:
|
|
hint = "Run without --check to fix automatically." if args.check else "Review and re-stage the changes."
|
|
print(f"\nrelease notes need double backticks (``like_this``) for inline code. {hint}", file=sys.stderr)
|
|
return ret
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|