/usr/share/perl5/MooseX/App/Utils.pm is in libmoosex-app-perl 1.22-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 | package MooseX::App::Utils;
use 5.010;
use utf8;
use strict;
use warnings;
no if $] >= 5.018000, warnings => qw/ experimental::smartmatch /;
use List::Util qw(max);
our $SCREEN_WIDTH = 78;
our $INDENT = 4;
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));
}
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
);
}
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;
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);
}
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;
}
sub parse_pod {
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;
# 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);
$content =~ s/^$package(\s-)?\s//;
chomp($content);
$pod{NAME} = $content;
} else {
my $content = _pod_node_to_text($element->children);
chomp($content);
$pod{uc($element->content)} = $content;
}
}
}
return %pod;
}
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;
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;
|