-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_groww_docs.py
More file actions
102 lines (75 loc) · 3.08 KB
/
Copy pathclean_groww_docs.py
File metadata and controls
102 lines (75 loc) · 3.08 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Clean up Groww scraped markdown files.
"""
import re
from pathlib import Path
OUTPUT_DIR = Path(__file__).parent / "output" / "groww-trade-api"
def clean_markdown(content: str) -> str:
"""Remove navigation noise from Groww markdown content."""
lines = content.split('\n')
cleaned_lines = []
in_nav_block = False
for line in lines:
# Remove "Skip to content" links
if re.match(r'\[Skip to content\]', line):
continue
# Remove logo/header links
if re.match(r'\[!\[.*?\]\([^)]+\)\]\([^)]+\)', line):
continue
# Remove navigation sidebar patterns
if re.match(r'^-+\s+\[', line) and 'docs' in line:
in_nav_block = True
continue
# Skip nav block until we hit content
if in_nav_block:
if line.strip() == '' or line.startswith('#'):
in_nav_block = False
else:
continue
# Remove "On this page" sections
if re.match(r'^##? On this page', line, re.IGNORECASE):
in_nav_block = True
continue
# Remove breadcrumb navigation
if re.match(r'^You are here:', line, re.IGNORECASE):
continue
if '›' in line and 'docs' in line.lower():
if line.count('›') > 1:
continue
# Remove "Edit this page" links
if re.match(r'^Edit this page', line, re.IGNORECASE):
continue
# Remove footer-like content
if re.match(r'^Built with', line, re.IGNORECASE):
continue
if re.match(r'^Made with', line, re.IGNORECASE):
continue
if 'Copyright ©' in line and 'Groww' in line:
continue
cleaned_lines.append(line)
content = '\n'.join(cleaned_lines)
# Remove multiple consecutive blank lines
content = re.sub(r'\n{3,}', '\n\n', content)
# Remove trailing whitespace
content = '\n'.join(line.rstrip() for line in content.split('\n'))
# Remove leading blank lines after frontmatter
content = re.sub(r'^(---\n(?:.*\n)*?---\n)\n+', r'\1', content)
return content
def clean_all_files():
"""Clean all markdown files in the output directory."""
cleaned_count = 0
total_chars_removed = 0
for md_file in OUTPUT_DIR.rglob('*.md'):
original_content = md_file.read_text(encoding='utf-8')
cleaned_content = clean_markdown(original_content)
chars_removed = len(original_content) - len(cleaned_content)
if chars_removed > 0:
md_file.write_text(cleaned_content, encoding='utf-8')
print(f"✓ {md_file.relative_to(OUTPUT_DIR)} (-{chars_removed:,} chars)")
cleaned_count += 1
total_chars_removed += chars_removed
else:
print(f"- {md_file.relative_to(OUTPUT_DIR)} (no change)")
print(f"\n✅ Cleaned {cleaned_count} files, removed {total_chars_removed:,} characters total")
if __name__ == "__main__":
clean_all_files()