-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_asciidoc_spacing.py
More file actions
executable file
·134 lines (108 loc) · 4.11 KB
/
format_asciidoc_spacing.py
File metadata and controls
executable file
·134 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""
format-asciidoc-spacing - Format AsciiDoc spacing.
Ensures blank lines after headings and around include directives.
"""
import argparse
import sys
from pathlib import Path
from doc_utils.format_asciidoc_spacing import process_file, find_adoc_files
from doc_utils.version_check import check_version_on_startup
from doc_utils.version import __version__
from doc_utils.spinner import Spinner
# Colors for output
class Colors:
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
NC = '\033[0m' # No Color
def print_colored(message: str, color: str = Colors.NC) -> None:
"""Print message with color"""
print(f"{color}{message}{Colors.NC}")
def main():
# Check for updates (non-blocking, won't interfere with tool operation)
check_version_on_startup()
"""Main entry point"""
parser = argparse.ArgumentParser(
description="Format AsciiDoc files to ensure proper spacing",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Format AsciiDoc files to ensure proper spacing:
- Blank line after headings (=, ==, ===, etc.)
- Blank lines around include:: directives
Examples:
%(prog)s # Process all .adoc files in current directory
%(prog)s modules/ # Process all .adoc files in modules/
%(prog)s assemblies/my-guide.adoc # Process single file
%(prog)s --dry-run modules/ # Preview changes without modifying
"""
)
parser.add_argument(
'path',
nargs='?',
default='.',
help='File or directory to process (default: current directory)'
)
parser.add_argument(
'-n', '--dry-run',
action='store_true',
help='Show what would be changed without modifying files'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Show detailed output'
)
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
args = parser.parse_args()
# Convert path to Path object
target_path = Path(args.path)
# Check if path exists
if not target_path.exists():
print_colored(f"Error: Path does not exist: {target_path}", Colors.RED)
sys.exit(1)
# Display dry-run mode message
if args.dry_run:
print_colored("DRY RUN MODE - No files will be modified", Colors.YELLOW)
# Find all AsciiDoc files
adoc_files = find_adoc_files(target_path)
if not adoc_files:
if target_path.is_file():
print_colored(f"Warning: {target_path} is not an AsciiDoc file (.adoc)", Colors.YELLOW)
print(f"Processed 0 AsciiDoc file(s)")
print("AsciiDoc spacing formatting complete!")
return
# Process each file
files_processed = 0
files_modified = 0
for file_path in adoc_files:
try:
changes_made, messages = process_file(file_path, args.dry_run, args.verbose)
# Print verbose messages
if args.verbose:
for msg in messages:
print(msg)
if changes_made:
files_modified += 1
if args.dry_run:
print_colored(f"Would modify: {file_path}", Colors.YELLOW)
else:
print_colored(f"Modified: {file_path}", Colors.GREEN)
elif args.verbose:
print(f" No changes needed for: {file_path}")
files_processed += 1
except KeyboardInterrupt:
print_colored("\nOperation cancelled by user", Colors.YELLOW)
sys.exit(1)
except IOError as e:
print_colored(f"{e}", Colors.RED)
except Exception as e:
print_colored(f"Unexpected error processing {file_path}: {e}", Colors.RED)
print(f"Processed {files_processed} AsciiDoc file(s)")
if args.dry_run and files_modified > 0:
print(f"Would modify {files_modified} file(s)")
elif files_modified > 0:
print(f"Modified {files_modified} file(s)")
print("AsciiDoc spacing formatting complete!")
if __name__ == "__main__":
main()