package Text::Styler; use strict; use Text::Wrap; use vars qw( $VERSION ); $VERSION = 0.1; sub new { my $class = shift; my $self = bless { }, $class; if ( my $a = shift ) { # getting set wrong when hash ref is handed +in. $self->left_margin( $a->{left_margin} || 0 ); $self->right_margin( $a->{right_margin} || 0 ); $self->columns( $a->{columns} || 60 ); } return $self; } sub text_style { my($self,$text) = @_; use Text::Styler::Parser; my $styler = new Text::Styler::Parser; $styler->parse($text); $self->text_wrap( $styler->contents ); } sub text_wrap { my($self,$text) = @_; local( $Text::Wrap::columns=$self->width ); my $lm = ' ' x $self->left_margin; # local( $Text::Wrap::unexpand=0 ); my $out = wrap($lm,$lm,$text); # stupid hack because $Text::Wrap::unexpand=0; is ignored when set. my $s = ' ' x 8; $out=~s/\t/$s/g; $out =~ s/\n\s*?\n/\n\n/gs; $out; } sub columns { $_[0]->{__columns} = $_[1] if $_[1]; $_[0]->{__columns}; } sub left_margin { $_[0]->{__left_margin} = $_[1] if $_[1]; $_[0]->{__left_margin}; } sub right_margin { $_[0]->{__right_margin} = $_[1] if $_[1]; $_[0]->{__right_margin}; } sub width { $_[0]->{__columns} - $_[0]->{__left_margin} - $_[0]->{__right_margin}; }