Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from CPAN-API/oalders/pod-parser
Make Pod parsing easier to test
- Loading branch information
Showing
5 changed files
with
179 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package MetaCPAN::Pod::Renderer; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Moose; | ||
|
||
use MetaCPAN::Pod::XHTML; | ||
use MetaCPAN::Types qw( Uri ); | ||
use Pod::Markdown; | ||
use Pod::POM; | ||
use Pod::POM::View::Pod; | ||
use Pod::Text; | ||
|
||
has perldoc_url_prefix => ( | ||
is => 'rw', | ||
isa => Uri, | ||
coerce => 1, | ||
default => 'https://metacpan.org/pod/', | ||
); | ||
|
||
sub markdown_renderer { | ||
my $self = shift; | ||
return Pod::Markdown->new( | ||
perldoc_url_prefix => $self->perldoc_url_prefix ); | ||
} | ||
|
||
sub pod_renderer { | ||
my $self = shift; | ||
return Pod::POM->new; | ||
} | ||
|
||
sub text_renderer { | ||
my $self = shift; | ||
return Pod::Text->new( sentence => 0, width => 78 ); | ||
} | ||
|
||
sub html_renderer { | ||
my $self = shift; | ||
|
||
my $parser = MetaCPAN::Pod::XHTML->new; | ||
|
||
$parser->html_footer(''); | ||
$parser->html_header(''); | ||
$parser->index(1); | ||
$parser->no_errata_section(1); | ||
$parser->perldoc_url_prefix( $self->perldoc_url_prefix ); | ||
|
||
return $parser; | ||
} | ||
|
||
sub to_markdown { | ||
my $self = shift; | ||
my $source = shift; | ||
|
||
return $self->_generic_render( $self->markdown_renderer, $source ); | ||
} | ||
|
||
sub to_text { | ||
my $self = shift; | ||
my $source = shift; | ||
|
||
return $self->_generic_render( $self->text_renderer, $source ); | ||
} | ||
|
||
sub to_html { | ||
my $self = shift; | ||
my $source = shift; | ||
|
||
return $self->_generic_render( $self->html_renderer, $source ); | ||
} | ||
|
||
sub to_pod { | ||
my $self = shift; | ||
my $source = shift; | ||
|
||
my $renderer = $self->pod_renderer; | ||
my $pom = $renderer->parse_text($source); | ||
return Pod::POM::View::Pod->print($pom); | ||
} | ||
|
||
sub _generic_render { | ||
my $self = shift; | ||
my $renderer = shift; | ||
my $source = shift; | ||
my $output = q{}; | ||
|
||
$renderer->output_string( \$output ); | ||
$renderer->parse_string_document($source); | ||
|
||
return $output; | ||
} | ||
|
||
__PACKAGE__->meta->make_immutable(); | ||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
|
||
use MetaCPAN::Pod::Renderer; | ||
|
||
my $factory = MetaCPAN::Pod::Renderer->new(); | ||
my $html_renderer = $factory->html_renderer; | ||
$html_renderer->index(0); | ||
|
||
my $got = q{}; | ||
|
||
my $source = <<'EOF'; | ||
=pod | ||
=head1 DESCRIPTION | ||
L<Plack> | ||
=cut | ||
EOF | ||
|
||
{ | ||
my $html = <<'EOF'; | ||
<h1 id="DESCRIPTION-Plack">DESCRIPTION <a href="https://metacpan.org/pod/Plack">Plack</a></h1> | ||
EOF | ||
|
||
$html_renderer->output_string( \$got ); | ||
$html_renderer->parse_string_document($source); | ||
is( $got, $html, 'XHTML linkifies to metacpan by default' ); | ||
} | ||
|
||
{ | ||
my $md = <<'EOF'; | ||
# DESCRIPTION | ||
[Plack](https://metacpan.org/pod/Plack) | ||
EOF | ||
|
||
is( $factory->to_markdown($source), $md, 'markdown' ); | ||
} | ||
|
||
{ | ||
my $text = <<'EOF'; | ||
DESCRIPTION | ||
Plack | ||
EOF | ||
|
||
is( $factory->to_text($source), $text, 'text' ); | ||
} | ||
|
||
{ | ||
my $pod = <<'EOF'; | ||
=head1 DESCRIPTION | ||
L<Plack> | ||
=cut | ||
EOF | ||
|
||
is( $factory->to_pod($source), $pod, 'pod' ); | ||
} | ||
done_testing(); |