/
vimfluence
executable file
·178 lines (136 loc) · 4.05 KB
/
vimfluence
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/perl
package main;
use strict;
use warnings;
use Net::Netrc;
use File::Temp qw( mkstemps );
use Digest::MD5 qw( md5_hex );
use URI::Split qw( uri_split uri_join );
my ($wiki_url, $space, $title, $editor);
$editor = $ENV{WIKI_EDITOR} || $ENV{VISUAL} || $ENV{EDITOR};
if ($#ARGV == 1) {
# `WIKI_URL=... vimfluence SPACE "PAGE NAME"`
$space = $ARGV[0];
$title = $ARGV[1];
$wiki_url = $ENV{WIKI_URL};
} elsif ($#ARGV == 0 && $ARGV[0] =~ /^https?:\/\//) {
# `vimfluence URL-OF-WIKI-PAGE`
my $uri = $ARGV[0];
my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
my @parts = split(/\//, $path);
$wiki_url = uri_join($scheme, $auth, join('/', @parts[0..$#parts-3]));
$title = urldecode($parts[$#parts]);
$space = $parts[$#parts - 1];
} else {
help();
exit 1;
}
# die "EDITOR : $editor, WIKI_URL: $wiki_url, SPACE: $space, TITLE: $title\n";
my $lookup = Net::Netrc->lookup($wiki_url);
my $login = $lookup->login;
my $password = $lookup->password;
my $confluence = Confluence->new($wiki_url, $login, $password);
my $output = $confluence->fetch_page($space, $title);
my $initialMD5 = md5_hex($output);
my ($fh, $filename) = mkstemps("tmpfileXXXXXX", ".confluencewiki");
print $fh $output;
close $fh;
# system("vim +\"set ft=confluencewiki\" $filename");
system("$editor $filename");
my $afterMD5 = md5_hex(readfile($filename));
if ($afterMD5 ne $initialMD5) {
print "File changed.\n";
print "Updating wiki...\n";
$confluence->store_page($space, $title, $filename);
my $url = $wiki_url . "/display/$space/" . urlencode($title);
print "Done <$url>.\n";
}
unlink $filename;
exit 0;
sub help {
print <<HELP;
NAME
vimfluence -- edit Confluence wiki pages in Vim
USAGE
WIKI_URL=http://hub.example.com/wiki vimfluence SPACE PAGE-TITLE
vimfluence WIKI-URL
EXAMPLES
WIKI_URL=http://hub.example.com/wiki vimfluence dev "Fu Machu"
If you do this frequently you might want to setup an alias something
like: `alias devwiki='WIKI_URL=http://hub.example.com/wiki vimfluence dev'`
vimfluence http://hub.example.com/wiki/display/dev/Fu-Machu
HELP
}
sub urlencode {
my $s = shift;
$s =~ s/ /+/g;
$s =~ s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;
return $s;
}
sub urldecode {
my $s = shift;
$s =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
$s =~ s/\+/ /g;
return $s;
}
sub readfile {
my $filename = shift;
open(my $fh, $filename);
my $s = join "", (<$fh>);
close $fh;
return $s;
}
1;
package Confluence;
use strict;
use warnings;
use File::Spec::Functions qw( catdir );
use File::Basename qw( dirname );
use constant confluence_path => catdir(dirname(__FILE__), "externals/confluence-cli");
sub new {
my $proto = shift;
my ($server, $login, $password) = @_;
return bless { server => $server
, login => $login
, password => $password
, common_params => [ '--server', $server, '-u', $login, '-p', "\"$password\"" ]
}, ref($proto) || $proto;
}
sub _run_cmd {
my $self = shift;
my ($params) = @_;
my $cmd = "java -jar \""
. confluence_path . "/release/confluence-cli-1.5.0.jar\" "
. join(" ", @$params);
open my $fh, $cmd . "|"
or die "Couldn't execute confluence cli script: '$cmd'\n";
# burn the first line
<$fh>;
my $s = join "", (<$fh>);
close $fh or die "could not close pipe";
return $s;
}
sub fetch_page {
my $self = shift;
my ($space, $title) = @_;
my $cmd = [ @{$self->{common_params}}
, '-a' => 'getSource'
, '--space' => $space
, '--title' => "\"$title\""
];
my $content = $self->_run_cmd($cmd);
return $content;
};
sub store_page {
my $self = shift;
my ($space, $title, $filename) = @_;
my $cmd = [ @{$self->{common_params}}
, '-a' => 'storePage'
, '--space' => $space
, '--title' => "\"$title\""
, '--file' => "\"$filename\""
];
my $content = $self->_run_cmd($cmd);
return $content;
};
1;