-
Notifications
You must be signed in to change notification settings - Fork 908
[Tooling] Add RST single-backtick guardrail #7862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ghost
wants to merge
6
commits into
PaddlePaddle:develop
from
StatefulDust:feat/rst-single-backtick-guardrail
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
906ed09
[Tooling] Add RST single-backtick guardrail
Noai-oss 33e83f7
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into f…
Noai-oss aee9255
[Tooling] Handle RST literal blocks in backtick guardrail
Noai-oss 9e561d8
[Tooling] Support parsed-literal in backtick guardrail
Noai-oss 45d8ea3
[Tooling] Add RST single-backtick baseline and diff script
Noai-oss 063948c
[Tooling] Fix literal block indent baseline in checker
Noai-oss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import argparse | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| SINGLE_BACKTICK_RE = re.compile(r"(?<!`)`([^`\n]+)`(?!`)") | ||
| ROLE_SUFFIX_RE = re.compile(r":[A-Za-z0-9_.:-]+:$") | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser( | ||
| description="check invalid single-backtick usage in rst files" | ||
| ) | ||
| parser.add_argument( | ||
| "files", | ||
| nargs="*", | ||
| help="rst files to check", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def should_ignore_match(line, start, end): | ||
| if end < len(line) and line[end] == "_": | ||
| return True | ||
|
|
||
| prefix = line[:start].rstrip() | ||
| if ROLE_SUFFIX_RE.search(prefix): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| def find_invalid_single_backticks(line): | ||
| matches = [] | ||
| for match in SINGLE_BACKTICK_RE.finditer(line): | ||
| start, end = match.span() | ||
| if should_ignore_match(line, start, end): | ||
| continue | ||
| matches.append(match) | ||
| return matches | ||
|
|
||
|
|
||
| def check_file(path): | ||
| has_error = False | ||
| lines = path.read_text(encoding="utf-8").splitlines() | ||
| for lineno, line in enumerate(lines, 1): | ||
| matches = find_invalid_single_backticks(line) | ||
| for match in matches: | ||
| has_error = True | ||
| column = match.start() + 1 | ||
| snippet = match.group(0) | ||
| print( | ||
| f"{path}:{lineno}:{column}: invalid single backticks {snippet}; use ``...`` for inline literals or an rst role/link when appropriate." | ||
| ) | ||
|
ooooo-create marked this conversation as resolved.
|
||
| return has_error | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
| has_error = False | ||
|
|
||
| for file_name in args.files: | ||
| path = Path(file_name) | ||
| if path.suffix != ".rst" or not path.exists(): | ||
| continue | ||
| has_error |= check_file(path) | ||
|
|
||
| if has_error: | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.