51 lines
1.0 KiB
Plaintext
51 lines
1.0 KiB
Plaintext
|
#!/usr/bin/env perl
|
||
|
|
||
|
# Creates a new website post, appends its link to the index.
|
||
|
|
||
|
use v5.14;
|
||
|
use autodie;
|
||
|
use warnings;
|
||
|
use strict;
|
||
|
|
||
|
use Tie::File;
|
||
|
use Time::Piece;
|
||
|
|
||
|
# '/' is both prepended, and appended.
|
||
|
my $post_url_dir = 'posts';
|
||
|
my $posts_dir = "./src/$post_url_dir";
|
||
|
my $index_file = './src/index.md';
|
||
|
|
||
|
my $post_title;
|
||
|
my $post_filename;
|
||
|
|
||
|
print "Post title: ";
|
||
|
$post_title = <>;
|
||
|
|
||
|
print "Post file name, without extension: ";
|
||
|
$post_filename = <>;
|
||
|
|
||
|
chomp $post_title;
|
||
|
chomp $post_filename;
|
||
|
|
||
|
die "File already exists" if -e "$posts_dir/$post_filename.md";
|
||
|
|
||
|
# Create a post file and insert the title.
|
||
|
open (my $fh, '>', "$posts_dir/$post_filename.md");
|
||
|
say $fh "# $post_title";
|
||
|
close $fh;
|
||
|
|
||
|
# Append a post URL to the index.
|
||
|
tie my @index_lines, 'Tie::File', $index_file;
|
||
|
my $posts_found = 0;
|
||
|
for (@index_lines) {
|
||
|
if ($posts_found) {
|
||
|
$_ .= "\n* [$post_title](/$post_url_dir/$post_filename.html)";
|
||
|
last;
|
||
|
}
|
||
|
$posts_found = 1 if ($_ =~ /and such:/i);
|
||
|
}
|
||
|
untie @index_lines;
|
||
|
|
||
|
my @cmd = ($ENV{EDITOR}, "$posts_dir/$post_filename.md");
|
||
|
system @cmd;
|