| 1 | #-*- perl -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Text::LineFold; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | require 5.008; | |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | Text::LineFold - Line Folding for Plain Text | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | use Text::LineFold; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | $lf = Text::LineFold->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # Fold lines | ||||||||||||||||||||||||||||||||||||||||||
| 18 | $folded = $lf->fold($string, 'PLAIN'); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | $indented = $lf->fold(' ' x 8, ' ' x 4, $string); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Unfold lines | ||||||||||||||||||||||||||||||||||||||||||
| 22 | $unfolded = $lf->unfold($string, 'FIXED'); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | Text::LineFold folds or unfolds lines of plain text. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | As it mainly focuses on plain text e-mail messages, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | RFC 3676 flowed format is also supported. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | ### Pragmas: | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use vars qw($VERSION @EXPORT_OK @ISA $Config); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | ### Exporting: | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | ### Inheritance: | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | our @ISA = qw(Exporter Unicode::LineBreak); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | ### Other modules: | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use Encode qw(is_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | use MIME::Charset; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | use Unicode::LineBreak qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | ### Globals | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | ### The package Version | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | our $VERSION = '2016.00702'; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | ### Public Configuration Attributes | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | our $Config = { | |||||||||||||||||||||||||||||||||||||||||
| 55 | ### %{$Unicode::LineBreak::Config}, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | Charset => 'UTF-8', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | Language => 'XX', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | OutputCharset => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | TabSize => 8, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | ### Privates | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | my %FORMAT_FUNCS = ( | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 'FIXED' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 144 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 144 | my $action = shift; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 144 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 144 | ▶ | if ($action =~ /^so[tp]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 30 | $self->{_} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 30 | $self->{_}->{'ColMax'} = $self->config('ColMax'); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 30 | ▶ | $self->config('ColMax' => 0) if $str =~ /^>/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } elsif ($action eq "") { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 48 | $self->{_}->{line} = $str; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } elsif ($action eq "eol") { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 18 | return $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } elsif ($action =~ /^eo/) { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 30 | ▶ | if (length $self->{_}->{line} and $self->config('ColMax')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $self->{"_"}{"line"} and $self->config("ColMax")
MC/DC100%2/2
length $self->{"_"}{"line"} and $self->config("ColMax")
length $self->{"_"}{"line"}
$self->config("ColMax")
Truth table100%3/3
A and B
A= length $self->{"_"}{"line"}B= $self->config("ColMax")
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 15 | $str = $self->config('Newline').$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | $str = $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 30 | $self->config('ColMax' => $self->{_}->{'ColMax'}); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 30 | delete $self->{_}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 30 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 96 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 'FLOWED' => sub { # RFC 3676 | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 162 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 162 | my $action = shift; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 162 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 162 | ▶ | if ($action eq 'sol') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 22 | ▶ | if ($self->{_}->{prefix}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | return $self->{_}->{prefix}.' '.$str; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } elsif ($action =~ /^so/) { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 32 | $self->{_} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 32 | ▶ | if ($str =~ /^(>+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | $self->{_}->{prefix} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 23 | $self->{_}->{prefix} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | } elsif ($action eq "") { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 54 | ▶ | if ($str =~ /^(?: |From )/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$str =~ /^(?: |From )/ or $str =~ /^>/ and not length $self->{"_"}{"prefix"}
$str =~ /^>/ and not length $self->{"_"}{"prefix"}
MC/DC33%1/3
$str =~ /^(?: |From )/ or $str =~ /^>/ and not length $self->{"_"}{"prefix"}
$str =~ /^(?: |From )/
$str =~ /^>/
not length $self->{"_"}{"prefix"}
Truth table50%2/4
A or B and not(C)
A= $str =~ /^(?: |From )/B= $str =~ /^>/C= not length $self->{"_"}{"prefix"}
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | or $str =~ /^>/ and !length $self->{_}->{prefix}) { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | return $self->{_}->{line} = ' ' . $str; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 52 | $self->{_}->{line} = $str; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif ($action eq 'eol') { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 22 | ▶ | $str = ' ' if length $str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 22 | return $str.' '.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } elsif ($action =~ /^eo/) { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 32 | ▶ | if (length $self->{_}->{line} and !length $self->{_}->{prefix}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $self->{"_"}{"line"} and not length $self->{"_"}{"prefix"}
MC/DC100%2/2
length $self->{"_"}{"line"} and not length $self->{"_"}{"prefix"}
length $self->{"_"}{"line"}
not length $self->{"_"}{"prefix"}
Truth table100%3/3
A and not(B)
A= length $self->{"_"}{"line"}B= not length $self->{"_"}{"prefix"}
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 17 | $str = ' '.$self->config('Newline').$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 15 | $str = $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 32 | delete $self->{_}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 32 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 104 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 'PLAIN' => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 150 | ▶ | return $_[0]->config('Newline') if $_[1] =~ /^eo/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 100 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head2 Public Interface | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =item new ([KEY => VALUE, ...]) | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | I<Constructor>. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | About KEY => VALUE pairs see config method. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 7 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | my $self = bless __PACKAGE__->SUPER::new(), $class; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | $self->config(@_); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 7 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =item $self->config (KEY) | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =item $self->config ([KEY => VAL, ...]) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | I<Instance method>. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | Get or update configuration. Following KEY => VALUE pairs may be specified. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =item Charset => CHARSET | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | Character set that is used to encode string. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | It may be string or L<MIME::Charset> object. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | Default is C<"UTF-8">. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =item Language => LANGUAGE | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | Along with Charset option, this may be used to define language/region | ||||||||||||||||||||||||||||||||||||||||||
| 170 | context. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | Default is C<"XX">. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | See also L<Unicode::LineBreak/Context> option. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =item Newline => STRING | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | String to be used for newline sequence. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | Default is C<"\n">. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | =item OutputCharset => CHARSET | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | Character set that is used to encode result of fold()/unfold(). | ||||||||||||||||||||||||||||||||||||||||||
| 182 | It may be string or L<MIME::Charset> object. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | If a special value C<"_UNICODE_"> is specified, result will be Unicode string. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | Default is the value of Charset option. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =item TabSize => NUMBER | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | Column width of tab stops. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | When 0 is specified, tab stops are ignored. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | Default is 8. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =item BreakIndent | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =item CharMax | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =item ColMax | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =item ColMin | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =item ComplexBreaking | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =item EAWidth | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =item HangulAsAL | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =item LBClass | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | =item LegacyCM | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | =item Prep | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | =item Urgent | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | See L<Unicode::LineBreak/Options>. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | sub config { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 85 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=18 SCAR=33.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 85 | my @opts = qw{Charset Language OutputCharset TabSize}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 85 | my %opts = map { (uc $_ => $_) } @opts; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 85 | my $newline = undef; | |||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Get config. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 85 | ▶ | if (scalar @_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 78 | ▶ | if ($opts{uc $_[0]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return $self->{$opts{uc $_[0]}}; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 78 | return $self->SUPER::config($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | # Set config. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 7 | my @o = (); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 7 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | foreach my $k (keys %params) { | |||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | my $v = $params{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | if ($opts{uc $k}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $self->{$opts{uc $k}} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } elsif (uc $k eq uc 'Newline') { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | $newline = $v; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | push @o, $k => $v; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 7 | ▶ | $self->SUPER::config(@o) if scalar @o; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # Character set and language assumed. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 7 | ▶ | if (ref $self->{Charset} eq 'MIME::Charset') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $self->{_charset} = $self->{Charset}; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 7 | ▶ | $self->{Charset} ||= $Config->{Charset}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"Charset"} ||= $Text::LineFold::Config->{"Charset"}
MC/DC0%0/2
$self->{"Charset"} ||= $Text::LineFold::Config->{"Charset"}
$self->{"Charset"}
$Text::LineFold::Config->{"Charset"}
Truth table33%1/3
A ||= B
A= $self->{"Charset"}B= $Text::LineFold::Config->{"Charset"}
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 7 | $self->{_charset} = MIME::Charset->new($self->{Charset}); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 7 | $self->{Charset} = $self->{_charset}->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 7 | ▶ | my $ocharset = uc($self->{OutputCharset} || $self->{Charset}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'OutputCharset'} || $self->{'Charset'}
MC/DC0%0/2
$self->{'OutputCharset'} || $self->{'Charset'}
$self->{'OutputCharset'}
$self->{'Charset'}
Truth table33%1/3
A || B
A= $self->{'OutputCharset'}B= $self->{'Charset'}
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | ▶ | $ocharset = MIME::Charset->new($ocharset) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $ocharset eq "MIME::Charset" or $ocharset eq "_UNICODE_"
MC/DC0%0/2
ref $ocharset eq "MIME::Charset" or $ocharset eq "_UNICODE_"
ref $ocharset eq "MIME::Charset"
$ocharset eq "_UNICODE_"
Truth table33%1/3
A or B
A= ref $ocharset eq "MIME::Charset"B= $ocharset eq "_UNICODE_"
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | unless ref $ocharset eq 'MIME::Charset' or $ocharset eq '_UNICODE_'; | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | ▶ | unless ($ocharset eq '_UNICODE_') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | $self->{_charset}->encoder($ocharset); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 7 | $self->{OutputCharset} = $ocharset->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 7 | ▶ | $self->{Language} = uc($self->{Language} || $Config->{Language}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'Language'} || $Text::LineFold::Config->{'Language'}
MC/DC0%0/2
$self->{'Language'} || $Text::LineFold::Config->{'Language'}
$self->{'Language'}
$Text::LineFold::Config->{'Language'}
Truth table33%1/3
A || B
A= $self->{'Language'}B= $Text::LineFold::Config->{'Language'}
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | ## Context | ||||||||||||||||||||||||||||||||||||||||||
| 269 | $self->SUPER::config(Context => | ||||||||||||||||||||||||||||||||||||||||||
| 270 | context(Charset => $self->{Charset}, | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 7 | Language => $self->{Language})); | |||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | ## Set sizing method. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | $self->SUPER::config(Sizing => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 2320 | ▶ | my ($self, $cols, $pre, $spc, $str) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 2320 | my $tabsize = $self->{TabSize}; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 2320 | my $spcstr = $spc.$str; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 2320 | $spcstr->pos(0); | |||||||||||||||||||||||||||||||||||||||||
| 280 | 2320 | ▶ | while (!$spcstr->eos and $spcstr->item->lbc == LB_SP) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $spcstr->eos and $spcstr->item->lbc == LB_SP()
MC/DC100%2/2
not $spcstr->eos and $spcstr->item->lbc == LB_SP()
not $spcstr->eos
$spcstr->item->lbc == LB_SP()
Truth table100%3/3
not(A) and B
A= not $spcstr->eosB= $spcstr->item->lbc == LB_SP()
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 1014 | my $c = $spcstr->next; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 1014 | ▶ | if ($c eq "\t") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 10 | ▶ | $cols += $tabsize - $cols % $tabsize if $tabsize; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 1004 | $cols += $c->columns; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 2320 | return $cols + $spcstr->substr($spcstr->pos)->columns; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | ## Classify horizontal tab as line breaking class SP. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 7 | $self->SUPER::config(LBClass => [ord("\t") => LB_SP]); | |||||||||||||||||||||||||||||||||||||||||
| 293 | ## Tab size | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 7 | ▶ | if (defined $self->{TabSize}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | croak "Invalid TabSize option" unless $self->{TabSize} =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $self->{TabSize} += 0; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 7 | $self->{TabSize} = $Config->{TabSize}; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | ## Newline | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 7 | ▶ | if (defined $newline) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | $newline = $self->{_charset}->decode($newline) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | unless is_utf8($newline); | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | $self->SUPER::config(Newline => $newline); | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | =item $self->fold (STRING, [METHOD]) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =item $self->fold (INITIAL_TAB, SUBSEQUENT_TAB, STRING, ...) | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | I<Instance method>. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | fold() folds lines of string STRING and returns it. | ||||||||||||||||||||||||||||||||||||||||||
| 317 | Surplus SPACEs and horizontal tabs at end of line are removed, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | newline sequences are replaced by that specified by Newline option | ||||||||||||||||||||||||||||||||||||||||||
| 319 | and newline is appended at end of text if it does not exist. | ||||||||||||||||||||||||||||||||||||||||||
| 320 | Horizontal tabs are treated as tab stops according to TabSize option. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | By the first style, following options may be specified for METHOD argument. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =item C<"FIXED"> | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | Lines preceded by C<"E<gt>"> won't be folded. | ||||||||||||||||||||||||||||||||||||||||||
| 329 | Paragraphs are separated by empty line. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =item C<"FLOWED"> | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | C<"Format=Flowed; DelSp=Yes"> formatting defined by RFC 3676. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =item C<"PLAIN"> | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | Default method. All lines are folded. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | Second style is similar to L<Text::Wrap/wrap()>. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | All lines are folded. | ||||||||||||||||||||||||||||||||||||||||||
| 343 | INITIAL_TAB is inserted at beginning of paragraphs and SUBSEQUENT_TAB | ||||||||||||||||||||||||||||||||||||||||||
| 344 | at beginning of other broken lines. | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | # Special breaking characters: VT, FF, NEL, LS, PS | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 3 | my $special_break = qr/([\x{000B}\x{000C}\x{0085}\x{2028}\x{2029}])/os; | |||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | sub fold { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fold: called CC=23 SCAR=41.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 12 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 12 | ▶ | if (2 < scalar @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 2 | ▶ | my $initial_tab = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 2 | ▶ | $initial_tab = $self->{_charset}->decode($initial_tab) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | unless is_utf8($initial_tab); | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 2 | ▶ | my $subsequent_tab = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 2 | ▶ | $subsequent_tab = $self->{_charset}->decode($subsequent_tab) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | unless is_utf8($subsequent_tab); | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 2 | my @str = @_; | |||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | ## Decode and concat strings. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 2 | $str = shift @str; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 2 | ▶ | $str = $self->{_charset}->decode($str) unless is_utf8($str); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 2 | foreach my $s (@str) { | |||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | next unless defined $s and length $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $s and length $s
MC/DC0%0/2
defined $s and length $s
defined $s
length $s
Truth table0%0/3
A and B
A= defined $sB= length $s
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | $s = $self->{_charset}->decode($s) unless is_utf8($s); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | unless (length $str) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $str = $s; | |||||||||||||||||||||||||||||||||||||||||
| 375 | ▶ | } elsif ($str =~ /(\s|$special_break)$/ or | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$str =~ /(\s|$special_break)$/ or $s =~ /^(\s|$special_break)/
MC/DC0%0/2
$str =~ /(\s|$special_break)$/ or $s =~ /^(\s|$special_break)/
$str =~ /(\s|$special_break)$/
$s =~ /^(\s|$special_break)/
Truth table0%0/3
A or B
A= $str =~ /(\s|$special_break)$/B= $s =~ /^(\s|$special_break)/
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | $s =~ /^(\s|$special_break)/) { | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | $str .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 378 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | $str .= ' ' if $self->breakingRule($str, $s) == INDIRECT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $str .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | ## Set format method. | ||||||||||||||||||||||||||||||||||||||||||
| 385 | $self->SUPER::config(Format => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 96 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | 96 | my $event = shift; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 96 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 96 | ▶ | if ($event =~ /^eo/) { return $self->config('Newline'); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 64 | ▶ | if ($event =~ /^so[tp]/) { return $initial_tab.$str; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 50 | ▶ | if ($event eq 'sol') { return $subsequent_tab.$str; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 32 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 393 | 2 | }); | |||||||||||||||||||||||||||||||||||||||||
| 394 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 10 | $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 10 | ▶ | my $method = uc(shift || ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 10 | ▶ | return '' unless defined $str and length $str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $str and length $str
MC/DC0%0/2
defined $str and length $str
defined $str
length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | ## Decode string. | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 10 | ▶ | $str = $self->{_charset}->decode($str) unless is_utf8($str); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | ## Set format method. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | $self->SUPER::config(Format => $FORMAT_FUNCS{$method} || | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 10 | ▶ | $FORMAT_FUNCS{'PLAIN'}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$FORMAT_FUNCS{$method} || $FORMAT_FUNCS{'PLAIN'}
MC/DC0%0/2
$FORMAT_FUNCS{$method} || $FORMAT_FUNCS{'PLAIN'}
$FORMAT_FUNCS{$method}
$FORMAT_FUNCS{'PLAIN'}
Truth table33%1/3
A || B
A= $FORMAT_FUNCS{$method}B= $FORMAT_FUNCS{'PLAIN'}
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | ## Do folding. | ||||||||||||||||||||||||||||||||||||||||||
| 408 | 12 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 12 | foreach my $s (split $special_break, $str) { | |||||||||||||||||||||||||||||||||||||||||
| 410 | 12 | ▶ | if ($s =~ $special_break) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $result .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 12 | $result .= $self->break($str); | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | ## Encode result. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 12 | ▶ | if ($self->{OutputCharset} eq '_UNICODE_') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 12 | return $self->{_charset}->encode($result); | |||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =item $self->unfold (STRING, METHOD) | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | Conjunct folded paragraphs of string STRING and returns it. | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | Following options may be specified for METHOD argument. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =item C<"FIXED"> | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | Default method. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | Lines preceded by C<"E<gt>"> won't be conjuncted. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | Treat empty line as paragraph separator. | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =item C<"FLOWED"> | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | Unfold C<"Format=Flowed; DelSp=Yes"> formatting defined by RFC 3676. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | =item C<"FLOWEDSP"> | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | Unfold C<"Format=Flowed; DelSp=No"> formatting defined by RFC 3676. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =begin comment | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =item C<"OBSFLOWED"> | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | Unfold C<"Format=Flowed> formatting defined by (obsoleted) RFC 2646 | ||||||||||||||||||||||||||||||||||||||||||
| 454 | as well as possible. | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =end comment | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | sub unfold { | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unfold: called CC=38 SCAR=43.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | 8 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 8 | ▶ | return '' unless defined $str and length $str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $str and length $str
MC/DC0%0/2
defined $str and length $str
defined $str
length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | ## Get format method. | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 8 | ▶ | my $method = uc(shift || 'FIXED'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'FIXED'
MC/DC0%0/1
shift() || 'FIXED'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 8 | ▶ | $method = 'FIXED' unless $method =~ /^(?:FIXED|FLOWED|FLOWEDSP|OBSFLOWED)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 8 | my $delsp = $method eq 'FLOWED'; | |||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | ## Decode string and canonizalize newline. | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 8 | ▶ | $str = $self->{_charset}->decode($str) unless is_utf8($str); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 8 | $str =~ s/\r\n|\r/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | ## Do unfolding. | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 8 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 480 | 8 | foreach my $s (split $special_break, $str) { | |||||||||||||||||||||||||||||||||||||||||
| 481 | 8 | ▶ | if ($s eq '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 483 | } elsif ($s =~ $special_break) { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | $result .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 486 | } elsif ($method eq 'FIXED') { | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 3 | pos($s) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 3 | ▶ | while ($s !~ /\G\z/cg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 48 | ▶ | if ($s =~ /\G\n/cg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 6 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 491 | } elsif ($s =~ /\G(.+)\n\n/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 15 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 493 | } elsif ($s =~ /\G(>.*)\n/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 9 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 495 | } elsif ($s =~ /\G(.+)\n(?=>)/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 497 | } elsif ($s =~ /\G(.+?)( *)\n(?=(.+))/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 498 | 18 | my ($l, $s, $n) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||
| 499 | 18 | $result .= $l; | |||||||||||||||||||||||||||||||||||||||||
| 500 | 18 | ▶ | if ($n =~ /^ /) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 502 | } elsif (length $s) { | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | $result .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 504 | } elsif (length $l) { | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 18 | ▶ | $result .= ' ' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | if $self->breakingRule($l, $n) == INDIRECT; | ||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | } elsif ($s =~ /\G(.+)\n/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 510 | } elsif ($s =~ /\G(.+)/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | ▶ | } elsif ($method eq 'FLOWED' or $method eq 'FLOWEDSP' or | |||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$method eq "FLOWED" or $method eq "FLOWEDSP" or $method eq "OBSFLOWED"
$method eq "FLOWED" or $method eq "FLOWEDSP"
MC/DC0%0/3
$method eq "FLOWED" or $method eq "FLOWEDSP" or $method eq "OBSFLOWED"
$method eq "FLOWED"
$method eq "FLOWEDSP"
$method eq "OBSFLOWED"
Truth table50%2/4
A or B or C
A= $method eq "FLOWED"B= $method eq "FLOWEDSP"C= $method eq "OBSFLOWED"
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | $method eq 'OBSFLOWED') { | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 5 | my $prefix = undef; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 5 | pos($s) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 5 | ▶ | while ($s !~ /\G\z/cg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 520 | 96 | ▶ | if ($s =~ /\G(>+) ?(.*?)( ?)\n/cg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 26 | my ($p, $l, $s) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||
| 522 | 26 | ▶ | unless (defined $prefix) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 17 | $result .= $p.' '.$l; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } elsif ($p ne $prefix) { | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 1 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 526 | 1 | $result .= $p.' '.$l; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 8 | $result .= $l; | |||||||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||||||
| 530 | 26 | ▶ | unless (length $s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 17 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 532 | 17 | $prefix = undef; | |||||||||||||||||||||||||||||||||||||||||
| 533 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 534 | 9 | $prefix = $p; | |||||||||||||||||||||||||||||||||||||||||
| 535 | 9 | ▶ | $result .= $s unless $delsp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | } elsif ($s =~ /\G ?(.*?)( ?)\n/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 70 | my ($l, $s) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 539 | 70 | ▶ | unless (defined $prefix) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 30 | $result .= $l; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } elsif ('' ne $prefix) { | ||||||||||||||||||||||||||||||||||||||||||
| 542 | 0 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | $result .= $l; | |||||||||||||||||||||||||||||||||||||||||
| 544 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 40 | $result .= $l; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 70 | ▶ | unless (length $s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 30 | $result .= $self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 549 | 30 | $prefix = undef; | |||||||||||||||||||||||||||||||||||||||||
| 550 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 40 | ▶ | $result .= $s unless $delsp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 40 | $prefix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | } elsif ($s =~ /\G ?(.*)/cg) { | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | $result .= $1.$self->config('Newline'); | |||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||
| 561 | ## Encode result. | ||||||||||||||||||||||||||||||||||||||||||
| 562 | 8 | ▶ | if ($self->{OutputCharset} eq '_UNICODE_') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 564 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 8 | return $self->{_charset}->encode($result); | |||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | Please report bugs or buggy behaviors to developer. | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | CPAN Request Tracker: | ||||||||||||||||||||||||||||||||||||||||||
| 574 | L<http://rt.cpan.org/Public/Dist/Display.html?Name=Unicode-LineBreak>. | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | Consult $VERSION variable. | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | L<Unicode::LineBreak>, L<Text::Wrap>. | ||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | Copyright (C) 2009-2012 Hatuka*nezumi - IKEDA Soji <hatuka(at)nezumi.nu>. | ||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 589 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||