Re issue #15 / make extension configurable #16

Merged
merged 1 commit into from Feb 3, 2012
Jump to file or symbol
Failed to load files and symbols.
+20 −18
Split
View
@@ -13,6 +13,7 @@ Updater::$source_path = '/home/marco/Dropbox/Blog';
Template::$template_dir = '/home/marco/Dropbox/Blog/templates';
Updater::$dest_path = '/home/marco/secondcrack/www';
Updater::$cache_path = '/home/marco/secondcrack/cache';
+Updater::$post_extension = '.md';
// Blog metadata
Post::$blog_title = 'My Blog';
View
@@ -51,7 +51,7 @@ public static function from_files(array $filenames)
public static function next_sequence_number_for_day($filename_prefix)
{
$max = 0;
- foreach (glob($filename_prefix . '*.txt') as $filename) {
+ foreach (glob($filename_prefix . '*' . Updater::$post_extension) as $filename) {
$n = intval(substr(substring_after($filename, $filename_prefix), 0, 2));
if ($n > $max) $max = $n;
}
@@ -116,7 +116,7 @@ public function __construct($source_filename, $is_draft = -1 /* auto */)
$this->month = intval(substr($filename_datestr, 4, 2));
$this->day = intval(substr($filename_datestr, 6, 2));
$this->offset_in_day = intval(substr($filename, 9, 2));
- $this->slug = ltrim(substr($filename, 11, -4), '-');
+ $this->slug = ltrim(substr($filename, 11, -(strlen(Updater::$post_extension))), '-');
} else {
$this->year = intval(idate('Y', $this->timestamp));
$this->month = intval(idate('m', $this->timestamp));
@@ -152,7 +152,7 @@ public function expected_source_filename($for_new_file = false)
$slug = $this->slug;
}
- return $prefix . $padded_offset . '-' . strtolower($slug) . '.txt';
+ return $prefix . $padded_offset . '-' . strtolower($slug) . Updater::$post_extension;
}
public function normalized_source()
View
@@ -8,6 +8,7 @@ class Updater
public static $source_path;
public static $dest_path;
public static $cache_path;
+ public static $post_extension;
public static $frontpage_post_limit = 20;
public static $frontpage_template = 'main.php';
@@ -57,7 +58,7 @@ public static function posts_in_year_month($year, $month, $require_tag = false,
$in_month = false;
$files = array();
foreach ($all_files as $fname => $info) {
- if (substr($fname, -4) != '.txt') continue;
+ if (substr($fname, -(strlen(self::$post_extension))) != self::$post_extension) continue;
// Tag/type filtering
list($ignored_hash, $type, $tags) = explode('|', $info, 3);
@@ -105,7 +106,7 @@ private static function resequence_post_offsets($year, $month, $day)
error_log("Resequencing post offsets for $year-$month-$day");
$prefix = self::$source_path . "/posts/$year/$month/$year$month$day-";
- $files = glob($prefix . '*.txt');
+ $files = glob($prefix . '*' . self::$post_extension);
if (count($files) > 99) {
error_log("Cannot resequence offsets for $year-$month-$day: too many posts (" . count($files) . ')');
return false;
@@ -130,15 +131,15 @@ private static function resequence_post_offsets($year, $month, $day)
foreach ($timestamps as $filename => $timestamp) {
$offset++;
- $slug = preg_replace('/-p[0-9]{1,2}$/ms', '', substr(basename($filename), 12, -4));
+ $slug = preg_replace('/-p[0-9]{1,2}$/ms', '', substr(basename($filename), 12, -(strlen(self::$post_extension))));
if (! isset($slugs[$slug])) $slugs[$slug] = 1;
else $slugs[$slug]++;
$correct_filename =
$prefix .
str_pad($offset, 2, '0', STR_PAD_LEFT) . '-' .
$slug . ($slugs[$slug] < 2 ? '' : '-p' .$slugs[$slug]) .
- '.txt'
+ self::$post_extension
;
if ($filename != $correct_filename) {
error_log("file [$filename] should be [$correct_filename]");
@@ -180,7 +181,7 @@ private static function filelist($dir, $parse_info_in_text_files = true, $use_ca
$tags = '';
$type = '';
- if ($parse_info_in_text_files && substr($fullpath, -4) == '.txt') {
+ if ($parse_info_in_text_files && substr($fullpath, -(strlen(self::$post_extension))) == self::$post_extension) {
$post = new Post($fullpath);
$tags = implode(',', $post->tags);
$type = $post->type;
@@ -237,7 +238,7 @@ public static function most_recent_post_filenames($limit = 0, $require_tag = fal
$posts = array();
foreach ($fileinfo as $filename => $info) {
- if (substr($filename, -4) != '.txt') continue;
+ if (substr($filename, -(strlen(self::$post_extension))) != self::$post_extension) continue;
list($ignored_hash, $type, $tags) = explode('|', $info, 3);
$include = true;
if ($require_type) {
@@ -284,7 +285,7 @@ public static function set_has_posts_for_month($year, $month, $scope = '')
public static function months_with_posts($scope = '')
{
$cache_fname = self::$cache_path . "/months-with-posts-$scope";
- if (! file_exists($cache_fname)) throw new Exception('Cache files not found, expected');
+ if (! file_exists($cache_fname)) throw new Exception('Cache files not found, expected ' . $cache_fname);
return unserialize(file_get_contents($cache_fname));
}
@@ -323,15 +324,15 @@ public static function update_drafts()
}
if (! file_exists($filename)) {
- if (ends_with($filename, '.txt')) {
+ if (ends_with($filename, self::$post_extension)) {
error_log("Deleted draft $filename");
$html_preview_filename = self::$source_path . '/drafts/_previews/' . substring_before(basename($filename), '.', true) . '.html';
if (file_exists($html_preview_filename)) safe_unlink($html_preview_filename);
}
continue;
}
- if (substr($filename, -4) == '.txt') {
+ if (substr($filename, -(strlen(self::$post_extension))) == self::$post_extension) {
$post = new Post($filename, true);
if ($post->publish_now) {
$post = new Post($filename, false);
@@ -378,17 +379,17 @@ public static function update_pages()
self::$changes_were_written = true;
if (! file_exists($filename)) {
- if (ends_with($filename, '.txt')) {
+ if (ends_with($filename, self::$post_extension)) {
error_log("Deleted page $filename");
$dest_filename = self::$dest_path . '/' . substring_before(basename($filename), '.', true);
if (file_exists($dest_filename)) safe_unlink($dest_filename);
}
continue;
}
- if (substr($filename, -4) == '.txt') {
+ if (substr($filename, -(strlen(self::$post_extension))) == self::$post_extension) {
$post = new Post($filename, true);
- $post->slug = basename($filename, '.txt');
+ $post->slug = basename($filename, self::$post_extension);
error_log("Writing page [{$post->slug}]");
$post->write_page(true);
}
@@ -438,7 +439,7 @@ private static function _update($restart_if_resequenced = true)
foreach (self::changed_files_in_directory(self::$source_path . '/posts') as $filename => $info) {
self::$posts_to_be_updated[$filename] = true;
- if (substr($filename, -4) == '.txt') {
+ if (substr($filename, -(strlen(self::$post_extension))) == self::$post_extension) {
list($old_info, $new_info) = $info;
list($old_hash, $old_type, $old_tags) = explode('|', $old_info, 3);
list($new_hash, $new_type, $new_tags) = explode('|', $new_info, 3);
@@ -467,7 +468,7 @@ private static function _update($restart_if_resequenced = true)
$year = substr($filename_datestr, 0, 4);
$month = substr($filename_datestr, 4, 2);
$day = substr($filename_datestr, 6, 2);
- $slug = substr(basename($filename), 12, -4);
+ $slug = substr(basename($filename), 12, -(strlen(self::$post_extension)));
$target_filename = self::$dest_path . "/$year/$month/$day/$slug";
if ($year && $month && $day && $slug && file_exists($target_filename)) {
error_log("Deleting abandoned target file: $target_filename");
@@ -480,7 +481,7 @@ private static function _update($restart_if_resequenced = true)
error_log("Updating post: $filename");
self::$changes_were_written = true;
- if (substr($filename, -4) == '.txt') {
+ if (substr($filename, -(strlen(self::$post_extension))) == self::$post_extension) {
$post = new Post($filename);
$post->write_permalink_page();