forked from mmistakes/minimal-mistakes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_post.py
More file actions
238 lines (205 loc) Β· 8.38 KB
/
new_post.py
File metadata and controls
238 lines (205 loc) Β· 8.38 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
"""
Jekyll Post Generator
Creates a new blog post with proper front matter and filename structure.
"""
import os
import sys
from datetime import datetime
import re
def slugify(text):
"""Convert text to URL-friendly slug"""
# Convert to lowercase and replace spaces with hyphens
slug = text.lower().strip()
# Remove special characters except hyphens and alphanumeric
slug = re.sub(r'[^a-z0-9\s-]', '', slug)
# Replace spaces and multiple hyphens with single hyphen
slug = re.sub(r'[\s-]+', '-', slug)
# Remove leading/trailing hyphens
slug = slug.strip('-')
return slug
def get_categories():
"""Get available categories from existing posts"""
posts_dir = "_posts"
categories = set()
if os.path.exists(posts_dir):
for filename in os.listdir(posts_dir):
if filename.endswith('.md'):
try:
with open(os.path.join(posts_dir, filename), 'r', encoding='utf-8') as f:
content = f.read()
# Extract category from front matter
if 'category:' in content:
lines = content.split('\n')
for line in lines:
if line.strip().startswith('category:'):
cat = line.split(':', 1)[1].strip().strip('"\'')
if cat:
categories.add(cat)
except:
continue
return sorted(list(categories))
def load_template():
"""Load the post template from _drafts/post-template.md"""
template_path = "_drafts/post-template.md"
if os.path.exists(template_path):
try:
with open(template_path, 'r', encoding='utf-8') as f:
return f.read()
except:
pass
return None
def create_post():
"""Main function to create a new post"""
print("π Jekyll Post Generator")
print("=" * 40)
# Get post title
while True:
title = input("π Post Title: ").strip()
if title:
break
print("β Title cannot be empty!")
# Get post category
existing_categories = get_categories()
if existing_categories:
print(f"\nπ Existing categories: {', '.join(existing_categories)}")
category = input("π Category (default: Software): ").strip()
if not category:
category = "Software"
# Get tags
print("\nπ·οΈ Tags (comma-separated, or press Enter to skip):")
tags_input = input(" Tags: ").strip()
tags = []
if tags_input:
tags = [tag.strip() for tag in tags_input.split(',') if tag.strip()]
# Get excerpt
excerpt = input("\nπ Excerpt (optional): ").strip()
# Ask for header image
header_image = input("\nπΌοΈ Header Image URL (optional): ").strip()
# Ask for teaser image
teaser_image = input("πΌοΈ Teaser Image URL (optional): ").strip()
# Ask for caption
caption = ""
if header_image:
caption = input("π Image Caption (optional): ").strip()
# Generate filename
today = datetime.now()
date_str = today.strftime("%Y-%m-%d")
slug = slugify(title)
filename = f"{date_str}-{slug}.md"
filepath = os.path.join("_posts", filename)
# Check if file already exists
if os.path.exists(filepath):
print(f"\nβ File {filename} already exists!")
overwrite = input("Do you want to overwrite it? (y/N): ").strip().lower()
if overwrite != 'y':
print("Aborted.")
return
# Load template and customize it
template_content = load_template()
if template_content:
# Use template and replace placeholders
content = template_content
content = content.replace('title: "Your Post Title Here"', f'title: "{title}"')
content = content.replace('category: Software # or other categories', f'category: {category}')
content = content.replace('date: 2025-01-15', f'date: {today.strftime("%Y-%m-%d")}')
content = content.replace('last_modified_at: 2025-01-15', f'last_modified_at: {today.strftime("%Y-%m-%d")}')
if excerpt:
content = content.replace('excerpt: "Brief description that appears in previews and social shares"', f'excerpt: "{excerpt}"')
else:
content = content.replace('excerpt: "Brief description that appears in previews and social shares"\n', '')
# Replace tags
if tags:
tags_yaml = "tags:\n" + "\n".join([f" - {tag}" for tag in tags])
content = content.replace('tags: \n - DevOps\n - Kubernetes\n - Tutorial', tags_yaml)
else:
# Remove default tags section
import re
content = re.sub(r'tags: \n(?: - .*\n)*', '', content)
# Replace header images
if header_image:
content = content.replace('image: /assets/images/posts/your-header-image.jpg', f'image: {header_image}')
if teaser_image:
content = content.replace('teaser: /assets/images/posts/your-teaser-image.jpg', f'teaser: {teaser_image}')
else:
content = content.replace('\n teaser: /assets/images/posts/your-teaser-image.jpg', '')
if caption:
content = content.replace('caption: "Photo credit: [**Source**](https://example.com)"', f'caption: "{caption}"')
else:
content = content.replace('\n caption: "Photo credit: [**Source**](https://example.com)"', '')
else:
# Remove header section if no images
import re
content = re.sub(r'header:\n(?: .*\n)*', '', content)
else:
# Fallback to original front matter creation
front_matter = ["---"]
front_matter.append(f'title: "{title}"')
if excerpt:
front_matter.append(f'excerpt: "{excerpt}"')
front_matter.append(f"category: {category}")
if tags:
front_matter.append("tags:")
for tag in tags:
front_matter.append(f" - {tag}")
if header_image or teaser_image:
front_matter.append("header:")
if header_image:
front_matter.append(f' image: {header_image}')
if teaser_image:
front_matter.append(f' teaser: {teaser_image}')
if caption:
front_matter.append(f' caption: "{caption}"')
front_matter.extend([
"toc: true",
"toc_sticky: true",
"comments: true",
f"date: {today.strftime('%Y-%m-%d')}",
f"last_modified_at: {today.strftime('%Y-%m-%d')}",
"author_profile: true",
"read_time: true",
"share: true",
"related: true",
"---",
"",
"## Introduction",
"",
f"Brief introduction to {title}...",
"",
"## Main Content",
"",
"Your detailed content here...",
"",
"## Conclusion",
"",
"Wrap up your post...",
""
])
content = '\n'.join(front_matter)
# Create _posts directory if it doesn't exist
os.makedirs("_posts", exist_ok=True)
# Write the file
try:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
print(f"\nβ
Post created successfully!")
print(f"π File: {filepath}")
print(f"π URL slug: {slug}")
print(f"\nπ‘ Edit the file to add your content, then commit and push to publish.")
# Ask if user wants to open the file
if sys.platform == "darwin": # macOS
open_file = input("\nπ Open the file now? (Y/n): ").strip().lower()
if open_file != 'n':
os.system(f'open "{filepath}"')
except Exception as e:
print(f"\nβ Error creating post: {e}")
if __name__ == "__main__":
# Change to the script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
try:
create_post()
except KeyboardInterrupt:
print("\n\nπ Goodbye!")
except Exception as e:
print(f"\nβ An error occurred: {e}")