| 1 | # | ||||||||||||||||||||
| 2 | # This file is part of Text-CGILike | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # This software is copyright (c) 2014 by celogeek <me@celogeek.com>. | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | package Text::CGILike; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # ABSTRACT: Wrapper to create text file using the CGI syntax | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use Text::Format; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | our $VERSION = '0.6'; # VERSION | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | my $_DEFAULT_CLASS; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | has 'columns' => ( | ||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||
| 25 | 0 | ▶ | default => sub {80}, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 26 | 1 | ); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub DEFAULT_CLASS { | ||||||||||||||||||
|
Subroutine
DEFAULT_CLASS: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 29 | 0 | return _self_or_default(shift); | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub start_html { | ||||||||||||||||||
|
Subroutine
start_html: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 33 | 0 | my ( $self, @texts ) = _self_or_default(@_); | |||||||||||||||||||
| 34 | 0 | my $text; | |||||||||||||||||||
| 35 | 0 | ▶ | if ( @texts > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | my %p = @texts; | |||||||||||||||||||
| 37 | 0 | ▶ | $text = $p{-title} || ""; | ||||||||||||||||||
|
Condition0%0/2
$p{'-title'} || ''
MC/DC0%0/1
$p{'-title'} || ''
$p{'-title'}
Truth table0%0/2
A
A= $p{'-title'}
| |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||
| 40 | 0 | ($text) = @texts; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 0 | $self->{_start_html} = $text; | |||||||||||||||||||
| 43 | 0 | return $self->hr . $self->_center( "# ", " #", $text ) . $self->hr . "\n"; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | sub end_html { | ||||||||||||||||||
|
Subroutine
end_html: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | my ($self) = _self_or_default(@_); | |||||||||||||||||||
| 48 | 0 | ▶ | my $text = $self->{_start_html} || "END"; | ||||||||||||||||||
|
Condition0%0/2
$self->{'_start_html'} || 'END'
MC/DC0%0/1
$self->{'_start_html'} || 'END'
$self->{'_start_html'}
Truth table0%0/2
A
A= $self->{'_start_html'}
| |||||||||||||||||||||
| 49 | 0 | return "\n" . $self->hr . $self->_center( "# ", " #", $text ) . $self->hr; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub meta { | ||||||||||||||||||
|
Subroutine
meta: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | #no meta in text | ||||||||||||||||||||
| 55 | 0 | return ""; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | sub h1 { | ||||||||||||||||||
|
Subroutine
h1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 59 | 0 | my ( $self, $title ) = _self_or_default(@_); | |||||||||||||||||||
| 60 | 0 | return $self->_left( "# ", $title ) . $self->br(); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub hr { | ||||||||||||||||||
|
Subroutine
hr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 64 | 0 | my ($self) = _self_or_default(@_); | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | return "#" x ( $self->columns ) . "\n"; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub br { | ||||||||||||||||||
|
Subroutine
br: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | return "\n"; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | sub center { | ||||||||||||||||||
|
Subroutine
center: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 74 | 0 | my ( $self, $text ) = _self_or_default(@_); | |||||||||||||||||||
| 75 | 0 | return $self->_center( '', '', $text ); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | sub ul { | ||||||||||||||||||
|
Subroutine
ul: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | 0 | my ( $self, @li ) = _self_or_default(@_); | |||||||||||||||||||
| 80 | 0 | return join( "", grep {defined} @li ); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | sub li { | ||||||||||||||||||
|
Subroutine
li: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 84 | 0 | my ( $self, $li ) = _self_or_default(@_); | |||||||||||||||||||
| 85 | 0 | my $TF | |||||||||||||||||||
| 86 | = Text::Format->new( | ||||||||||||||||||||
| 87 | { firstIndent => 0, bodyIndent => 2, columns => $self->columns - 2 } | ||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||
| 89 | 0 | return "- " . $TF->format($li); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | ### PRIVATE ### | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | sub _self_or_default { | ||||||||||||||||||
|
Subroutine
_self_or_default: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 95 | 0 | my ( $may_class, @param ) = @_; | |||||||||||||||||||
| 96 | 0 | ▶ | if ( ref $may_class eq __PACKAGE__ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | return ( $may_class, @param ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||
| 100 | 0 | ▶ | $_DEFAULT_CLASS ||= __PACKAGE__->new; | ||||||||||||||||||
|
Condition0%0/3
$_DEFAULT_CLASS ||= __PACKAGE__->new
MC/DC0%0/2
$_DEFAULT_CLASS ||= __PACKAGE__->new
$_DEFAULT_CLASS
__PACKAGE__->new
Truth table0%0/3
A ||= B
A= $_DEFAULT_CLASSB= __PACKAGE__->new
| |||||||||||||||||||||
| 101 | 0 | return ( $_DEFAULT_CLASS, $may_class, @param ); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | sub _center { | ||||||||||||||||||
|
Subroutine
_center: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 106 | 0 | my ( $self, $left_pad, $right_pad, $text ) = @_; | |||||||||||||||||||
| 107 | 0 | ▶ | $left_pad = "" unless defined $left_pad; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | ▶ | $right_pad = "" unless defined $right_pad; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | my $size = $self->columns - length($left_pad) - length($right_pad); | |||||||||||||||||||
| 111 | 0 | my $TF = Text::Format->new( | |||||||||||||||||||
| 112 | { firstIndent => 0, bodyIndent => 0, columns => $size } ); | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | my @texts = $TF->format($text); | |||||||||||||||||||
| 115 | return join( | ||||||||||||||||||||
| 116 | "", | ||||||||||||||||||||
| 117 | map { | ||||||||||||||||||||
| 118 | 0 | sprintf( $left_pad . "%-" . $size . "s" . $right_pad . "\n", $_ ) | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | map { | ||||||||||||||||||||
| 121 | 0 | do { chomp; $_ } | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 0 | map { $TF->center($_) } @texts | |||||||||||||||||||
| 124 | ); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | sub _left { | ||||||||||||||||||
|
Subroutine
_left: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 129 | 0 | my ( $self, $left_pad, $text ) = @_; | |||||||||||||||||||
| 130 | 0 | ▶ | $left_pad = "" unless defined $left_pad; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | my $size = $self->columns - length($left_pad); | |||||||||||||||||||
| 133 | 0 | my $TF = Text::Format->new( | |||||||||||||||||||
| 134 | { firstIndent => 0, bodyIndent => 0, columns => $size } ); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | my @texts = $TF->format($text); | |||||||||||||||||||
| 137 | 0 | return $left_pad . join( "" . $left_pad, @texts ); | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | sub _expand_arr { | ||||||||||||||||||
|
Subroutine
_expand_arr: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 142 | 0 | my ( $tags, @arr ) = @_; | |||||||||||||||||||
| 143 | 0 | my @res; | |||||||||||||||||||
| 144 | 0 | for my $tag (@arr) { | |||||||||||||||||||
| 145 | 0 | ▶ | if ( substr( $tag, 0, 1 ) eq ':' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | push @res, _expand_arr( $tags, @{ $tags->{$tag} } ); | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||
| 149 | 0 | push @res, $tag; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 0 | return @res; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 1 | my %EXPORT_MAP = ( | |||||||||||||||||||
| 156 | ':html2' => [ | ||||||||||||||||||||
| 157 | 'h1' .. 'h6', qw/p br hr ol ul li dl dt dd menu code var strong em | ||||||||||||||||||||
| 158 | tt u i b blockquote pre img a address cite samp dfn html head | ||||||||||||||||||||
| 159 | base body Link nextid title meta kbd start_html end_html | ||||||||||||||||||||
| 160 | input Select option comment charset escapeHTML/ | ||||||||||||||||||||
| 161 | ], | ||||||||||||||||||||
| 162 | ':html3' => [ | ||||||||||||||||||||
| 163 | qw/div table caption th td TR Tr sup Sub strike applet Param nobr | ||||||||||||||||||||
| 164 | embed basefont style span layer ilayer font frameset frame script small big Area Map/ | ||||||||||||||||||||
| 165 | ], | ||||||||||||||||||||
| 166 | ':html4' => [ | ||||||||||||||||||||
| 167 | qw/abbr acronym bdo col colgroup del fieldset iframe | ||||||||||||||||||||
| 168 | ins label legend noframes noscript object optgroup Q | ||||||||||||||||||||
| 169 | thead tbody tfoot/ | ||||||||||||||||||||
| 170 | ], | ||||||||||||||||||||
| 171 | ':netscape' => [qw/blink fontsize center/], | ||||||||||||||||||||
| 172 | ':form' => [ | ||||||||||||||||||||
| 173 | qw/textfield textarea filefield password_field hidden checkbox checkbox_group | ||||||||||||||||||||
| 174 | submit reset defaults radio_group popup_menu button autoEscape | ||||||||||||||||||||
| 175 | scrolling_list image_button start_form end_form startform endform | ||||||||||||||||||||
| 176 | start_multipart_form end_multipart_form isindex tmpFileName uploadInfo URL_ENCODED MULTIPART/ | ||||||||||||||||||||
| 177 | ], | ||||||||||||||||||||
| 178 | ':cgi' => [ | ||||||||||||||||||||
| 179 | qw/param upload path_info path_translated request_uri url self_url script_name | ||||||||||||||||||||
| 180 | cookie Dump | ||||||||||||||||||||
| 181 | raw_cookie request_method query_string Accept user_agent remote_host content_type | ||||||||||||||||||||
| 182 | remote_addr referer server_name server_software server_port server_protocol virtual_port | ||||||||||||||||||||
| 183 | virtual_host remote_ident auth_type http append | ||||||||||||||||||||
| 184 | save_parameters restore_parameters param_fetch | ||||||||||||||||||||
| 185 | remote_user user_name header redirect import_names put | ||||||||||||||||||||
| 186 | Delete Delete_all url_param cgi_error/ | ||||||||||||||||||||
| 187 | ], | ||||||||||||||||||||
| 188 | ':ssl' => [qw/https/], | ||||||||||||||||||||
| 189 | ':cgi-lib' => [qw/ReadParse PrintHeader HtmlTop HtmlBot SplitParam Vars/], | ||||||||||||||||||||
| 190 | ':html' => [qw/:html2 :html3 :html4 :netscape/], | ||||||||||||||||||||
| 191 | ':standard' => [qw/:html2 :html3 :html4 :form :cgi/], | ||||||||||||||||||||
| 192 | ':push' => | ||||||||||||||||||||
| 193 | [qw/multipart_init multipart_start multipart_end multipart_final/], | ||||||||||||||||||||
| 194 | ':all' => [qw/:html2 :html3 :netscape :form :cgi :internal :html4/] | ||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: not called CC=4 SCAR=26.3
| |||||||||||||||||||||
| 198 | 0 | my ( $self, $to_import_str ) = @_; | |||||||||||||||||||
| 199 | 0 | my @to_import = $to_import_str =~ /(:?\w+)/gx; | |||||||||||||||||||
| 200 | 0 | my $caller = caller; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 0 | for my $sym ( _expand_arr( \%EXPORT_MAP, @to_import ) ) { | |||||||||||||||||||
| 203 | 0 | ▶ | unless ( $caller->can($sym) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 0 | ▶ | my $meth = $self->can($sym) // sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 205 | 0 | carp "Missing tag : ", $sym; | |||||||||||||||||||
| 206 | 0 | return join( '', @_ ); | |||||||||||||||||||
| 207 | 0 | ▶ | }; | ||||||||||||||||||
|
Condition0%0/3
$self->can($sym) // sub {
carp("Missing tag : ", $sym);
return join("", @_);
}
MC/DC0%0/2
$self->can($sym) // sub {
carp("Missing tag : ", $sym);
return join("", @_);
}
$self->can($sym)
sub {
carp("Missing tag : ", $sym);
return join("", @_);
}
Truth table0%0/3
A // B
A= $self->can($sym)B= sub {
carp("Missing tag : ", $sym);
return join("", @_);
}
| |||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | { | ||||||||||||||||||||
| 210 | ## no critic qw(ProhibitNoStrict) | ||||||||||||||||||||
| 211 | 0 | ▶ | no strict qw/refs/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||
| 212 | 0 | *{"${caller}::$sym"} = $meth; | |||||||||||||||||||
| 213 | ## use critic | ||||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 0 | return; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 1 | 1; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||