-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwc.py
More file actions
55 lines (43 loc) · 1.67 KB
/
wc.py
File metadata and controls
55 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import re
import sys
def remove_code_blocks(text):
"""Remove fenced and inline code from markdown text."""
# Remove fenced code blocks: ```lang ... ```
text = re.sub(r"```.*?```", "", text, flags=re.DOTALL)
# Remove inline code: `code`
text = re.sub(r"`[^`]+`", "", text)
return text
def count_words_in_text(text):
"""Count words in cleaned text."""
words = re.findall(r"\b\w+\b", text)
return len(words)
def count_words_in_markdown_file(file_path):
"""Read markdown file, strip code blocks, and count words."""
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
cleaned_text = remove_code_blocks(content)
return count_words_in_text(cleaned_text)
def count_words_in_folder(folder_path):
total_words = 0
print(f"📁 Scanning folder: {folder_path}\n")
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(".md"):
file_path = os.path.join(root, file)
try:
word_count = count_words_in_markdown_file(file_path)
total_words += word_count
print(f"{file_path}: {word_count} words")
except Exception as e:
print(f"⚠️ Error reading {file_path}: {e}")
print(f"\n🧮 Total word count (excluding code blocks): {total_words}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python wc.py <folder_path>")
sys.exit(1)
folder = sys.argv[1]
if not os.path.isdir(folder):
print(f"❌ Invalid folder path: {folder}")
sys.exit(1)
count_words_in_folder(folder)