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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.