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
|
#!/usr/bin/perl
no lib '.';
use warnings;
use strict;
use FindBin; use lib $FindBin::Bin; # For use in nonstandard directory, munged by Makefile.
use IkiWiki;
use IkiWiki::Plugin::comments;
use Getopt::Long;
sub usage {
die gettext("usage: ikiwiki-comment pagefile [options]") . "\n";
}
sub main {
my $pagefile=shift || usage();
my $interactive = -t STDIN;
my $content;
my ($format, $username, $subject, $date, $url, $email, $ip);
GetOptions(
'format:s' => \$format,
'username:s' => \$username,
'subject:s' => \$subject,
'date:s' => \$date,
'url:s' => \$url,
'email:s' => \$email,
'ip:s' => \$ip,
) || usage();
my $dir=get_dir($pagefile);
my $page=get_page($pagefile);
IkiWiki::Plugin::comments::checkconfig();
if ($interactive) {
$format ||= 'mdwn';
$username ||= get_username();
$subject ||= get_subject($page, $dir);
$date ||= IkiWiki::Plugin::comments::commentdate();
$url ||= undef;
$email ||= undef;
$ip ||= undef;
} else {
$format ||= undef;
die "must supply username" unless defined $username;
$subject ||= get_subject($page, $dir);
die "must supply date" unless defined $date;
$url ||= undef;
$email ||= undef;
$ip ||= undef;
chomp($content = join('', <STDIN>));
}
my $comment=get_comment($format, $username, $subject, $date, $url, $email, $ip, $content);
# For interactive use, this will yield a hash of the comment before
# it's edited, but that's ok; the date provides sufficient entropy
# to avoid collisions, and the hash of a comment does not need to
# match its actual content.
# Doing it this way avoids needing to move the file to a final
# location after it's edited.
my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
IkiWiki::writefile($location, $dir, $comment);
exec_editor("$dir/$location") if $interactive;
}
sub get_dir {
my ($file) = @_;
my $dir=IkiWiki::dirname($file);
$dir="." unless length $dir;
return $dir;
}
sub get_page {
my ($file) = @_;
my $page=IkiWiki::basename($file);
$page=~s/\.[^.]+$// unless -d $file;
return $page;
}
sub get_username {
my $username = getpwuid($<);
$username="" unless defined $username;
return $username;
}
sub get_subject {
my ($page, $dir) = @_;
my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
return "comment $comment_num";
}
sub get_comment {
my ($format, $username, $subject, $date, $url, $email, $ip, $content) = @_;
$format = defined $format ? $format = " format=$format" : q{};
$content = '' unless defined $content;
my $comment="[[!comment$format\n";
$comment.=" username=\"$username\"\n";
$comment.=" subject=\"\"\"$subject\"\"\"\n";
$comment.=" date=\"$date\"\n";
$comment.=" url=\"$url\"\n" if defined $url;
$comment.=" email=\"$email\"\n" if defined $email;
$comment.=" ip=\"$ip\"\n" if defined $ip;
$comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
return $comment;
}
sub exec_editor {
my ($file) = @_;
my @editor="vi";
if (-x "/usr/bin/editor") {
@editor="/usr/bin/editor";
}
if (exists $ENV{EDITOR}) {
@editor=split(' ', $ENV{EDITOR});
}
if (exists $ENV{VISUAL}) {
@editor=split(' ', $ENV{VISUAL});
}
exec(@editor, $file);
}
main(@ARGV);
|