Browse Source

Skip long line check for rst definition list term

Change-Id: Ieb272e506034eac7129c24ee0e0fbc40b55d11b1
Closes-Bug: #1533238
KATO Tomoyuki 3 years ago
parent
commit
a96c31fb3e
2 changed files with 35 additions and 0 deletions
  1. 18
    0
      doc8/checks.py
  2. 17
    0
      doc8/tests/test_checks.py

+ 18
- 0
doc8/checks.py View File

@@ -206,6 +206,24 @@ class CheckMaxLineLength(ContentCheck):
206 206
                 directives.append((i, find_directive_end(i, lines)))
207 207
             elif re.match(r"^::\s*$", line):
208 208
                 directives.append((i, find_directive_end(i, lines)))
209
+
210
+        # Find definition terms in definition lists
211
+        # This check may match the code, which is already appended
212
+        lwhitespaces = r"^\s*"
213
+        listspattern = r"^\s*(\* |- |#\. |\d+\. )"
214
+        for i in range(0, len(lines) - 1):
215
+            line = lines[i]
216
+            next_line = lines[i + 1]
217
+            # if line is a blank, line is not a definition term
218
+            if all_whitespace(line):
219
+                continue
220
+            # if line is a list, line is checked as normal line
221
+            if re.match(listspattern, line):
222
+                continue
223
+            if (len(re.search(lwhitespaces, line).group()) <
224
+                    len(re.search(lwhitespaces, next_line).group())):
225
+                directives.append((i, i))
226
+
209 227
         return directives
210 228
 
211 229
     def _txt_checker(self, parsed_file):

+ 17
- 0
doc8/tests/test_checks.py View File

@@ -156,6 +156,23 @@ test
156 156
                 errors = list(check.report_iter(parsed_file))
157 157
                 self.assertEqual(expected_errors, len(errors))
158 158
 
159
+    def test_definition_term_length(self):
160
+        conf = {
161
+            'max_line_length': 79,
162
+            'allow_long_titles': True,
163
+        }
164
+        with tempfile.NamedTemporaryFile(suffix='.rst') as fh:
165
+            fh.write(b'Definition List which contains long term.\n\n'
166
+                     b'looooooooooooooooooooooooooooooong definition term'
167
+                     b'this line exceeds 80 chars but should be ignored\n'
168
+                     b' this is a definition\n')
169
+            fh.flush()
170
+
171
+            parsed_file = parser.ParsedFile(fh.name, encoding='utf-8')
172
+            check = checks.CheckMaxLineLength(conf)
173
+            errors = list(check.report_iter(parsed_file))
174
+            self.assertEqual(0, len(errors))
175
+
159 176
 
160 177
 class TestNewlineEndOfFile(testtools.TestCase):
161 178
     def test_newline(self):

Loading…
Cancel
Save