/usr/share/perl5/MooseX/App/Utils.pm is in libmoosex-app-perl 1.37-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | package MooseX::App::Utils;
use 5.010;
use utf8;
use strict;
use warnings;
use List::Util qw(max);
our $SCREEN_WIDTH = 78;
our $INDENT = 4;
use Moose::Util::TypeConstraints;
subtype 'MooseX::App::Types::List'
=> as 'ArrayRef';
coerce 'MooseX::App::Types::List'
=> from 'Str'
=> via { [$_] };
subtype 'MooseX::App::Types::CmdTypes'
=> as enum([qw(proto option parameter)]);
subtype 'MooseX::App::Types::MessageString'
=> as 'Str';
coerce 'MooseX::App::Types::MessageString'
=> from 'ArrayRef'
=> via { sprintf(@{$_}) };
subtype 'MooseX::App::Types::Env'
=> as 'Str'
=> where { m/^[A-Z0-9_]+$/ };
subtype 'MooseX::App::Types::Identifier'
=> as 'Str'
=> where {
$_ eq '?'
|| (m/^[A-Za-z0-9][A-Za-z0-9_-]*$/ && m/[^-_]$/) };
subtype 'MooseX::App::Types::IdentifierList'
=> as 'ArrayRef[MooseX::App::Types::Identifier]';
coerce 'MooseX::App::Types::IdentifierList'
=> from 'MooseX::App::Types::Identifier'
=> via { [$_] };
no Moose::Util::TypeConstraints;
no if $] >= 5.018000, warnings => qw/ experimental::smartmatch /;
# Default package name to command name translation function
sub class_to_command {
my ($class) = @_;
return
unless defined $class;
my @parts = split( /[_\s]+|\b|(?<![A-Z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/, $class );
return lc(join('_',@parts));
}
# Format output text for fixed screen width
sub format_text {
my ($text) = @_;
my @lines;
foreach my $line (split(/\n/,$text)) {
push(@lines,split_string($SCREEN_WIDTH-$INDENT,$line));
}
return join(
"\n",
map { (' ' x $INDENT).$_ }
@lines
);
}
# Format bullet list for fixed screen width
sub format_list {
my (@list) = @_;
my $max_length = max(map { length($_->[0]) } @list);
my $description_length = $SCREEN_WIDTH - $max_length - 7;
my $prefix_length = $max_length + $INDENT + 2;
my @return;
# Loop all items
foreach my $command (@list) {
my $description = $command->[1] // '';
my @lines = split_string($description_length,$description);
push (@return,(' 'x$INDENT).sprintf('%-*s %s',$max_length,$command->[0],shift(@lines)));
while (my $line = shift (@lines)) {
push(@return,' 'x $prefix_length.$line);
}
}
return join("\n",@return);
}
# Simple splitting of long sentences on whitespaces or punctuation
sub split_string {
my ($maxlength, $string) = @_;
return
unless defined $string;
return $string
if length $string <= $maxlength;
my (@lines,$line);
$line = '';
foreach my $word (split(m/(\p{IsPunct}|\p{IsSpace})/,$string)) {
if (length($line.$word) <= $maxlength) {
$line .= $word;
} else {
push(@lines,$line)
if ($line ne '');
$line = '';
if (length($word) > $maxlength) {
my (@parts) = grep { $_ ne '' } split(/(.{$maxlength})/,$word);
my $lastline = pop(@parts);
push(@lines,@parts);
if (defined $lastline && $lastline ne '') {
$line = $lastline;
}
} else {
$line = $word;
}
}
}
push(@lines,$line)
if ($line ne '');
@lines = map { m/^\s*(.+?)\s*$/ ? $1 : $_ } @lines;
return @lines;
}
# Try to get filename for a given package name
sub package_to_filename {
my ($package) = @_;
# Package to filename
my $package_filename = $package;
$package_filename =~ s/::/\//g;
$package_filename .= '.pm';
my $package_filepath;
if (defined $INC{$package_filename}) {
$package_filepath = $INC{$package_filename};
$package_filepath =~ s/\/{2,}/\//g;
}
# No filename available
return
unless defined $package_filepath
&& -e $package_filepath;
return $package_filepath;
}
# Parse pod
sub parse_pod {
my ($package) = @_;
my $package_filepath = package_to_filename($package);
return
unless $package_filepath;
# Parse pod
my $document = Pod::Elemental->read_file($package_filepath);
Pod::Elemental::Transformer::Pod5->new->transform_node($document);
my $nester_head = Pod::Elemental::Transformer::Nester->new({
top_selector => Pod::Elemental::Selectors::s_command('head1'),
content_selectors => [
Pod::Elemental::Selectors::s_command([ qw(head2 head3 head4 over back item) ]),
Pod::Elemental::Selectors::s_flat()
],
});
$document = $nester_head->transform_node($document);
# Process pod
my %pod;
foreach my $element (@{$document->children}) {
# Distzilla ABSTRACT tag
if ($element->isa('Pod::Elemental::Element::Pod5::Nonpod')) {
if ($element->content =~ m/^\s*#+\s*ABSTRACT:\s*(.+)$/m) {
$pod{ABSTRACT} ||= $1;
}
# Pod head1 sections
} elsif ($element->isa('Pod::Elemental::Element::Nested')
&& $element->command eq 'head1') {
if ($element->content eq 'NAME') {
my $content = _pod_node_to_text($element->children);
next unless defined $content;
$content =~ s/^$package(\s-)?\s//;
chomp($content);
$pod{NAME} = $content;
} else {
my $content = _pod_node_to_text($element->children);
next unless defined $content;
chomp($content);
$pod{uc($element->content)} = $content;
}
}
}
return %pod;
}
# Transform POD to simple markup
sub _pod_node_to_text {
my ($node,$indent) = @_;
unless (defined $indent) {
my $indent_init = 0;
$indent = \$indent_init;
}
my (@lines);
if (ref $node eq 'ARRAY') {
foreach my $element (@$node) {
push (@lines,_pod_node_to_text($element,$indent));
}
} else {
given (ref($node)) {
when ('Pod::Elemental::Element::Pod5::Ordinary') {
my $content = $node->content;
return
if $content =~ m/^=cut/;
$content =~ s/\n/ /g;
$content =~ s/\s+/ /g;
push (@lines,$content."\n");
}
when ('Pod::Elemental::Element::Pod5::Verbatim') {
push (@lines,$node->content."\n");
}
when ('Pod::Elemental::Element::Pod5::Command') {
given ($node->command) {
when ('over') {
${$indent}++;
}
when ('item') {
push (@lines,(' ' x ($$indent-1)) . $node->content);
}
when ('back') {
push (@lines,"\n");
${$indent}--;
}
}
}
}
}
return
unless scalar @lines;
# Convert text markup
my $return = join ("\n", grep { defined $_ } @lines);
$return =~ s/\n\n\n+/\n\n/g; # Max one empty line
$return =~ s/I<([^>]+)>/_$1_/g;
$return =~ s/B<([^>]+)>/*$1*/g;
$return =~ s/[LCBI]<([^>]+)>/$1/g;
$return =~ s/[LCBI]<([^>]+)>/$1/g;
return $return;
}
1;
=pod
=head1 NAME
MooseX::App::Utils - Utility functions
=head1 DESCRIPTION
This package holds various utility functions used by MooseX-App internally.
Unless you develop plugins you will not need to interact with this class.
=head1 FUNCTIONS
=head2 class_to_command
=head2 package_to_filename
Tries to determine the filename containing the given package name.
=head2 format_list
=head2 format_text
=head2 parse_pod
=head2 split_string
=head1 GLOBALS
=head2 $MooseX::App::Utils::SCREEN_WIDTH
Screen width for printing help and error messages
=head2 $MooseX::App::Utils::INDENT
Indent for printing help and error messages
=cut
|