File: textviewer.pl

package info (click to toggle)
claws-mail 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 51,124 kB
  • sloc: ansic: 268,194; cpp: 19,477; xml: 11,269; sh: 5,794; perl: 2,767; makefile: 2,509; yacc: 2,470; python: 334; lex: 293
file content (178 lines) | stat: -rwxr-xr-x 4,500 bytes parent folder | download | duplicates (5)
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

# COPYRIGHT AND LICENSE
#        Copyright (C) 2005-2018 H.Merijn Brand
#
#        This script is free software; you can redistribute it and/or modify it
#        under the same terms as Perl and/or Claws Mail itself. (GPL)

use 5.14.1;
use warnings;

our $VERSION = "1.01 - 2018-10-08";
our $CMD = $0 =~ s{.*/}{}r;

sub usage {
    my ($err, $str) = (@_, "");
    $err and select STDERR;
    say "usage: $CMD [--html] [--type=<type>] file\n",
	"       --html    Generate HTML (if supported)\n",
	"       --type=X  X as mimetype (msword => doc)\n",
	"  $CMD --list will show all implemented conversions";
    $str and say $str;
    exit $err;
    } # usage

use Getopt::Long qw(:config bundling nopermute);
my $opt_v = 0;
my $opt_h = "text";
GetOptions (
    "help|?"		=> sub { usage (0); },
    "V|version"		=> sub { say "$CMD [$VERSION]"; exit 0; },

    "v|verbose:1"	=> \$opt_v,
    "t|type|mimetype=s"	=> \my $opt_t,
    "h|html"		=> sub { $opt_h = "html" },
    "l|list!"		=> \my $opt_l,
    ) or usage (1);

$opt_v and say "$0 @ARGV";

# anon-list contains all possible commands to show content
# plain text is a reference to same type (alias)
# %f will be replaced with file. If no %f, file will be the last arg
my %fh = (
    text => {
	bin	=> [ "strings"		], # fallback for binary files

	txt	=> [ "cat"		], # Plain text

	html	=> [ "htm2txt",
		     "html2text"	], # HTML

	msword	=> "doc",
	doc	=> [ "catdoc -x -dutf-8",
		     "wvText",
		     "antiword -w 72"	], # M$ Word
	"vnd.ms-excel" => "xls",
	"ms-excel"     => "xls",
	docx	=> [ "unoconv -f text --stdout"	], # MS Word
	xlsx	=> "xls",
	xls	=> [ "xlscat -L",
		     "catdoc -x -dutf-8",
		     "wvText"		], # M$ Excel
#	ppt	=> [ "ppthtml"		], # M$ PowerPoint
#			ppthtml "$1" | html2text
	csv	=> "xls",		   # Comma Separated Values

	ics	=> [ "ics2txt"		], # ICS calendar request

	rtf	=> [ "rtf2text",
		     "unrtf -t text"	], # RTF
	pdf	=> [ "pdftotext %f -"	], # Adobe PDF

	ods	=> "xls",		   # OpenOffice spreadsheet
	sxc	=> "xls",		   # OpenOffice spreadsheet
	odt	=> [ "oo2pod %f | pod2text",
		     "ooo2txt"		], # OpenOffice writer
	rtf	=> [ "rtf2text"		], # RTF

	pl	=> [ "perltidy -st -se",
		     "cat"		], # Perl
	pm	=> "pl",

	jsn	=> [ "json_pp"		], # JSON
	json	=> "jsn",

	xml	=> [ "xml_pp"		], # XML

	( map { $_ => "txt" } qw(
	    patch diff
	    c h ic ec cc
	    sh sed awk
	    plain
	    yml yaml
	    )),

	bz2	=> [ "bzip2 -d < %f | strings" ],

	zip	=> [ "unzip -l %f"	], # ZIP

	test	=> [ \&test		], # Internal

	tgz	=> [ "tar tvf"		], # Tar     uncompressed
	tgz	=> [ "tar tzvf"		], # Tar GZ    compressed
	tbz	=> [ "tar tjvf"		], # Tar BZip2 compressed
	txz	=> [ "tar tJvf"		], # Tar XZ    compressed

	rar	=> [ "unrar l"		], # RAR
	},

    html => {
	rtf	=> [ "rtf2html"		],
	},
    );

if ($opt_l) {
    my %tc = %{$fh{text}};
    foreach my $ext (sort keys %tc) {
	my $exe = $tc{$ext};
	ref $exe or $exe = $tc{$exe};
	printf "  .%-12s %s\n", $ext, $_ for @$exe;
	}
    exit 0;
    }

my $file = shift or usage (1, "File argument is missing");
-f $file         or usage (1, "File argument is not a plain file");
-r $file         or usage (1, "File argument is not a readable file");
-s $file         or usage (1, "File argument is an empty file");

my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
$opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
unless (exists $fh{text}{$ext}) {
    my $ftype = `file --brief $file`;
    $ext =
	$ftype =~ m/^pdf doc/i					? "pdf" :
	$ftype =~ m/^ascii( english)? text/i			? "txt" :
	$ftype =~ m/^(utf-8 unicode|iso-\d+)( english)? text/i	? "txt" :
	$ftype =~ m/^xml doc/i					? "xml" :
	$ftype =~ m/^\w+ compress/i				? "bin" :
								  "bin" ;
    # \w+ archive
    # \w+ image
    # ...
    }
$ext ||= "txt";
exists $fh{$opt_h}{$ext} or $opt_h = "text";
exists $fh{$opt_h}{$ext} or $ext   = "txt";
my          $ref = $fh{$opt_h}{$ext};
ref $ref or $ref = $fh{$opt_h}{$ref};

$opt_v and warn "[ @$ref ] $file\n";

sub which {
    (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
    foreach my $path (split m/:+/, $ENV{PATH}) {
	-x "$path/$cmd" and return "$path/$cmd";
	}
    return 0;
    } # which

my $cmd = "cat -ve";
foreach my $c (@$ref) {
    if (ref $c) {
	$c->($file);
	exit;
	}

    my $cp = which ($c) or next;
    $cmd = $c;
    last;
    }

my @cmd = split m/ +/ => $cmd;
grep { s/%f\b/$file/ } @cmd or push @cmd, $file;
#$cmd =~ s/%f\b/$file/g or $cmd .= " $file";
$opt_v and say "@cmd";
exec @cmd;