Skip to content

Commit 7733afe

Browse files
committed
feat(merge_posts): extract date from filename if missing
1 parent 9bb925c commit 7733afe

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

scripts/merge/merge_posts.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def extract_date_from_filename(filename):
2626
return date_str.replace('-', '.')
2727
return None
2828

29-
def process_post_content(content):
29+
def process_post_content(content, filename=None):
3030
"""Process a single post's content and extract metadata."""
3131
sub_parts = content.split("---", 2)
3232
date = None
@@ -43,6 +43,10 @@ def process_post_content(content):
4343
elif line.startswith('date:'):
4444
date = line.split(':', 1)[1].strip().strip('"\'')
4545

46+
# If no date in front matter, try to extract from filename
47+
if not date and filename:
48+
date = extract_date_from_filename(filename)
49+
4650
return {
4751
'content': content,
4852
'body': body,
@@ -82,10 +86,50 @@ def merge_post_contents(contents):
8286

8387
return combined_content
8488

89+
def merge_post_contents_with_filenames(post_data):
90+
"""Merge multiple post contents into one, using filename for date extraction."""
91+
contents = [post['content'] for post in post_data]
92+
filenames = [post['filename'] for post in post_data]
93+
94+
if len(contents) < 2:
95+
print("Error: At least 2 posts are required")
96+
return None
97+
if len(contents) > 10:
98+
print("Error: Maximum 10 posts allowed")
99+
return None
100+
101+
# Process and sort posts
102+
processed_posts = []
103+
for i, content in enumerate(contents):
104+
filename = filenames[i] if i < len(filenames) else None
105+
processed_posts.append(process_post_content(content, filename))
106+
107+
processed_posts.sort(key=lambda x: x['date'] if x['date'] else '', reverse=True)
108+
109+
# First post is main post
110+
main_content = processed_posts[0]['content']
111+
combined_content = main_content
112+
113+
# Process sub posts - add date in the format *2025.07.12* after the section title
114+
for post in processed_posts[1:]:
115+
if post['title']:
116+
header = f"## {post['title']}"
117+
if post['date']:
118+
# Date is already in format YYYY.MM.DD from filename extraction
119+
header += f"\n\n*{post['date']}*"
120+
sub_body = f"{header}\n\n{post['body']}"
121+
else:
122+
sub_body = post['body'].strip()
123+
124+
if sub_body:
125+
combined_content += "\n\n---\n\n" + sub_body
126+
127+
return combined_content
128+
85129
def combine_posts(posts):
86130
"""Combine multiple posts from files."""
87131
# Read contents from files
88-
contents = []
132+
post_data = []
89133
for path in posts:
90134
abs_path = os.path.abspath(path) if not os.path.isabs(path) else path
91135
if not os.path.exists(abs_path):
@@ -94,13 +138,14 @@ def combine_posts(posts):
94138

95139
try:
96140
with open(abs_path, "r", encoding="utf-8") as f:
97-
contents.append(f.read())
141+
content = f.read()
142+
post_data.append({'content': content, 'filename': os.path.basename(abs_path)})
98143
except Exception as e:
99144
print(f"Error reading file {abs_path}: {e}")
100145
return
101146

102147
# Merge contents
103-
combined_content = merge_post_contents(contents)
148+
combined_content = merge_post_contents_with_filenames(post_data)
104149
if combined_content is None:
105150
return
106151

0 commit comments

Comments
 (0)