in reply to HTML --> text formatting with curses color codes?

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.

  • Comment on Re: HTML --> text formatting with curses color codes?

Replies are listed 'Best First'.
Re^2: HTML --> text formatting with curses color codes?
by dstar (Scribe) on Apr 08, 2005 at 12:47 UTC
    I'm fairly sure there's no css involved in this part; it's the display for a web-based chat. I had to write my own client for it since links can't deal with server-push.

    I'm using a curses module (a hacked-up version of Term::Visual to add a third pane down the right side to display currently-logged-in users), so I'll need to convert it down to curses colors. I'm not sure how to do that yet; I guess I'll just break the colors down into ranges, each range mapping to a curses color.

    Shalon Wood