-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.pl
executable file
·236 lines (178 loc) · 4.93 KB
/
update.pl
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env perl
use MooseX::Declare;
use MooseX::Getopt (); # preload the traits
use 5.010;
class FakeGistUpdater with MooseX::Getopt::Dashes {
use AnyEvent::HTTP;
use Carp qw(croak);
use MooseX::Types::Path::Class qw(File);
use autodie;
has file => (
traits => [qw(Getopt)],
isa => File,
is => "ro",
coerce => 1,
lazy_build => 1,
cmd_aliases => ['f'],
documentation => "The file to update",
);
sub _build_file {
my $self = shift;
Path::Class::file($self->ARGV->[0] || croak "Either specify --file or provide one as an argument");
}
has allow_network => (
traits => [qw(Getopt)],
isa => "Bool",
is => "ro",
default => 0,
cmd_aliases => ['n'],
documentation => "If true XML::LibXML will be allowed to fetch DTDs etc (defaults to false)",
);
has strip_leading_newline => (
traits => [qw(Getopt)],
isa => "Bool",
is => "ro",
default => 1,
cmd_aliases => ['s'],
documentation => "Remove leading newline before posting (defaults to true)",
);
has chomp => (
traits => [qw(Getopt)],
isa => "Bool",
is => "ro",
default => 1,
cmd_aliases => ['c'],
documentation => "Remove trailing newline before posting (defaults to true)",
);
has in_place => (
traits => [qw(Getopt)],
isa => "Bool",
is => "ro",
default => 0,
cmd_aliases => ['i'],
documentation => "Update the file in place (defaults to false)",
);
has backup_extension => (
traits => [qw(Getopt)],
isa => "Str",
is => "ro",
default => '~',
cmd_aliases => ['b'],
documentation => "Backup extension (defaults to ~)"
);
has parser_class => (
traits => [qw(Getopt)],
isa => "Str",
is => "ro",
default => "XML::LibXML",
documentation => "Specify an XML parser class (e.g. XML::Liberal) (defaults to XML::LibXML)",
);
has parser => (
traits => [qw(NoGetopt)],
isa => "Object",
is => "ro",
lazy_build => 1,
handles => [qw(parse_file)],
);
method _build_parser {
my $class = $self->parser_class;
Class::MOP::load_class($class);
my $parser = $class->new;
$parser->no_network(1) unless $self->allow_network;
return $parser;
}
has dom => (
traits => [qw(NoGetopt)],
isa => "Object",
is => "ro",
lazy_build => 1,
);
method _build_dom {
$self->parse_file($self->file->stringify);
}
has output_handle => (
traits => [qw(NoGetopt)],
isa => "FileHandle",
is => "ro",
lazy_build => 1,
);
method _build_output_handle {
if ( $self->in_place ) {
rename ( $self->file, $self->file . $self->backup_extension );
return $self->file->openw;
} else {
require FileHandle;
return \*STDOUT;
}
}
method get_fake_gist_nodes {
$self->dom->documentElement->findnodes(q{//*[contains(concat(' ', @class, ' '), ' fake-gist ')]})->get_nodelist;
}
method gist_uri ($gist) {
"http://gist.github.com/${gist}.txt";
}
method replace_text ($elem, $text) {
$elem->removeChild($_) for $elem->getChildNodes;
$elem->appendText($text);
}
method update_gist ($node) {
my $id = $node->getAttribute('id');
my ( $gist_id ) = ( $id =~ /^fake-gist-(.+)$/ );
my $v = AnyEvent->condvar;
say "Fetching gist $gist_id";
http_get $self->gist_uri($gist_id), sub {
my $gist_text = shift;
$self->replace_text($node, $gist_text);
$v->send($node);
};
return $v;
}
method extract_text ($node) {
my $text = $node->getChildNodes;
$text =~ s/^\n// if $self->strip_leading_newline;
chomp($text) if $self->chomp;
return $text;
}
method extract_gist_args ($node) {
my $lang = $node->getAttribute('lang');
return (
text => $self->extract_text($node),
( defined $lang ? ( lang => $lang ) : () ),
);
}
method post_gist ($node) {
require App::Nopaste::Service::Gist;
croak "App::Nopaste::Service::Gist unavailable"
unless App::Nopaste::Service::Gist->available;
my %args = $self->extract_gist_args($node);
say "Creating new gist";
my ( $ok, $link ) = App::Nopaste::Service::Gist->nopaste(%args);
croak "Gist creation failed: $link" unless $ok;
my ( $id ) = ( $link =~ /(\d+)$/ );
say "Created gist $id";
$node->removeAttribute('lang');
$self->replace_text($node, $args{text}); # for consistency
$node->setAttribute(id => "fake-gist-$id");
my $v = AnyEvent->condvar;
$v->send($node);
return $v;
}
method process_node ($node) {
if ( $node->getAttribute('id') ) {
return $self->update_gist($node);
} else {
return $self->post_gist($node);
}
}
method process_dom {
$_->recv for map { $self->process_node($_) } $self->get_fake_gist_nodes;
}
method output_dom {
$self->output_handle->print($self->dom->toString);
}
method run {
$self->process_dom;
$self->output_dom;
}
}
FakeGistUpdater->new_with_options->run;