Skip to content

Commit

Permalink
Detect whether a file contains Pod 5 or Pod 6
Browse files Browse the repository at this point in the history
  • Loading branch information
hinrik committed Jul 1, 2009
1 parent a8f879c commit 2d5db1d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,3 +1,6 @@
0.09
- Detect whether a file contains Pod 5 or Pod 6

0.08 Wed Jul 1 14:14:10 GMT 2009
- Add -i/--index to list known targets
- Rename -v (--version) option to -V
Expand Down
6 changes: 3 additions & 3 deletions README
Expand Up @@ -5,19 +5,19 @@ SYNOPSIS
grok <options> <target>

Options:
-F FILE, --file=FILE A file to read Pod 6 from
-F FILE, --file=FILE A file to read Pod from
-f FORMAT, --format=FORMAT The output format, ansi/text/xhtml
-h, --help Print this help message
-i, --index Print index of things grok knows about
-l, --only Only print the path to the target file
-T, --no-pager Send output to STDOUT without any pager
-V, --version Print version information

If you don't supply the -F option, you need to supply a target.
If you don't supply the -F or -i options, you need to supply a target.
The following targets are recognized:

* A synopsis (e.g. 's02', 's02-bits', 's32-rules')
* A path to a file containing Pod 6
* A path to a file containing Pod

DESCRIPTION
This little application is intended to be a "perldoc" for Perl 6.
Expand Down
2 changes: 0 additions & 2 deletions TODO
Expand Up @@ -4,8 +4,6 @@ Features
* Add switch to display the raw pod output. perldoc uses -u (unformatted)
* Add -m switch to display module's file in its entirety like perldoc
* Think about i18n, add -L for lang code like perldoc
* Somehow detect whether a file is Pod 5 or Pod 6. A simple test might
be to look for =begin (Pod 6) and =cut (Pod 5)
* Borrow the S32 function lookup feature (grok -f FUNCTION) from
Padre::Plugin::Perl6

Expand Down
59 changes: 36 additions & 23 deletions lib/App/Grok.pm
Expand Up @@ -29,27 +29,24 @@ sub run {
my ($self) = @_;

$self->get_options();
my ($target, $renderer);

if ($opt{index}) {
print $self->target_index();
return;
}

if (defined $opt{file}) {
($target, $renderer) = ($opt{file}, 'App::Grok::Pod6');
}
else {
($target, $renderer) = $self->find_target($ARGV[0]);
}
my $target = defined $opt{file}
? $opt{file}
: $self->find_target($ARGV[0])
;

die "No matching files found for target '$target'" if !-e $target;

if ($opt{only}) {
print "$target\n";
}
else {
$self->render_file($target, $renderer);
$self->render_file($target);
}
}

Expand All @@ -66,7 +63,10 @@ sub get_options {
'V|version' => sub { print "grok $VERSION\n"; exit },
) or pod2usage();

die "Too few arguments\n" if !$opt{index} && !defined $opt{file} && !@ARGV;
if (!$opt{index} && !defined $opt{file} && !@ARGV) {
warn "Too few arguments\n";
pod2usage();
}
}

sub target_index {
Expand All @@ -85,15 +85,33 @@ sub target_index {
return join("\n", @index) . "\n";
}

sub detect_source {
my ($self, $file) = @_;

open my $handle, '<', $file or die "Can't open $file";
my $contents = do { local $/; 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, $renderer);
($target, $renderer) = $self->find_synopsis($arg);
($target, $renderer) = $self->find_file($arg) if !defined $target;
my $target = $self->find_synopsis($arg);
$target = $self->find_file($arg) if !defined $target;

die "Target '$arg' not recognized\n" if !$target;
return ($target, $renderer);
return $target;
}

sub find_synopsis {
Expand All @@ -105,21 +123,15 @@ sub find_synopsis {
my $found = first { /$syn/i } @synopses;

return if !defined $found;

if ($found =~ /^S26/) {
return (catfile($dir, $found), 'App::Grok::Pod6');
}
else {
return (catfile($dir, $found), 'App::Grok::Pod5');
}
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), 'App::Grok::Pod5');
return catfile($S32_dir, $found);
}
}

Expand All @@ -130,12 +142,13 @@ sub find_file {
my ($self, $file) = @_;

# TODO: do a grand search
return ($file, 'App::Grok::Pod6');
return $file;
}

sub render_file {
my ($self, $file, $renderer) = @_;
my ($self, $file) = @_;

my $renderer = $self->detect_source($file);
eval "require $renderer";
die $@ if $@;
my $pod = $renderer->new->render($file, $opt{format});
Expand Down
6 changes: 3 additions & 3 deletions script/grok
Expand Up @@ -16,19 +16,19 @@ grok - Perl 6 documentation reader
B<grok> <options> <target>
Options:
-F FILE, --file=FILE A file to read Pod 6 from
-F FILE, --file=FILE A file to read Pod from
-f FORMAT, --format=FORMAT The output format, ansi/text/xhtml
-h, --help Print this help message
-i, --index Print index of things grok knows about
-l, --only Only print the path to the target file
-T, --no-pager Send output to STDOUT without any pager
-V, --version Print version information
If you don't supply the -F option, you need to supply a target.
If you don't supply the -F or -i options, you need to supply a target.
The following targets are recognized:
* A synopsis (e.g. 's02', 's02-bits', 's32-rules')
* A path to a file containing Pod 6
* A path to a file containing Pod
=head1 DESCRIPTION
Expand Down
5 changes: 1 addition & 4 deletions t/03_opts/02_format.t
@@ -1,7 +1,7 @@
use strict;
use warnings;
use File::Spec::Functions 'catfile';
use Test::More tests => 6;
use Test::More tests => 12;

my $script = catfile('script', 'grok');

Expand All @@ -20,8 +20,6 @@ like($pod6_ansi_long, qr/\e\[/, "Pod 6 ANSI has color codes (--format)");
isnt($pod6_text_long, $pod6_xhtml_long, "Pod 6 text and xhtml are different (--format)");
like($pod6_xhtml_long, qr/<p>/, "Pod 6 xhtml has <p> (--format)");

# TODO: uncomment these when grok learns how to detect Pod 5
=pod
my $pod5 = catfile('t_source', 'basic5.pod');
my $pod5_text_short = qx/$^X $script -F $pod5 -f text/;
my $pod5_text_long = qx/$^X $script -F $pod5 --format text/;
Expand All @@ -36,4 +34,3 @@ isnt($pod5_text_long, $pod5_ansi_long, "Pod 5 text and ANSI are different (--for
like($pod5_ansi_long, qr/\e\[/, "Pod 5 ANSI has color codes (--format)");
isnt($pod5_text_long, $pod5_xhtml_long, "Pod 5 text and xhtml are different (--format)");
like($pod5_xhtml_long, qr/<p>/, "Pod 5 xhtml has <p> (--format)");
=cut

0 comments on commit 2d5db1d

Please sign in to comment.