rolfedh-doc-utils 0.1.0__py3-none-any.whl
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.
- doc_utils/__init__.py +3 -0
- doc_utils/file_utils.py +60 -0
- doc_utils/scannability.py +23 -0
- doc_utils/unused_adoc.py +24 -0
- doc_utils/unused_attributes.py +50 -0
- doc_utils/unused_images.py +28 -0
- rolfedh_doc_utils-0.1.0.dist-info/METADATA +83 -0
- rolfedh_doc_utils-0.1.0.dist-info/RECORD +12 -0
- rolfedh_doc_utils-0.1.0.dist-info/WHEEL +5 -0
- rolfedh_doc_utils-0.1.0.dist-info/entry_points.txt +5 -0
- rolfedh_doc_utils-0.1.0.dist-info/licenses/LICENSE +21 -0
- rolfedh_doc_utils-0.1.0.dist-info/top_level.txt +1 -0
doc_utils/__init__.py
ADDED
doc_utils/file_utils.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# doc_utils/file_utils.py
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import zipfile
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def collect_files(scan_dirs, extensions, exclude_dirs=None, exclude_files=None):
|
|
10
|
+
"""
|
|
11
|
+
Recursively collect files with given extensions from scan_dirs, excluding symlinks, exclude_dirs, and exclude_files.
|
|
12
|
+
Returns a list of normalized file paths.
|
|
13
|
+
"""
|
|
14
|
+
exclude_dirs = set(os.path.abspath(os.path.normpath(d)) for d in (exclude_dirs or []))
|
|
15
|
+
exclude_files = set(os.path.abspath(os.path.normpath(f)) for f in (exclude_files or []))
|
|
16
|
+
found_files = []
|
|
17
|
+
for base_dir in scan_dirs:
|
|
18
|
+
for root, dirs, files in os.walk(base_dir):
|
|
19
|
+
abs_root = os.path.abspath(root)
|
|
20
|
+
# Exclude directories by absolute path
|
|
21
|
+
dirs[:] = [d for d in dirs if not os.path.islink(os.path.join(root, d)) and os.path.abspath(os.path.join(root, d)) not in exclude_dirs]
|
|
22
|
+
for f in files:
|
|
23
|
+
file_path = os.path.normpath(os.path.join(root, f))
|
|
24
|
+
abs_file_path = os.path.abspath(file_path)
|
|
25
|
+
if os.path.islink(file_path):
|
|
26
|
+
continue
|
|
27
|
+
if abs_file_path in exclude_files:
|
|
28
|
+
continue
|
|
29
|
+
if os.path.splitext(f)[1].lower() in extensions:
|
|
30
|
+
found_files.append(file_path)
|
|
31
|
+
return list(dict.fromkeys(found_files))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def write_manifest_and_archive(unused_files, archive_dir, manifest_prefix, archive_prefix, archive=False):
|
|
35
|
+
"""
|
|
36
|
+
Write a manifest of unused files and optionally archive and delete them.
|
|
37
|
+
Returns the manifest path and (if archive=True) the archive path.
|
|
38
|
+
"""
|
|
39
|
+
if not unused_files:
|
|
40
|
+
print("No unused files found. No manifest or ZIP archive created.")
|
|
41
|
+
return None, None
|
|
42
|
+
now = datetime.now()
|
|
43
|
+
datetime_str = now.strftime('%Y-%m-%d_%H%M%S')
|
|
44
|
+
output_file = f'{manifest_prefix}-{datetime_str}.txt'
|
|
45
|
+
os.makedirs(archive_dir, exist_ok=True)
|
|
46
|
+
manifest_path = os.path.join(archive_dir, output_file)
|
|
47
|
+
with open(manifest_path, 'w') as f:
|
|
48
|
+
for file in unused_files:
|
|
49
|
+
print(file)
|
|
50
|
+
f.write(file + '\n')
|
|
51
|
+
archive_path = None
|
|
52
|
+
if archive:
|
|
53
|
+
archive_path = os.path.join(archive_dir, f"{archive_prefix}-{datetime_str}.zip")
|
|
54
|
+
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|
55
|
+
for file in unused_files:
|
|
56
|
+
arcname = os.path.relpath(file)
|
|
57
|
+
print(f"Archiving: {file} -> {archive_path} ({arcname})")
|
|
58
|
+
zipf.write(file, arcname)
|
|
59
|
+
os.remove(file)
|
|
60
|
+
return manifest_path, archive_path
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# doc_utils/scannability.py
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
def check_scannability(adoc_files, max_sentence_length=22, max_paragraph_sentences=3):
|
|
7
|
+
long_sentences = []
|
|
8
|
+
long_paragraphs = []
|
|
9
|
+
for file_path in adoc_files:
|
|
10
|
+
try:
|
|
11
|
+
with open(file_path, 'r', encoding='utf-8') as f:
|
|
12
|
+
content = f.read()
|
|
13
|
+
paragraphs = content.split('\n\n')
|
|
14
|
+
for i, para in enumerate(paragraphs):
|
|
15
|
+
sentences = re.split(r'(?<=[.!?]) +', para.strip())
|
|
16
|
+
for sent in sentences:
|
|
17
|
+
if len(sent.split()) > max_sentence_length:
|
|
18
|
+
long_sentences.append((file_path, i+1, sent.strip()))
|
|
19
|
+
if len(sentences) > max_paragraph_sentences:
|
|
20
|
+
long_paragraphs.append((file_path, i+1, len(sentences)))
|
|
21
|
+
except Exception as e:
|
|
22
|
+
print(f"Warning: could not read {file_path}: {e}")
|
|
23
|
+
return long_sentences, long_paragraphs
|
doc_utils/unused_adoc.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# doc_utils/unused_adoc.py
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
from .file_utils import collect_files, write_manifest_and_archive
|
|
6
|
+
|
|
7
|
+
def find_unused_adoc(scan_dirs, archive_dir, archive=False, exclude_dirs=None, exclude_files=None):
|
|
8
|
+
asciidoc_files = collect_files(scan_dirs, {'.adoc'}, exclude_dirs, exclude_files)
|
|
9
|
+
include_pattern = re.compile(r'include::(.+?)\[')
|
|
10
|
+
included_files = set()
|
|
11
|
+
adoc_files = collect_files(['.'], {'.adoc'}, exclude_dirs, exclude_files)
|
|
12
|
+
for file_path in adoc_files:
|
|
13
|
+
try:
|
|
14
|
+
with open(file_path, 'r', encoding='utf-8') as f:
|
|
15
|
+
content = f.read()
|
|
16
|
+
includes = include_pattern.findall(content)
|
|
17
|
+
included_files.update(os.path.basename(include) for include in includes)
|
|
18
|
+
except Exception as e:
|
|
19
|
+
print(f"Warning: could not read {file_path}: {e}")
|
|
20
|
+
unused_files = [f for f in asciidoc_files if os.path.basename(f) not in included_files]
|
|
21
|
+
unused_files = list(dict.fromkeys(unused_files))
|
|
22
|
+
return write_manifest_and_archive(
|
|
23
|
+
unused_files, archive_dir, 'to-archive', 'to-archive', archive=archive
|
|
24
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module for finding unused AsciiDoc attributes.
|
|
3
|
+
|
|
4
|
+
Functions:
|
|
5
|
+
- parse_attributes_file: Parse attribute names from an attributes.adoc file.
|
|
6
|
+
- find_adoc_files: Recursively find all .adoc files in a directory (ignoring symlinks).
|
|
7
|
+
- scan_for_attribute_usage: Find which attributes are used in a set of .adoc files.
|
|
8
|
+
- find_unused_attributes: Main function to return unused attributes.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import re
|
|
13
|
+
from typing import Set, List
|
|
14
|
+
|
|
15
|
+
def parse_attributes_file(attr_file: str) -> Set[str]:
|
|
16
|
+
attributes = set()
|
|
17
|
+
with open(attr_file, 'r', encoding='utf-8') as f:
|
|
18
|
+
for line in f:
|
|
19
|
+
match = re.match(r'^:([\w-]+):', line.strip())
|
|
20
|
+
if match:
|
|
21
|
+
attributes.add(match.group(1))
|
|
22
|
+
return attributes
|
|
23
|
+
|
|
24
|
+
def find_adoc_files(root_dir: str) -> List[str]:
|
|
25
|
+
adoc_files = []
|
|
26
|
+
for dirpath, dirnames, filenames in os.walk(root_dir, followlinks=False):
|
|
27
|
+
for fname in filenames:
|
|
28
|
+
if fname.endswith('.adoc'):
|
|
29
|
+
full_path = os.path.join(dirpath, fname)
|
|
30
|
+
if not os.path.islink(full_path):
|
|
31
|
+
adoc_files.append(full_path)
|
|
32
|
+
return adoc_files
|
|
33
|
+
|
|
34
|
+
def scan_for_attribute_usage(adoc_files: List[str], attributes: Set[str]) -> Set[str]:
|
|
35
|
+
used = set()
|
|
36
|
+
attr_pattern = re.compile(r'\{([\w-]+)\}')
|
|
37
|
+
for file in adoc_files:
|
|
38
|
+
with open(file, 'r', encoding='utf-8') as f:
|
|
39
|
+
for line in f:
|
|
40
|
+
for match in attr_pattern.findall(line):
|
|
41
|
+
if match in attributes:
|
|
42
|
+
used.add(match)
|
|
43
|
+
return used
|
|
44
|
+
|
|
45
|
+
def find_unused_attributes(attr_file: str, adoc_root: str = '.') -> List[str]:
|
|
46
|
+
attributes = parse_attributes_file(attr_file)
|
|
47
|
+
adoc_files = find_adoc_files(adoc_root)
|
|
48
|
+
used = scan_for_attribute_usage(adoc_files, attributes)
|
|
49
|
+
unused = sorted(attributes - used)
|
|
50
|
+
return unused
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# doc_utils/unused_images.py
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
from .file_utils import collect_files, write_manifest_and_archive
|
|
6
|
+
|
|
7
|
+
IMAGE_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.gif', '.svg'}
|
|
8
|
+
|
|
9
|
+
def find_unused_images(scan_dirs, archive_dir, archive=False, exclude_dirs=None, exclude_files=None):
|
|
10
|
+
image_files = collect_files(scan_dirs, IMAGE_EXTENSIONS, exclude_dirs, exclude_files)
|
|
11
|
+
adoc_files = collect_files(['.'], {'.adoc'}, exclude_dirs, exclude_files)
|
|
12
|
+
referenced_images = set()
|
|
13
|
+
image_ref_pattern = re.compile(r'(?i)image::([^\[]+)[\[]|image:([^\[]+)[\[]|"([^"\s]+\.(?:png|jpg|jpeg|gif|svg))"')
|
|
14
|
+
for adoc_file in adoc_files:
|
|
15
|
+
try:
|
|
16
|
+
with open(adoc_file, 'r', encoding='utf-8') as f:
|
|
17
|
+
content = f.read()
|
|
18
|
+
for match in image_ref_pattern.findall(content):
|
|
19
|
+
for group in match:
|
|
20
|
+
if group:
|
|
21
|
+
referenced_images.add(os.path.basename(group))
|
|
22
|
+
except Exception as e:
|
|
23
|
+
print(f"Warning: could not read {adoc_file}: {e}")
|
|
24
|
+
unused_images = [f for f in image_files if os.path.basename(f) not in referenced_images]
|
|
25
|
+
unused_images = list(dict.fromkeys(unused_images))
|
|
26
|
+
return write_manifest_and_archive(
|
|
27
|
+
unused_images, archive_dir, 'unused-images', 'unused-images', archive=archive
|
|
28
|
+
)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rolfedh-doc-utils
|
|
3
|
+
Version: 0.1.0
|
|
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
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# doc-utils
|
|
34
|
+
|
|
35
|
+
This repository contains modular Python utilities and CLI scripts to help technical writers with documentation tasks.
|
|
36
|
+
|
|
37
|
+
**Purpose:**
|
|
38
|
+
Each script is now a thin CLI wrapper that delegates to reusable modules in the `doc_utils` package. For details on how to use a script, see the initial docstring, comments, or the corresponding Markdown help file.
|
|
39
|
+
|
|
40
|
+
## Current Scripts
|
|
41
|
+
|
|
42
|
+
- **check_scannability.py**
|
|
43
|
+
Checks the scannability of AsciiDoc (`.adoc`) files in the current directory. Reports sentences that are too long and paragraphs that contain too many sentences, based on configurable limits. See [check_scannability.md](check_scannability.md) for usage and examples.
|
|
44
|
+
|
|
45
|
+
- **archive_unused_files.py**
|
|
46
|
+
Scans `./modules` and `./assemblies` for AsciiDoc files not referenced by any other AsciiDoc file in the project. Optionally archives and deletes them. See [archive_unused_files.md](archive_unused_files.md) for usage and examples.
|
|
47
|
+
|
|
48
|
+
- **archive_unused_images.py**
|
|
49
|
+
Scans all directories for image files (e.g., `.png`, `.jpg`, `.jpeg`, `.gif`, `.svg`) not referenced by any AsciiDoc file in the project. Optionally archives and deletes them. See [archive_unused_images.md](archive_unused_images.md) for usage and examples.
|
|
50
|
+
|
|
51
|
+
- **find_unused_attributes.py**
|
|
52
|
+
Scans a user-specified attributes file (e.g., `attributes.adoc`) for attribute definitions (e.g., `:version:`) and recursively scans all `.adoc` files in the current directory for usages (e.g., `{version}`). Reports any attribute that is defined but not used in any `.adoc` file as **NOT USED** in both the command line output and a timestamped output file. See [find_unused_attributes.md](find_unused_attributes.md) for usage and examples.
|
|
53
|
+
|
|
54
|
+
## Modular Python Package
|
|
55
|
+
|
|
56
|
+
The core logic for all scripts is implemented in the `doc_utils/` package. You can import and reuse these utilities in your own scripts or tests:
|
|
57
|
+
|
|
58
|
+
- `doc_utils.file_utils` — file collection, manifest, and archiving utilities
|
|
59
|
+
- `doc_utils.unused_images` — logic for finding and archiving unused images
|
|
60
|
+
- `doc_utils.unused_adoc` — logic for finding and archiving unused AsciiDoc files
|
|
61
|
+
- `doc_utils.scannability` — logic for checking AsciiDoc scannability
|
|
62
|
+
- `doc_utils.unused_attributes` — logic for finding unused AsciiDoc attributes
|
|
63
|
+
|
|
64
|
+
## How to Use
|
|
65
|
+
|
|
66
|
+
1. Open the script you are interested in (for example, `check_scannability.py` or `find_unused_attributes.py`).
|
|
67
|
+
2. Read the top of the script or the corresponding `.md` file for instructions, options, and examples.
|
|
68
|
+
3. Run the script from your terminal as described in the usage section.
|
|
69
|
+
|
|
70
|
+
## Running Tests
|
|
71
|
+
|
|
72
|
+
To run all tests, install the development requirements and run pytest from the project root:
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
pip install -r requirements-dev.txt
|
|
76
|
+
pytest
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
All tests and fixtures are located in the `tests/` directory. See `tests/README.md` for details.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
*For licensing information, see [LICENSE](LICENSE).*
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
doc_utils/__init__.py,sha256=qqZR3lohzkP63soymrEZPBGzzk6-nFzi4_tSffjmu_0,74
|
|
2
|
+
doc_utils/file_utils.py,sha256=ftX4XN4tD2kLPkJzKDXsJUZLBq2R2hTDkK08FqNNqlU,2606
|
|
3
|
+
doc_utils/scannability.py,sha256=XwlmHqDs69p_V36X7DLjPTy0DUoLszSGqYjJ9wE-3hg,982
|
|
4
|
+
doc_utils/unused_adoc.py,sha256=gvP1eClEbVebN2jXA41-bPnbVhYz6JHEIbGZCg8JD0s,1115
|
|
5
|
+
doc_utils/unused_attributes.py,sha256=HBgmHelqearfWl3TTC2bZGiJytjLADIgiGQUNKqXXPg,1847
|
|
6
|
+
doc_utils/unused_images.py,sha256=P9vcm00BidrLmxhjeczBtiFU-1wgfN5nCYdZjeCH1kM,1329
|
|
7
|
+
rolfedh_doc_utils-0.1.0.dist-info/licenses/LICENSE,sha256=vLxtwMVOJA_hEy8b77niTkdmQI9kNJskXHq0dBS36e0,1075
|
|
8
|
+
rolfedh_doc_utils-0.1.0.dist-info/METADATA,sha256=NpSNWXDb4TmGY3rOMOPM-85jF74BLllx4e5mrsmvwQk,4417
|
|
9
|
+
rolfedh_doc_utils-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
10
|
+
rolfedh_doc_utils-0.1.0.dist-info/entry_points.txt,sha256=i8LqEsp0KD4YyVI_7wQ1TMgCuag32D7gQes6bLufmtM,216
|
|
11
|
+
rolfedh_doc_utils-0.1.0.dist-info/top_level.txt,sha256=KeCjW0XQZ4Qx_nIFjNLhIqrL5_mxHhGxSwsZglGWKDk,10
|
|
12
|
+
rolfedh_doc_utils-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rolfe Dlugy-Hegwer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
doc_utils
|