-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_html_from_excerpt.py
More file actions
90 lines (74 loc) · 2.83 KB
/
remove_html_from_excerpt.py
File metadata and controls
90 lines (74 loc) · 2.83 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
#!/usr/bin/env python3
"""Remove HTML tags from excerpt fields in markdown files."""
import re
from pathlib import Path
import html
def remove_html_tags(text):
"""Remove all HTML tags from text and decode HTML entities."""
# First decode HTML entities like "
text = html.unescape(text)
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', text)
# Clean up extra whitespace
text = re.sub(r'\s+', ' ', text)
text = text.strip()
return text
def process_file(file_path):
"""Process a single markdown file to remove HTML tags from excerpt."""
content = file_path.read_text(encoding='utf-8')
# Pattern to match excerpt field with HTML tags
# Handle both single-line and multi-line excerpts
pattern = r'^excerpt:\s*["\']?(<[^>]*>.*?(?:</[^>]*>)?)["\']?\s*$'
modified = False
lines = content.split('\n')
new_lines = []
for line in lines:
if line.strip().startswith('excerpt:'):
# Extract the excerpt content
match = re.match(r'^excerpt:\s*(["\']?)(.+?)\1\s*$', line)
if match:
quote_char = match.group(1) or '"'
excerpt_content = match.group(2)
# Check if it contains HTML tags
if '<' in excerpt_content and '>' in excerpt_content:
# Remove HTML tags
cleaned_content = remove_html_tags(excerpt_content)
# Reconstruct the line
new_line = f'excerpt: {quote_char}{cleaned_content}{quote_char}'
new_lines.append(new_line)
modified = True
print(f"Modified: {file_path.name}")
print(f" Before: {line}")
print(f" After: {new_line}")
else:
new_lines.append(line)
else:
new_lines.append(line)
else:
new_lines.append(line)
if modified:
file_path.write_text('\n'.join(new_lines), encoding='utf-8')
return True
return False
def main():
"""Process all markdown files in posts directories."""
base_path = Path(__file__).parent
# Process both tw and en posts
dirs_to_process = [
base_path / 'src' / 'content' / 'posts-tw',
base_path / 'src' / 'content' / 'posts-en',
]
total_modified = 0
for posts_dir in dirs_to_process:
if not posts_dir.exists():
print(f"Directory not found: {posts_dir}")
continue
print(f"\nProcessing directory: {posts_dir}")
md_files = list(posts_dir.glob('*.md'))
print(f"Found {len(md_files)} markdown files")
for md_file in md_files:
if process_file(md_file):
total_modified += 1
print(f"\n✅ Total files modified: {total_modified}")
if __name__ == '__main__':
main()