| 1 | package Pod::Weaver::Plugin::Regexp::Pattern; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | with 'Pod::Weaver::Role::AddTextToSection'; | |||||||||||||||||||||||||||
| 6 | 1 | with 'Pod::Weaver::Role::Section'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #has include_module => (is=>'rw'); | ||||||||||||||||||||||||||||
| 9 | #has exclude_module => (is=>'rw'); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Dmp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 14 | 1 | our $DATE = '2022-05-08'; # DATE | |||||||||||||||||||||||||||
| 15 | 1 | our $DIST = 'Pod-Weaver-Plugin-Regexp-Pattern'; # DIST | |||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.011'; # VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _md2pod { | ||||||||||||||||||||||||||
|
Subroutine
_md2pod: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 0 | require Markdown::To::POD; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | my ($self, $md) = @_; | |||||||||||||||||||||||||||
| 22 | 0 | my $pod = Markdown::To::POD::markdown_to_pod($md); | |||||||||||||||||||||||||||
| 23 | # make sure we add a couple of blank lines in the end | ||||||||||||||||||||||||||||
| 24 | 0 | $pod =~ s/\s+\z//s; | |||||||||||||||||||||||||||
| 25 | 0 | $pod . "\n\n\n"; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub _process_module { | ||||||||||||||||||||||||||
|
Subroutine
_process_module: not called CC=27 SCAR=65.7
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | my ($self, $document, $input, $package) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $filename = $input->{filename}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # XXX handle dynamically generated module (if there is such thing in the | ||||||||||||||||||||||||||||
| 36 | # future) | ||||||||||||||||||||||||||||
| 37 | 0 | local @INC = ("lib", @INC); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 0 | my $package_pm = $package; | |||||||||||||||||||||||||||
| 41 | 0 | $package_pm =~ s!::!/!g; | |||||||||||||||||||||||||||
| 42 | 0 | $package_pm .= ".pm"; | |||||||||||||||||||||||||||
| 43 | 0 | require $package_pm; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | (my $rp_package = $package) =~ s/\ARegexp::Pattern:://; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $var = \%{"$package\::RE"}; | |||||||||||||||||||||||||||
| 49 | 0 | my @patnames = sort keys %$var; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # add Synopsis section | ||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 0 | my @pod; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | last unless @patnames; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | push @pod, "Using with L<Regexp::Pattern>:\n"; | |||||||||||||||||||||||||||
| 56 | 0 | push @pod, " \n"; | |||||||||||||||||||||||||||
| 57 | 0 | push @pod, " use Regexp::Pattern; # exports re()\n"; | |||||||||||||||||||||||||||
| 58 | 0 | push @pod, " my \$re = re(", dmp("$rp_package\::$patnames[0]"), ");\n"; | |||||||||||||||||||||||||||
| 59 | 0 | push @pod, " \n"; | |||||||||||||||||||||||||||
| 60 | 0 | push @pod, " # see Regexp::Pattern for more details on how to use with Regexp::Pattern\n"; | |||||||||||||||||||||||||||
| 61 | 0 | push @pod, " \n"; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($patnames[0]{pat} && !$patnames[0]{gen}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$patnames[0]{"pat"} and not $patnames[0]{"gen"}
MC/DC0%0/2
$patnames[0]{"pat"} and not $patnames[0]{"gen"}
$patnames[0]{"pat"}
not $patnames[0]{"gen"}
Truth table0%0/3
A and not(B)
A= $patnames[0]{"pat"}B= not $patnames[0]{"gen"}
| |||||||||||||||||||||||||||||
| 64 | 0 | push @pod, "Using the pattern(s) directly:\n"; | |||||||||||||||||||||||||||
| 65 | 0 | push @pod, " \n"; | |||||||||||||||||||||||||||
| 66 | 0 | push @pod, " use $package;\n"; | |||||||||||||||||||||||||||
| 67 | 0 | push @pod, " if ('some string' =~ \$$package\::RE{$patnames[0]}{pat}) { ... }\n"; | |||||||||||||||||||||||||||
| 68 | 0 | push @pod, "\n"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $self->add_text_to_section( | ||||||||||||||||||||||||||||
| 72 | 0 | $document, join("", @pod), 'SYNOPSIS', | |||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | after_section => ['VERSION', 'NAME'], | ||||||||||||||||||||||||||||
| 75 | before_section => 'DESCRIPTION', | ||||||||||||||||||||||||||||
| 76 | ignore => 1, | ||||||||||||||||||||||||||||
| 77 | }); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # add Description section | ||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | my @pod; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # blurb about Regexp::Pattern | ||||||||||||||||||||||||||||
| 85 | 0 | push @pod, "L<Regexp::Pattern> is a convention for organizing reusable regex patterns.\n\n"; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | $self->add_text_to_section( | |||||||||||||||||||||||||||
| 88 | $document, join("", @pod), 'DESCRIPTION', | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | after_section => ['SYNOPSIS'], | ||||||||||||||||||||||||||||
| 91 | ignore => 1, | ||||||||||||||||||||||||||||
| 92 | }); | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # add Patterns section | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 0 | my @pod; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | push @pod, "=over\n\n"; | |||||||||||||||||||||||||||
| 100 | 0 | for my $patname (@patnames) { | |||||||||||||||||||||||||||
| 101 | 0 | my $patspec = $var->{$patname}; | |||||||||||||||||||||||||||
| 102 | 0 | push @pod, "=item * $patname\n\n"; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | if (defined $patspec->{tags}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | push @pod, "Tags: ".(join ", ", @{ $patspec->{tags} })."\n\n"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (defined $patspec->{summary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | require String::PodQuote; | |||||||||||||||||||||||||||
| 108 | 0 | push @pod, String::PodQuote::pod_quote($patspec->{summary}), ".\n\n"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | push @pod, $self->_md2pod($patspec->{description}) | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if $patspec->{description}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($patspec->{gen}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | push @pod, "This is a dynamic pattern which will be generated on-demand.\n\n"; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($patspec->{gen_args} && keys(%{$patspec->{gen_args}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$patspec->{"gen_args"} and keys %{$$patspec{"gen_args"};}
MC/DC0%0/2
$patspec->{"gen_args"} and keys %{$$patspec{"gen_args"};}
$patspec->{"gen_args"}
keys %{$$patspec{"gen_args"};}
Truth table0%0/3
A and B
A= $patspec->{"gen_args"}B= keys %{$$patspec{"gen_args"};}
| |||||||||||||||||||||||||||||
| 116 | 0 | push @pod, "The following arguments are available to customize the generated pattern:\n\n"; | |||||||||||||||||||||||||||
| 117 | 0 | push @pod, "=over\n\n"; | |||||||||||||||||||||||||||
| 118 | 0 | for my $argname (sort keys %{ $patspec->{gen_args} }) { # XXX sort by position, then name | |||||||||||||||||||||||||||
| 119 | 0 | my $argspec = $patspec->{gen_args}{$argname}; | |||||||||||||||||||||||||||
| 120 | 0 | push @pod, "=item * $argname\n\n"; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (defined $argspec->{summary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | require String::PodQuote; | |||||||||||||||||||||||||||
| 123 | 0 | push @pod, String::PodQuote::pod_quote($argspec->{summary}), ".\n\n"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | push @pod, $self->_md2pod($argspec->{description}) | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if $argspec->{description}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | push @pod, "=back\n\n"; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | push @pod, "\n\n"; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | RENDER_EXAMPLES: | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | last unless $patspec->{examples}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | my @eg; | |||||||||||||||||||||||||||
| 137 | 0 | for my $eg (@{ $patspec->{examples} }) { | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | next unless $eg->{doc} // 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$eg->{"doc"} // 1
MC/DC0%0/1
$eg->{"doc"} // 1
$eg->{"doc"}
Truth table0%0/2
A
A= $eg->{"doc"}
| |||||||||||||||||||||||||||||
| 139 | 0 | push @eg, $eg; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | last unless @eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | push @pod, "Examples:\n\n"; | |||||||||||||||||||||||||||
| 143 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 144 | 0 | for my $eg (@eg) { | |||||||||||||||||||||||||||
| 145 | 0 | $i++; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $summary = $eg->{summary} // "Example #$i"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$eg->{"summary"} // "Example #$i"
MC/DC0%0/1
$eg->{"summary"} // "Example #$i"
$eg->{"summary"}
Truth table0%0/2
A
A= $eg->{"summary"}
| |||||||||||||||||||||||||||||
| 147 | 0 | require String::PodQuote; | |||||||||||||||||||||||||||
| 148 | 0 | push @pod, String::PodQuote::pod_quote($summary), ".\n\n"; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | push @pod, " ", dmp($eg->{str}), " =~ re(", dmp("$rp_package\::$patname"), ($eg->{gen_args} ? ", ".dmp($eg->{gen_args}) : ""), "); "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (ref $eg->{matches} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if (@{ $eg->{matches} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | push @pod, "# matches, ", | ||||||||||||||||||||||||||||
| 154 | join(", ", map { | ||||||||||||||||||||||||||||
| 155 | 0 | "\$".($_+1)."=".dmp($eg->{matches}[$_])} | |||||||||||||||||||||||||||
| 156 | 0 | 0..$#{$eg->{matches}}); | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | push @pod, " # DOESN'T MATCH"; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } elsif (ref $eg->{matches} eq 'HASH') { | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if (keys %{ $eg->{matches} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | push @pod, "# matches, ", | ||||||||||||||||||||||||||||
| 163 | join(", ", map { | ||||||||||||||||||||||||||||
| 164 | 0 | "\$+{" . dmp($_) . "}=" . dmp($eg->{matches}{$_})} | |||||||||||||||||||||||||||
| 165 | 0 | sort keys %{$eg->{matches}}); | |||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||
| 167 | 0 | push @pod, " # DOESN'T MATCH"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if ($eg->{matches}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | push @pod, " # matches"; | |||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 0 | push @pod, " # DOESN'T MATCH"; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 0 | push @pod, "\n\n"; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | push @pod, "=back\n\n"; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $self->add_text_to_section( | |||||||||||||||||||||||||||
| 183 | $document, join("", @pod), 'REGEXP PATTERNS', | ||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | after_section => ['DESCRIPTION'], | ||||||||||||||||||||||||||||
| 186 | }); | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # mention some modules in See Also section | ||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||
| 191 | 0 | my @pod = ( | |||||||||||||||||||||||||||
| 192 | "L<Regexp::Pattern>\n\n", | ||||||||||||||||||||||||||||
| 193 | "Some utilities related to Regexp::Pattern: L<App::RegexpPatternUtils>, L<rpgrep> from L<App::rpgrep>.\n\n", | ||||||||||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||||||||||
| 195 | 0 | $self->add_text_to_section( | |||||||||||||||||||||||||||
| 196 | $document, join('', @pod), 'SEE ALSO', | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | $self->log(["Generated POD for '%s'", $filename]); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub weave_section { | ||||||||||||||||||||||||||
|
Subroutine
weave_section: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 204 | 0 | my ($self, $document, $input) = @_; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | my $filename = $input->{filename}; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my $package; | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ($filename =~ m!^lib/(Regexp/Pattern/.+)\.pm$!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $package = $1; | |||||||||||||||||||||||||||
| 211 | 0 | $package =~ s!/!::!g; | |||||||||||||||||||||||||||
| 212 | #if ($self->include_module && @{ $self->include_module }) { | ||||||||||||||||||||||||||||
| 213 | # return unless grep {"Regexp::Pattern::$_" eq $package} @{ $self->include_module }; | ||||||||||||||||||||||||||||
| 214 | #} | ||||||||||||||||||||||||||||
| 215 | #if ($self->exclude_module && @{ $self->exclude_module }) { | ||||||||||||||||||||||||||||
| 216 | # return if grep {"Regexp::Pattern::$_" eq $package} @{ $self->exclude_module }; | ||||||||||||||||||||||||||||
| 217 | #} | ||||||||||||||||||||||||||||
| 218 | 0 | $self->_process_module($document, $input, $package); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | 1; | |||||||||||||||||||||||||||
| 223 | # ABSTRACT: Plugin to use when building Regexp::Pattern::* distribution | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | __END__ | ||||||||||||||||||||||||||||