forked from mmistakes/minimal-mistakes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft_to_post.py
More file actions
86 lines (69 loc) · 2.53 KB
/
Copy pathdraft_to_post.py
File metadata and controls
86 lines (69 loc) · 2.53 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
#!/usr/bin/env python3
"""
Draft to Post Converter
Copies the draft template to create a new post with today's date.
Usage: python3 draft_to_post.py "Post Title"
"""
import os
import sys
from datetime import datetime
import re
def slugify(text):
"""Convert text to URL-friendly slug"""
slug = text.lower().strip()
slug = re.sub(r'[^a-z0-9\s-]', '', slug)
slug = re.sub(r'[\s-]+', '-', slug)
slug = slug.strip('-')
return slug
def copy_draft_template(title):
"""Copy the draft template to create a new post"""
# Load template
template_path = "_drafts/post-template.md"
if not os.path.exists(template_path):
print(f"❌ Template not found: {template_path}")
return False
try:
with open(template_path, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
print(f"❌ Error reading template: {e}")
return False
# 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"❌ File {filename} already exists!")
return False
# Update template with current date and title
content = content.replace('title: "Your Post Title Here"', f'title: "{title}"')
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")}')
# 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"✅ Post created from template: {filepath}")
print(f"🌐 URL slug: {slug}")
print(f"📝 Edit the file to customize categories, tags, and content.")
return True
except Exception as e:
print(f"❌ Error creating post: {e}")
return False
def main():
if len(sys.argv) < 2:
print("Usage: python3 draft_to_post.py \"Post Title\"")
print("Example: python3 draft_to_post.py \"My Awesome New Post\"")
sys.exit(1)
title = sys.argv[1]
# Change to script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
copy_draft_template(title)
if __name__ == "__main__":
main()