Skip to content

Commit

Permalink
Cleaning up coding style and reacquainting myself with this codebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrowley committed Sep 5, 2009
1 parent fc7fcc4 commit 9899925
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 202 deletions.
9 changes: 4 additions & 5 deletions bin/drafts
@@ -1,11 +1,11 @@
#!/usr/bin/php
<?php

require_once dirname(__FILE__) . '/../include/init.php';
require_once dirname(__FILE__) . "/../include/init.php";
loadlib('dir');

# Get a list of posts and drafts
$tree = dir_rscandir(dirname(__FILE__) . '/../posts', true);
$tree = dir_rscandir(dirname(__FILE__) . "/../posts", true);
$list = end(dir_flatten($tree));

# Narrow the list down to just drafts
Expand All @@ -17,10 +17,9 @@ foreach ($list as $l) {

# Print the list or an "all clear"
if (sizeof($drafts)) {
$posts = realpath(dirname(__FILE__) . '/../posts');
$posts = realpath(dirname(__FILE__) . "/../posts");
foreach ($drafts as $d) { echo "[drafts] $posts$d\n"; }
} else {
echo "[drafts] no drafts found\n";
}
else { err("[drafts] no drafts found\n"); }

exit(0);
27 changes: 14 additions & 13 deletions bin/new
@@ -1,38 +1,39 @@
#!/usr/bin/env php
<?php

require_once dirname(__FILE__) . '/../include/init.php';
loadlib('url');
require_once dirname(__FILE__) . "/../include/init.php";

# Post title in URI form
if (2 != sizeof($argv)) {
echo "Usage: {$argv[0]} <title>\n";
err("Usage: {$argv[0]} <title>\n");
exit(1);
}

loadlib('url');

$title = url_sanitize($argv[1]);
if ('' == $title) {
echo "[new] post title resulted in an empty URI\n";
if ("" == $title) {
err("[new] post title resulted in an empty URI\n");
exit(1);
}
$posts = dirname(__FILE__) . '/../posts';
$posts = dirname(__FILE__) . "/../posts";
if (!is_dir($posts)) { mkdir($posts); }
if (file_exists("$posts/$y/$m/$d/$title")) {
echo "[new] there is already a post by that name today\n";
err("[new] there is already a post by that name today\n");
exit(1);
}

# Date directories
$y = date('Y');
$m = date('m');
$d = date('d');
$y = date("Y");
$m = date("m");
$d = date("d");
if (!is_dir("$posts/$y")) { mkdir("$posts/$y", 0755); }
if (!is_dir("$posts/$y/$m")) { mkdir("$posts/$y/$m", 0755); }
if (!is_dir("$posts/$y/$m/$d")) { mkdir("$posts/$y/$m/$d", 0755); }
file_put_contents("$posts/$y/$m/$d/$title",
"{$argv[1]}\n\n[tags]\n\n<p>[body]</p>");

# Great success!
echo "[new] post has been created and can be edited in:\n[new] ",
realpath("$posts/$y/$m/$d/$title"), "\n";

err("[new] post has been created and can be edited in:\n[new] ",
realpath("$posts/$y/$m/$d/$title"), "\n");
exit(0);
21 changes: 11 additions & 10 deletions bin/preview
Expand Up @@ -6,21 +6,22 @@
# TODO: Make sure it's a draft
#

require_once dirname(__FILE__) . "/../include/init.php";

if (2 != sizeof($argv)) {
echo "Usage: {$argv[0]} <post>\n";
err("Usage: {$argv[0]} <post>\n");
exit(1);
}
if (!file_exists($argv[1])) {
echo "[publish] can't find a post at {$argv[1]}\n";
err("[publish] can't find a post at {$argv[1]}\n");
exit(1);
}
$base = realpath(dirname(__FILE__) . '/../posts');
$base = realpath(dirname(__FILE__) . "/../posts");
if ($base != substr(realpath($argv[1]), 0, strlen($base))) {
echo "[publish] can only publish from the posts/ directory\n";
err("[publish] can only publish from the posts/ directory\n");
exit(1);
}

require_once dirname(__FILE__) . '/../include/init.php';
loadlib('blog_publish');


Expand All @@ -30,8 +31,8 @@ loadlib('blog_publish');
#

# Get the path to the post
$parts = explode('/', str_replace(
realpath(dirname(__FILE__) . '/../posts') . '/', '', realpath($argv[1])));
$parts = explode("/", str_replace(
realpath(dirname(__FILE__) . "/../posts") . "/", "", realpath($argv[1])));
$file = array_pop($parts);
$path = blog_publish_dir("{$smarty->template_dir}/.posts", $parts);

Expand All @@ -42,12 +43,12 @@ $tags = preg_split('!\s+!', $tags);
$body = input_footnoterize($body, end($parts));
blog_publish_smarty($path, "$file.preview", time(), $title, $tags, $body,
false);
echo "[publish] created a preview of the post's Smarty template\n";
err("[publish] created a preview of the post's Smarty template\n");



# Great success!
$hash = sha1_file($argv[1]);
echo "[publish] previewed the post at\n",
"[publish] http://$FQDN$path/$file/$hash\n";
err("[publish] previewed the post at\n",
"[publish] http://$FQDN$path/$file/$hash\n");
exit(0);
57 changes: 30 additions & 27 deletions bin/publish
@@ -1,25 +1,27 @@
#!/usr/bin/env php
<?php

require_once dirname(__FILE__) . '/../include/init.php';
require_once dirname(__FILE__) . "/../include/init.php";

if (3 == sizeof($argv) && '-c' == $argv[1]) {
if (3 == sizeof($argv) && "-c" == $argv[1]) {
$comments = false;
$arg = $argv[2];
} else if (2 == sizeof($argv)) {
}
else if (2 == sizeof($argv)) {
$comments = $COMMENTS;
$arg = $argv[1];
} else {
echo "Usage: {$argv[0]} [-c] <post>\n";
}
else {
err("Usage: {$argv[0]} [-c] <post>\n");
exit(1);
}
if (!file_exists($arg)) {
echo "[publish] can't find a post at $arg\n";
err("[publish] can't find a post at $arg\n");
exit(1);
}
$base = realpath(dirname(__FILE__) . '/../posts');
$base = realpath(dirname(__FILE__) . "/../posts");
if ($base != substr(realpath($arg), 0, strlen($base))) {
echo "[publish] can only publish from the posts/ directory\n";
err("[publish] can only publish from the posts/ directory\n");
exit(1);
}

Expand All @@ -28,20 +30,20 @@ loadlib('input');

# Check if this post has already been published
# Don't allow republishing, instead, send them to the bin/republish script
$path = str_replace(realpath(dirname(__FILE__) . '/../posts'), '',
$path = str_replace(realpath(dirname(__FILE__) . "/../posts"), "",
realpath($arg));
if (file_exists("{$smarty->template_dir}/.posts$path.html")) {
echo "[publish] can't republish $arg\n[publish] try bin/republish\n";
err("[publish] can't republish $arg\n[publish] try bin/republish\n");
exit(1);
}

# Move this post up to today if necessary
$parts = explode('/', str_replace(
realpath(dirname(__FILE__) . '/../posts') . '/', '', realpath($arg)));
$parts = explode("/", str_replace(
realpath(dirname(__FILE__) . "/../posts") . "/", "", realpath($arg)));
$file = array_pop($parts);
if (date('Y-m-d') != implode('-', $parts)) {
$path = blog_publish_dir(dirname(__FILE__) . '/../posts',
array(date('Y'), date('m'), date('d')));
if (date('Y-m-d') != implode("-", $parts)) {
$path = blog_publish_dir(dirname(__FILE__) . "/../posts",
array(date("Y"), date("m"), date("d")));
rename($argv[1], dirname(__FILE__) . "/../posts$path/$file");

# Delete empty directories in the date hierarchy to keep things tidy
Expand All @@ -54,8 +56,8 @@ if (date('Y-m-d') != implode('-', $parts)) {
}

# Create directories for the <year>/<month>/<day>
$parts = explode('/', str_replace(
realpath(dirname(__FILE__) . '/../posts') . '/', '', realpath($arg)));
$parts = explode("/", str_replace(
realpath(dirname(__FILE__) . "/../posts") . "/", "", realpath($arg)));
$file = array_pop($parts);
$path = blog_publish_dir("{$smarty->template_dir}/.posts", $parts);

Expand Down Expand Up @@ -90,36 +92,37 @@ $tags = preg_split('!\s+!', $tags);
$body = input_footnoterize($body, end($parts));
blog_publish_smarty($path, $file, $timestamp, $title, $tags, $body,
$comments);
echo "[publish] created the post's Smarty template\n";
err("[publish] created the post's Smarty template\n");

# Update the homepage
prepend('/.index', 5);
echo "[publish] updated the homepage\n";
prepend("/.index", 5);
err("[publish] updated the homepage\n");

# Update the Atom feed
blog_publish_feed($path, $file, $timestamp, $title, $body, false);
echo "[publish] updated the Atom feed\n";
err("[publish] updated the Atom feed\n");

# Prepend to archive pages
$archive = '/.posts';
$archive = "/.posts";
foreach ($parts as $p) {
$archive .= "/$p";
prepend($archive);
}
echo "[publish] added this post to the archives\n";
err("[publish] added this post to the archives\n");

# Prepend to tag pages
if (!is_dir("{$smarty->template_dir}/.tags")) {
mkdir("{$smarty->template_dir}/.tags", 0755);
}
foreach ($tags as $t) { prepend("/.tags/$t"); }
echo "[publish] added this post to the tag pages\n";
err("[publish] added this post to the tag pages\n");

function prepend($tpl, $count = 0) {
$tpl = "{$GLOBALS['smarty']->template_dir}$tpl.html";
if (file_exists($tpl)) {
$lines = explode("\n", file_get_contents($tpl));
} else { $lines = array(); }
}
else { $lines = array(); }
global $path, $file;
array_unshift($lines, "{include file=\".posts$path/$file.html\"}");
if (0 < $count) { $lines = array_slice($lines, 0, $count); }
Expand All @@ -129,10 +132,10 @@ function prepend($tpl, $count = 0) {
# Update the archive indices
blog_publish_months();
blog_publish_tags($tags);
echo "[publish] updated archive pages\n";
err("[publish] updated archive pages\n");



# Great success!
echo "[publish] published the post at\n[publish] http://$FQDN$path/$file\n";
err("[publish] published the post at\n[publish] http://$FQDN$path/$file\n");
exit(0);
34 changes: 18 additions & 16 deletions bin/republish
@@ -1,25 +1,27 @@
#!/usr/bin/php
<?php

require_once dirname(__FILE__) . '/../include/init.php';
require_once dirname(__FILE__) . "/../include/init.php";

if (3 == sizeof($argv) && '-c' == $argv[1]) {
if (3 == sizeof($argv) && "-c" == $argv[1]) {
$comments = false;
$arg = $argv[2];
} else if (2 == sizeof($argv)) {
}
else if (2 == sizeof($argv)) {
$comments = $COMMENTS;
$arg = $argv[1];
} else {
echo "Usage: {$argv[0]} [-c] <post>\n";
}
else {
err("Usage: {$argv[0]} [-c] <post>\n");
exit(1);
}
if (!file_exists($arg)) {
echo "[republish] can't find a post at $arg\n";
err("[republish] can't find a post at $arg\n");
exit(1);
}
$base = realpath(dirname(__FILE__) . '/../posts');
$base = realpath(dirname(__FILE__) . "/../posts");
if ($base != substr(realpath($arg), 0, strlen($base))) {
echo "[republish] can only publish from the posts/ directory\n";
err("[republish] can only publish from the posts/ directory\n");
exit(1);
}

Expand All @@ -32,34 +34,34 @@ loadlib('blog_publish');
#

# Get the path to the post
$parts = explode('/', str_replace(
realpath(dirname(__FILE__) . '/../posts') . '/', '', realpath($arg)));
$parts = explode("/", str_replace(
realpath(dirname(__FILE__) . "/../posts") . "/", "", realpath($arg)));
$file = array_pop($parts);
$path = '/' . implode('/', $parts);
$path = "/" . implode("/", $parts);

# Get the date stored at original publishing time
$timestamp = strtotime(file_get_contents(
"{$smarty->template_dir}/.posts$path/$file.date"));

# Rewrite the post's Smarty template
list($title, $tags, $body) = preg_split("!\r?\n\r?\n!",
list($title, $tags, $body) = preg_split('!\r?\n\r?\n!',
file_get_contents($arg), 3);
$tags = preg_split('!\s+!', $tags);
$body = input_footnoterize($body, end($parts));
blog_publish_smarty($path, $file, $timestamp, $title, $tags, $body,
$comments);
echo "[republish] recreated the post's Smarty template\n";
err("[republish] recreated the post's Smarty template\n");

# Rewrite the post in the feed, if it's there
blog_publish_feed($path, $file, $timestamp, $title, $body, true);
echo "[republish] updated the Atom feed\n";
err("[republish] updated the Atom feed\n");

# TODO: Update the tag pages
#echo "[republish] updated archive pages\n";



# Great success!
echo "[republish] republished the post at\n",
"[republish] http://$FQDN$path/$file\n";
err("[republish] republished the post at\n",
"[republish] http://$FQDN$path/$file\n");
exit(0);

0 comments on commit 9899925

Please sign in to comment.