| 1 | package Web::PerlDistSite::MenuItem::Pod; | ||
| 2 | |||
| 3 | our $VERSION = '0.001008'; | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | use Web::PerlDistSite::Common -lexical, -all; | ||
| 7 | |||
| 8 | use Pod::Find qw( pod_where ); | ||
| 9 | |||
| 10 | extends 'Web::PerlDistSite::MenuItem'; | ||
| 11 | with 'Web::PerlDistSite::MenuItem::_PodCommon'; | ||
| 12 | |||
| 13 | has '+title' => ( | ||
| 14 | is => 'lazy', | ||
| 15 | default => sub ( $s ) { | ||
| 16 | $s->pod; | ||
| 17 | }, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has '+name' => ( | ||
| 21 | is => 'lazy', | ||
| 22 | default => sub ( $s ) { | ||
| 23 | $s->pod =~ s{::}{-}gr; | ||
| 24 | }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has pod => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => Str, | ||
| 30 | ); | ||
| 31 | |||
| 32 | has raw_content => ( | ||
| 33 | is => 'lazy', | ||
| 34 | isa => Str, | ||
| 35 | builder => true, | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub body_class { | ||
| 39 | return 'page from-pod'; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _build_raw_content ( $self ) { | ||
| 43 | |||
| 44 | my $local = pod_where( { -inc => true }, $self->pod ); | ||
| 45 | return path( $local )->slurp_utf8 if $local; | ||
| 46 | |||
| 47 | require HTTP::Tiny; | ||
| 48 | state $ua = HTTP::Tiny->new; | ||
| 49 | my $response = $ua->get( sprintf( | ||
| 50 | 'https://fastapi.metacpan.org/v1/pod/%s?content-type=text/x-pod', | ||
| 51 | $self->pod, | ||
| 52 | ) ); | ||
| 53 | return $response->{content} if $response->{success}; | ||
| 54 | |||
| 55 | die sprintf( "pod not found: %s\n", $self->pod ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub write_page ( $self ) { | ||
| 59 | $self->system_path->parent->mkpath; | ||
| 60 | $self->system_path->spew_if_changed( $self->compile_page ); | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; |