tags being broken in the wrong places
--0022152d5cc1ac953d049cdee816
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I've tried to fiure this out on my own but hit a road block. I'm reading in
an external file, then doind a search and replace. I am also using the
following to break lines at 256 characters
use Text::Wrap qw(wrap $columns $huge);
$columns = 256;
$huge = "die";
Problem I'm having is if there is a tag like below
<test attr1="var" attr2="var" attr3="var" attr4="var" attr5="var"
attr6="var" attr7="var">
It may get broken like so:
<test attr1="var" attr2="var" attr3="var"
attr4="var" attr5="var" attr6="var" attr7="var">
I don't want this to happen. How can I avoid this? The program breaks lines
at 256 characters. If its in the middle of a tag I want it to break before
or after and NOT in between. How do I do this?
Code below:
#!/usr/local/bin/perl
require 5.000;
use Env;
use Cwd;
use File::Basename;
use Text::Wrap qw(wrap $columns $huge);
$columns = 256;
$huge = "die";
my $infile = $ARGV[0];
open(FILEREAD, "$infile.txt");
open(FILEWRITE, "> $infile.temp");
$i=1;
while (<FILEREAD>)
{
chomp $_;
$_ =~ s/<p>/\n<p>/ig;
print FILEWRITE wrap("", "", $_), "\n";
$i++;
}
close FILEWRITE;
close FILEREAD;
--0022152d5cc1ac953d049cdee816--
Re: tags being broken in the wrong places
On 22/02/2011 13:25, Kill Switch wrote:
> Hi,
>
> I've tried to fiure this out on my own but hit a road block. I'm reading in
> an external file, then doind a search and replace. I am also using the
> following to break lines at 256 characters
>
> use Text::Wrap qw(wrap $columns $huge);
> $columns = 256;
> $huge = "die";
>
>
> Problem I'm having is if there is a tag like below
>
> <test attr1="var" attr2="var" attr3="var" attr4="var" attr5="var"
> attr6="var" attr7="var">
>
> It may get broken like so:
>
> <test attr1="var" attr2="var" attr3="var"
> attr4="var" attr5="var" attr6="var" attr7="var">
>
> I don't want this to happen. How can I avoid this? The program breaks lines
> at 256 characters. If its in the middle of a tag I want it to break before
> or after and NOT in between. How do I do this?
>
>
>
> Code below:
>
> #!/usr/local/bin/perl
>
> require 5.000;
> use Env;
> use Cwd;
> use File::Basename;
> use Text::Wrap qw(wrap $columns $huge);
>
> $columns = 256;
> $huge = "die";
>
> my $infile = $ARGV[0];
> open(FILEREAD, "$infile.txt");
> open(FILEWRITE, "> $infile.temp");
> $i=1;
> while (<FILEREAD>)
> {
> chomp $_;
> $_ =~ s/<p>/\n<p>/ig;
> print FILEWRITE wrap("", "", $_), "\n";
> $i++;
> }
> close FILEWRITE;
> close FILEREAD;
You can't prevent Text::Wrap from breaking XML tags as it knows nothing
about them. I suggest you do one of the following:
- Parse your original file into tags and data, wrap the data elements
separately and then recombine them.
- Wrap the entire file, then check for broken tags. If one id found
then put a newline before it and rewrap from there onward. Repeat
until clean.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: tags being broken in the wrong places
On Tue, Feb 22, 2011 at 5:48 AM, Rob Dixon <rob.dixon [at] gmx.com> wrote:
> On 22/02/2011 13:25, Kill Switch wrote:
>>
>> Hi,
>>
>> I've tried to fiure this out on my own but hit a road block. I'm reading
>> in
>> an external file, then doind a search and replace. I am also using the
>> following to break lines at 256 characters
>>
>> use Text::Wrap qw(wrap $columns $huge);
>> $columns =3D 256;
>> $huge =3D "die";
>>
>>
>> Problem I'm having is if there is a tag like below
>>
>> <test attr1=3D"var" attr2=3D"var" attr3=3D"var" attr4=3D"var" attr5=3D"v=
ar"
>> attr6=3D"var" attr7=3D"var">
>>
>> It may get broken like so:
>>
>> <test attr1=3D"var" attr2=3D"var" attr3=3D"var"
>> attr4=3D"var" attr5=3D"var" attr6=3D"var" attr7=3D"var">
>>
>> I don't want this to happen. How can I avoid this? The program breaks
>> lines
>> at 256 characters. If its in the middle of a tag I want it to break befo=
re
>> or after and NOT in between. How do I do this?
>>
>>
>>
>> Code below:
>>
>> #!/usr/local/bin/perl
>>
>> require 5.000;
>> use Env;
>> use Cwd;
>> use File::Basename;
>> use Text::Wrap qw(wrap $columns $huge);
>>
>> $columns =3D 256;
>> $huge =3D "die";
>>
>> my $infile =3D $ARGV[0];
>> open(FILEREAD, "$infile.txt");
>> =A0open(FILEWRITE, "> =A0$infile.temp");
>> =A0 =A0$i=3D1;
>> =A0 while (<FILEREAD>)
>> =A0 {
>> =A0 =A0chomp $_;
>> =A0 =A0$_ =3D~ s/<p>/\n<p>/ig;
>> =A0 =A0print FILEWRITE wrap("", "", $_), "\n";
>> =A0 =A0$i++;
>> =A0 }
>> =A0close FILEWRITE;
>> close FILEREAD;
>
> You can't prevent Text::Wrap from breaking XML tags as it knows nothing
> about them. I suggest you do one of the following:
>
> - Parse your original file into tags and data, wrap the data elements
> =A0separately and then recombine them.
For this task you can use HTML::Parser
http://search.cpan.org/~gaas/HTML-Parser-3.68/Parser.pm
>
> - Wrap the entire file, then check for broken tags. If one id found
> =A0then put a newline before it and rewrap from there onward. Repeat
> =A0until clean.
>
> HTH,
>
> Rob
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/