It looks like you are implementing the beginnings of a templating system. As you've probably guessed, this isn't the first time someone's thought about doing this.
I've have very good success with Template::Toolkit as a templating system. The advantage of doing this is that it separates the presentation of the output from the business logic within the script.
In your case, you would pass the information in to the template, and then use logic in the template (example below) to modify the display of the data.
[% IF line.match('MATCH') %]
<div style="background-color: orange">
[% ELSE %]
<div>
[% END %]
[% line %]
</div>
See
this page for information on how I wrote this example.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.