|
1 | 1 | import argparse |
| 2 | +import os |
2 | 3 | import re |
3 | 4 | import sys |
4 | 5 | from pathlib import Path |
5 | 6 |
|
| 7 | +IN_GITHUB_ACTIONS = os.environ.get("GITHUB_ACTIONS") == "true" |
| 8 | + |
| 9 | + |
| 10 | +def emit(path, line, message): |
| 11 | + print(f"{path}:{line}: {message}") |
| 12 | + if IN_GITHUB_ACTIONS: |
| 13 | + print(f"::error file={path},line={line}::{message}") |
| 14 | + |
6 | 15 | SMALL_WORDS = { |
7 | 16 | "a", "an", "the", |
8 | 17 | "and", "or", "but", "nor", "so", "yet", |
|
17 | 26 | INLINE_CODE_RE = re.compile(r"`[^`]*`") |
18 | 27 |
|
19 | 28 |
|
| 29 | +TOKEN_RE = re.compile(r"[A-Za-z0-9'’]+|[^A-Za-z0-9'’]+") |
| 30 | + |
| 31 | + |
20 | 32 | def split_words_preserving_delims(text): |
21 | 33 | tokens = [] |
22 | | - for part in re.split(r"(\s+|-)", text): |
23 | | - if part == "": |
24 | | - continue |
25 | | - is_word = bool(re.match(r"^[A-Za-z][A-Za-z0-9'’]*$", part)) |
| 34 | + for part in TOKEN_RE.findall(text): |
| 35 | + is_word = part[0].isalpha() |
26 | 36 | tokens.append((part, is_word)) |
27 | 37 | return tokens |
28 | 38 |
|
@@ -139,8 +149,8 @@ def main(): |
139 | 149 | issues = process_file(path, fix=args.fix) |
140 | 150 | for lineno, hashes, raw_text, suggestion in issues: |
141 | 151 | total_issues += 1 |
142 | | - print(f"{path}:{lineno + 1}: {hashes} {raw_text}") |
143 | | - print(f" suggestion -> {hashes} {suggestion}") |
| 152 | + message = f"Heading case: '{hashes} {raw_text}' should be '{hashes} {suggestion}'" |
| 153 | + emit(str(path), lineno + 1, message) |
144 | 154 |
|
145 | 155 | if total_issues == 0: |
146 | 156 | print("All headings pass title case check.") |
|
0 commit comments