|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Check markdown files for missing blank lines before list items. |
| 4 | +
|
| 5 | +This script identifies places where text is immediately followed by a list item |
| 6 | +without a blank line, which can cause rendering issues in markdown parsers. |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import os |
| 11 | +import re |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +def get_indentation(line): |
| 17 | + """Get the number of leading spaces/tabs in a line.""" |
| 18 | + return len(line) - len(line.lstrip()) |
| 19 | + |
| 20 | + |
| 21 | +def is_list_item(line): |
| 22 | + """Check if a line is a list item (ordered, unordered, or nested).""" |
| 23 | + stripped = line.lstrip() |
| 24 | + # Unordered list: starts with -, *, or + |
| 25 | + if re.match(r'^[-*+]\s', stripped): |
| 26 | + return True |
| 27 | + # Ordered list: starts with number followed by . or ) |
| 28 | + if re.match(r'^\d+[.)]\s', stripped): |
| 29 | + return True |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +def is_blank(line): |
| 34 | + """Check if a line is blank or whitespace only.""" |
| 35 | + return line.strip() == '' |
| 36 | + |
| 37 | + |
| 38 | +def is_code_fence(line): |
| 39 | + """Check if a line is a code fence.""" |
| 40 | + stripped = line.strip() |
| 41 | + return stripped.startswith('```') or stripped.startswith('~~~') |
| 42 | + |
| 43 | + |
| 44 | +def is_within_list_context(lines, current_idx): |
| 45 | + """ |
| 46 | + Check if we're currently within a list context by looking backwards. |
| 47 | + Returns True if there's a recent list item without intervening blank lines. |
| 48 | + """ |
| 49 | + # Look back up to 10 lines for a list item |
| 50 | + for i in range(current_idx - 1, max(current_idx - 10, -1), -1): |
| 51 | + line = lines[i] |
| 52 | + |
| 53 | + if is_blank(line): |
| 54 | + # Hit a blank line, no longer in list context |
| 55 | + return False |
| 56 | + |
| 57 | + if is_list_item(line): |
| 58 | + # Found a list item, we're in list context |
| 59 | + return True |
| 60 | + |
| 61 | + return False |
| 62 | + |
| 63 | + |
| 64 | +def needs_blank_line_before_list(lines, current_idx): |
| 65 | + """ |
| 66 | + Determine if a blank line is needed before the current line. |
| 67 | +
|
| 68 | + Returns True if: |
| 69 | + - Current line is a list item |
| 70 | + - Previous line is NOT blank |
| 71 | + - Previous line is NOT a code fence |
| 72 | + - We're NOT already within a list context |
| 73 | + - Previous line is NOT a list item |
| 74 | + - Current line is NOT more indented (nested list) |
| 75 | + """ |
| 76 | + if current_idx == 0: |
| 77 | + return False |
| 78 | + |
| 79 | + curr_line = lines[current_idx] |
| 80 | + prev_line = lines[current_idx - 1] |
| 81 | + |
| 82 | + if not is_list_item(curr_line): |
| 83 | + return False |
| 84 | + |
| 85 | + if is_blank(prev_line): |
| 86 | + return False |
| 87 | + |
| 88 | + # If previous line is a code fence, no blank line needed |
| 89 | + if is_code_fence(prev_line): |
| 90 | + return False |
| 91 | + |
| 92 | + # If previous line is also a list item, no blank line needed |
| 93 | + if is_list_item(prev_line): |
| 94 | + return False |
| 95 | + |
| 96 | + # Check if we're within a list context (continuing list) |
| 97 | + if is_within_list_context(lines, current_idx): |
| 98 | + return False |
| 99 | + |
| 100 | + # Get indentation levels |
| 101 | + prev_indent = get_indentation(prev_line) |
| 102 | + curr_indent = get_indentation(curr_line) |
| 103 | + |
| 104 | + # If current line is more indented than previous, it's likely a nested list |
| 105 | + # Allow some flexibility (at least 2 spaces more for nesting) |
| 106 | + if curr_indent > prev_indent + 1: |
| 107 | + return False |
| 108 | + |
| 109 | + # If previous line ends with certain patterns, it might be okay |
| 110 | + prev_stripped = prev_line.strip() |
| 111 | + |
| 112 | + # Skip if previous line looks like a heading |
| 113 | + if prev_stripped.startswith('#'): |
| 114 | + return False |
| 115 | + |
| 116 | + # Skip if previous line is HTML/markdown directive |
| 117 | + if prev_stripped.startswith('<') or prev_stripped.startswith('>'): |
| 118 | + return False |
| 119 | + |
| 120 | + # Otherwise, we likely need a blank line |
| 121 | + return True |
| 122 | + |
| 123 | + |
| 124 | +def check_file(filepath): |
| 125 | + """Check a single markdown file for formatting issues.""" |
| 126 | + issues = [] |
| 127 | + |
| 128 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 129 | + lines = f.readlines() |
| 130 | + |
| 131 | + in_code_block = False |
| 132 | + |
| 133 | + for i, line in enumerate(lines): |
| 134 | + # Track code blocks to skip them |
| 135 | + if is_code_fence(line): |
| 136 | + in_code_block = not in_code_block |
| 137 | + continue |
| 138 | + |
| 139 | + if in_code_block: |
| 140 | + continue |
| 141 | + |
| 142 | + # Check if we need a blank line before this line |
| 143 | + if needs_blank_line_before_list(lines, i): |
| 144 | + issues.append({ |
| 145 | + 'line_num': i + 1, |
| 146 | + 'line': line.rstrip(), |
| 147 | + 'prev_line': lines[i - 1].rstrip() |
| 148 | + }) |
| 149 | + |
| 150 | + return issues |
| 151 | + |
| 152 | + |
| 153 | +def main(): |
| 154 | + """Main entry point.""" |
| 155 | + parser = argparse.ArgumentParser( |
| 156 | + description='Check markdown files for missing blank lines before list items.' |
| 157 | + ) |
| 158 | + parser.add_argument( |
| 159 | + 'paths', |
| 160 | + nargs='*', |
| 161 | + help='Files or directories to check (default: docs/ directory)' |
| 162 | + ) |
| 163 | + parser.add_argument( |
| 164 | + '-v', '--verbose', |
| 165 | + action='store_true', |
| 166 | + help='Show all files being checked, not just files with issues' |
| 167 | + ) |
| 168 | + |
| 169 | + args = parser.parse_args() |
| 170 | + |
| 171 | + if args.paths: |
| 172 | + # Check if argument is a directory or file(s) |
| 173 | + files_to_check = [] |
| 174 | + for path_str in args.paths: |
| 175 | + arg_path = Path(path_str) |
| 176 | + if arg_path.is_dir(): |
| 177 | + # Recursively find all .md files in directory |
| 178 | + files_to_check.extend(arg_path.rglob('*.md')) |
| 179 | + elif arg_path.is_file(): |
| 180 | + # Add specific file |
| 181 | + files_to_check.append(arg_path) |
| 182 | + else: |
| 183 | + print(f"Warning: {path_str} is not a valid file or directory") |
| 184 | + else: |
| 185 | + # Check all markdown files in docs/ |
| 186 | + docs_dir = Path(__file__).parent.parent / 'docs' |
| 187 | + if not docs_dir.exists(): |
| 188 | + print(f"Error: docs directory not found at {docs_dir}") |
| 189 | + return 1 |
| 190 | + |
| 191 | + files_to_check = list(docs_dir.rglob('*.md')) |
| 192 | + |
| 193 | + total_issues = 0 |
| 194 | + files_with_issues = [] |
| 195 | + |
| 196 | + for filepath in files_to_check: |
| 197 | + filepath = Path(filepath) |
| 198 | + if not filepath.exists(): |
| 199 | + print(f"Warning: {filepath} does not exist") |
| 200 | + continue |
| 201 | + |
| 202 | + if args.verbose: |
| 203 | + print(f"Checking {filepath}...", end='', flush=True) |
| 204 | + |
| 205 | + issues = check_file(filepath) |
| 206 | + |
| 207 | + if issues: |
| 208 | + if args.verbose: |
| 209 | + print(f" {len(issues)} issue(s) found") |
| 210 | + else: |
| 211 | + print(f"{filepath}: {len(issues)} issue(s) found") |
| 212 | + |
| 213 | + files_with_issues.append(filepath) |
| 214 | + total_issues += len(issues) |
| 215 | + for issue in issues: |
| 216 | + print(f" Line {issue['line_num']}: Missing blank line before list item") |
| 217 | + print(f" Previous: {issue['prev_line']}") |
| 218 | + print(f" Current: {issue['line']}") |
| 219 | + else: |
| 220 | + if args.verbose: |
| 221 | + print(" OK") |
| 222 | + |
| 223 | + if total_issues > 0: |
| 224 | + print(f"\nFound {total_issues} issue(s) in {len(files_with_issues)} file(s)") |
| 225 | + return 1 |
| 226 | + else: |
| 227 | + if args.verbose: |
| 228 | + print("No issues found!") |
| 229 | + return 0 |
| 230 | + |
| 231 | + |
| 232 | +if __name__ == '__main__': |
| 233 | + sys.exit(main()) |
0 commit comments