Updates and hotkey fixes.
[claws.git] / tools / textviewer.pl
1 #!/usr/bin/perl
2
3 # COPYRIGHT AND LICENSE
4 #        Copyright (C) 2005-2006 H.Merijn Brand
5 #
6 #        This script is free software; you can redistribute it and/or modify it
7 #        under the same terms as Perl and/or Claws Mail itself. (GPL)
8
9 use strict;
10 use warnings;
11
12 sub usage ($;$)
13 {
14     my ($err, $str) = (@_, "");
15     $err and select STDERR;
16     print
17         "usage: $0 [--html] [--type=<type>] file\n",
18         "       --html    Generate HTML (if supported)\n",
19         "       --type=X  X as mimetype (msword => doc)\n";
20     $str and print "$str\n";
21     exit $err;
22     } # usage
23
24 @ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
25
26 use Getopt::Long qw(:config bundling nopermute);
27 my $opt_v = 0;
28 my $opt_t;
29 my $opt_h = "text";
30 GetOptions (
31     "v|verbose:1"       => \$opt_v,
32     "t|type|mimetype=s" => \$opt_t,
33     "h|html"            => sub { $opt_h = "html" },
34     ) or usage (1);
35
36 $opt_v and print "$0 @ARGV\n";
37
38 my $file = shift or usage (1, "File argument is missing");
39 -f $file         or usage (1, "File argument is not a plain file");
40 -r $file         or usage (1, "File argument is not a readable file");
41 -s $file         or usage (1, "File argument is an empty file");
42
43 # anon-list contains all possible commands to show content
44 # plain text is a reference to same type (alias)
45 # %f will be replaced with file. If no %f, file will be the last arg
46 my %fh = (
47     text => {
48         bin     => [ "strings"          ], # fallback for binary files
49
50         txt     => [ "cat"              ], # Plain text
51
52         html    => [ "txt2htm",
53                      "text2html"        ], # HTML
54
55         msword  => "doc",
56         doc     => [ "antiword -w 72"   ], # M$ Word
57         "vnd.ms-excel" => "xls",
58         "ms-excel"     => "xls",
59         xls     => [ "xlscat -L"        ], # M$ Excel
60 #       ppt     => [ "ppthtml"          ], # M$ PowerPoint
61 #                       ppthtml "$1" | html2text
62
63         rtf     => [ "rtf2text",
64                      "unrtf -t text"    ], # RTF
65         pdf     => [ "pdftotext %f -"   ], # Adobe PDF
66
67         sxc     => "xls",                  # OpenOffice spreadsheet
68         odt     => [ "ooo2txt"          ], # OpenOffice writer
69
70         csv     => "xls",                  # Comma Separated Values
71
72         pl      => [ "perltidy -st -se",
73                      "cat"              ], # Perl
74         pm      => "pl",
75
76         ( map { $_ => "txt" } qw(
77             diff
78             c h ic ec cc
79             sh sed awk
80             plain
81             )),
82
83         bz2     => [ "bzip2 -d < %f | strings" ],
84
85         test    => [ \&test             ], # Internal
86         },
87
88     html => {
89         rtf     => [ "rtf2html"         ],
90         },
91     );
92
93 my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
94 $opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
95 unless (exists $fh{text}{$ext}) {
96     my $ftype = `file --brief $file`;
97     $ext =
98         $ftype =~ m/^pdf doc/i                                  ? "pdf" :
99         $ftype =~ m/^ascii( english)? text/i                    ? "txt" :
100         $ftype =~ m/^(utf-8 unicode|iso-\d+)( english)? text/i  ? "txt" :
101         $ftype =~ m/^xml doc/i                                  ? "xml" :
102         $ftype =~ m/^\w+ compress/i                             ? "bin" :
103                                                                   "bin" ;
104     # \w+ archive
105     # \w+ image
106     # ...
107     }
108 $ext ||= "txt";
109 exists $fh{$opt_h}{$ext} or $opt_h = "text";
110 exists $fh{$opt_h}{$ext} or $ext   = "txt";
111 my          $ref = $fh{$opt_h}{$ext};
112 ref $ref or $ref = $fh{$opt_h}{$ref};
113
114 $opt_v and print STDERR "[ @$ref ] $file\n";
115
116 sub which ($)
117 {
118     (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
119     foreach my $path (split m/:+/, $ENV{PATH}) {
120         -x "$path/$cmd" and return "$path/$cmd";
121         }
122     return 0;
123     } # which
124
125 my $cmd = "cat -ve";
126 foreach my $c (@$ref) {
127     if (ref $c) {
128         $c->($file);
129         exit;
130         }
131
132     my $cp = which ($c) or next;
133     $cmd = $c;
134     last;
135     }
136
137 $cmd =~ s/%f\b/$file/g or $cmd .= " $file";
138 $opt_v and print "$cmd\n";
139 exec $cmd;