Hello,

i will answer myself, may be it will help someone:


Here is my question:

####################################################
i want to convert <ul>, <ol> and <li> elements to ascii text

<ul>
  <li>One</li>
  <li>Two</li>
</ul>
...will become:
    * One
    * Two

<ol>
  <li>One</li>
  <li>Two</li>
</ol>
...will become:
    1. One
    2. Two

Did somebody already have fun with this ?
#####################################################


And here is my answer:

#!/usr/bin/perl

use strict;
use HTML::Parser 3.00 ();


#// Repeat a character
sub repStr {
        my $car = shift;
        my $number = shift;
        my $str;
        for (my $ctr = 1; $ctr <= $number; $ctr++) {
                $str .= $car;
        }
        return $str;
}


my %inside;

my $level = 0;
my $ctrOl = 0;
my %levels;
my %OlNumbers;

sub tag
{
        my($tag, $num) = @_;
        $inside{$tag} += $num;

        #// LI
        if ( ($tag eq 'li') && ($num eq '+1') ) {
                if ($levels{$level} eq 'ol') {
                        $OlNumbers{$level}++;
                        print repStr(' ', 4 * $level) . $OlNumbers{$level} . ".";
                } else {
                        #// LI normal
                        print repStr(' ', 4 * $level) . '•';
                }
        #// UL OPEN
        } elsif ( ($tag eq 'ul') && ($num eq '+1') ) {
                $level++;
                $levels{$level} = 'ul';
        #// UL CLOSE
        } elsif ( ($tag eq 'ul') && ($num eq '-1') ) {
                $level--;
        #// OL OPEN
        } elsif ( ($tag eq 'ol') && ($num eq '+1') ) {
                $level++;
                $levels{$level} = 'ol';
                $OlNumbers{$level} = 0;
        #// OL CLOSE
        } elsif ( ($tag eq 'ol') && ($num eq '-1') ) {
                $level--;
        } else {
                print "";  # not for all tags
        }
}

sub text
{
    return if $inside{script} || $inside{style};

    print $_[0];
}

HTML::Parser->new(api_version => 3,
                  handlers    => [start => [\&tag, "tagname, '+1'"],
                                  end   => [\&tag, "tagname, '-1'"],
                                  text  => [\&text, "dtext"],
                                 ],
                  marked_sections => 1,
        )->parse_file(shift) || die "Can't open file: $!\n";;

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to