You might consider that (depending on the input) you're not going to get colour information from just the HTML. You'd have to also take CSS into consideration as well, both from any inline style attributes as well as any linked stylesheets. There is a CSS module on CPAN, but I've never used it so I don't know how applicable it'd be to this application.
But having said that, what you'd want to do is use something like HTML::TokeParser (or ...::Simple depending on personal taste) to walk through the document. When you encounter a new element, look at the style, class, and id attributes and figure out (using whatever CSS gives you) what the colour should be. You'll probably need to somehow convert that colour down to something you can display using Term::ANSIColor or Curses. Keep the current colour in a variable, and a stack of previous colours in an array. When you change the color, push the old current onto the stack; when you come to the end of that element, pop off the old and switch back to it.
Update: Oop, you'd probably want to push the end tag and the colour to restore onto the stack. When you get an end tag from the parser, look at the end tag on the top of the stack to determine if you want to pop or not.
In reply to Re: HTML --> text formatting with curses color codes?
by Fletch
in thread HTML --> text formatting with curses color codes?
by dstar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |