tocs 1.0.4__tar.gz → 1.0.6__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.
- {tocs-1.0.4 → tocs-1.0.6}/PKG-INFO +1 -1
- {tocs-1.0.4 → tocs-1.0.6}/pyproject.toml +1 -1
- {tocs-1.0.4 → tocs-1.0.6}/tests/test_toc.py +23 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs/main.py +41 -20
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/PKG-INFO +1 -1
- {tocs-1.0.4 → tocs-1.0.6}/LICENSE +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/README.md +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/setup.cfg +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs/__init__.py +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/SOURCES.txt +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/dependency_links.txt +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/entry_points.txt +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/requires.txt +0 -0
- {tocs-1.0.4 → tocs-1.0.6}/tocs.egg-info/top_level.txt +0 -0
|
@@ -3,7 +3,7 @@ name = "tocs"
|
|
|
3
3
|
description = "Generate TOC for markdown files"
|
|
4
4
|
authors = [{ name = "Andrea Bondanini", email = "bondaniniandrea@gmail.com" }]
|
|
5
5
|
license = { text = "MIT" }
|
|
6
|
-
version = "1.0.
|
|
6
|
+
version = "1.0.6"
|
|
7
7
|
requires-python = ">=3.11.9"
|
|
8
8
|
readme = "README.md"
|
|
9
9
|
keywords = ["markdown", "tocs", "cli", "documentation", "generator"]
|
|
@@ -146,3 +146,26 @@ def test_create_toc_remove_special_chars():
|
|
|
146
146
|
result = create_toc_row(header)
|
|
147
147
|
|
|
148
148
|
assert result == "- [Hello!?.:;'\"()[]{}@#$%^&*+=<>/\\|`~World](#helloworld)"
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_dry_run():
|
|
152
|
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".md") as tmp:
|
|
153
|
+
shutil.copy("tests/fixtures/lorem_ipsum_input.md", tmp.name)
|
|
154
|
+
|
|
155
|
+
result = subprocess.run(
|
|
156
|
+
["python3", "tocs/main.py", tmp.name, "--dry-run"],
|
|
157
|
+
capture_output=True,
|
|
158
|
+
text=True,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
assert result.returncode == 0
|
|
162
|
+
|
|
163
|
+
# Verify file was not modified
|
|
164
|
+
with open(tmp.name) as f:
|
|
165
|
+
actual = f.read()
|
|
166
|
+
|
|
167
|
+
with open("tests/fixtures/lorem_ipsum_input.md") as f:
|
|
168
|
+
expected = f.read()
|
|
169
|
+
|
|
170
|
+
assert actual == expected
|
|
171
|
+
assert "- [Header 1](#header-1)" in result.stdout
|
|
@@ -6,7 +6,7 @@ import sys
|
|
|
6
6
|
INDENT_LEVEL = " "
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
def create_toc_row(line
|
|
9
|
+
def create_toc_row(line, max_depth=None):
|
|
10
10
|
hashes = 0
|
|
11
11
|
j = 0
|
|
12
12
|
|
|
@@ -23,12 +23,12 @@ def create_toc_row(line: str, max_depth: int = None) -> str:
|
|
|
23
23
|
return toc_row
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
def create_anchor(line_content
|
|
27
|
-
anchor = re.sub(r"[
|
|
26
|
+
def create_anchor(line_content):
|
|
27
|
+
anchor = re.sub(r"[^\w\-_ ]", "", line_content)
|
|
28
28
|
return anchor.replace(" ", "-").lower()
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
def toggle_ignore_rows_flag(ignore_rows
|
|
31
|
+
def toggle_ignore_rows_flag(ignore_rows, line):
|
|
32
32
|
"""
|
|
33
33
|
Controls whether to ignore rows inside code snippets that start with ```.
|
|
34
34
|
Also handles multiline snippets.
|
|
@@ -43,21 +43,13 @@ def toggle_ignore_rows_flag(ignore_rows: bool, line: str):
|
|
|
43
43
|
return ignore_rows
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
def
|
|
47
|
-
lines = []
|
|
46
|
+
def generate_toc(original_lines, depth):
|
|
48
47
|
tocs = []
|
|
49
48
|
init_toc_position = -1
|
|
50
49
|
end_toc_position = -1
|
|
51
50
|
ignore_rows = False
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
with open(path, "rt") as f:
|
|
55
|
-
lines = f.read().splitlines()
|
|
56
|
-
except (IOError, PermissionError) as e:
|
|
57
|
-
print(f"Error while reading {path}: {e}", file=sys.stderr)
|
|
58
|
-
sys.exit(1)
|
|
59
|
-
|
|
60
|
-
for i, line in enumerate(lines):
|
|
52
|
+
for i, line in enumerate(original_lines):
|
|
61
53
|
ignore_rows = toggle_ignore_rows_flag(ignore_rows, line)
|
|
62
54
|
|
|
63
55
|
if ignore_rows:
|
|
@@ -83,19 +75,43 @@ def process_file(path, depth=None):
|
|
|
83
75
|
)
|
|
84
76
|
sys.exit(1)
|
|
85
77
|
|
|
86
|
-
lines_before_tocs =
|
|
87
|
-
lines_after_tocs =
|
|
88
|
-
|
|
78
|
+
lines_before_tocs = original_lines[: init_toc_position + 1]
|
|
79
|
+
lines_after_tocs = original_lines[end_toc_position:]
|
|
80
|
+
return [lines_before_tocs, tocs, lines_after_tocs]
|
|
81
|
+
|
|
89
82
|
|
|
83
|
+
def read_lines_from(path):
|
|
84
|
+
try:
|
|
85
|
+
with open(path, "rt") as f:
|
|
86
|
+
lines = f.read().splitlines()
|
|
87
|
+
except (IOError, PermissionError) as e:
|
|
88
|
+
print(f"Error while reading {path}: {e}", file=sys.stderr)
|
|
89
|
+
sys.exit(1)
|
|
90
|
+
|
|
91
|
+
return lines
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def write_to(path, lines):
|
|
90
95
|
try:
|
|
91
96
|
with open(path, "w") as f:
|
|
92
|
-
for l in
|
|
97
|
+
for l in lines:
|
|
93
98
|
f.write(l + "\n")
|
|
94
99
|
except (IOError, PermissionError) as e:
|
|
95
100
|
print(f"Error while writing {path}: {e}", file=sys.stderr)
|
|
96
101
|
sys.exit(1)
|
|
97
102
|
|
|
98
103
|
|
|
104
|
+
def process(path: Path, depth: int | None = None, dry_run: bool = False):
|
|
105
|
+
original_lines = read_lines_from(path)
|
|
106
|
+
lines_before_tocs, tocs, lines_after_tocs = generate_toc(original_lines, depth)
|
|
107
|
+
|
|
108
|
+
if dry_run:
|
|
109
|
+
for t in tocs:
|
|
110
|
+
print(t)
|
|
111
|
+
else:
|
|
112
|
+
write_to(path, lines_before_tocs + tocs + lines_after_tocs)
|
|
113
|
+
|
|
114
|
+
|
|
99
115
|
def main():
|
|
100
116
|
parser = argparse.ArgumentParser(
|
|
101
117
|
prog="tocs",
|
|
@@ -114,7 +130,12 @@ def main():
|
|
|
114
130
|
parser.add_argument(
|
|
115
131
|
"--depth", type=int, help="Maximum header depth to include in TOC"
|
|
116
132
|
)
|
|
117
|
-
parser.add_argument("--version", action="version", version="tocs 1.0.
|
|
133
|
+
parser.add_argument("--version", action="version", version="tocs 1.0.6")
|
|
134
|
+
parser.add_argument(
|
|
135
|
+
"--dry-run",
|
|
136
|
+
action="store_true",
|
|
137
|
+
help="Print TOC to stdout without modifying the file",
|
|
138
|
+
)
|
|
118
139
|
args = parser.parse_args()
|
|
119
140
|
path = Path(args.file).resolve()
|
|
120
141
|
|
|
@@ -126,7 +147,7 @@ def main():
|
|
|
126
147
|
print(f"Error: depth must be greater than 0", file=sys.stderr)
|
|
127
148
|
sys.exit(1)
|
|
128
149
|
|
|
129
|
-
|
|
150
|
+
process(path, args.depth, args.dry_run)
|
|
130
151
|
|
|
131
152
|
|
|
132
153
|
if __name__ == "__main__":
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|