aboutsummaryrefslogtreecommitdiff
path: root/ikiwiki-comment.in
diff options
context:
space:
mode:
authorAmitai Schlair <schmonz-web-ikiwiki@schmonz.com>2014-12-27 19:27:55 -0500
committerAmitai Schlair <schmonz-web-ikiwiki@schmonz.com>2014-12-27 19:27:55 -0500
commit47954c09ee9c200548824371a896da8bbda0443b (patch)
tree57e13978ba6aa908e59f34c1b628462784cc0bcd /ikiwiki-comment.in
parentc668c2c7e248286e978e53cb89c9195dcf526ae4 (diff)
downloadikiwiki-47954c09ee9c200548824371a896da8bbda0443b.tar
ikiwiki-47954c09ee9c200548824371a896da8bbda0443b.tar.gz
Accept comments on stdin (useful for importing).
Diffstat (limited to 'ikiwiki-comment.in')
-rwxr-xr-xikiwiki-comment.in92
1 files changed, 61 insertions, 31 deletions
diff --git a/ikiwiki-comment.in b/ikiwiki-comment.in
index b0cea4a4a..8f3db8988 100755
--- a/ikiwiki-comment.in
+++ b/ikiwiki-comment.in
@@ -9,44 +9,74 @@ sub usage () {
die gettext("usage: ikiwiki-comment pagefile"), "\n";
}
-my $pagefile=shift || usage ();
+sub main {
+ my $pagefile=shift || usage ();
+ my $dir=get_dir($pagefile);
+ my $page=get_page($pagefile);
-my $dir=IkiWiki::dirname($pagefile);
-$dir="." unless length $dir;
-my $page=IkiWiki::basename($pagefile);
-if (! -d $pagefile) {
- $page=~s/\.[^.]+$//;
-}
+ IkiWiki::Plugin::comments::checkconfig();
+ my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
-IkiWiki::Plugin::comments::checkconfig();
-my $comment_num=1 + IkiWiki::Plugin::comments::num_comments($page, $dir);
+ chomp(my $content = join('', <STDIN>)) unless -t STDIN;
-my $username = getpwuid($<);
-if (! defined $username) { $username="" }
+ my $comment=get_comment(get_username(), $comment_num, $content);
-my $comment="[[!comment format=mdwn\n";
-$comment.=" username=\"$username\"\n";
-$comment.=" subject=\"\"\"comment $comment_num\"\"\"\n";
-$comment.=" " . IkiWiki::Plugin::comments::commentdate() . "\n";
-$comment.=" content=\"\"\"\n\n\"\"\"]]\n";
+ # 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";
-# 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 -t STDIN;
+}
-IkiWiki::writefile($location, $dir, $comment);
+sub get_dir {
+ my ($file) = @_;
+ my $dir=IkiWiki::dirname($file);
+ $dir="." unless length $dir;
+ return $dir;
+}
-my @editor="vi";
-if (-x "/usr/bin/editor") {
- @editor="/usr/bin/editor";
+sub get_page {
+ my ($file) = @_;
+ my $page=IkiWiki::basename($file);
+ $page=~s/\.[^.]+$// unless -d $file;
+ return $page;
}
-if (exists $ENV{EDITOR}) {
- @editor=split(' ', $ENV{EDITOR});
+
+sub get_username {
+ my $username = getpwuid($<);
+ $username="" unless defined $username;
+ return $username;
}
-if (exists $ENV{VISUAL}) {
-@editor=split(' ', $ENV{VISUAL});
+
+sub get_comment {
+ my ($username, $comment_num, $content) = @_;
+ $content = '' unless defined $content;
+ my $comment="[[!comment format=mdwn\n";
+ $comment.=" username=\"$username\"\n";
+ $comment.=" subject=\"\"\"comment $comment_num\"\"\"\n";
+ $comment.=" " . IkiWiki::Plugin::comments::commentdate() . "\n";
+ $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
+ return $comment;
}
-exec(@editor, "$dir/$location");
+
+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);