Hi everyone,

First off, this is Ultimate Red Dragon, changed email addys.  (If anyone
cares)

I've finished what seems to be a working version of the HTML to RTF
converter.  It isn't all that complex, but it does the job.  Don't
expect it to do any detailed formatting, it will only really attempt to
work with Bold, Italics, Underline and I'm considering adding in FONT
support.

All I can say for certain is that my previously posted RTF2HTML sub does
work with this, so it can be used to temporaraly store RTF data in a
variable for example.  (Other uses should be equally obvious.)

Anyway, as before, suggestions and comments are welcome.

The following REQUIRES HTML::Parser

sub html2rtf($){
   my $re = $main->reDesc;  #Just set this to the RichEdit object
   my $text = shift;
   $text =~ s/<BR>/\r\n/gi;
   my $cpos = 0;
   $re->Text('');

   my @b;
   my @i;
   my @u;

   my $start = sub {
      my $operator = shift;
         $re->Select(999999,999999);
         my ($temp) = $re->Selection();
      if($operator eq 'b'){
         push(@b,[$temp]);
      }elsif($operator eq 'i'){
         push(@i,[$temp]);
      }elsif($operator eq 'u'){
         push(@u,[$temp]);
      }
   };

   my $end = sub {
      my $operator = shift;
      $re->Select(99999,99999);
      my($stop) = $re->Selection();
      if($operator eq 'b' && defined($b[-1][0])){
         $b[-1][1] = $stop;
      }elsif($operator eq 'i' && defined($i[-1][0])){
         $i[-1][1] = $stop;
      }elsif($operator eq 'u' && defined($u[-1][0])){
         $u[-1][1] = $stop;
      }
   };

   my $texth = sub {
      my $txt = shift;
      $re->Text($re->Text().$txt);
   };


   my $p = HTML::Parser->new(api_version => 3,
                             start_h     =>[$start,'tagname'],
                             end_h       =>[$end,'tagname'],
                             text_h      =>[$texth,'dtext'],

                             report_tags => [qw(b i u br text)],
                            );

   $p->parse($text)->eof();

   foreach my $bld (@b){
      unless(defined($bld->[1])){
         $re->Select(999999,999999);
         my ($temp) = $re->Selection();
         $bld->[1] = $temp;
      }
      $re->Select($bld->[0],$bld->[1]);
      $re->SetCharFormat(-bold=>1);
   }
   foreach my $bld (@i){
         unless(defined($bld->[1])){
            $re->Select(999999,999999);
            my ($temp) = $re->Selection();
            $bld->[1] = $temp;
         }
         $re->Select($bld->[0],$bld->[1]);
         $re->SetCharFormat(-italic=>1);
   }
   foreach my $bld (@u){
         unless(defined($bld->[1])){
            $re->Select(999999,999999);
            my ($temp) = $re->Selection();
            $bld->[1] = $temp;
         }
         $re->Select($bld->[0],$bld->[1]);
         $re->SetCharFormat(-underline=>1);
   }

   $re->Select(999999,999999);

   return 1;
}

Reply via email to