-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnew_post
More file actions
executable file
·86 lines (77 loc) · 2.41 KB
/
new_post
File metadata and controls
executable file
·86 lines (77 loc) · 2.41 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
#!/usr/bin/env perl
use v5.36;
use open ':std', ':encoding(UTF-8)';
use Sq -sig => 1;
use Getopt::Long::Descriptive;
use Path::Tiny;
use Time::localtime;
# TODO: Correct handling with folders (when folders don't exists?)
my ($opt, $usage) = describe_options(
'Usage: %c %o',
['title|t=s', 'title of file to create', {required => 1 }],
['tags=s', 'comma separated tags', {default => ""}],
['help|h', 'Print this message', {shortcircuit => 1 }],
);
$usage->die if $opt->help;
# generate the filename (including path) that should be created
my ($file, $slug) = assign {
my $time = localtime();
my $date = sprintf "%04d-%02d-%02d",
($time->year + 1900),
($time->mon + 1),
$time->mday;
my $title = Str->trim($opt->title);
# file always only lower case
$title = lc $title;
# only keep alphanumeric, whitespace and -
$title = Str->keep($title, qr/(?: [a-zA-Z0-9] | \s | - ) /x);
# replace multiple ws with -
$title =~ s/\s+/-/g;
my $name = sprintf "%s-%s.md", $date, $title;
path(content => posts => $name), $title;
};
# Abort when destination file exists
die(sprintf("Error: file '%s' already exists. Use another title to fix.\n", $file))
if $file->exists;
# write template output to file
Sq->fs->write_text($file, template({
title => $opt->title,
slug => $slug,
tags => [split /,/, $opt->tags],
}));
printf "Created: '%s'\n", $file;
# Function to generate template output
sub template($arg) {
state $input = type [hash =>
[keys =>
title => ['str'],
slug => ['str'],
date => [maybe => [match => qr/\d\d\d\d-\d\d-\d\d T \d\d:\d\d:\d\d/x]],
tags => [maybe => [array => [of => ['str']]]],
],
];
Sq::Type::t_assert($input, $arg);
# Add blessings
sq($arg);
my $date = assign {
return $arg->{date} if defined $arg->{date};
my $time = localtime();
my $date = sprintf("%04d-%02d-%02dT00:00:00",
($time->year + 1900),
($time->mon + 1),
$time->mday
);
return $date;
};
return [
'---',
'layout: post',
'title: "' . Str->escape_html($arg->{title}) . '"',
'slug: ' . $arg->{slug},
'date: ' . $date,
'lastmod: ' . $date,
'tags: [' . $arg->{tags}->join(",") . ']',
'description:',
'---',
];
}