102 lines
2.2 KiB
PHP
Executable File
102 lines
2.2 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
/**
|
|
* Add a new blog post from a markdown file
|
|
* Usage: php add-blog-post.php post.md
|
|
*
|
|
* Markdown format:
|
|
* ---
|
|
* title: Your Post Title
|
|
* excerpt: Brief excerpt for homepage
|
|
* ---
|
|
*
|
|
* Your full blog post content here...
|
|
*
|
|
* Use double newlines for paragraphs.
|
|
*/
|
|
|
|
$blogFile = __DIR__ . '/../data/posts.json';
|
|
|
|
if ($argc < 2) {
|
|
echo "Usage: php add-blog-post.php post.md\n";
|
|
exit(1);
|
|
}
|
|
|
|
$markdownFile = $argv[1];
|
|
|
|
if (!file_exists($markdownFile)) {
|
|
echo "Error: File '$markdownFile' not found\n";
|
|
exit(1);
|
|
}
|
|
|
|
$content = file_get_contents($markdownFile);
|
|
|
|
// Parse frontmatter
|
|
if (!preg_match('/^---\s*\n(.*?)\n---\s*\n(.*)$/s', $content, $matches)) {
|
|
echo "Error: Invalid markdown format. Expected frontmatter with --- delimiters.\n";
|
|
echo "Format:\n---\ntitle: Your Title\nexcerpt: Your excerpt\n---\n\nContent...\n";
|
|
exit(1);
|
|
}
|
|
|
|
$frontmatter = $matches[1];
|
|
$postContent = trim($matches[2]);
|
|
|
|
// Parse frontmatter fields
|
|
$title = null;
|
|
$excerpt = null;
|
|
|
|
foreach (explode("\n", $frontmatter) as $line) {
|
|
if (preg_match('/^title:\s*(.+)$/i', trim($line), $m)) {
|
|
$title = trim($m[1]);
|
|
} elseif (preg_match('/^excerpt:\s*(.+)$/i', trim($line), $m)) {
|
|
$excerpt = trim($m[1]);
|
|
}
|
|
}
|
|
|
|
if (!$title || !$excerpt) {
|
|
echo "Error: Missing required fields. Need 'title:' and 'excerpt:' in frontmatter.\n";
|
|
exit(1);
|
|
}
|
|
|
|
if (empty($postContent)) {
|
|
echo "Error: Post content is required\n";
|
|
exit(1);
|
|
}
|
|
|
|
// Load existing posts
|
|
$posts = [];
|
|
if (file_exists($blogFile)) {
|
|
$json = file_get_contents($blogFile);
|
|
$posts = json_decode($json, true) ?: [];
|
|
}
|
|
|
|
// Get next ID
|
|
$nextId = 0;
|
|
if (!empty($posts)) {
|
|
$ids = array_column($posts, 'id');
|
|
$nextId = max($ids) + 1;
|
|
}
|
|
|
|
// Get today's date
|
|
$date = date('Y-m-d');
|
|
|
|
// Create new post
|
|
$newPost = [
|
|
'id' => $nextId,
|
|
'title' => $title,
|
|
'date' => $date,
|
|
'excerpt' => $excerpt,
|
|
'content' => $postContent
|
|
];
|
|
|
|
// Add to array
|
|
$posts[] = $newPost;
|
|
|
|
// Write back to file
|
|
file_put_contents($blogFile, json_encode($posts, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n");
|
|
|
|
echo "Blog post added successfully!\n";
|
|
echo "ID: $nextId\n";
|
|
echo "Date: $date\n";
|
|
echo "Title: $title\n";
|