rolfedh-doc-utils 0.1.16__py3-none-any.whl → 0.1.18__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.
@@ -15,6 +15,29 @@ from pathlib import Path
15
15
  from typing import Set, List, Optional
16
16
 
17
17
  def parse_attributes_file(attr_file: str) -> Set[str]:
18
+ # AsciiDoc configuration attributes that control the processor itself
19
+ # These should be ignored as they won't appear in content
20
+ IGNORED_ATTRIBUTES = {
21
+ 'data-uri',
22
+ 'doctype',
23
+ 'experimental',
24
+ 'idprefix',
25
+ 'imagesdir',
26
+ 'includes',
27
+ 'sectanchors',
28
+ 'sectlinks',
29
+ 'source-highlighter',
30
+ 'linkattrs',
31
+ 'toclevels',
32
+ 'idseparator',
33
+ 'icons',
34
+ 'iconsdir',
35
+ 'generated-dir',
36
+ 'code-examples',
37
+ 'doc-guides',
38
+ 'doc-examples',
39
+ }
40
+
18
41
  attributes = set()
19
42
 
20
43
  # Check if file exists
@@ -30,7 +53,10 @@ def parse_attributes_file(attr_file: str) -> Set[str]:
30
53
  for line in f:
31
54
  match = re.match(r'^:([\w-]+):', line.strip())
32
55
  if match:
33
- attributes.add(match.group(1))
56
+ attr_name = match.group(1)
57
+ # Skip ignored configuration attributes
58
+ if attr_name not in IGNORED_ATTRIBUTES:
59
+ attributes.add(attr_name)
34
60
  except PermissionError:
35
61
  raise PermissionError(f"Permission denied reading file: {attr_file}")
36
62
  except UnicodeDecodeError as e:
@@ -50,13 +76,22 @@ def find_adoc_files(root_dir: str) -> List[str]:
50
76
 
51
77
  def scan_for_attribute_usage(adoc_files: List[str], attributes: Set[str]) -> Set[str]:
52
78
  used = set()
79
+ # Pattern for attribute references: {attribute-name}
53
80
  attr_pattern = re.compile(r'\{([\w-]+)\}')
81
+ # Patterns for conditional directives: ifdef::attr[], ifndef::attr[], endif::attr[]
82
+ conditional_pattern = re.compile(r'(?:ifdef|ifndef|endif)::([\w-]+)\[')
83
+
54
84
  for file in adoc_files:
55
85
  with open(file, 'r', encoding='utf-8') as f:
56
86
  for line in f:
87
+ # Check for {attribute} references
57
88
  for match in attr_pattern.findall(line):
58
89
  if match in attributes:
59
90
  used.add(match)
91
+ # Check for ifdef::attribute[], ifndef::attribute[], endif::attribute[]
92
+ for match in conditional_pattern.findall(line):
93
+ if match in attributes:
94
+ used.add(match)
60
95
  return used
61
96
 
62
97
  def find_attributes_files(root_dir: str = '.') -> List[str]:
@@ -136,3 +171,44 @@ def find_unused_attributes(attr_file: str, adoc_root: str = '.') -> List[str]:
136
171
  used = scan_for_attribute_usage(adoc_files, attributes)
137
172
  unused = sorted(attributes - used)
138
173
  return unused
174
+
175
+
176
+ def comment_out_unused_attributes(attr_file: str, unused_attrs: List[str]) -> int:
177
+ """
178
+ Comment out unused attributes in the attributes file.
179
+
180
+ Args:
181
+ attr_file: Path to the attributes file
182
+ unused_attrs: List of unused attribute names
183
+
184
+ Returns:
185
+ Number of attributes commented out
186
+ """
187
+ if not unused_attrs:
188
+ return 0
189
+
190
+ # Read the file
191
+ with open(attr_file, 'r', encoding='utf-8') as f:
192
+ lines = f.readlines()
193
+
194
+ # Create a set for faster lookup
195
+ unused_set = set(unused_attrs)
196
+ commented_count = 0
197
+
198
+ # Process each line
199
+ new_lines = []
200
+ for line in lines:
201
+ # Check if this line defines an attribute
202
+ match = re.match(r'^:([\w-]+):', line)
203
+ if match and match.group(1) in unused_set:
204
+ # Comment out this line
205
+ new_lines.append(f'// Unused {line}')
206
+ commented_count += 1
207
+ else:
208
+ new_lines.append(line)
209
+
210
+ # Write back to the file
211
+ with open(attr_file, 'w', encoding='utf-8') as f:
212
+ f.writelines(new_lines)
213
+
214
+ return commented_count
find_unused_attributes.py CHANGED
@@ -12,7 +12,7 @@ import argparse
12
12
  import os
13
13
  import sys
14
14
  from datetime import datetime
15
- from doc_utils.unused_attributes import find_unused_attributes, find_attributes_files, select_attributes_file
15
+ from doc_utils.unused_attributes import find_unused_attributes, find_attributes_files, select_attributes_file, comment_out_unused_attributes
16
16
  from doc_utils.spinner import Spinner
17
17
  from doc_utils.version_check import check_version_on_startup
18
18
 
@@ -26,6 +26,7 @@ def main():
26
26
  help='Path to the attributes file. If not specified, auto-discovers attributes files.'
27
27
  )
28
28
  parser.add_argument('-o', '--output', action='store_true', help='Write results to a timestamped txt file in your home directory.')
29
+ parser.add_argument('-c', '--comment-out', action='store_true', help='Comment out unused attributes in the attributes file with "// Unused".')
29
30
  args = parser.parse_args()
30
31
 
31
32
  # Determine which attributes file to use
@@ -84,6 +85,16 @@ def main():
84
85
  f.write(output + '\n')
85
86
  print(f'Results written to: {filename}')
86
87
 
88
+ if args.comment_out and output:
89
+ # Ask for confirmation before modifying the file
90
+ print(f'\nThis will comment out {len(unused)} unused attributes in: {attr_file}')
91
+ response = input('Continue? (y/n): ').strip().lower()
92
+ if response == 'y':
93
+ commented_count = comment_out_unused_attributes(attr_file, unused)
94
+ print(f'Commented out {commented_count} unused attributes in: {attr_file}')
95
+ else:
96
+ print('Operation cancelled.')
97
+
87
98
  return 0
88
99
 
89
100
  if __name__ == '__main__':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rolfedh-doc-utils
3
- Version: 0.1.16
3
+ Version: 0.1.18
4
4
  Summary: CLI tools for AsciiDoc documentation projects
5
5
  Author: Rolfe Dlugy-Hegwer
6
6
  License: MIT License
@@ -2,7 +2,7 @@ archive_unused_files.py,sha256=VL4hEN40CTYJzScyevY1fn7Fs2O083qfJUYdrv5JxxM,2142
2
2
  archive_unused_images.py,sha256=uZ4-OSMja98toyraDaeG6OMjV7kU2hq-QsTjnDl-BfQ,1732
3
3
  check_scannability.py,sha256=IdHwzQtBWzx95zIDOx3B7OyzoKxTVtpn0wJUErFQRC0,5346
4
4
  extract_link_attributes.py,sha256=bfWC6h4QWj6YvwZ3-tc3K7oMC2wN4QdA4YMCKcYdKCg,3384
5
- find_unused_attributes.py,sha256=v1IPuM5RAN9JZwWnOilgXlMCC2kNA5umiyCJ-pyIumI,3456
5
+ find_unused_attributes.py,sha256=KYVhIgJobEtMzzVI4P7wkMym7Z6N27IdxQmg3qySvAo,4131
6
6
  format_asciidoc_spacing.py,sha256=B3Bo68_i_RJNlUdNLZ4TiYUslrbwhSkzX3Dyd9JWRo0,4080
7
7
  replace_link_attributes.py,sha256=OGD-COUG3JUCYOoARa32xqaSEcA-p31AJLk-d-FPUUQ,7492
8
8
  validate_links.py,sha256=KmN4dULwLiLM2lUyteXwbBuTuQHcNkCq0nhiaZ48BtI,6013
@@ -15,13 +15,13 @@ doc_utils/scannability.py,sha256=XwlmHqDs69p_V36X7DLjPTy0DUoLszSGqYjJ9wE-3hg,982
15
15
  doc_utils/spinner.py,sha256=lJg15qzODiKoR0G6uFIk2BdVNgn9jFexoTRUMrjiWvk,3554
16
16
  doc_utils/topic_map_parser.py,sha256=tKcIO1m9r2K6dvPRGue58zqMr0O2zKU1gnZMzEE3U6o,4571
17
17
  doc_utils/unused_adoc.py,sha256=2cbqcYr1os2EhETUU928BlPRlsZVSdI00qaMhqjSIqQ,5263
18
- doc_utils/unused_attributes.py,sha256=EjTtWIKW_aXsR1JOgw5RSDVAqitJ_NfRMVOXVGaiWTY,5282
18
+ doc_utils/unused_attributes.py,sha256=OHyAdaBD7aNo357B0SLBN5NC_jNY5TWXMwgtfJNh3X8,7621
19
19
  doc_utils/unused_images.py,sha256=nqn36Bbrmon2KlGlcaruNjJJvTQ8_9H0WU9GvCW7rW8,1456
20
20
  doc_utils/validate_links.py,sha256=iBGXnwdeLlgIT3fo3v01ApT5k0X2FtctsvkrE6E3VMk,19610
21
21
  doc_utils/version_check.py,sha256=1ySC6Au21OqUMZr7AkIa3nMNh3M6wLQmPQCi-ZFIqoE,6338
22
- rolfedh_doc_utils-0.1.16.dist-info/licenses/LICENSE,sha256=vLxtwMVOJA_hEy8b77niTkdmQI9kNJskXHq0dBS36e0,1075
23
- rolfedh_doc_utils-0.1.16.dist-info/METADATA,sha256=quUI6eIDAbfEKSAELOZtyy5be7CfnLBJsvDN0TBU50E,7824
24
- rolfedh_doc_utils-0.1.16.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
- rolfedh_doc_utils-0.1.16.dist-info/entry_points.txt,sha256=2J4Ojc3kkuArpe2xcUOPc0LxSWCmnctvw8hy8zpnbO4,418
26
- rolfedh_doc_utils-0.1.16.dist-info/top_level.txt,sha256=1w0JWD7w7gnM5Sga2K4fJieNZ7CHPTAf0ozYk5iIlmo,182
27
- rolfedh_doc_utils-0.1.16.dist-info/RECORD,,
22
+ rolfedh_doc_utils-0.1.18.dist-info/licenses/LICENSE,sha256=vLxtwMVOJA_hEy8b77niTkdmQI9kNJskXHq0dBS36e0,1075
23
+ rolfedh_doc_utils-0.1.18.dist-info/METADATA,sha256=0MRwKnjHS1EEX5--O9m0UQVvQpYSICZ1k-VSTX3VhQA,7824
24
+ rolfedh_doc_utils-0.1.18.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
+ rolfedh_doc_utils-0.1.18.dist-info/entry_points.txt,sha256=2J4Ojc3kkuArpe2xcUOPc0LxSWCmnctvw8hy8zpnbO4,418
26
+ rolfedh_doc_utils-0.1.18.dist-info/top_level.txt,sha256=1w0JWD7w7gnM5Sga2K4fJieNZ7CHPTAf0ozYk5iIlmo,182
27
+ rolfedh_doc_utils-0.1.18.dist-info/RECORD,,