summaryrefslogtreecommitdiff
path: root/scripts/new.php
blob: 4f03df369ea2bc7cbb054b1b056221867c077b19 (plain) (blame)
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
#!/usr/bin/env php
<?php
if (empty($argv[1])) {
	exit;
}

// Gather arguments and join into title-cased string.
$title_words = array_map(
	fn ($w): string => trim($w, ".:;()[]{}'\"<>,/?"),
	array_slice($argv, 1)
);

// Generate HTML title.
$title = implode(' ', $title_words);
$title = "<h1>$title</h1>\n\n";

// Generate post filename/URL.
$filename = strtolower(implode('-', $title_words));
$date = date('Y-m-d');
$filename = "$date-$filename.php";

if (!$handle = fopen("./drafts/$filename", 'a')) {
	fwrite(STDERR, "Cannot append to file: $filename\n");
	exit;
}

if (fwrite($handle, $title) === false) {
	fwrite(STDERR, "Cannot write contents to file: $filename\n");
	exit;
}

printf("Created draft %s\n", $filename);