///
/// converts html to plain text.
///
class htmltotext
{
// static data tables
protected static dictionary _tags;
protected static hashset _ignoretags;
// instance variables
protected textbuilder _text;
protected string _html;
protected int _pos;
// static constructor (one time only)
static htmltotext()
{
_tags = new dictionary();
_tags.add(address, \n);
_tags.add(blockquote, \n);
_tags.add(div, \n);
_tags.add(dl, \n);
_tags.add(fieldset, \n);
_tags.add(form, \n);
_tags.add(h1, \n);
_tags.add(/h1, \n);
_tags.add(h2, \n);
_tags.add(/h2, \n);
_tags.add(h3, \n);
_tags.add(/h3, \n);
_tags.add(h4, \n);
_tags.add(/h4, \n);
_tags.add(h5, \n);
_tags.add(/h5, \n);
_tags.add(h6, \n);
_tags.add(/h6, \n);
_tags.add(p, \n);
_tags.add(/p, \n);
_tags.add(table, \n);
_tags.add(/table, \n);
_tags.add(ul, \n);
_tags.add(/ul, \n);
_tags.add(ol, \n);
_tags.add(/ol, \n);
_tags.add(/li, \n);
_tags.add(br, \n);
_tags.add(/td, \t);
_tags.add(/tr, \n);
_tags.add(/pre, \n);
_ignoretags = new hashset();
_ignoretags.add(script);
_ignoretags.add(noscript);
_ignoretags.add(style);
_ignoretags.add(object);
}
///
/// converts the given html to plain text and returns the result.
///
/// html to be converted
/// resulting plain text
public string convert(string html)
{
// initialize state variables
_text = new textbuilder();
_html = html;
_pos = 0;
// process input
while (!endoftext)
{
if (peek() == '')
{
if (peek() == '' || peek() == '\'')
eatquotedvalue();
else
{
if (peek() == '/')
selfclosing = true;
moveahead();
}
}
moveahead();
}
return tag;
}
// consumes inner content from the current tag
protected void eatinnercontent(string tag)
{
string endtag = / + tag;
while (!endoftext)
{
if (peek() == '= _html.length); }
}
// safely returns the character at the current position
protected char peek()
{
return (_pos 0)
flushcurrline();
return _text.tostring();
}
}
}