| 1 | ## | ||||||||||||||
| 2 | # name: Stardoc::Module::Perl | ||||||||||||||
| 3 | # abstract: Stardoc Perl Module | ||||||||||||||
| 4 | # author: Ingy döt Net <ingy@cpan.org> | ||||||||||||||
| 5 | # copyright: 2011 | ||||||||||||||
| 6 | # license: perl | ||||||||||||||
| 7 | |||||||||||||||
| 8 | package Stardoc::Module::Perl; | ||||||||||||||
| 9 | 1 | ▶ | use Mouse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | extends 'Stardoc::Module'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use IO::All; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use YAML::XS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | has meta => ( | ||||||||||||||
| 16 | is => 'ro', | ||||||||||||||
| 17 | 2 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | { | ||||||||||||||
| 19 | 2 | markup => 'pod', | |||||||||||||
| 20 | encoding => 'utf8', | ||||||||||||||
| 21 | } | ||||||||||||||
| 22 | }, | ||||||||||||||
| 23 | 1 | ); | |||||||||||||
| 24 | has other => ( | ||||||||||||||
| 25 | is => 'ro', | ||||||||||||||
| 26 | 2 | ▶ | default => sub {[]}, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 1 | ); | |||||||||||||
| 28 | 1 | has name => ( | |||||||||||||
| 29 | is => 'rw', | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | 1 | has synopsis => ( | |||||||||||||
| 32 | is => 'rw', | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | 1 | has description => ( | |||||||||||||
| 35 | is => 'rw', | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | 1 | has usage => ( | |||||||||||||
| 38 | is => 'rw', | ||||||||||||||
| 39 | ); | ||||||||||||||
| 40 | 1 | has see => ( | |||||||||||||
| 41 | is => 'rw', | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | 1 | has author => ( | |||||||||||||
| 44 | is => 'rw', | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | 1 | has copyright => ( | |||||||||||||
| 47 | is => 'rw', | ||||||||||||||
| 48 | ); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 2 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 51 | 2 | my ($self) = @_; | |||||||||||||
| 52 | 2 | $self->parse(); | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 1 | my $pod_re = qr/^=\w+.*?(?:^=cut\s*\n|(?=^=\w)|\z)/ms; | |||||||||||||
| 56 | # XXX - Need to account for perlcritic ## at some point. | ||||||||||||||
| 57 | 1 | my $star_re = qr/^##\s.*\n(?:#.*\n)*/m; | |||||||||||||
| 58 | 1 | my $end_re = qr/^__(?:END|DATA)__.*/ms; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 2 | ▶ | sub parse { | ||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 61 | 2 | my ($self) = @_; | |||||||||||||
| 62 | 2 | my $text = io($self->file)->all; | |||||||||||||
| 63 | 2 | my $sections = $self->sections; | |||||||||||||
| 64 | 2 | for ($text =~ /($star_re|$pod_re|$end_re)/g) { | |||||||||||||
| 65 | 8 | push @$sections, $self->make_sections($_) | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 2 | ▶ | sub merge_meta { | ||||||||||||
|
Subroutine
merge_meta: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 70 | 2 | my ($self, $data) = @_; | |||||||||||||
| 71 | 2 | my $meta = $self->meta; | |||||||||||||
| 72 | 2 | for my $key (keys %$data) { | |||||||||||||
| 73 | 11 | my $val = $data->{$key}; | |||||||||||||
| 74 | 11 | ▶ | if ($key eq 'author') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 75 | 2 | $meta->{author} = $self->parse_author($val); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | elsif ($key eq 'see') { | ||||||||||||||
| 78 | 1 | ▶ | $meta->{see} = ref($val) ? $val : [$val]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | else { | ||||||||||||||
| 81 | 8 | $meta->{$key} = $data->{$key}; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 2 | ▶ | sub parse_author { | ||||||||||||
|
Subroutine
parse_author: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||
| 87 | 2 | my ($self, $val) = @_; | |||||||||||||
| 88 | 2 | ▶ | return [$val] if ref $val eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 2 | ▶ | my $list = [ (ref $val eq 'ARRAY') ? @$val : $val ]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 2 | ▶ | for (my $i = 0; $i < @$list; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 2 | ▶ | if (not ref $list->[$i]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 2 | my $string = $list->[$i]; | |||||||||||||
| 93 | 2 | my $hash = $list->[$i] = {}; | |||||||||||||
| 94 | 2 | ▶ | if ($string =~ /^\s*(.*?)\s*<(.*)>$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 2 | $hash->{name} = $1; | |||||||||||||
| 96 | 2 | $hash->{email} = $2; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | else { | ||||||||||||||
| 99 | 0 | $hash->{name} = $val; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | 2 | return $list; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | 8 | ▶ | sub make_sections { | ||||||||||||
|
Subroutine
make_sections: called CC=4 SCAR=16.9
Pod
undocumented
| |||||||||||||||
| 108 | 8 | my ($self, $text) = @_; | |||||||||||||
| 109 | 8 | $text =~ s/\s\z/\n/; | |||||||||||||
| 110 | 8 | ▶ | if ($text =~ /^##\s/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 2 | return $self->make_comment_sections($text); | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | 6 | ▶ | if ($text =~ /^=\w/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 6 | $text =~ s/^\s*\n=cut\s*\n/\n/m; | |||||||||||||
| 115 | 6 | return $self->make_pod_sections($text); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | 0 | ▶ | if ($text =~ s/^__(END|DATA)__\s\n//) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | $self->merge_meta; | |||||||||||||
| 119 | return map { | ||||||||||||||
| 120 | 0 | $self->make_sections($_) | |||||||||||||
| 121 | } ($text =~ /($star_re|$pod_re)/g); | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | 0 | die $text; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 2 | ▶ | sub make_comment_sections { | ||||||||||||
|
Subroutine
make_comment_sections: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||||||
| 127 | 2 | my ($self, $text) = @_; | |||||||||||||
| 128 | 2 | $text =~ s/^##.*\n//; | |||||||||||||
| 129 | 2 | $text =~ s/^# ?//gm; | |||||||||||||
| 130 | 2 | my @sections; | |||||||||||||
| 131 | 2 | ▶ | while ($text) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 2 | $text =~ s/^\s*//; | |||||||||||||
| 133 | 2 | ▶ | if ($text =~ s/(^\w+:\s.*?\n)(\.\.\.\s*\n|\n\s*|\z)//s) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 134 | 2 | push @sections, $self->make_meta_data($1); | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | elsif ($text =~ s/^(=\w.*)//s) { | ||||||||||||||
| 137 | 0 | push @sections, $self->make_pod_sections($1); | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | else { | ||||||||||||||
| 140 | 0 | die; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | 2 | return @sections; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 1 | my $name_re = qr/\S+\s+(NAME|DESCRIPTION|SYNOPSIS)/; | |||||||||||||
| 147 | 6 | ▶ | sub make_pod_sections { | ||||||||||||
|
Subroutine
make_pod_sections: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 148 | 6 | my ($self, $text) = @_; | |||||||||||||
| 149 | 6 | my $other = $self->other; | |||||||||||||
| 150 | 6 | for my $section ($text =~ /(=\w.*?\n)\s*(?=^=\w|\z)/gms) { | |||||||||||||
| 151 | 6 | my $hash = { | |||||||||||||
| 152 | type => 'pod', | ||||||||||||||
| 153 | text => $section, | ||||||||||||||
| 154 | }; | ||||||||||||||
| 155 | 6 | ▶ | if ($section =~ $name_re) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 4 | my $name = lc($1); | |||||||||||||
| 157 | 4 | $self->$name($hash); | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | else { | ||||||||||||||
| 160 | 2 | push @$other, $hash; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | } | ||||||||||||||
| 163 | 6 | return (); | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | 2 | ▶ | sub make_meta_data { | ||||||||||||
|
Subroutine
make_meta_data: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||
| 167 | 2 | my ($self, $text) = @_; | |||||||||||||
| 168 | 2 | my $data; | |||||||||||||
| 169 | 2 | eval { $data = Load($text) }; | |||||||||||||
| 170 | 2 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | warn "Invalid YAML in Stardoc. Skipping section.\n$@"; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | 2 | $self->merge_meta($data); | |||||||||||||
| 174 | 2 | return; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | 1 | 1; | |||||||||||||