forked from mmistakes/minimal-mistakes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_post.py
More file actions
137 lines (114 loc) · 4.42 KB
/
quick_post.py
File metadata and controls
137 lines (114 loc) · 4.42 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
#!/usr/bin/env python3
"""
Quick Jekyll Post Generator
Creates a new blog post with minimal prompts - perfect for quick posts.
Usage: python3 quick_post.py "Post Title" [category] [tags]
"""
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 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_quick_post(title, category="Software", tags_str=""):
"""Create a post with minimal front matter"""
# 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
# Parse tags
tags = []
if tags_str:
tags = [tag.strip() for tag in tags_str.split(',') if tag.strip()]
# Load template and customize it for quick post
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")}')
# Remove excerpt for quick posts
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)
# Remove header section for quick posts
import re
content = re.sub(r'header:\n(?: .*\n)*', '', content)
# Simplify content for quick posts
content = re.sub(r'## Introduction.*?## Main Content', f'## {title}\n\nYour content goes here...\n\n## Main Content', content, flags=re.DOTALL)
else:
# Fallback to simple front matter creation
front_matter = [
"---",
f'title: "{title}"',
f"category: {category}",
]
if tags:
front_matter.append("tags:")
for tag in tags:
front_matter.append(f" - {tag}")
front_matter.extend([
"comments: true",
f"date: {today.strftime('%Y-%m-%d')}",
"---",
"",
f"## {title}",
"",
"Your content goes here...",
""
])
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"✅ Post created: {filepath}")
return True
except Exception as e:
print(f"❌ Error creating post: {e}")
return False
def main():
if len(sys.argv) < 2:
print("Usage: python3 quick_post.py \"Post Title\" [category] [tags]")
print("Example: python3 quick_post.py \"My New Post\" \"Tutorial\" \"Docker,Kubernetes\"")
sys.exit(1)
title = sys.argv[1]
category = sys.argv[2] if len(sys.argv) > 2 else "Software"
tags = sys.argv[3] if len(sys.argv) > 3 else ""
# Change to script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
create_quick_post(title, category, tags)
if __name__ == "__main__":
main()