Render html field

From EPrints Documentation
Revision as of 12:59, 29 March 2011 by Tdb01r (talk | contribs) (Created page with 'EPrints::Extras provides a render_xhtml_field but that method only works with well-formed XML. This method uses the HTML::Parser tool to render plain-HTML: use HTML::Parser; …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

EPrints::Extras provides a render_xhtml_field but that method only works with well-formed XML.

This method uses the HTML::Parser tool to render plain-HTML:

use HTML::Parser;
sub render_html_field
{
   my( $repo, $field, $value ) = @_;

   my $xml = $repo->xml;

   my $frag = $xml->create_document_fragment;

   my %compress = map { $_ => 1 } @EPrints::XML::COMPRESS_TAGS;

   my $c = $frag;
   my $p = HTML::Parser->new(
       api_version => 3,
       start_h => [sub {
           eval { if( $compress{$_[0]} )
           {
               $c->appendChild( $xml->create_element( @_ ) )
           }
           else
           {
               $c = $c->appendChild( $xml->create_element( @_ ) )
           } };
           $c->appendChild( $xml->create_text_node( "Error parsing '@_': $@" ) ) if $@;
       }, 'tagname,@attr'],
       end_h => [sub { $c = $c->parentNode if $c->nodeName eq $_[0] && !$compress{$_[0]} }, 'tagname' ],
       text_h => [sub { $c->appendChild( $xml->create_text_node( Encode::encode_utf8($_[0]) ) ) }, 'dtext'],
   );
   $p->empty_element_tags( 1 );
   $p->strict_end( 1 );
   $p->parse( $value );
   $p->eof;

   return $frag;
}

To use this method drop the above into a cfg.d script - recommend you use the name '00_render_html_field.pl' to ensure it's loaded before use. Add this to a field definition:

render_value => \&render_html_field,