Take advantage of having changed commentdate().
[ikiwiki] / ikiwiki-comment.in
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use lib '.'; # For use in nonstandard directory, munged by Makefile.
5 use IkiWiki;
6 use IkiWiki::Plugin::comments;
7 use Getopt::Long;
8
9 sub usage {
10         die gettext("usage: ikiwiki-comment pagefile [options]") . "\n";
11 }
12
13 sub main {
14         my $pagefile=shift || usage();
15         my $interactive = -t STDIN;
16         my $content;
17         my ($format, $username, $subject, $date);
18         GetOptions(
19                 'format:s'      => \$format,
20                 'username:s'    => \$username,
21                 'subject:s'     => \$subject,
22                 'date:s'        => \$date,
23         ) || usage();
24
25         my $dir=get_dir($pagefile);
26         my $page=get_page($pagefile);
27
28         IkiWiki::Plugin::comments::checkconfig();
29
30         if ($interactive) {
31                 $format         ||= 'mdwn';
32                 $username       ||= get_username();
33                 $subject        ||= get_subject($page, $dir);
34                 $date           ||= IkiWiki::Plugin::comments::commentdate();
35         } else {
36                 $format         ||= undef;
37                 die "must supply username" unless defined $username;
38                 $subject        ||= get_subject($page, $dir);
39                 die "must supply date" unless defined $date;
40                 chomp($content = join('', <STDIN>));
41         }
42
43         my $comment=get_comment($format, $username, $subject, $date, $content);
44
45         # For interactive use, this will yield a hash of the comment before
46         # it's edited, but that's ok; the date provides sufficient entropy
47         # to avoid collisions, and the hash of a comment does not need to
48         # match its actual content.
49         # Doing it this way avoids needing to move the file to a final
50         # location after it's edited.
51         my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
52
53         IkiWiki::writefile($location, $dir, $comment);
54         exec_editor("$dir/$location") if $interactive;
55 }
56
57 sub get_dir {
58         my ($file) = @_;
59         my $dir=IkiWiki::dirname($file);
60         $dir="." unless length $dir;
61         return $dir;
62 }
63
64 sub get_page {
65         my ($file) = @_;
66         my $page=IkiWiki::basename($file);
67         $page=~s/\.[^.]+$// unless -d $file;
68         return $page;
69 }
70
71 sub get_username {
72         my $username = getpwuid($<);
73         $username="" unless defined $username;
74         return $username;
75 }
76
77 sub get_subject {
78         my ($page, $dir) = @_;
79         my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
80         return "comment $comment_num";
81 }
82
83 sub get_comment {
84         my ($format, $username, $subject, $date, $content) = @_;
85         $format = defined $format ? $format = " format=$format" : q{};
86         $content = '' unless defined $content;
87         my $comment="[[!comment$format\n";
88         $comment.=" username=\"$username\"\n";
89         $comment.=" subject=\"\"\"$subject\"\"\"\n";
90         $comment.=" date=\"$date\"\n";
91         $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
92         return $comment;
93 }
94
95 sub exec_editor {
96         my ($file) = @_;
97
98         my @editor="vi";
99         if (-x "/usr/bin/editor") {
100                 @editor="/usr/bin/editor";
101         }
102         if (exists $ENV{EDITOR}) {
103                 @editor=split(' ', $ENV{EDITOR});
104         }
105         if (exists $ENV{VISUAL}) {
106         @editor=split(' ', $ENV{VISUAL});
107         }
108         exec(@editor, $file);
109 }
110
111 main(@ARGV);