| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Mail::Summary::Tools::CLI::Edit; | ||
| 4 | use base qw/Mail::Summary::Tools::CLI::Command/; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Class::Autouse (<<'#\'END_USE' =~ m!(\w+::[\w:]+)!g); | ||
| 10 | #\ | ||
| 11 | |||
| 12 | use Mail::Summary::Tools::Summary; | ||
| 13 | use Mail::Summary::Tools::FlatFile; | ||
| 14 | use Proc::InvokeEditor; | ||
| 15 | |||
| 16 | #'END_USE | ||
| 17 | |||
| 18 | use constant options => ( | ||
| 19 | [ 'verbose|v!' => 'Output progress information' ], | ||
| 20 | [ 'input|i=s' => 'The summary file to edit' ], | ||
| 21 | [ 'mode' => hidden => { default => "interactive", one_of => [ | ||
| 22 | [ 'interactive' => "Edit the file interactively (the default)" ], | ||
| 23 | [ 'save' => "Output a flatfile to a file or STDOUT" ], | ||
| 24 | [ 'load' => "Load the specified file or STDIN into the summary" ], | ||
| 25 | ] } ], | ||
| 26 | [ 'skip|s!' => 'Skip threads that have already been summarized' ], | ||
| 27 | [ 'hidden|H!' => 'Include hidden threads' ], | ||
| 28 | [ 'links|l!' => 'Include links to on-line archives', { default => 1 } ], # TODO shorten | ||
| 29 | [ 'archive|a=s' => 'The rchive to use (defaults to "google")', { default => "google" } ], | ||
| 30 | [ 'posters|p!' => 'Include posters names in the comment section', { default => 1 } ], | ||
| 31 | [ 'dates|d!' => 'Include start and end dates in the comment section', { default => 1 } ], | ||
| 32 | [ 'misc|m!' => 'Include misc info in the comment section', { default => 1 } ], | ||
| 33 | [ 'extra_fields|e=s@' => "Additional fields to include in the YAML header (can be used several times)" ], | ||
| 34 | [ 'pattern|P=s@' => "Only include summaries matching this regex (matches against formatted text)" ], | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub validate { | ||
| 38 | my ( $self, $opt, $args ) = @_; | ||
| 39 | @$args and $opt->{$_} ||= shift @$args for qw/input/; | ||
| 40 | |||
| 41 | unless ( $opt->{input} ) { | ||
| 42 | $self->usage_error("Please specify an input summary YAML file."); | ||
| 43 | } | ||
| 44 | |||
| 45 | if ( @$args ) { | ||
| 46 | $self->usage_error("Unknown arguments: @$args."); | ||
| 47 | } | ||
| 48 | |||
| 49 | if ( defined($opt->{load}) and $opt->{load} eq "" || $opt->{load} ) { | ||
| 50 | $self->usage_error("You can choose one and only one of --interactive, --load or --save.") if $opt->{interactive}; | ||
| 51 | $opt->{load} = \*STDIN; | ||
| 52 | unless ( ref( my $file = $opt->{load} ) ) { | ||
| 53 | open my $in, "<", $file || die "open($file): $!"; | ||
| 54 | $opt->{load} = $in; | ||
| 55 | } | ||
| 56 | |||
| 57 | binmode $opt->{load}, ":utf8"; | ||
| 58 | } | ||
| 59 | |||
| 60 | if ( defined($opt->{save}) and $opt->{save} eq "" || $opt->{save} ) { | ||
| 61 | $self->usage_error("You can choose one and only one of --interactive, --load or --save.") if $opt->{load}; | ||
| 62 | $opt->{save} = \*STDOUT; | ||
| 63 | unless ( ref( my $file = $opt->{save} ) ) { | ||
| 64 | open my $out, ">", $file || die "open($file): $!"; | ||
| 65 | $opt->{save} = $out; | ||
| 66 | } | ||
| 67 | |||
| 68 | binmode $opt->{save}, ":utf8"; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->{opt} = $opt; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub load_summary { | ||
| 75 | my $self = shift; | ||
| 76 | my $opt = $self->{opt}; | ||
| 77 | |||
| 78 | Mail::Summary::Tools::Summary->load( | ||
| 79 | $opt->{input}, | ||
| 80 | thread => { | ||
| 81 | default_archive => $opt->{archive}, | ||
| 82 | archive_link_params => { cache => $self->app->context->cache }, | ||
| 83 | }, | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub create_flatfile { | ||
| 88 | my ( $self, $summary ) = @_; | ||
| 89 | my $opt = $self->{opt}; | ||
| 90 | |||
| 91 | Mail::Summary::Tools::FlatFile->new( | ||
| 92 | summary => $summary, | ||
| 93 | skip_summarized => $opt->{skip}, | ||
| 94 | include_hidden => $opt->{hidden}, | ||
| 95 | list_posters => $opt->{posters}, | ||
| 96 | list_dates => $opt->{dates}, | ||
| 97 | list_misc => $opt->{misc}, | ||
| 98 | add_links => $opt->{links}, | ||
| 99 | extra_fields => [ map { split ',', $_ } @{ $opt->{extra_fields} } ], | ||
| 100 | patterns => [ map { qr/$_/ } @{ $opt->{pattern} || [] } ], | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub run { | ||
| 105 | my ( $self, $opt, $args ) = @_; | ||
| 106 | my $method = "run_$opt->{mode}"; | ||
| 107 | $self->$method; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub run_save { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $out = $self->{opt}{save}; | ||
| 114 | print $out $self->create_flatfile( $self->load_summary )->save; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub run_load { | ||
| 118 | my $self = shift; | ||
| 119 | my $opt = $self->{opt}; | ||
| 120 | |||
| 121 | my $summary = $self->load_summary; | ||
| 122 | my $flat = $self->create_flatfile( $summary ); | ||
| 123 | |||
| 124 | my $in = $opt->{load}; # aha | ||
| 125 | my $buffer = do { local $/; <$in> }; | ||
| 126 | |||
| 127 | $flat->load($buffer); | ||
| 128 | $summary->save( $opt->{input} ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub run_interactive { | ||
| 132 | my $self = shift; | ||
| 133 | my $opt = $self->{opt}; | ||
| 134 | |||
| 135 | my $summary = $self->load_summary; | ||
| 136 | my $flat = $self->create_flatfile( $summary ); | ||
| 137 | |||
| 138 | my $buffer = $flat->save; | ||
| 139 | do { | ||
| 140 | if ( $@ ) { | ||
| 141 | my $err = $@; | ||
| 142 | $err =~ s/^/# /mg; | ||
| 143 | $buffer = "# There was an error in your output:\n\n$err\n\n# to abort clear the entire file\n\n$buffer"; | ||
| 144 | } | ||
| 145 | |||
| 146 | $buffer = Proc::InvokeEditor->edit( $buffer ); | ||
| 147 | |||
| 148 | exit unless $buffer =~ /\S/; | ||
| 149 | |||
| 150 | } until eval { $flat->load($buffer) }; | ||
| 151 | |||
| 152 | $summary->save( $opt->{input} ); | ||
| 153 | } | ||
| 154 | |||
| 155 | __PACKAGE__; | ||
| 156 | |||
| 157 | __END__ |