| 1 | ######################################################################## | ||
| 2 | package DarkPAN::Utils::Docs; | ||
| 3 | ######################################################################## | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use English qw(-no_match_vars); | ||
| 9 | use Data::Dumper; | ||
| 10 | use IO::Scalar; | ||
| 11 | use Pod::Extract qw(extract_pod); | ||
| 12 | use Scalar::Util qw(openhandle); | ||
| 13 | use Text::Markdown::Discount qw(markdown); | ||
| 14 | |||
| 15 | use Readonly; | ||
| 16 | |||
| 17 | Readonly::Scalar our $TRUE => 1; | ||
| 18 | Readonly::Scalar our $FALSE => 0; | ||
| 19 | |||
| 20 | our %ATTRIBUTES = ( | ||
| 21 | text => $TRUE, # required | ||
| 22 | pod => $FALSE, | ||
| 23 | code => $FALSE, | ||
| 24 | sections => $FALSE, | ||
| 25 | markdown => $FALSE, | ||
| 26 | html => $FALSE, | ||
| 27 | url_prefix => $FALSE, | ||
| 28 | ); | ||
| 29 | |||
| 30 | __PACKAGE__->setup_accessors( keys %ATTRIBUTES ); | ||
| 31 | |||
| 32 | use parent qw(Class::Accessor::Validated); | ||
| 33 | |||
| 34 | our $VERSION = '1.1.0'; | ||
| 35 | |||
| 36 | ######################################################################## | ||
| 37 | sub new { | ||
| 38 | ######################################################################## | ||
| 39 | my ( $class, @args ) = @_; | ||
| 40 | |||
| 41 | my $self = $class->SUPER::new(@args); | ||
| 42 | |||
| 43 | # text is required so we will alway have text | ||
| 44 | my $text = $self->get_text; | ||
| 45 | |||
| 46 | if ( ref $text && openhandle $text ) { | ||
| 47 | my $fh = $text; | ||
| 48 | |||
| 49 | local $RS = undef; | ||
| 50 | $text = <$fh>; | ||
| 51 | close $fh; | ||
| 52 | } | ||
| 53 | elsif ( ref $text ) { | ||
| 54 | $text = ${$text}; | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->set_text($text); | ||
| 58 | |||
| 59 | return $self->parse_pod(); | ||
| 60 | } | ||
| 61 | |||
| 62 | ######################################################################## | ||
| 63 | sub parse_pod { | ||
| 64 | ######################################################################## | ||
| 65 | my ($self) = @_; | ||
| 66 | |||
| 67 | my $text = $self->get_text; | ||
| 68 | |||
| 69 | my $fh = IO::Scalar->new( \$text ); | ||
| 70 | |||
| 71 | my @result = extract_pod( $fh, { markdown => $TRUE, url_prefix => $self->get_url_prefix } ); | ||
| 72 | |||
| 73 | close $fh; | ||
| 74 | |||
| 75 | foreach my $attr (qw(pod code sections markdown)) { | ||
| 76 | my $setter = "set_$attr"; | ||
| 77 | |||
| 78 | $self->$setter( shift @result ); | ||
| 79 | } | ||
| 80 | |||
| 81 | if ( $self->get_pod ) { | ||
| 82 | $self->set_html( Text::Markdown::Discount::markdown( $self->get_markdown ) ); | ||
| 83 | } | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | ######################################################################## | ||
| 89 | sub to_html { | ||
| 90 | ######################################################################## | ||
| 91 | my ( $self, $markdown ) = @_; | ||
| 92 | |||
| 93 | $markdown //= $self->get_markdown; | ||
| 94 | $markdown =~ s/^\s+$//xsm; | ||
| 95 | |||
| 96 | if ( !$markdown ) { | ||
| 97 | $markdown = $self->get_text; | ||
| 98 | } | ||
| 99 | |||
| 100 | return | ||
| 101 | if !$markdown; | ||
| 102 | |||
| 103 | $self->set_html( Text::Markdown::Discount::markdown($markdown) ); | ||
| 104 | |||
| 105 | return $self->get_html; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |