Skip to content

Commit

Permalink
Refactor the code a bit and document public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hinrik committed Jul 1, 2009
1 parent 5446fa1 commit 7d7f20f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
0.09
- Detect whether a file contains Pod 5 or Pod 6
- Add homepage and source repository to Makefile.PL
- Refactor the code a bit and document public methods

0.08 Wed Jul 1 14:14:10 GMT 2009
- Add -i/--index to list known targets
Expand Down
65 changes: 57 additions & 8 deletions lib/App/Grok.pm
Expand Up @@ -28,7 +28,7 @@ sub new {
sub run {
my ($self) = @_;

$self->get_options();
$self->_get_options();

if ($opt{index}) {
print $self->target_index();
Expand All @@ -46,11 +46,12 @@ sub run {
print "$target\n";
}
else {
$self->render_file($target);
my $output = $self->render_file($target, $opt{format});
$self->_print($output);
}
}

sub get_options {
sub _get_options {
my ($self) = @_;

GetOptions(
Expand Down Expand Up @@ -110,7 +111,7 @@ sub find_target {
my $target = $self->find_synopsis($arg);
$target = $self->find_file($arg) if !defined $target;

die "Target '$arg' not recognized\n" if !$target;
return if !defined $target;
return $target;
}

Expand Down Expand Up @@ -146,20 +147,24 @@ sub find_file {
}

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

my $renderer = $self->detect_source($file);
eval "require $renderer";
die $@ if $@;
my $pod = $renderer->new->render($file, $opt{format});
return $renderer->new->render($file, $format);
}

sub _print {
my ($self, $output) = @_;

if ($opt{no_pager} || !is_interactive()) {
print $pod;
print $output;
}
else {
my $pager = $Config{pager};
my ($temp_fh, $temp) = tempfile(UNLINK => 1);
print $temp_fh $pod;
print $temp_fh $output;
close $temp_fh;

# $pager might contain options (e.g. "more /e") so we pass a string
Expand All @@ -178,6 +183,50 @@ sub render_file {
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.
Note: this method is called by L<C<find_target>|/find_target>.
=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>
Expand Down
16 changes: 15 additions & 1 deletion lib/App/Grok/Pod5.pm
Expand Up @@ -17,7 +17,9 @@ sub render {
? 'Pod::Text::Color'
: $format eq 'xhtml'
? 'Pod::Xhtml'
: 'Pod::Text'
: $format eq 'text'
? 'Pod::Text'
: die "Unsupported format '$format'";
;

eval "require $formatter";
Expand All @@ -38,6 +40,18 @@ sub render {
App::Grok::Pod5 - A Pod 5 backend for grok
=head1 METHODS
=head2 C<new>
This is the constructor. It currently takes no arguments.
=head2 C<render>
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>
Expand Down
12 changes: 12 additions & 0 deletions lib/App/Grok/Pod6.pm
Expand Up @@ -34,6 +34,18 @@ sub render {
App::Grok::Pod6 - A Pod 6 backend for grok
=head1 METHODS
=head2 C<new>
This is the constructor. It currently takes no arguments.
=head2 C<render>
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>
Expand Down

0 comments on commit 7d7f20f

Please sign in to comment.