/
Grok.pm
257 lines (187 loc) · 6.04 KB
/
Grok.pm
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package App::Grok;
use strict;
use warnings;
use Config qw<%Config>;
use File::ShareDir qw<dist_dir>;
use File::Spec::Functions qw<catdir catfile splitpath>;
use File::Temp qw<tempfile>;
use IO::Interactive qw<is_interactive>;
use Getopt::Long qw<:config bundling>;
use List::Util qw<first>;
use Pod::Usage;
our $VERSION = '0.09';
my %opt;
sub new {
my ($package, %self) = @_;
$self{share_dir} = defined $ENV{GROK_SHAREDIR}
? $ENV{GROK_SHAREDIR}
: dist_dir('grok')
;
return bless \%self, $package;
}
sub run {
my ($self) = @_;
$self->_get_options();
if ($opt{index}) {
print join("\n", $self->target_index()) . "\n";
return;
}
my $target = defined $opt{file}
? $opt{file}
: $self->find_target($ARGV[0])
;
die "No matching files found for target '$target'\n" if !-e $target;
if ($opt{only}) {
print "$target\n";
}
else {
my $output = $self->render_file($target, $opt{output});
$self->_print($output);
}
return;
}
sub _get_options {
my ($self) = @_;
GetOptions(
'F|file=s' => \$opt{file},
'h|help' => sub { pod2usage(1) },
'i|index' => \$opt{index},
'l|only' => \$opt{only},
'o|output=s' => \($opt{output} = 'ansi'),
'T|no-pager' => \$opt{no_pager},
'u|unformatted' => \$opt{unformatted},
'V|version' => sub { print "grok $VERSION\n"; exit },
) or pod2usage();
$opt{output} = 'pod' if $opt{unformatted};
if (!$opt{index} && !defined $opt{file} && !@ARGV) {
warn "Too few arguments\n";
pod2usage();
}
return;
}
sub target_index {
my ($self) = @_;
my $dir = catdir($self->{share_dir}, 'Spec');
my @index;
my @synopses = map { (splitpath($_))[2] } glob "$dir/*.pod";
push @index, @synopses;
my $S32_dir = catdir($dir, 'S32-setting-library');
my @sections = map { (splitpath($_))[2] } glob "$S32_dir/*.pod";
push @index, map { "S32-$_" } @sections;
s/\.pod$// for @index;
return @index;
}
sub detect_source {
my ($self, $file) = @_;
open my $handle, '<', $file or die "Can't open $file";
my $contents = do { local $/ = undef; scalar <$handle> };
close $handle;
my ($first_pod) = $contents =~ /(^=(?!encoding)\S+)/m;
return if !defined $first_pod; # no Pod found
if ($first_pod =~ /^=(?:pod|head\d+|over)$/
|| $contents =~ /^=cut\b/m) {
return 'App::Grok::Pod5';
}
else {
return 'App::Grok::Pod6';
}
}
sub find_target {
my ($self, $arg) = @_;
my $target = $self->find_synopsis($arg);
$target = $self->find_module_or_program($arg) if !defined $target;
return if !defined $target;
return $target;
}
sub find_synopsis {
my ($self, $syn) = @_;
my $dir = catdir($self->{share_dir}, 'Spec');
if ($syn =~ /^S\d+$/i) {
my @synopses = map { (splitpath($_))[2] } glob "$dir/*.pod";
my $found = first { /$syn/i } @synopses;
return if !defined $found;
return catfile($dir, $found);
}
elsif (my ($section) = $syn =~ /^S32-(\S+)$/i) {
my $S32_dir = catdir($dir, 'S32-setting-library');
my @sections = map { (splitpath($_))[2] } glob "$S32_dir/*.pod";
my $found = first { /$section/i } @sections;
if (defined $found) {
return catfile($S32_dir, $found);
}
}
return;
}
sub find_module_or_program {
my ($self, $file) = @_;
# TODO: do a grand search
return $file;
}
sub render_file {
my ($self, $file, $output) = @_;
my $renderer = $self->detect_source($file);
eval "require $renderer";
die $@ if $@;
return $renderer->new->render($file, $output);
}
sub _print {
my ($self, $output) = @_;
if ($opt{no_pager} || !is_interactive()) {
print $output;
}
else {
my $pager = $Config{pager};
my ($temp_fh, $temp) = tempfile(UNLINK => 1);
print $temp_fh $output;
close $temp_fh;
# $pager might contain options (e.g. "more /e") so we pass a string
$^O eq 'MSWin32'
? system $pager . qq{ "$temp"}
: system $pager . qq{ '$temp'}
;
}
return;
}
1;
=encoding UTF-8
=head1 NAME
App::Grok - Does most of grok's heavy lifting
=head1 DESCRIPTION
This class provides the main functionality needed by grok. It has some
methods you can use if you need to hook into grok.
=head1 METHODS
=head2 C<new>
This is the constructor. It takes no arguments.
=head2 C<run>
If you call this method, it will look at the command line arguments in
C<@ARGV> and act accordingly. This is basically what the L<C<grok>|grok>
program does. Takes no arguments.
=head2 C<target_index>
Takes no arguments. Returns a list of all the targets known to C<grok>.
=head2 C<detect_source>
Takes a filename as an argument. Returns the name of the appropriate
C<App::Grok::*> class to parse it. Returns nothing if the file doesn't contain
any Pod.
=head2 C<find_target>
Takes a valid C<grok> target as an argument. If found, it will return a path
to a matching file, otherwise it returns nothing.
=head2 C<find_synopsis>
Takes the name (or a substring of a name) of a Synopsis as an argument.
Returns a path to a matching file if one is found, otherwise returns nothing.
B<Note:> this method is called by L<C<find_target>|/find_target>.
=head2 C<find_module_or_program>
Takes the name of a module or a program. Returns a path to a matching file
if one is found, otherwise returns nothing. B<Note:> this doesn't do anything
yet.
=head2 C<render_file>
Takes two arguments, a filename and the name of an output format. Returns
a string containing the rendered document. It will C<die> if there is an
error.
=head1 AUTHOR
Hinrik Örn Sigurðsson, L<hinrik.sig@gmail.com>
=head1 LICENSE AND COPYRIGHT
Copyright 2009 Hinrik Örn Sigurðsson
C<grok> is distributed under the terms of the Artistic License 2.0.
For more details, see the full text of the license in the file F<LICENSE>
that came with this distribution.
=cut