@@ -26,7 +26,7 @@ def extract_date_from_filename(filename):
26
26
return date_str .replace ('-' , '.' )
27
27
return None
28
28
29
- def process_post_content (content ):
29
+ def process_post_content (content , filename = None ):
30
30
"""Process a single post's content and extract metadata."""
31
31
sub_parts = content .split ("---" , 2 )
32
32
date = None
@@ -43,6 +43,10 @@ def process_post_content(content):
43
43
elif line .startswith ('date:' ):
44
44
date = line .split (':' , 1 )[1 ].strip ().strip ('"\' ' )
45
45
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
+
46
50
return {
47
51
'content' : content ,
48
52
'body' : body ,
@@ -82,10 +86,50 @@ def merge_post_contents(contents):
82
86
83
87
return combined_content
84
88
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
+
85
129
def combine_posts (posts ):
86
130
"""Combine multiple posts from files."""
87
131
# Read contents from files
88
- contents = []
132
+ post_data = []
89
133
for path in posts :
90
134
abs_path = os .path .abspath (path ) if not os .path .isabs (path ) else path
91
135
if not os .path .exists (abs_path ):
@@ -94,13 +138,14 @@ def combine_posts(posts):
94
138
95
139
try :
96
140
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 )})
98
143
except Exception as e :
99
144
print (f"Error reading file { abs_path } : { e } " )
100
145
return
101
146
102
147
# Merge contents
103
- combined_content = merge_post_contents ( contents )
148
+ combined_content = merge_post_contents_with_filenames ( post_data )
104
149
if combined_content is None :
105
150
return
106
151
0 commit comments