summaryrefslogtreecommitdiff
path: root/scripts/new.php
diff options
context:
space:
mode:
author53hornet <atc@53hor.net>2021-12-16 22:08:00 -0500
committer53hornet <atc@53hor.net>2021-12-16 22:08:00 -0500
commitdd04ad281d9123296be1a904cf647f8e7a232d3f (patch)
treef00d56f908dcd33ddf29c8a31bba8e831496fac9 /scripts/new.php
parentc3b77872285c05ed42359434d5c306304b8dfaeb (diff)
download53hor-dd04ad281d9123296be1a904cf647f8e7a232d3f.tar.xz
53hor-dd04ad281d9123296be1a904cf647f8e7a232d3f.zip
feat: rescue post, colors, centered titles
Diffstat (limited to 'scripts/new.php')
-rwxr-xr-xscripts/new.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/scripts/new.php b/scripts/new.php
new file mode 100755
index 0000000..f272a8a
--- /dev/null
+++ b/scripts/new.php
@@ -0,0 +1,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.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 post %s\n", $filename);