/
publish
executable file
·141 lines (116 loc) · 4.09 KB
/
publish
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env php
<?php
require_once dirname(__FILE__) . "/../include/init.php";
if (3 == sizeof($argv) && "-c" == $argv[1]) {
$comments = false;
$arg = $argv[2];
}
else if (2 == sizeof($argv)) {
$comments = $COMMENTS;
$arg = $argv[1];
}
else {
err("Usage: {$argv[0]} [-c] <post>\n");
exit(1);
}
if (!file_exists($arg)) {
err("[publish] can't find a post at $arg\n");
exit(1);
}
$base = realpath(dirname(__FILE__) . "/../posts");
if ($base != substr(realpath($arg), 0, strlen($base))) {
err("[publish] can only publish from the posts/ directory\n");
exit(1);
}
loadlib('blog_publish');
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"), "",
realpath($arg));
if (file_exists("{$smarty->template_dir}/.posts$path.html")) {
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)));
$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")));
rename($argv[1], dirname(__FILE__) . "/../posts$path/$file");
# Delete empty directories in the date hierarchy to keep things tidy
$ii = sizeof($parts);
for ($i = 0; $i < $ii; ++$i) {
if (!@rmdir($arg = dirname($arg))) { break; }
}
$arg = dirname(__FILE__) . "/../posts/$path/$file";
}
# Create directories for the <year>/<month>/<day>
$parts = explode("/", str_replace(
realpath(dirname(__FILE__) . "/../posts") . "/", "", realpath($arg)));
$file = array_pop($parts);
$path = blog_publish_dir("{$smarty->template_dir}/.posts", $parts);
# Create newer/older pointers in the directory tree
blog_publish_pointers($parts);
# Touch the post's comments files
touch("{$smarty->template_dir}/.posts$path/$file.comments.html");
chmod("{$smarty->template_dir}/.posts$path/$file.comments.html", 0666);
touch("{$smarty->template_dir}/.posts$path/$file.count");
chmod("{$smarty->template_dir}/.posts$path/$file.count", 0666);
echo "[publish] touched the post's comments files\n";
# Save the post's publishing detailed date
$timestamp = time();
file_put_contents("{$smarty->template_dir}/.posts$path/$file.date",
date('r', $timestamp), LOCK_EX);
# Delete the post's preview
@unlink("{$smarty->template_dir}/.posts$path/$file.preview.html");
#
# Write the post in all its various forms
#
# Write the post's Smarty template
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);
err("[publish] created the post's Smarty template\n");
# Update the homepage
prepend("/.index", 5);
err("[publish] updated the homepage\n");
# Update the Atom feed
blog_publish_feed($path, $file, $timestamp, $title, $body, false);
err("[publish] updated the Atom feed\n");
# Prepend to archive pages
$archive = "/.posts";
foreach ($parts as $p) {
$archive .= "/$p";
prepend($archive);
}
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"); }
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(); }
global $path, $file;
array_unshift($lines, "{include file=\".posts$path/$file.html\"}");
if (0 < $count) { $lines = array_slice($lines, 0, $count); }
file_put_contents($tpl, implode("\n", $lines), LOCK_EX);
}
# Update the archive indices
blog_publish_months();
blog_publish_tags($tags);
err("[publish] updated archive pages\n");
# Great success!
err("[publish] published the post at\n[publish] http://$FQDN$path/$file\n");
exit(0);