Repository navigation
Expand file tree
/
Copy pathnew_post.py
More file actions
71 lines (56 loc) · 1.61 KB
/
Copy pathnew_post.py
File metadata and controls
71 lines (56 loc) · 1.61 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
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "click",
# "rich",
# ]
# ///
"""
Hugo Post Creator for HackWimbledon.
Creates a new Page Bundle with YAML front matter and opens the editor.
"""
import datetime
import re
from pathlib import Path
import click
from rich.console import Console
console = Console()
def get_yaml_template(title: str) -> str:
"""Returns a standard YAML front matter block."""
date_str = datetime.datetime.now().isoformat(timespec="seconds")
return f"""---
title: "{title}"
date: {date_str}
draft: true
---
Write your content here...
"""
@click.command()
@click.argument("title")
def create_post(title: str):
"""
Create a new Hugo post bundle in content/news/ and open the editor.
"""
# Create slug from title
slug = re.sub(
r"-+", "-", re.sub(r"[^\w\s-]", "", title.lower()).replace(" ", "-")
).strip("-")
post_dir = Path(f"content/news/{slug}")
if post_dir.exists():
console.print(
f"[bold red]Error:[/bold red] Directory {post_dir} already exists!"
)
return
# Create the bundle structure
post_dir.mkdir(parents=True)
(post_dir / "images").mkdir()
index_file = post_dir / "index.md"
# Write the initial content
content = get_yaml_template(title)
index_file.write_text(content, encoding="utf-8")
console.print(f"[green]✓[/green] Created post bundle at [bold]{post_dir}[/bold]")
console.print("[blue]Opening editor...[/blue]")
# Opens the file in the system default editor
click.edit(filename=str(index_file))
if __name__ == "__main__":
create_post()