/ scripts / validation / markdown_complexity.py
markdown_complexity.py
 1  #!/usr/bin/env python3
 2  import os
 3  import re
 4  import sys
 5  
 6  def analyze_complexity(file_path):
 7      with open(file_path, 'r', encoding='utf-8') as f:
 8          content = f.read()
 9  
10      # Count various elements
11      headings = len(re.findall(r'^#{1,6}', content, re.MULTILINE))
12      links = len(re.findall(r'\[.*?\]\(.*?\)', content))
13      code_blocks = len(re.findall(r'```', content)) // 2
14      lists = len(re.findall(r'^\s*[-*+]\s', content, re.MULTILINE))
15  
16      # Calculate complexity score
17      complexity = headings + (links * 0.5) + (code_blocks * 2) + (lists * 0.3)
18  
19      return {
20          'headings': headings,
21          'links': links,
22          'code_blocks': code_blocks,
23          'lists': lists,
24          'complexity': complexity
25      }
26  
27  if __name__ == "__main__":
28      high_complexity_files = []
29      for root, dirs, files in os.walk('docs'):
30          for file in files:
31              if file.endswith('.md'):
32                  file_path = os.path.join(root, file)
33                  stats = analyze_complexity(file_path)
34                  if stats['complexity'] > 50:  # Threshold for high complexity
35                      high_complexity_files.append((file_path, stats))
36  
37      if high_complexity_files:
38          print("❌ High complexity files detected:")
39          for file_path, stats in high_complexity_files:
40              print(f"  - {file_path}: complexity={stats['complexity']:.1f}")
41          sys.exit(1)
42      else:
43          print("✅ All files have reasonable complexity")
44          sys.exit(0)