rolfedh-doc-utils 0.1.6__tar.gz → 0.1.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. rolfedh_doc_utils-0.1.8/PKG-INFO +209 -0
  2. rolfedh_doc_utils-0.1.8/README.md +176 -0
  3. rolfedh_doc_utils-0.1.8/format_asciidoc_spacing.py +318 -0
  4. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/pyproject.toml +1 -1
  5. rolfedh_doc_utils-0.1.8/rolfedh_doc_utils.egg-info/PKG-INFO +209 -0
  6. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/rolfedh_doc_utils.egg-info/SOURCES.txt +1 -0
  7. rolfedh_doc_utils-0.1.6/PKG-INFO +0 -303
  8. rolfedh_doc_utils-0.1.6/README.md +0 -270
  9. rolfedh_doc_utils-0.1.6/rolfedh_doc_utils.egg-info/PKG-INFO +0 -303
  10. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/LICENSE +0 -0
  11. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/archive_unused_files.py +0 -0
  12. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/archive_unused_images.py +0 -0
  13. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/check_scannability.py +0 -0
  14. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/__init__.py +0 -0
  15. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/file_utils.py +0 -0
  16. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/scannability.py +0 -0
  17. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/topic_map_parser.py +0 -0
  18. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/unused_adoc.py +0 -0
  19. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/unused_attributes.py +0 -0
  20. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/doc_utils/unused_images.py +0 -0
  21. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/find_unused_attributes.py +0 -0
  22. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/rolfedh_doc_utils.egg-info/dependency_links.txt +0 -0
  23. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/rolfedh_doc_utils.egg-info/entry_points.txt +0 -0
  24. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/rolfedh_doc_utils.egg-info/requires.txt +0 -0
  25. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/rolfedh_doc_utils.egg-info/top_level.txt +0 -0
  26. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/setup.cfg +0 -0
  27. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/setup.py +0 -0
  28. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_archive_unused_files.py +0 -0
  29. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_archive_unused_images.py +0 -0
  30. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_auto_discovery.py +0 -0
  31. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_check_scannability.py +0 -0
  32. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_cli_entry_points.py +0 -0
  33. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_file_utils.py +0 -0
  34. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_fixture_archive_unused_files.py +0 -0
  35. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_fixture_archive_unused_images.py +0 -0
  36. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_fixture_check_scannability.py +0 -0
  37. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_parse_exclude_list.py +0 -0
  38. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_symlink_handling.py +0 -0
  39. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_topic_map_parser.py +0 -0
  40. {rolfedh_doc_utils-0.1.6 → rolfedh_doc_utils-0.1.8}/tests/test_unused_attributes.py +0 -0
@@ -0,0 +1,209 @@
1
+ Metadata-Version: 2.4
2
+ Name: rolfedh-doc-utils
3
+ Version: 0.1.8
4
+ Summary: CLI tools for AsciiDoc documentation projects
5
+ Author: Rolfe Dlugy-Hegwer
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Rolfe Dlugy-Hegwer
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Requires-Python: >=3.8
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: PyYAML>=6.0
32
+ Dynamic: license-file
33
+
34
+ # doc-utils
35
+
36
+ [![PyPI version](https://badge.fury.io/py/rolfedh-doc-utils.svg)](https://pypi.org/project/rolfedh-doc-utils/)
37
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
38
+ [![License](https://img.shields.io/github/license/rolfedh/doc-utils)](https://github.com/rolfedh/doc-utils/blob/main/LICENSE)
39
+ [![Documentation](https://img.shields.io/badge/docs-GitHub%20Pages-brightgreen)](https://rolfedh.github.io/doc-utils/)
40
+
41
+ Python CLI tools for maintaining clean, consistent AsciiDoc documentation repositories.
42
+
43
+ 📚 **[View Full Documentation](https://rolfedh.github.io/doc-utils/)** | 📦 **[PyPI Package](https://pypi.org/project/rolfedh-doc-utils/)** | 🐙 **[GitHub](https://github.com/rolfedh/doc-utils)**
44
+
45
+ ## ⚠️ Safety First
46
+
47
+ These tools can modify or delete files. **Always:**
48
+ - Work in a git branch (never main/master)
49
+ - Review changes with `git diff`
50
+ - Test documentation builds after changes
51
+
52
+ ## 🚀 Quick Start
53
+
54
+ ### Install with pipx (Recommended)
55
+
56
+ ```bash
57
+ # Install
58
+ pipx install rolfedh-doc-utils
59
+
60
+ # Upgrade to latest version
61
+ pipx upgrade rolfedh-doc-utils
62
+ ```
63
+
64
+ ### Alternative Installation
65
+
66
+ ```bash
67
+ # With pip user flag
68
+ pip install --user rolfedh-doc-utils
69
+
70
+ # For development
71
+ git clone https://github.com/rolfedh/doc-utils.git
72
+ cd doc-utils
73
+ pip install -e .
74
+ ```
75
+
76
+ ## 🛠️ Available Tools
77
+
78
+ **Note:** Commands use hyphens (`-`), while Python files use underscores (`_`). After installing with pipx, use the hyphenated commands directly.
79
+
80
+ | Tool | Description | Usage |
81
+ |------|-------------|-------|
82
+ | **`format-asciidoc-spacing`** | Standardizes spacing after headings and around includes | `format-asciidoc-spacing --dry-run modules/` |
83
+ | **`check-scannability`** | Analyzes readability (sentence/paragraph length) | `check-scannability --max-words 25` |
84
+ | **`archive-unused-files`** | Finds and archives unreferenced .adoc files | `archive-unused-files` (preview)<br>`archive-unused-files --archive` (execute) |
85
+ | **`archive-unused-images`** | Finds and archives unreferenced images | `archive-unused-images` (preview)<br>`archive-unused-images --archive` (execute) |
86
+ | **`find-unused-attributes`** | Identifies unused attribute definitions | `find-unused-attributes attributes.adoc` |
87
+
88
+ ## 📖 Documentation
89
+
90
+ Comprehensive documentation is available at **[rolfedh.github.io/doc-utils](https://rolfedh.github.io/doc-utils/)**
91
+
92
+ ### OpenShift-docs Example
93
+
94
+ For OpenShift documentation repositories:
95
+
96
+ ```bash
97
+ # Format spacing in specific directories
98
+ format-asciidoc-spacing modules/
99
+ format-asciidoc-spacing microshift_networking/
100
+
101
+ # Preview changes first
102
+ format-asciidoc-spacing --dry-run modules/networking/
103
+
104
+ # Process specific file
105
+ format-asciidoc-spacing modules/networking/about-networking.adoc
106
+ ```
107
+
108
+ - [Getting Started Guide](https://rolfedh.github.io/doc-utils/getting-started)
109
+ - [Tools Reference](https://rolfedh.github.io/doc-utils/tools/)
110
+ - [Best Practices](https://rolfedh.github.io/doc-utils/best-practices)
111
+ - [Contributing](https://rolfedh.github.io/doc-utils/contributing)
112
+
113
+ ## 💡 Common Workflows
114
+
115
+ ### Clean Up Documentation
116
+
117
+ ```bash
118
+ # 1. Create a branch
119
+ git checkout -b doc-cleanup
120
+
121
+ # 2. Preview what would change
122
+ format-asciidoc-spacing --dry-run .
123
+ archive-unused-files
124
+ archive-unused-images
125
+
126
+ # 3. Apply changes
127
+ format-asciidoc-spacing .
128
+ archive-unused-files --archive
129
+ archive-unused-images --archive
130
+
131
+ # 4. Review and commit
132
+ git diff
133
+ git add -A && git commit -m "Clean up documentation"
134
+ ```
135
+
136
+ ### Check Documentation Quality
137
+
138
+ ```bash
139
+ # Check readability
140
+ check-scannability --max-words 30 --max-sentences 5
141
+
142
+ # Find unused attributes
143
+ find-unused-attributes attributes.adoc
144
+
145
+ # Save results
146
+ find-unused-attributes attributes.adoc --output unused.txt
147
+ ```
148
+
149
+ ## 🔧 Exclusions
150
+
151
+ All tools support excluding files and directories:
152
+
153
+ ```bash
154
+ # Exclude specific directories
155
+ archive-unused-files --exclude-dir ./temp --exclude-dir ./drafts
156
+
157
+ # Exclude specific files
158
+ check-scannability --exclude-file ./README.adoc
159
+
160
+ # Use exclusion list file
161
+ echo "./deprecated/" > .docutils-ignore
162
+ archive-unused-images --exclude-list .docutils-ignore
163
+ ```
164
+
165
+ ## 🧪 Development
166
+
167
+ ```bash
168
+ # Install dev dependencies
169
+ pip install -r requirements-dev.txt
170
+
171
+ # Run tests
172
+ python -m pytest tests/ -v
173
+
174
+ # Run specific test
175
+ python -m pytest tests/test_file_utils.py
176
+ ```
177
+
178
+ ## 🤝 Contributing
179
+
180
+ We welcome contributions! See our [Contributing Guide](https://rolfedh.github.io/doc-utils/contributing) for details.
181
+
182
+ Before submitting PRs:
183
+ - ✅ All tests pass
184
+ - ✅ Code follows PEP 8
185
+ - ✅ Documentation updated
186
+ - ✅ Changelog entry added
187
+
188
+ ## 📊 Project Status
189
+
190
+ - **Latest Version**: 0.1.6
191
+ - **Python Support**: 3.8+
192
+ - **Test Coverage**: 66+ tests (100% passing)
193
+ - **Dependencies**: Minimal (PyYAML for OpenShift-docs support)
194
+
195
+ ## 🔗 Resources
196
+
197
+ - [Documentation](https://rolfedh.github.io/doc-utils/)
198
+ - [PyPI Package](https://pypi.org/project/rolfedh-doc-utils/)
199
+ - [Issue Tracker](https://github.com/rolfedh/doc-utils/issues)
200
+ - [Changelog](https://github.com/rolfedh/doc-utils/blob/main/CHANGELOG.md)
201
+ - [License](https://github.com/rolfedh/doc-utils/blob/main/LICENSE)
202
+
203
+ ## 📝 License
204
+
205
+ This project is licensed under the terms specified in the [LICENSE](https://github.com/rolfedh/doc-utils/blob/main/LICENSE) file.
206
+
207
+ ---
208
+
209
+ Created by [Rolfe Dlugy-Hegwer](https://github.com/rolfedh) for technical writers everywhere.
@@ -0,0 +1,176 @@
1
+ # doc-utils
2
+
3
+ [![PyPI version](https://badge.fury.io/py/rolfedh-doc-utils.svg)](https://pypi.org/project/rolfedh-doc-utils/)
4
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
5
+ [![License](https://img.shields.io/github/license/rolfedh/doc-utils)](https://github.com/rolfedh/doc-utils/blob/main/LICENSE)
6
+ [![Documentation](https://img.shields.io/badge/docs-GitHub%20Pages-brightgreen)](https://rolfedh.github.io/doc-utils/)
7
+
8
+ Python CLI tools for maintaining clean, consistent AsciiDoc documentation repositories.
9
+
10
+ 📚 **[View Full Documentation](https://rolfedh.github.io/doc-utils/)** | 📦 **[PyPI Package](https://pypi.org/project/rolfedh-doc-utils/)** | 🐙 **[GitHub](https://github.com/rolfedh/doc-utils)**
11
+
12
+ ## ⚠️ Safety First
13
+
14
+ These tools can modify or delete files. **Always:**
15
+ - Work in a git branch (never main/master)
16
+ - Review changes with `git diff`
17
+ - Test documentation builds after changes
18
+
19
+ ## 🚀 Quick Start
20
+
21
+ ### Install with pipx (Recommended)
22
+
23
+ ```bash
24
+ # Install
25
+ pipx install rolfedh-doc-utils
26
+
27
+ # Upgrade to latest version
28
+ pipx upgrade rolfedh-doc-utils
29
+ ```
30
+
31
+ ### Alternative Installation
32
+
33
+ ```bash
34
+ # With pip user flag
35
+ pip install --user rolfedh-doc-utils
36
+
37
+ # For development
38
+ git clone https://github.com/rolfedh/doc-utils.git
39
+ cd doc-utils
40
+ pip install -e .
41
+ ```
42
+
43
+ ## 🛠️ Available Tools
44
+
45
+ **Note:** Commands use hyphens (`-`), while Python files use underscores (`_`). After installing with pipx, use the hyphenated commands directly.
46
+
47
+ | Tool | Description | Usage |
48
+ |------|-------------|-------|
49
+ | **`format-asciidoc-spacing`** | Standardizes spacing after headings and around includes | `format-asciidoc-spacing --dry-run modules/` |
50
+ | **`check-scannability`** | Analyzes readability (sentence/paragraph length) | `check-scannability --max-words 25` |
51
+ | **`archive-unused-files`** | Finds and archives unreferenced .adoc files | `archive-unused-files` (preview)<br>`archive-unused-files --archive` (execute) |
52
+ | **`archive-unused-images`** | Finds and archives unreferenced images | `archive-unused-images` (preview)<br>`archive-unused-images --archive` (execute) |
53
+ | **`find-unused-attributes`** | Identifies unused attribute definitions | `find-unused-attributes attributes.adoc` |
54
+
55
+ ## 📖 Documentation
56
+
57
+ Comprehensive documentation is available at **[rolfedh.github.io/doc-utils](https://rolfedh.github.io/doc-utils/)**
58
+
59
+ ### OpenShift-docs Example
60
+
61
+ For OpenShift documentation repositories:
62
+
63
+ ```bash
64
+ # Format spacing in specific directories
65
+ format-asciidoc-spacing modules/
66
+ format-asciidoc-spacing microshift_networking/
67
+
68
+ # Preview changes first
69
+ format-asciidoc-spacing --dry-run modules/networking/
70
+
71
+ # Process specific file
72
+ format-asciidoc-spacing modules/networking/about-networking.adoc
73
+ ```
74
+
75
+ - [Getting Started Guide](https://rolfedh.github.io/doc-utils/getting-started)
76
+ - [Tools Reference](https://rolfedh.github.io/doc-utils/tools/)
77
+ - [Best Practices](https://rolfedh.github.io/doc-utils/best-practices)
78
+ - [Contributing](https://rolfedh.github.io/doc-utils/contributing)
79
+
80
+ ## 💡 Common Workflows
81
+
82
+ ### Clean Up Documentation
83
+
84
+ ```bash
85
+ # 1. Create a branch
86
+ git checkout -b doc-cleanup
87
+
88
+ # 2. Preview what would change
89
+ format-asciidoc-spacing --dry-run .
90
+ archive-unused-files
91
+ archive-unused-images
92
+
93
+ # 3. Apply changes
94
+ format-asciidoc-spacing .
95
+ archive-unused-files --archive
96
+ archive-unused-images --archive
97
+
98
+ # 4. Review and commit
99
+ git diff
100
+ git add -A && git commit -m "Clean up documentation"
101
+ ```
102
+
103
+ ### Check Documentation Quality
104
+
105
+ ```bash
106
+ # Check readability
107
+ check-scannability --max-words 30 --max-sentences 5
108
+
109
+ # Find unused attributes
110
+ find-unused-attributes attributes.adoc
111
+
112
+ # Save results
113
+ find-unused-attributes attributes.adoc --output unused.txt
114
+ ```
115
+
116
+ ## 🔧 Exclusions
117
+
118
+ All tools support excluding files and directories:
119
+
120
+ ```bash
121
+ # Exclude specific directories
122
+ archive-unused-files --exclude-dir ./temp --exclude-dir ./drafts
123
+
124
+ # Exclude specific files
125
+ check-scannability --exclude-file ./README.adoc
126
+
127
+ # Use exclusion list file
128
+ echo "./deprecated/" > .docutils-ignore
129
+ archive-unused-images --exclude-list .docutils-ignore
130
+ ```
131
+
132
+ ## 🧪 Development
133
+
134
+ ```bash
135
+ # Install dev dependencies
136
+ pip install -r requirements-dev.txt
137
+
138
+ # Run tests
139
+ python -m pytest tests/ -v
140
+
141
+ # Run specific test
142
+ python -m pytest tests/test_file_utils.py
143
+ ```
144
+
145
+ ## 🤝 Contributing
146
+
147
+ We welcome contributions! See our [Contributing Guide](https://rolfedh.github.io/doc-utils/contributing) for details.
148
+
149
+ Before submitting PRs:
150
+ - ✅ All tests pass
151
+ - ✅ Code follows PEP 8
152
+ - ✅ Documentation updated
153
+ - ✅ Changelog entry added
154
+
155
+ ## 📊 Project Status
156
+
157
+ - **Latest Version**: 0.1.6
158
+ - **Python Support**: 3.8+
159
+ - **Test Coverage**: 66+ tests (100% passing)
160
+ - **Dependencies**: Minimal (PyYAML for OpenShift-docs support)
161
+
162
+ ## 🔗 Resources
163
+
164
+ - [Documentation](https://rolfedh.github.io/doc-utils/)
165
+ - [PyPI Package](https://pypi.org/project/rolfedh-doc-utils/)
166
+ - [Issue Tracker](https://github.com/rolfedh/doc-utils/issues)
167
+ - [Changelog](https://github.com/rolfedh/doc-utils/blob/main/CHANGELOG.md)
168
+ - [License](https://github.com/rolfedh/doc-utils/blob/main/LICENSE)
169
+
170
+ ## 📝 License
171
+
172
+ This project is licensed under the terms specified in the [LICENSE](https://github.com/rolfedh/doc-utils/blob/main/LICENSE) file.
173
+
174
+ ---
175
+
176
+ Created by [Rolfe Dlugy-Hegwer](https://github.com/rolfedh) for technical writers everywhere.
@@ -0,0 +1,318 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """Format AsciiDoc spacing - ensures blank lines after headings and around include directives"""
4
+
5
+ import argparse
6
+ import os
7
+ import re
8
+ import sys
9
+ from pathlib import Path
10
+ from typing import List, Tuple
11
+
12
+
13
+ # Colors for output
14
+ class Colors:
15
+ RED = '\033[0;31m'
16
+ GREEN = '\033[0;32m'
17
+ YELLOW = '\033[1;33m'
18
+ NC = '\033[0m' # No Color
19
+
20
+
21
+ def print_colored(message: str, color: str = Colors.NC) -> None:
22
+ """Print message with color"""
23
+ print(f"{color}{message}{Colors.NC}")
24
+
25
+
26
+ def process_file(file_path: Path, dry_run: bool = False, verbose: bool = False) -> bool:
27
+ """
28
+ Process a single AsciiDoc file to fix spacing issues.
29
+
30
+ Args:
31
+ file_path: Path to the file to process
32
+ dry_run: If True, show what would be changed without modifying
33
+ verbose: If True, show detailed output
34
+
35
+ Returns:
36
+ True if changes were made (or would be made in dry-run), False otherwise
37
+ """
38
+ if verbose:
39
+ print(f"Processing: {file_path}")
40
+
41
+ try:
42
+ with open(file_path, 'r', encoding='utf-8') as f:
43
+ lines = f.readlines()
44
+ except (IOError, UnicodeDecodeError) as e:
45
+ print_colored(f"Error reading {file_path}: {e}", Colors.RED)
46
+ return False
47
+
48
+ # Remove trailing newlines from lines for processing
49
+ lines = [line.rstrip('\n\r') for line in lines]
50
+
51
+ new_lines = []
52
+ changes_made = False
53
+ in_block = False # Track if we're inside a block (admonition, listing, etc.)
54
+ in_conditional = False # Track if we're inside a conditional block
55
+
56
+ for i, current_line in enumerate(lines):
57
+ prev_line = lines[i-1] if i > 0 else ""
58
+ next_line = lines[i+1] if i + 1 < len(lines) else ""
59
+
60
+ # Check for conditional start (ifdef:: or ifndef::)
61
+ if re.match(r'^(ifdef::|ifndef::)', current_line):
62
+ in_conditional = True
63
+ # Add blank line before conditional if needed
64
+ if (prev_line and
65
+ not re.match(r'^\s*$', prev_line) and
66
+ not re.match(r'^(ifdef::|ifndef::|endif::)', prev_line)):
67
+ new_lines.append("")
68
+ changes_made = True
69
+ if verbose:
70
+ print(f" Added blank line before conditional block")
71
+ new_lines.append(current_line)
72
+
73
+ # Check for conditional end (endif::)
74
+ elif re.match(r'^endif::', current_line):
75
+ new_lines.append(current_line)
76
+ in_conditional = False
77
+ # Add blank line after conditional if needed
78
+ if (next_line and
79
+ not re.match(r'^\s*$', next_line) and
80
+ not re.match(r'^(ifdef::|ifndef::|endif::)', next_line)):
81
+ new_lines.append("")
82
+ changes_made = True
83
+ if verbose:
84
+ print(f" Added blank line after conditional block")
85
+
86
+ # Check for block delimiters (====, ----, ...., ____)
87
+ # These are used for admonitions, listing blocks, literal blocks, etc.
88
+ elif re.match(r'^(====+|----+|\.\.\.\.+|____+)$', current_line):
89
+ in_block = not in_block # Toggle block state
90
+ new_lines.append(current_line)
91
+ # Check if current line is a heading (but not if we're in a block)
92
+ elif not in_block and re.match(r'^=+\s+', current_line):
93
+ new_lines.append(current_line)
94
+
95
+ # Check if next line is not empty and not another heading
96
+ if (next_line and
97
+ not re.match(r'^=+\s+', next_line) and
98
+ not re.match(r'^\s*$', next_line)):
99
+ new_lines.append("")
100
+ changes_made = True
101
+ if verbose:
102
+ truncated = current_line[:50] + "..." if len(current_line) > 50 else current_line
103
+ print(f" Added blank line after heading: {truncated}")
104
+
105
+ # Check if current line is a comment (AsciiDoc comments start with //)
106
+ elif re.match(r'^//', current_line):
107
+ # Skip special handling if we're inside a conditional block
108
+ if in_conditional:
109
+ new_lines.append(current_line)
110
+ else:
111
+ # Check if next line is an include directive
112
+ if next_line and re.match(r'^include::', next_line):
113
+ # This comment belongs to the include, add blank line before comment if needed
114
+ if (prev_line and
115
+ not re.match(r'^\s*$', prev_line) and
116
+ not re.match(r'^//', prev_line) and
117
+ not re.match(r'^:', prev_line)): # Don't add if previous is attribute
118
+ new_lines.append("")
119
+ changes_made = True
120
+ if verbose:
121
+ print(f" Added blank line before comment above include")
122
+ new_lines.append(current_line)
123
+
124
+ # Check if current line is an attribute (starts with :)
125
+ elif re.match(r'^:', current_line):
126
+ # Skip special handling if we're inside a conditional block
127
+ if in_conditional:
128
+ new_lines.append(current_line)
129
+ else:
130
+ # Check if next line is an include directive
131
+ if next_line and re.match(r'^include::', next_line):
132
+ # This attribute belongs to the include, add blank line before attribute if needed
133
+ if (prev_line and
134
+ not re.match(r'^\s*$', prev_line) and
135
+ not re.match(r'^//', prev_line) and
136
+ not re.match(r'^:', prev_line)): # Don't add if previous is comment or attribute
137
+ new_lines.append("")
138
+ changes_made = True
139
+ if verbose:
140
+ print(f" Added blank line before attribute above include")
141
+ new_lines.append(current_line)
142
+
143
+ # Check if current line is an include directive
144
+ elif re.match(r'^include::', current_line):
145
+ # Skip special handling if we're inside a conditional block
146
+ if in_conditional:
147
+ new_lines.append(current_line)
148
+ else:
149
+ # Check if this is an attribute include (contains "attribute" in the path)
150
+ is_attribute_include = 'attribute' in current_line.lower()
151
+
152
+ # Check if this appears near the top of the file (within first 10 lines after H1)
153
+ # Find the H1 heading position
154
+ h1_position = -1
155
+ for j in range(min(i, 10)): # Look back up to 10 lines or to current position
156
+ if re.match(r'^=\s+', lines[j]): # H1 heading starts with single =
157
+ h1_position = j
158
+ break
159
+
160
+ # If this is an attribute include near the H1 heading, don't add surrounding blank lines
161
+ is_near_h1 = h1_position >= 0 and (i - h1_position) <= 2
162
+
163
+ # Check if previous line is a comment or attribute (which belongs to this include)
164
+ has_comment_above = prev_line and re.match(r'^//', prev_line)
165
+ has_attribute_above = prev_line and re.match(r'^:', prev_line)
166
+
167
+ # If it's an attribute include near H1, only the heading's blank line is needed
168
+ if not (is_attribute_include and is_near_h1):
169
+ # Don't add blank line if there's a comment or attribute above (it was handled by the comment/attribute logic)
170
+ if not has_comment_above and not has_attribute_above:
171
+ # Add blank line before include if previous line is not empty and not an include
172
+ if (prev_line and
173
+ not re.match(r'^\s*$', prev_line) and
174
+ not re.match(r'^include::', prev_line)):
175
+ new_lines.append("")
176
+ changes_made = True
177
+ if verbose:
178
+ truncated = current_line[:50] + "..." if len(current_line) > 50 else current_line
179
+ print(f" Added blank line before include: {truncated}")
180
+
181
+ new_lines.append(current_line)
182
+
183
+ # If it's an attribute include near H1, don't add blank line after
184
+ if not (is_attribute_include and is_near_h1):
185
+ # Add blank line after include if next line exists and is not empty and not an include
186
+ if (next_line and
187
+ not re.match(r'^\s*$', next_line) and
188
+ not re.match(r'^include::', next_line)):
189
+ new_lines.append("")
190
+ changes_made = True
191
+ if verbose:
192
+ truncated = current_line[:50] + "..." if len(current_line) > 50 else current_line
193
+ print(f" Added blank line after include: {truncated}")
194
+
195
+ else:
196
+ new_lines.append(current_line)
197
+
198
+ # Apply changes if any were made
199
+ if changes_made:
200
+ # Clean up any consecutive blank lines we may have added
201
+ cleaned_lines = []
202
+ for i, line in enumerate(new_lines):
203
+ # Check if this is a blank line we're about to add
204
+ if line == "":
205
+ # Check if the previous line is also a blank line
206
+ if i > 0 and cleaned_lines and cleaned_lines[-1] == "":
207
+ # Skip this blank line as we already have one
208
+ continue
209
+ cleaned_lines.append(line)
210
+
211
+ if dry_run:
212
+ print_colored(f"Would modify: {file_path}", Colors.YELLOW)
213
+ else:
214
+ try:
215
+ with open(file_path, 'w', encoding='utf-8') as f:
216
+ for line in cleaned_lines:
217
+ f.write(line + '\n')
218
+ print_colored(f"Modified: {file_path}", Colors.GREEN)
219
+ except IOError as e:
220
+ print_colored(f"Error writing {file_path}: {e}", Colors.RED)
221
+ return False
222
+ else:
223
+ if verbose:
224
+ print(" No changes needed")
225
+
226
+ return changes_made
227
+
228
+
229
+ def find_adoc_files(path: Path) -> List[Path]:
230
+ """Find all .adoc files in the given path"""
231
+ adoc_files = []
232
+
233
+ if path.is_file():
234
+ if path.suffix == '.adoc':
235
+ adoc_files.append(path)
236
+ else:
237
+ print_colored(f"Warning: {path} is not an AsciiDoc file (.adoc)", Colors.YELLOW)
238
+ elif path.is_dir():
239
+ adoc_files = list(path.rglob('*.adoc'))
240
+
241
+ return adoc_files
242
+
243
+
244
+ def main():
245
+ """Main entry point"""
246
+ parser = argparse.ArgumentParser(
247
+ description="Format AsciiDoc files to ensure proper spacing",
248
+ formatter_class=argparse.RawDescriptionHelpFormatter,
249
+ epilog="""
250
+ Format AsciiDoc files to ensure proper spacing:
251
+ - Blank line after headings (=, ==, ===, etc.)
252
+ - Blank lines around include:: directives
253
+
254
+ Examples:
255
+ %(prog)s # Process all .adoc files in current directory
256
+ %(prog)s modules/ # Process all .adoc files in modules/
257
+ %(prog)s assemblies/my-guide.adoc # Process single file
258
+ %(prog)s --dry-run modules/ # Preview changes without modifying
259
+ """
260
+ )
261
+
262
+ parser.add_argument(
263
+ 'path',
264
+ nargs='?',
265
+ default='.',
266
+ help='File or directory to process (default: current directory)'
267
+ )
268
+ parser.add_argument(
269
+ '-n', '--dry-run',
270
+ action='store_true',
271
+ help='Show what would be changed without modifying files'
272
+ )
273
+ parser.add_argument(
274
+ '-v', '--verbose',
275
+ action='store_true',
276
+ help='Show detailed output'
277
+ )
278
+
279
+ args = parser.parse_args()
280
+
281
+ # Convert path to Path object
282
+ target_path = Path(args.path)
283
+
284
+ # Check if path exists
285
+ if not target_path.exists():
286
+ print_colored(f"Error: Path does not exist: {target_path}", Colors.RED)
287
+ sys.exit(1)
288
+
289
+ # Display dry-run mode message
290
+ if args.dry_run:
291
+ print_colored("DRY RUN MODE - No files will be modified", Colors.YELLOW)
292
+
293
+ # Find all AsciiDoc files
294
+ adoc_files = find_adoc_files(target_path)
295
+
296
+ if not adoc_files:
297
+ print(f"Processed 0 AsciiDoc file(s)")
298
+ print("AsciiDoc spacing formatting complete!")
299
+ return
300
+
301
+ # Process each file
302
+ files_processed = 0
303
+ for file_path in adoc_files:
304
+ try:
305
+ process_file(file_path, args.dry_run, args.verbose)
306
+ files_processed += 1
307
+ except KeyboardInterrupt:
308
+ print_colored("\nOperation cancelled by user", Colors.YELLOW)
309
+ sys.exit(1)
310
+ except Exception as e:
311
+ print_colored(f"Unexpected error processing {file_path}: {e}", Colors.RED)
312
+
313
+ print(f"Processed {files_processed} AsciiDoc file(s)")
314
+ print("AsciiDoc spacing formatting complete!")
315
+
316
+
317
+ if __name__ == "__main__":
318
+ main()