0
  0   ˆl–Ü4ý('Ô¾R( å ÕÀ¿p@S�ÿÁ„o2_‹uÐb&=LV[(ÁÀ     ˆ0_’I|¥‰ÓMjqØ‚¦ð¬÷ÂÁ =Ö    #-*- perl -*-

package Text::LineFold;
require 5.008;

=encoding utf-8

=head1 NAME

Text::LineFold - Line Folding for Plain Text

=head1 SYNOPSIS

    use Text::LineFold;
    $lf = Text::LineFold->new();
    
    # Fold lines
    $folded = $lf->fold($string, 'PLAIN');
    $indented = $lf->fold(' ' x 8, ' ' x 4, $string);

    # Unfold lines
    $unfolded = $lf->unfold($string, 'FIXED');

=head1 DESCRIPTION

Text::LineFold folds or unfolds lines of plain text.
As it mainly focuses on plain text e-mail messages,
RFC 3676 flowed format is also supported.

=cut

### Pragmas:
use strict;
use vars qw($VERSION @EXPORT_OK @ISA $Config);

### Exporting:
use Exporter;

### Inheritance:
our @ISA = qw(Exporter Unicode::LineBreak);

### Other modules:
use Carp qw(croak carp);
use Encode qw(is_utf8);
use MIME::Charset;
use Unicode::LineBreak qw(:all);

### Globals

### The package Version
our $VERSION = '2018.012';

### Public Configuration Attributes
our $Config = {
    ### %{$Unicode::LineBreak::Config},
    Charset => 'UTF-8',
    Language => 'XX',
    OutputCharset => undef,
    TabSize => 8,
};

### Privates

my %FORMAT_FUNCS = (
    'FIXED' => sub {
        my $self = shift;
        my $action = shift;
        my $str = shift;
        if ($action =~ /^so[tp]/) {
            $self->{_} = {};
            $self->{_}->{'ColMax'} = $self->config('ColMax');
            $self->config('ColMax' => 0) if $str =~ /^>/;
        } elsif ($action eq "") {
            $self->{_}->{line} = $str;
        } elsif ($action eq "eol") {
            return $self->config('Newline');
        } elsif ($action =~ /^eo/) {
            if (length $self->{_}->{line} and $self->config('ColMax')) {
                $str = $self->config('Newline').$self->config('Newline');
            } else {
                $str = $self->config('Newline');
            }
            $self->config('ColMax' => $self->{_}->{'ColMax'});
            delete $self->{_};
            return $str;
        }
        undef;
    },
    'FLOWED' => sub { # RFC 3676
        my $self = shift;
        my $action = shift;
        my $str = shift;
        if ($action eq 'sol') {
            if ($self->{_}->{prefix}) {
                return $self->{_}->{prefix}.' '.$str;
            }
        } elsif ($action =~ /^so/) {
            $self->{_} = {};
            if ($str =~ /^(>+)/) {
                $self->{_}->{prefix} = $1;
            } else {
                $self->{_}->{prefix} = '';
            }
        } elsif ($action eq "") {
            if ($str =~ /^(?: |From )/
                or $str =~ /^>/ and !length $self->{_}->{prefix}) {
                return $self->{_}->{line} = ' ' . $str;
            }
            $self->{_}->{line} = $str;
        } elsif ($action eq 'eol') {
            $str = ' ' if length $str;
            return $str.' '.$self->config('Newline');
        } elsif ($action =~ /^eo/) {
            if (length $self->{_}->{line} and !length $self->{_}->{prefix}) {
                $str = ' '.$self->config('Newline').$self->config('Newline');
            } else {
                $str = $self->config('Newline');
            }
            delete $self->{_};
            return $str;
        }
        undef;
    },
    'PLAIN' => sub {
        return $_[0]->config('Newline') if $_[1] =~ /^eo/;
        undef;
    },
);

=head2 Public Interface

=over 4

=item new ([KEY => VALUE, ...])

I<Constructor>.
About KEY => VALUE pairs see config method.

=back

=cut

sub new {
    my $class = shift;
    my $self = bless __PACKAGE__->SUPER::new(), $class;
    $self->config(@_);
    $self;
}

=over 4

=item $self->config (KEY)

=item $self->config ([KEY => VAL, ...])

I<Instance method>.
Get or update configuration.  Following KEY => VALUE pairs may be specified.

=over 4

=item Charset => CHARSET

Character set that is used to encode string.
It may be string or L<MIME::Charset> object.
Default is C<"UTF-8">.

=item Language => LANGUAGE

Along with Charset option, this may be used to de