-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathreverse_dates.py
More file actions
executable file
·147 lines (114 loc) · 4.64 KB
/
reverse_dates.py
File metadata and controls
executable file
·147 lines (114 loc) · 4.64 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""
Script to reverse date format in filenames from MM-YYYY to YYYY-MM
in the content folder and update title fields in frontmatter.
"""
import os
import re
import sys
def update_file_content(file_path):
"""
Update the title field in the frontmatter to reverse MM-YYYY to YYYY-MM
Args:
file_path (str): Path to the markdown file
Returns:
bool: True if content was updated, False otherwise
"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern to match title: MM-YYYY-<name> in frontmatter
title_pattern = r'^title:\s*(\d{2})-(\d{4})-(.+)$'
lines = content.split('\n')
updated = False
for i, line in enumerate(lines):
match = re.match(title_pattern, line)
if match:
month, year, rest = match.groups()
new_title = f"title: {year}-{month}-{rest}"
lines[i] = new_title
updated = True
print(f" Updated title: {line.strip()} -> {new_title}")
break
if updated:
with open(file_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(lines))
return updated
except Exception as e:
print(f" Error updating content: {e}")
return False
def reverse_date_format(content_dir="content"):
"""
Reverse date format in filenames from MM-YYYY-<name>.md to YYYY-MM-<name>.md
and update title fields in frontmatter
Args:
content_dir (str): Directory containing the files to rename
"""
if not os.path.exists(content_dir):
print(f"Error: Directory '{content_dir}' does not exist.")
return
# Pattern to match MM-YYYY-<filename>.md
pattern = r'^(\d{2})-(\d{4})-(.+)\.md$'
renamed_files = []
skipped_files = []
updated_content = []
try:
files = os.listdir(content_dir)
for filename in files:
match = re.match(pattern, filename)
if match:
month, year, rest = match.groups()
new_filename = f"{year}-{month}-{rest}.md"
old_path = os.path.join(content_dir, filename)
new_path = os.path.join(content_dir, new_filename)
# Check if target file already exists
if os.path.exists(new_path):
print(f"Warning: Target file '{new_filename}' already exists. Skipping '{filename}'")
skipped_files.append(filename)
continue
try:
# First, update the content of the file
print(f"Processing: {filename}")
content_updated = update_file_content(old_path)
if content_updated:
updated_content.append(filename)
# Then rename the file
os.rename(old_path, new_path)
renamed_files.append((filename, new_filename))
print(f"Renamed: {filename} -> {new_filename}")
except OSError as e:
print(f"Error renaming '{filename}': {e}")
skipped_files.append(filename)
else:
print(f"Skipping '{filename}' (doesn't match MM-YYYY-<name>.md pattern)")
skipped_files.append(filename)
except OSError as e:
print(f"Error reading directory '{content_dir}': {e}")
return
# Summary
print(f"\n--- Summary ---")
print(f"Files renamed: {len(renamed_files)}")
print(f"Files with updated content: {len(updated_content)}")
print(f"Files skipped: {len(skipped_files)}")
if renamed_files:
print("\nSuccessfully renamed files:")
for old, new in renamed_files:
print(f" {old} -> {new}")
if updated_content:
print("\nFiles with updated title fields:")
for filename in updated_content:
print(f" {filename}")
def main():
"""Main function to run the script."""
print("Date Format Reversal Script")
print("Converting MM-YYYY-<name>.md to YYYY-MM-<name>.md")
print("Also updating title fields in frontmatter")
print("-" * 60)
# Ask for confirmation
response = input("Do you want to proceed with renaming files? (y/N): ")
if response.lower() not in ['y', 'yes']:
print("Operation cancelled.")
return
reverse_date_format()
if __name__ == "__main__":
main()