summaryrefslogtreecommitdiff
path: root/scripts/new.php
blob: b746888c5eb986e863bb01e48555ea8e2e1e3c29 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env php
<?php
if (empty($argv[1])) {
	exit;
}

const exceptions = [
	"vs", "above", "across", "against", "at", "between", "by", "along", "among", "down", "in",
	"once", "around", "of", "off", "on", "to", "with", "before", "behind", "below", "beneath",
	"down", "from", "near", "toward", "upon", "within", "a", "an", "the", "and", "but", "for",
	"or", "nor", "so", "till", "when", "yet", "that", "than", "in", "into", "like", "onto", "over",
	"past", "as", "if"
];

// Gather arguments and join into title-cased string.
$title_words = array_slice($argv, 1);
$title_words = array_map(
	fn (string $word) => in_array($word, exceptions) ? lcfirst($word) : ucfirst($word),
	$title_words
);
$title_words[0] = ucfirst($title_words[0]);
end($title_words);
$key = key($title_words);
$title_words[$key] = ucfirst($title_words[$key]);

// 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.md";

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);