|
49 | 49 |
|
50 | 50 | CAP_EXCEPTIONS_DICT = {word.lower(): word for word in CAPITALIZATION_EXCEPTIONS}
|
51 | 51 |
|
52 |
| -bad_title_dict = {} |
53 |
| - |
54 | 52 | err_msg = "Heading capitalization formatted incorrectly. Please correctly capitalize"
|
55 | 53 |
|
56 | 54 |
|
@@ -79,36 +77,12 @@ def correct_title_capitalization(title: str) -> str:
|
79 | 77 | for word in word_list:
|
80 | 78 | if word.lower() in CAP_EXCEPTIONS_DICT:
|
81 | 79 | correct_title = re.sub(
|
82 |
| - r"\b" + word + r"\b", CAP_EXCEPTIONS_DICT[word.lower()], correct_title |
| 80 | + rf"\b{word}\b", CAP_EXCEPTIONS_DICT[word.lower()], correct_title |
83 | 81 | )
|
84 | 82 |
|
85 | 83 | return correct_title
|
86 | 84 |
|
87 | 85 |
|
88 |
| -def is_following_capitalization_convention(title: str) -> bool: |
89 |
| - """ |
90 |
| - Function to return if a given title is capitalized correctly |
91 |
| -
|
92 |
| - Parameters |
93 |
| - ---------- |
94 |
| - title : str |
95 |
| - Heading string to validate |
96 |
| -
|
97 |
| - Returns |
98 |
| - ------- |
99 |
| - bool |
100 |
| - True if title capitalized correctly, False if not |
101 |
| -
|
102 |
| - """ |
103 |
| - |
104 |
| - correct_title = correct_title_capitalization(title) |
105 |
| - |
106 |
| - if title != correct_title: |
107 |
| - return False |
108 |
| - else: |
109 |
| - return True |
110 |
| - |
111 |
| - |
112 | 86 | def find_titles(rst_file: str) -> Generator[Tuple[str, int], None, None]:
|
113 | 87 | """
|
114 | 88 | Algorithm to identify particular text that should be considered headings in an
|
@@ -161,28 +135,6 @@ def find_titles(rst_file: str) -> Generator[Tuple[str, int], None, None]:
|
161 | 135 | yield lines[line_no - 1].translate(table), line_no
|
162 | 136 |
|
163 | 137 |
|
164 |
| -def fill_bad_title_dict(rst_file: str) -> None: |
165 |
| - """ |
166 |
| - Method that fills up the bad_title_dict with incorrectly capitalized headings |
167 |
| -
|
168 |
| - Parameters |
169 |
| - ---------- |
170 |
| - rst_file : str |
171 |
| - Directory address of a .rst file as a string |
172 |
| -
|
173 |
| - """ |
174 |
| - |
175 |
| - if rst_file in bad_title_dict: |
176 |
| - return |
177 |
| - |
178 |
| - for title, line_number in find_titles(rst_file): |
179 |
| - if not is_following_capitalization_convention(title): |
180 |
| - if rst_file not in bad_title_dict: |
181 |
| - bad_title_dict[rst_file] = [(title, line_number)] |
182 |
| - else: |
183 |
| - bad_title_dict[rst_file].append((title, line_number)) |
184 |
| - |
185 |
| - |
186 | 138 | def find_rst_files(source_paths: List[str]) -> Generator[str, None, None]:
|
187 | 139 | """
|
188 | 140 | Given the command line arguments of directory paths, this method
|
@@ -234,21 +186,14 @@ def main(source_paths: List[str], output_format: str) -> bool:
|
234 | 186 |
|
235 | 187 | number_of_errors: int = 0
|
236 | 188 |
|
237 |
| - directory_list = find_rst_files(source_paths) |
238 |
| - |
239 |
| - for filename in directory_list: |
240 |
| - fill_bad_title_dict(filename) |
241 |
| - |
242 |
| - if len(bad_title_dict) == 0: |
243 |
| - return number_of_errors |
244 |
| - |
245 |
| - for key in bad_title_dict: |
246 |
| - for line in bad_title_dict[key]: |
247 |
| - print( |
248 |
| - f"""{key}:{line[1]}:{err_msg} "{line[0]}" to "{ |
249 |
| - correct_title_capitalization(line[0])}" """ |
250 |
| - ) |
251 |
| - number_of_errors += 1 |
| 189 | + for filename in find_rst_files(source_paths): |
| 190 | + for title, line_number in find_titles(filename): |
| 191 | + if title != correct_title_capitalization(title): |
| 192 | + print( |
| 193 | + f"""{filename}:{line_number}:{err_msg} "{title}" to "{ |
| 194 | + correct_title_capitalization(title)}" """ |
| 195 | + ) |
| 196 | + number_of_errors += 1 |
252 | 197 |
|
253 | 198 | return number_of_errors
|
254 | 199 |
|
|
0 commit comments