|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Documentation linting script for OpenMS-docs. |
| 4 | +Checks for common style and consistency issues. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +class DocLinter: |
| 14 | + def __init__(self): |
| 15 | + self.errors = [] |
| 16 | + self.warnings = [] |
| 17 | + |
| 18 | + def check_file(self, filepath): |
| 19 | + """Check a single file for issues.""" |
| 20 | + with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: |
| 21 | + content = f.read() |
| 22 | + lines = content.split('\n') |
| 23 | + |
| 24 | + for i, line in enumerate(lines, 1): |
| 25 | + # Check for trailing whitespace |
| 26 | + if line.endswith(' ') or line.endswith('\t'): |
| 27 | + self.errors.append(f"{filepath}:{i}: Trailing whitespace") |
| 28 | + |
| 29 | + # Check for insecure HTTP links (excluding specific domains) |
| 30 | + if 'http://' in line and not any(x in line for x in ['localhost', 'openms.org', 'openms.de', 'rforge.net']): |
| 31 | + self.warnings.append(f"{filepath}:{i}: Consider using HTTPS instead of HTTP") |
| 32 | + |
| 33 | + # Check for inconsistent terminology |
| 34 | + if re.search(r'\b(openMS|Openms)\b', line): |
| 35 | + # Check if it's not in a URL, path, filename, or image directive |
| 36 | + if not re.search(r'(https?://|\.openms|/openms|openms-|openms\.|image::|\.\.\s+figure::|\{image\})', line): |
| 37 | + self.errors.append(f"{filepath}:{i}: Use 'OpenMS' instead of variant spelling") |
| 38 | + |
| 39 | + if re.search(r'\b(Toppview)\b', line): |
| 40 | + # Check if it's not in a URL, path, or image |
| 41 | + if not re.search(r'(https?://|/toppview|toppview-|image::|\.\.\s+figure::)', line, re.IGNORECASE): |
| 42 | + self.errors.append(f"{filepath}:{i}: Use 'TOPPView' (correct capitalization)") |
| 43 | + |
| 44 | + # Check for TODO/FIXME in markdown comments (but allow in code blocks) |
| 45 | + if re.search(r'<!--.*TODO.*-->', line) or re.search(r'<!--.*FIXME.*-->', line): |
| 46 | + self.warnings.append(f"{filepath}:{i}: TODO/FIXME comment found - consider creating an issue") |
| 47 | + |
| 48 | + def check_directory(self, directory): |
| 49 | + """Check all documentation files in a directory.""" |
| 50 | + for root, dirs, files in os.walk(directory): |
| 51 | + for file in files: |
| 52 | + if file.endswith('.md') or file.endswith('.rst'): |
| 53 | + filepath = os.path.join(root, file) |
| 54 | + self.check_file(filepath) |
| 55 | + |
| 56 | + def report(self): |
| 57 | + """Print the linting report.""" |
| 58 | + print(f"\n{'='*60}") |
| 59 | + print("OpenMS Documentation Linting Report") |
| 60 | + print(f"{'='*60}\n") |
| 61 | + |
| 62 | + if self.errors: |
| 63 | + print(f"❌ ERRORS ({len(self.errors)}):") |
| 64 | + for error in self.errors[:20]: # Show first 20 |
| 65 | + print(f" {error}") |
| 66 | + if len(self.errors) > 20: |
| 67 | + print(f" ... and {len(self.errors) - 20} more") |
| 68 | + print() |
| 69 | + |
| 70 | + if self.warnings: |
| 71 | + print(f"⚠️ WARNINGS ({len(self.warnings)}):") |
| 72 | + for warning in self.warnings[:20]: # Show first 20 |
| 73 | + print(f" {warning}") |
| 74 | + if len(self.warnings) > 20: |
| 75 | + print(f" ... and {len(self.warnings) - 20} more") |
| 76 | + print() |
| 77 | + |
| 78 | + if not self.errors and not self.warnings: |
| 79 | + print("✅ No issues found! Documentation looks good.") |
| 80 | + print() |
| 81 | + |
| 82 | + return len(self.errors) |
| 83 | + |
| 84 | + |
| 85 | +def main(): |
| 86 | + """Main entry point.""" |
| 87 | + linter = DocLinter() |
| 88 | + |
| 89 | + # Check docs directory |
| 90 | + docs_dir = Path(__file__).parent / 'docs' |
| 91 | + if docs_dir.exists(): |
| 92 | + linter.check_directory(docs_dir) |
| 93 | + else: |
| 94 | + print(f"Error: docs directory not found at {docs_dir}") |
| 95 | + return 1 |
| 96 | + |
| 97 | + # Generate report |
| 98 | + error_count = linter.report() |
| 99 | + |
| 100 | + # Return error code if errors found |
| 101 | + return 1 if error_count > 0 else 0 |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + sys.exit(main()) |
0 commit comments