-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
60 lines (47 loc) · 1.52 KB
/
Copy pathcli.py
File metadata and controls
60 lines (47 loc) · 1.52 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
from datetime import datetime
from pathlib import Path
import click
import re
post_folder = Path("./_posts")
def slugify(title: str) -> str:
title = title.lower()
title = re.sub(r'[^\w\s-]', '', title)
title = re.sub(r'[-\s]+', '-', title).strip('-_')
return title
@click.group()
def cli():
"""Blog post management CLI."""
pass
@cli.command()
@click.argument('title')
def create(title):
today = datetime.today()
date_str = today.strftime('%Y-%m-%d')
year_str = today.strftime('%Y')
slug = slugify(title)
directory = post_folder / year_str
directory.mkdir(parents=True, exist_ok=True)
filename = f'{date_str}-{slug}.md'
filepath = directory / filename
if filepath.exists():
click.echo(f"⚠️ Post already exists: {filepath}")
if not click.confirm("Do you want to overwrite it?"):
click.echo("❌ Aborted.")
return
with open(filepath, 'w') as f:
# with open(post_folder / "template.txt", "r") as templ:
# f.write(templ.read())
f.write("\n".join([
"---",
"author: owner",
f'title: "{title}"',
f"date: {date_str}",
"categories: [ ]",
"tags: [ ] # TAG n ames should always be lowercase",
"description: # add some description here",
"math: true # optional",
"--- ",
]))
click.echo(f"✅ Post created: {filepath}")
if __name__ == '__main__':
cli()