Indexing HTML parts

While HTML integration is improving in Manitou-Mail,the current version (0.9.12) does not index the contents of HTML parts. This is generally not a problem because messages tend to carry a text version inside a multipart/alternative MIME construct,and that version gets indexed so that the message can still be retrieved by the words it contains. But still,some people send HTML-only messages,in which case we want to automatically extract the text from the HTML and pass it to the indexer.

It’s relatively easy to write a manitou-mdx Perl plugin that does just that,by using a CPAN module to do the HTML to text conversion:HTML::FormatText

Apart from the usual init and process functions that are described in the mdx plugins reference,we need to provide two functions:one that recursively descends the MIME tree to find the html parts,and another that extracts them to text and pass them to the indexer.

sub index_contents { my ($fh, $ctxt)=@_; my $html; my $text; {  local $/;  $html = $fh->getline(); } if (defined $html) {  my $tree = HTML::TreeBuilder->new;  $tree->parse_content($html);  my $formatter = HTML::FormatText->new(leftmargin=>0, rightmargin=>78);  $text = $formatter->format($tree); } if (defined $text) {  Manitou::Words::index_words($ctxt->{'dbh'}, $ctxt->{'mail_id'}, \$text); }} sub process_parts { my ($obj,$ctxt) = @_;; if ($obj->is_multipart) {  foreach my $subobj ($obj->parts) { process_parts($subobj, $ctxt);  # recurse  } } else {  my $type=$obj->effective_type;  if ($type eq "text/html") { my $io = $obj->bodyhandle->open("r"); index_contents($io, $ctxt); $io->close;  } }}

The full source code and download link are available on the wiki

Share:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • Reddit
  • FriendFeed
  • RSS

Leave a Reply