Skip to content

Commit

Permalink
Replace _save_pages() with _transform()
Browse files Browse the repository at this point in the history
_save_pages() is a Pod::Simple::Search method which takes two specific
arguments.  From the point of view of making Pod-Html OO, it was
problematic because (a) it used a variable ($Podroot) defined outside of
its scope and (b) it assigned to a variable (%Pages) defined outside of
its scope.  It was therefore resistant to encapsulation.

Experimentation showed that if we use the return value of
Pod::Simple::Search::survey(), we could parse that hashref using what
was the guts of _save_pages() and assign explicitly to %Pages.

TODO:  Move %Pages within the Pod::Html object.  Handle $object
properly.

Signed-off-by: James E Keenan <jkeenan@cpan.org>
  • Loading branch information
jkeenan committed May 12, 2021
1 parent 4d2e653 commit 25ea7ee
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions ext/Pod-Html/lib/Pod/Html.pm
Expand Up @@ -238,16 +238,16 @@ sub pod2html {
my $opts = process_command_line;
$self->process_options($opts);

my $globals = $self->refine_globals();
$self->refine_globals();

# load or generate/cache %Pages
unless ($self->get_cache()) {
# generate %Pages
%Pages = generate_cache($globals, \%Pages);
%Pages = $self->generate_cache(\%Pages);
}
my $input = $self->identify_input();
my $podtree = $self->parse_input_for_podtree($input);
$globals = $self->set_Title_from_podtree($podtree);
$self->set_Title_from_podtree($podtree);

# set options for the HTML generator
my $parser = Pod::Simple::XHTML::LocalPodLinks->new();
Expand Down Expand Up @@ -371,13 +371,15 @@ sub generate_cache {
# limit search to those in @{$self->{Podpath}}
# - verbose: report (via 'warn') what search is doing
# - laborious: to allow '.' in dirnames (e.g., /usr/share/perl/5.14.1)
# - callback: used to remove Podroot and extension from each file
# - recurse: go into subdirectories
# - survey: search for POD files in PodPath
my ($name2path, $path2name) =
Pod::Simple::Search->new->inc(0)->verbose($self->{Verbose})->laborious(1)
->callback(\&_save_page)->recurse($self->{Recurse})->survey(@{$self->{Podpath}});
#print STDERR Data::Dumper::Dumper($name2path, $path2name) if ($self->{Verbose});
->recurse($self->{Recurse})->survey(@{$self->{Podpath}});
# remove Podroot and extension from each file
for my $k (keys %{$name2path}) {
$Pages{$k} = _transform($self, $name2path->{$k});
}

chdir($pwd) || die "$0: error changing to directory $pwd: $!\n";

Expand All @@ -403,23 +405,18 @@ sub generate_cache {
return %{$Pagesref};
}

#
# store POD files in %Pages
#
sub _save_page {
my ($modspec, $modname) = @_;

# Remove Podroot from path
$modspec = $Podroot eq File::Spec->curdir
? File::Spec->abs2rel($modspec)
: File::Spec->abs2rel($modspec,
File::Spec->canonpath($Podroot));
sub _transform {
my ($self, $v) = @_;
$v = $self->{Podroot} eq File::Spec->curdir
? File::Spec->abs2rel($v)
: File::Spec->abs2rel($v,
File::Spec->canonpath($self->{Podroot}));

# Convert path to unix style path
$modspec = unixify($modspec);
$v = unixify($v);

my ($file, $dir) = fileparse($modspec, qr/\.[^.]*/); # strip .ext
$Pages{$modname} = $dir.$file;
my ($file, $dir) = fileparse($v, qr/\.[^.]*/); # strip .ext
return $dir.$file;
}

sub get_cache {
Expand Down Expand Up @@ -545,7 +542,7 @@ sub set_Title_from_podtree {
}

$self->{Title} //= "";
return { %{$self} };
return $self;
}

sub refine_parser {
Expand Down Expand Up @@ -627,6 +624,14 @@ sub write_file {
chmod 0644, $self->{Htmlfile} unless $self->{Htmlfile} eq '-';
}
#sub compare {
# my ($ha, $hb) = @_;
# local $Data::Dumper::Terse = 1;
# local $Data::Dumper::Indent = 0;
# local $Data::Dumper::Sortkeys = 1;
# return (Dumper($ha) eq Dumper($hb)) ? 1 : 0;
#}
package Pod::Simple::XHTML::LocalPodLinks;
use strict;
use warnings;
Expand Down

0 comments on commit 25ea7ee

Please sign in to comment.