sphinx-cmd 0.2.2__tar.gz → 0.3.1__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 (25) hide show
  1. {sphinx_cmd-0.2.2/sphinx_cmd.egg-info → sphinx_cmd-0.3.1}/PKG-INFO +40 -1
  2. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/README.md +37 -0
  3. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/pyproject.toml +6 -1
  4. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/__init__.py +1 -1
  5. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/cli.py +12 -0
  6. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/commands/rm.py +27 -24
  7. sphinx_cmd-0.3.1/sphinx_cmd/config.py +93 -0
  8. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1/sphinx_cmd.egg-info}/PKG-INFO +40 -1
  9. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd.egg-info/SOURCES.txt +3 -0
  10. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd.egg-info/requires.txt +4 -0
  11. sphinx_cmd-0.3.1/tests/test_config.py +109 -0
  12. sphinx_cmd-0.3.1/tests/test_custom_directives.py +123 -0
  13. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/tests/test_rm.py +3 -0
  14. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/LICENSE +0 -0
  15. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/MANIFEST.in +0 -0
  16. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/setup.cfg +0 -0
  17. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/commands/__init__.py +0 -0
  18. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/commands/mv.py +0 -0
  19. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd/py.typed +0 -0
  20. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
  21. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd.egg-info/entry_points.txt +0 -0
  22. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/sphinx_cmd.egg-info/top_level.txt +0 -0
  23. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/tests/test_cli.py +0 -0
  24. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/tests/test_integration.py +0 -0
  25. {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.1}/tests/test_mv.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sphinx-cmd
3
- Version: 0.2.2
3
+ Version: 0.3.1
4
4
  Summary: Command-line tools for Sphinx documentation management
5
5
  Author-email: Jared Dillard <jared.dillard@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
@@ -16,12 +16,14 @@ Classifier: Programming Language :: Python :: 3.12
16
16
  Requires-Python: >=3.9
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
+ Requires-Dist: tomli>=2.0.0; python_version < "3.11"
19
20
  Provides-Extra: dev
20
21
  Requires-Dist: pytest>=7.0.0; extra == "dev"
21
22
  Requires-Dist: black; extra == "dev"
22
23
  Requires-Dist: flake8; extra == "dev"
23
24
  Requires-Dist: mypy; extra == "dev"
24
25
  Requires-Dist: isort; extra == "dev"
26
+ Requires-Dist: pre-commit; extra == "dev"
25
27
  Provides-Extra: test
26
28
  Requires-Dist: pytest>=7.0.0; extra == "test"
27
29
  Dynamic: license-file
@@ -50,8 +52,15 @@ sphinx-cmd rm path/to/docs
50
52
 
51
53
  # Dry run to preview deletions
52
54
  sphinx-cmd rm path/to/docs --dry-run
55
+
56
+ # Process additional directives beyond defaults
57
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
53
58
  ```
54
59
 
60
+ #### Features
61
+
62
+ - Configure custom directives to be processed
63
+
55
64
  ### `sphinx-cmd mv`
56
65
 
57
66
  Move/rename .rst files and automatically update all references to them.
@@ -68,6 +77,9 @@ sphinx-cmd mv old-file.rst new-file.rst --dry-run
68
77
 
69
78
  # Move without updating references
70
79
  sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
80
+
81
+ # Process additional directives beyond defaults
82
+ sphinx-cmd mv old-file.rst new-file.rst --directives drawio-figure,drawio-image
71
83
  ```
72
84
 
73
85
  #### Features
@@ -77,6 +89,33 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
77
89
  - Updates `include` and `literalinclude` directives
78
90
  - Handles relative paths correctly
79
91
  - Preserves file relationships
92
+ - Configure custom directives to be processed
93
+
94
+ ## Configuration
95
+
96
+ You can add custom directives to be processed in two ways:
97
+
98
+ ### 1. Command Line
99
+
100
+ Use the `--directives` option with any command to add custom directives for a single run:
101
+
102
+ ```bash
103
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
104
+ ```
105
+
106
+ ### 2. Configuration File
107
+
108
+ Create a `.sphinx-cmd.toml` file in your home directory with your custom directives:
109
+
110
+ ```toml
111
+ directives = [
112
+ "drawio-figure",
113
+ "drawio-image"
114
+ ]
115
+ ```
116
+
117
+ Command line directives will be combined with those in the configuration file and the built-in defaults.
118
+
80
119
 
81
120
  ## Development
82
121
 
@@ -22,8 +22,15 @@ sphinx-cmd rm path/to/docs
22
22
 
23
23
  # Dry run to preview deletions
24
24
  sphinx-cmd rm path/to/docs --dry-run
25
+
26
+ # Process additional directives beyond defaults
27
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
25
28
  ```
26
29
 
30
+ #### Features
31
+
32
+ - Configure custom directives to be processed
33
+
27
34
  ### `sphinx-cmd mv`
28
35
 
29
36
  Move/rename .rst files and automatically update all references to them.
@@ -40,6 +47,9 @@ sphinx-cmd mv old-file.rst new-file.rst --dry-run
40
47
 
41
48
  # Move without updating references
42
49
  sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
50
+
51
+ # Process additional directives beyond defaults
52
+ sphinx-cmd mv old-file.rst new-file.rst --directives drawio-figure,drawio-image
43
53
  ```
44
54
 
45
55
  #### Features
@@ -49,6 +59,33 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
49
59
  - Updates `include` and `literalinclude` directives
50
60
  - Handles relative paths correctly
51
61
  - Preserves file relationships
62
+ - Configure custom directives to be processed
63
+
64
+ ## Configuration
65
+
66
+ You can add custom directives to be processed in two ways:
67
+
68
+ ### 1. Command Line
69
+
70
+ Use the `--directives` option with any command to add custom directives for a single run:
71
+
72
+ ```bash
73
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
74
+ ```
75
+
76
+ ### 2. Configuration File
77
+
78
+ Create a `.sphinx-cmd.toml` file in your home directory with your custom directives:
79
+
80
+ ```toml
81
+ directives = [
82
+ "drawio-figure",
83
+ "drawio-image"
84
+ ]
85
+ ```
86
+
87
+ Command line directives will be combined with those in the configuration file and the built-in defaults.
88
+
52
89
 
53
90
  ## Development
54
91
 
@@ -20,7 +20,7 @@ classifiers = [
20
20
  "Programming Language :: Python :: 3.11",
21
21
  "Programming Language :: Python :: 3.12",
22
22
  ]
23
- dependencies = []
23
+ dependencies = ["tomli>=2.0.0; python_version < '3.11'"]
24
24
 
25
25
  [project.urls]
26
26
  "Homepage" = "https://github.com/jdillard/sphinx-cmd"
@@ -37,6 +37,7 @@ dev = [
37
37
  "flake8",
38
38
  "mypy",
39
39
  "isort",
40
+ "pre-commit",
40
41
  ]
41
42
  test = [
42
43
  "pytest>=7.0.0",
@@ -77,6 +78,10 @@ profile = "black"
77
78
  [tool.mypy]
78
79
  python_version = "3.9"
79
80
 
81
+ [[tool.mypy.overrides]]
82
+ module = "tomli"
83
+ ignore_missing_imports = true
84
+
80
85
  [tool.pytest.ini_options]
81
86
  testpaths = ["tests"]
82
87
  python_files = ["test_*.py"]
@@ -1,4 +1,4 @@
1
1
  # This file makes sphinx_cmd a Python package
2
2
  """Sphinx command-line utilities."""
3
3
 
4
- __version__ = "0.2.2"
4
+ __version__ = "0.3.1"
@@ -39,6 +39,12 @@ def create_parser():
39
39
  action="store_true",
40
40
  help="Preview deletions without removing files",
41
41
  )
42
+ rm_parser.add_argument(
43
+ "--directives",
44
+ type=lambda s: [x.strip() for x in s.split(",")],
45
+ help="Additional directives to process as comma-separated list"
46
+ " (e.g. 'drawio-figure,drawio-image')",
47
+ )
42
48
  rm_parser.set_defaults(command_name="rm")
43
49
 
44
50
  # Add 'mv' subcommand
@@ -58,6 +64,12 @@ def create_parser():
58
64
  action="store_true",
59
65
  help="Do not update references to the moved file",
60
66
  )
67
+ mv_parser.add_argument(
68
+ "--directives",
69
+ type=lambda s: [x.strip() for x in s.split(",")],
70
+ help="Additional directives to process as comma-separated list"
71
+ " (e.g. 'drawio-figure,drawio-image')",
72
+ )
61
73
  mv_parser.set_defaults(command_name="mv")
62
74
 
63
75
  return parser
@@ -4,15 +4,9 @@ Command to delete unused .rst files and their unique assets.
4
4
  """
5
5
 
6
6
  import os
7
- import re
8
7
  from collections import defaultdict
9
8
 
10
- # Regex patterns for reStructuredText directives
11
- DIRECTIVE_PATTERNS = {
12
- "image": re.compile(r"^\s*\.\.\s+image::\s+(.+)$", re.MULTILINE),
13
- "figure": re.compile(r"^\s*\.\.\s+figure::\s+(.+)$", re.MULTILINE),
14
- "include": re.compile(r"^\s*\.\.\s+include::\s+(.+)$", re.MULTILINE),
15
- }
9
+ from sphinx_cmd.config import get_directive_patterns
16
10
 
17
11
 
18
12
  def find_rst_files(path):
@@ -27,7 +21,7 @@ def find_rst_files(path):
27
21
  return rst_files
28
22
 
29
23
 
30
- def extract_assets(file_path, visited=None):
24
+ def extract_assets(file_path, visited=None, cli_directives=None):
31
25
  """Extract asset references from an .rst file, recursively parsing includes."""
32
26
  if visited is None:
33
27
  visited = set()
@@ -39,6 +33,7 @@ def extract_assets(file_path, visited=None):
39
33
  visited.add(abs_path)
40
34
 
41
35
  asset_directives = {}
36
+ directive_patterns = get_directive_patterns(cli_directives)
42
37
 
43
38
  # If file doesn't exist, skip it
44
39
  if not os.path.exists(file_path):
@@ -47,7 +42,7 @@ def extract_assets(file_path, visited=None):
47
42
  try:
48
43
  with open(file_path, encoding="utf-8") as f:
49
44
  content = f.read()
50
- for directive, pattern in DIRECTIVE_PATTERNS.items():
45
+ for directive, pattern in directive_patterns.items():
51
46
  for match in pattern.findall(content):
52
47
  asset_path = match.strip()
53
48
  asset_full_path = os.path.normpath(
@@ -68,14 +63,14 @@ def extract_assets(file_path, visited=None):
68
63
  return asset_directives
69
64
 
70
65
 
71
- def build_asset_index(rst_files):
66
+ def build_asset_index(rst_files, cli_directives=None):
72
67
  """Build an index of assets and which files reference them."""
73
68
  asset_to_files = defaultdict(set)
74
69
  file_to_assets = {}
75
70
  asset_directive_map = {}
76
71
 
77
72
  for rst in rst_files:
78
- asset_directives = extract_assets(rst)
73
+ asset_directives = extract_assets(rst, cli_directives=cli_directives)
79
74
  file_to_assets[rst] = set(asset_directives.keys())
80
75
  for asset, directive in asset_directives.items():
81
76
  asset_to_files[asset].add(rst)
@@ -83,7 +78,7 @@ def build_asset_index(rst_files):
83
78
  return asset_to_files, file_to_assets, asset_directive_map
84
79
 
85
80
 
86
- def get_transitive_includes(file_path, visited=None):
81
+ def get_transitive_includes(file_path, visited=None, cli_directives=None):
87
82
  """Get all files included transitively from a file."""
88
83
  if visited is None:
89
84
  visited = set()
@@ -99,20 +94,26 @@ def get_transitive_includes(file_path, visited=None):
99
94
  if not os.path.exists(file_path):
100
95
  return includes
101
96
 
97
+ directive_patterns = get_directive_patterns(cli_directives)
98
+
102
99
  try:
103
100
  with open(file_path, encoding="utf-8") as f:
104
101
  content = f.read()
105
- pattern = DIRECTIVE_PATTERNS["include"]
106
- for match in pattern.findall(content):
107
- include_path = match.strip()
108
- include_full_path = os.path.normpath(
109
- os.path.join(os.path.dirname(file_path), include_path)
110
- )
111
- includes.add(include_full_path)
112
- # Recursively get includes from the included file
113
- includes.update(
114
- get_transitive_includes(include_full_path, visited.copy())
115
- )
102
+ # Only process include directive
103
+ if "include" in directive_patterns:
104
+ pattern = directive_patterns["include"]
105
+ for match in pattern.findall(content):
106
+ include_path = match.strip()
107
+ include_full_path = os.path.normpath(
108
+ os.path.join(os.path.dirname(file_path), include_path)
109
+ )
110
+ includes.add(include_full_path)
111
+ # Recursively get includes from the included file
112
+ includes.update(
113
+ get_transitive_includes(
114
+ include_full_path, visited.copy(), cli_directives
115
+ )
116
+ )
116
117
  except Exception as e:
117
118
  print(f"Warning: Could not read {file_path}: {e}")
118
119
 
@@ -238,7 +239,9 @@ def execute(args):
238
239
  """Execute the rm command."""
239
240
  original_path = os.path.abspath(args.path)
240
241
  rst_files = find_rst_files(args.path)
241
- asset_to_files, file_to_assets, asset_directive_map = build_asset_index(rst_files)
242
+ asset_to_files, file_to_assets, asset_directive_map = build_asset_index(
243
+ rst_files, cli_directives=args.directives
244
+ )
242
245
  deleted_pages, deleted_assets, affected_dirs = delete_unused_assets_and_pages(
243
246
  asset_to_files, file_to_assets, asset_directive_map, args.dry_run
244
247
  )
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Configuration handling for sphinx-cmd.
4
+ """
5
+
6
+ import re
7
+ import sys
8
+ from pathlib import Path
9
+ from typing import Dict, Optional
10
+
11
+ # Use the standard library tomllib if Python 3.11+, otherwise use tomli package
12
+ if sys.version_info >= (3, 11):
13
+ import tomllib
14
+ else:
15
+ import tomli as tomllib
16
+
17
+ # Default configuration with built-in directives
18
+ DEFAULT_CONFIG = {"directives": ["image", "figure", "include"]}
19
+
20
+
21
+ def get_config_path() -> Optional[Path]:
22
+ """Get the path to the configuration file."""
23
+ # Check for config in user's home directory
24
+ home_config = Path.home() / ".sphinx-cmd.toml"
25
+ if home_config.exists():
26
+ return home_config
27
+
28
+ return None
29
+
30
+
31
+ def load_config(cli_directives=None) -> Dict:
32
+ """
33
+ Load configuration from a TOML file and merge with CLI directives.
34
+
35
+ The function looks for a config file at:
36
+ - user's home directory
37
+
38
+ Args:
39
+ cli_directives: Optional list of directive names passed from the command line
40
+
41
+ Returns:
42
+ Dict: The merged configuration (defaults + user config + CLI directives)
43
+ """
44
+ config = DEFAULT_CONFIG.copy()
45
+
46
+ config_path = get_config_path()
47
+ if config_path:
48
+ try:
49
+ with open(config_path, "rb") as f:
50
+ user_config = tomllib.load(f)
51
+
52
+ # Merge user directives with default directives
53
+ if "directives" in user_config:
54
+ # If user config has list of directive names, extend default list
55
+ config["directives"].extend(
56
+ [
57
+ name
58
+ for name in user_config["directives"]
59
+ if name not in config["directives"]
60
+ ]
61
+ )
62
+
63
+ except Exception as e:
64
+ print(f"Warning: Error loading config from {config_path}: {e}")
65
+
66
+ # Add CLI directives if provided
67
+ if cli_directives:
68
+ config["directives"].extend(
69
+ [name for name in cli_directives if name not in config["directives"]]
70
+ )
71
+
72
+ return config
73
+
74
+
75
+ def get_directive_patterns(cli_directives=None) -> Dict[str, re.Pattern]:
76
+ """
77
+ Get compiled regex patterns for all directives.
78
+
79
+ Args:
80
+ cli_directives: Optional list of directive names passed from the command line
81
+
82
+ Returns:
83
+ Dict[str, re.Pattern]: Dictionary of directive names to compiled regex patterns
84
+ """
85
+ config = load_config(cli_directives)
86
+ patterns = {}
87
+
88
+ for name in config["directives"]:
89
+ # Generate regex pattern from directive name
90
+ pattern = rf"^\s*\.\.\s+{name}::\s+(.+)$"
91
+ patterns[name] = re.compile(pattern, re.MULTILINE)
92
+
93
+ return patterns
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sphinx-cmd
3
- Version: 0.2.2
3
+ Version: 0.3.1
4
4
  Summary: Command-line tools for Sphinx documentation management
5
5
  Author-email: Jared Dillard <jared.dillard@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
@@ -16,12 +16,14 @@ Classifier: Programming Language :: Python :: 3.12
16
16
  Requires-Python: >=3.9
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
+ Requires-Dist: tomli>=2.0.0; python_version < "3.11"
19
20
  Provides-Extra: dev
20
21
  Requires-Dist: pytest>=7.0.0; extra == "dev"
21
22
  Requires-Dist: black; extra == "dev"
22
23
  Requires-Dist: flake8; extra == "dev"
23
24
  Requires-Dist: mypy; extra == "dev"
24
25
  Requires-Dist: isort; extra == "dev"
26
+ Requires-Dist: pre-commit; extra == "dev"
25
27
  Provides-Extra: test
26
28
  Requires-Dist: pytest>=7.0.0; extra == "test"
27
29
  Dynamic: license-file
@@ -50,8 +52,15 @@ sphinx-cmd rm path/to/docs
50
52
 
51
53
  # Dry run to preview deletions
52
54
  sphinx-cmd rm path/to/docs --dry-run
55
+
56
+ # Process additional directives beyond defaults
57
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
53
58
  ```
54
59
 
60
+ #### Features
61
+
62
+ - Configure custom directives to be processed
63
+
55
64
  ### `sphinx-cmd mv`
56
65
 
57
66
  Move/rename .rst files and automatically update all references to them.
@@ -68,6 +77,9 @@ sphinx-cmd mv old-file.rst new-file.rst --dry-run
68
77
 
69
78
  # Move without updating references
70
79
  sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
80
+
81
+ # Process additional directives beyond defaults
82
+ sphinx-cmd mv old-file.rst new-file.rst --directives drawio-figure,drawio-image
71
83
  ```
72
84
 
73
85
  #### Features
@@ -77,6 +89,33 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
77
89
  - Updates `include` and `literalinclude` directives
78
90
  - Handles relative paths correctly
79
91
  - Preserves file relationships
92
+ - Configure custom directives to be processed
93
+
94
+ ## Configuration
95
+
96
+ You can add custom directives to be processed in two ways:
97
+
98
+ ### 1. Command Line
99
+
100
+ Use the `--directives` option with any command to add custom directives for a single run:
101
+
102
+ ```bash
103
+ sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
104
+ ```
105
+
106
+ ### 2. Configuration File
107
+
108
+ Create a `.sphinx-cmd.toml` file in your home directory with your custom directives:
109
+
110
+ ```toml
111
+ directives = [
112
+ "drawio-figure",
113
+ "drawio-image"
114
+ ]
115
+ ```
116
+
117
+ Command line directives will be combined with those in the configuration file and the built-in defaults.
118
+
80
119
 
81
120
  ## Development
82
121
 
@@ -4,6 +4,7 @@ README.md
4
4
  pyproject.toml
5
5
  sphinx_cmd/__init__.py
6
6
  sphinx_cmd/cli.py
7
+ sphinx_cmd/config.py
7
8
  sphinx_cmd/py.typed
8
9
  sphinx_cmd.egg-info/PKG-INFO
9
10
  sphinx_cmd.egg-info/SOURCES.txt
@@ -15,6 +16,8 @@ sphinx_cmd/commands/__init__.py
15
16
  sphinx_cmd/commands/mv.py
16
17
  sphinx_cmd/commands/rm.py
17
18
  tests/test_cli.py
19
+ tests/test_config.py
20
+ tests/test_custom_directives.py
18
21
  tests/test_integration.py
19
22
  tests/test_mv.py
20
23
  tests/test_rm.py
@@ -1,10 +1,14 @@
1
1
 
2
+ [:python_version < "3.11"]
3
+ tomli>=2.0.0
4
+
2
5
  [dev]
3
6
  pytest>=7.0.0
4
7
  black
5
8
  flake8
6
9
  mypy
7
10
  isort
11
+ pre-commit
8
12
 
9
13
  [test]
10
14
  pytest>=7.0.0
@@ -0,0 +1,109 @@
1
+ import tempfile
2
+ from pathlib import Path
3
+ from unittest.mock import patch
4
+
5
+ from sphinx_cmd.config import get_directive_patterns, load_config
6
+
7
+
8
+ def test_default_config():
9
+ """Test that default config is returned when no config file exists."""
10
+ with patch("sphinx_cmd.config.get_config_path", return_value=None):
11
+ config = load_config()
12
+
13
+ # Check that default directives are present
14
+ assert "directives" in config
15
+ assert "image" in config["directives"]
16
+ assert "figure" in config["directives"]
17
+ assert "include" in config["directives"]
18
+
19
+
20
+ def test_load_custom_config():
21
+ """Test loading a custom configuration file."""
22
+ with tempfile.TemporaryDirectory() as tmpdir:
23
+ config_path = Path(tmpdir) / ".sphinx-cmd.toml"
24
+
25
+ # Create a custom config file with proper TOML syntax
26
+ toml_content = """
27
+ directives = ["image", "figure", "include", "drawio-figure", "drawio-image"]
28
+ """
29
+ with open(config_path, "wb") as f:
30
+ f.write(toml_content.encode())
31
+
32
+ with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
33
+ config = load_config()
34
+
35
+ # Check that custom directives are present
36
+ assert "directives" in config
37
+ assert "drawio-figure" in config["directives"]
38
+ assert "drawio-image" in config["directives"]
39
+
40
+ # Check original directives are still there
41
+ assert "image" in config["directives"]
42
+ assert "figure" in config["directives"]
43
+ assert "include" in config["directives"]
44
+
45
+
46
+ def test_get_directive_patterns():
47
+ """Test that directive patterns are compiled correctly."""
48
+ with tempfile.TemporaryDirectory() as tmpdir:
49
+ config_path = Path(tmpdir) / ".sphinx-cmd.toml"
50
+
51
+ # Create a custom config file with drawio directives with proper TOML syntax
52
+ toml_content = """
53
+ directives = ["drawio-figure"]
54
+ """
55
+ with open(config_path, "wb") as f:
56
+ f.write(toml_content.encode())
57
+
58
+ with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
59
+ patterns = get_directive_patterns()
60
+
61
+ # Default patterns should be included
62
+ assert "image" in patterns
63
+ assert "figure" in patterns
64
+ assert "include" in patterns
65
+
66
+ # Custom pattern should be included and be a compiled regex
67
+ assert "drawio-figure" in patterns
68
+
69
+ # Test the pattern works
70
+ test_string = ".. drawio-figure:: path/to/diagram.drawio"
71
+ match = patterns["drawio-figure"].findall(test_string)
72
+ assert len(match) == 1
73
+ assert match[0] == "path/to/diagram.drawio"
74
+
75
+
76
+ def test_cli_directives():
77
+ """Test that CLI directives are properly merged with config directives."""
78
+ with tempfile.TemporaryDirectory() as tmpdir:
79
+ config_path = Path(tmpdir) / ".sphinx-cmd.toml"
80
+
81
+ # Create a custom config file with some directives
82
+ toml_content = """
83
+ directives = ["drawio-figure"]
84
+ """
85
+ with open(config_path, "wb") as f:
86
+ f.write(toml_content.encode())
87
+
88
+ with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
89
+ # Add CLI directives
90
+ cli_directives = ["drawio-image", "custom-directive"]
91
+ patterns = get_directive_patterns(cli_directives)
92
+
93
+ # Default patterns should be included
94
+ assert "image" in patterns
95
+ assert "figure" in patterns
96
+ assert "include" in patterns
97
+
98
+ # Config file directives should be included
99
+ assert "drawio-figure" in patterns
100
+
101
+ # CLI directives should be included
102
+ assert "drawio-image" in patterns
103
+ assert "custom-directive" in patterns
104
+
105
+ # Test a CLI directive pattern works
106
+ test_string = ".. custom-directive:: path/to/custom.file"
107
+ match = patterns["custom-directive"].findall(test_string)
108
+ assert len(match) == 1
109
+ assert match[0] == "path/to/custom.file"
@@ -0,0 +1,123 @@
1
+ import os
2
+ import tempfile
3
+ from pathlib import Path
4
+ from unittest.mock import Mock, patch
5
+
6
+ from sphinx_cmd.commands.rm import execute, extract_assets
7
+
8
+
9
+ def test_custom_directive_extraction():
10
+ """Test that custom directives are extracted from RST files."""
11
+ with tempfile.TemporaryDirectory() as tmpdir:
12
+ # Create a test config with custom directives
13
+ config_path = Path(tmpdir) / ".sphinx-cmd.toml"
14
+
15
+ # Create config with proper TOML syntax
16
+ toml_content = """
17
+ directives = ["drawio-figure", "drawio-image"]
18
+ """
19
+ with open(config_path, "wb") as f:
20
+ f.write(toml_content.encode())
21
+
22
+ # Create a test file with custom directives
23
+ test_file = os.path.join(tmpdir, "test.rst")
24
+ with open(test_file, "w") as f:
25
+ f.write(
26
+ """
27
+ Test Document
28
+ ============
29
+
30
+ .. image:: standard-image.png
31
+ .. drawio-figure:: custom-figure.drawio
32
+ .. drawio-image:: custom-image.drawio
33
+ """
34
+ )
35
+
36
+ # Mock the config path function to use our temp config
37
+ with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
38
+ # Extract assets
39
+ assets = extract_assets(test_file)
40
+
41
+ # Check we have the right number of assets (3)
42
+ assert len(assets) == 3
43
+
44
+ # Check all asset paths are correct
45
+ img_path = os.path.join(tmpdir, "standard-image.png")
46
+ custom_figure_path = os.path.join(tmpdir, "custom-figure.drawio")
47
+ custom_image_path = os.path.join(tmpdir, "custom-image.drawio")
48
+
49
+ assert img_path in assets
50
+ assert custom_figure_path in assets
51
+ assert custom_image_path in assets
52
+
53
+ # Check the directive types are correct
54
+ assert assets[img_path] == "image"
55
+ assert assets[custom_figure_path] == "drawio-figure"
56
+ assert assets[custom_image_path] == "drawio-image"
57
+
58
+
59
+ def test_rm_command_with_custom_directives():
60
+ """Test the rm command with custom directives (using dry-run)."""
61
+ with tempfile.TemporaryDirectory() as tmpdir:
62
+ # Create a test config with custom directives
63
+ config_path = Path(tmpdir) / ".sphinx-cmd.toml"
64
+
65
+ # Create config with proper TOML syntax
66
+ toml_content = """
67
+ directives = ["drawio-figure"]
68
+ """
69
+ with open(config_path, "wb") as f:
70
+ f.write(toml_content.encode())
71
+
72
+ # Create test directory
73
+ test_dir = os.path.join(tmpdir, "docs")
74
+ os.makedirs(test_dir)
75
+
76
+ # Create an RST file with custom directives
77
+ rst_content = """
78
+ Test Page
79
+ =========
80
+
81
+ .. drawio-figure:: custom.drawio
82
+ """
83
+ with open(os.path.join(test_dir, "test.rst"), "w") as f:
84
+ f.write(rst_content)
85
+
86
+ # Create the asset files
87
+ with open(os.path.join(test_dir, "custom.drawio"), "w") as f:
88
+ f.write("fake drawio")
89
+
90
+ # Execute the rm command with mocked config
91
+ with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
92
+ # Setup to capture stdout
93
+ import sys
94
+ from io import StringIO
95
+
96
+ captured_output = StringIO()
97
+ original_stdout = sys.stdout
98
+ sys.stdout = captured_output
99
+
100
+ try:
101
+ args = Mock()
102
+ args.path = test_dir
103
+ args.dry_run = True
104
+ args.directives = None
105
+
106
+ # Run with dry-run to test detection
107
+ execute(args)
108
+
109
+ # Get the captured output
110
+ output = captured_output.getvalue()
111
+
112
+ # Verify the correct files would be deleted
113
+ assert "[dry-run] Would delete drawio-figure:" in output
114
+ assert "custom.drawio" in output
115
+ assert "[dry-run] Would delete page:" in output
116
+ assert "test.rst" in output
117
+ finally:
118
+ sys.stdout = original_stdout
119
+
120
+ # Files should still exist after dry run
121
+ assert os.path.exists(os.path.join(test_dir, "test.rst"))
122
+ assert os.path.exists(os.path.join(test_dir, "custom.drawio"))
123
+ assert os.path.exists(test_dir)
@@ -84,6 +84,7 @@ Test Page
84
84
  args = Mock()
85
85
  args.path = test_dir
86
86
  args.dry_run = True
87
+ args.directives = None
87
88
  execute(args)
88
89
 
89
90
  # Verify files still exist after dry run
@@ -127,6 +128,7 @@ def test_empty_directory_removal():
127
128
  args = Mock()
128
129
  args.path = docs_dir
129
130
  args.dry_run = False
131
+ args.directives = None
130
132
 
131
133
  # Execute the command to remove files
132
134
  execute(args)
@@ -169,6 +171,7 @@ def test_non_empty_directory_retained():
169
171
  args = Mock()
170
172
  args.path = docs_dir
171
173
  args.dry_run = False
174
+ args.directives = None
172
175
 
173
176
  # Execute the command to remove files
174
177
  execute(args)
File without changes
File without changes
File without changes
File without changes
File without changes