MathML-Entities

 view release on metacpan or  search on metacpan

lib/MathML/Entities.pm  view on Meta::CPAN

    my $content = shift;
    $content =~ s/(&(?:(lt|gt|amp|quot|apos)|[a-zA-Z0-9]+);)/$2 ? lc($1) : _convert2utf8($1)/eig;
    return $content;
}

sub _convert2numbered {
    my $reference = shift;
    $reference =~ /^&([a-zA-Z0-9]+);$/;
    my $name = $1;
    return (exists $ENTITIES{$name}) ?
	$ENTITIES{$name} : "&$name;" ;
}

sub _convert2utf8 {
    my $reference = shift;
    $reference =~ /^&([a-zA-Z0-9]+);$/;
    my $name = $1;
    if (exists $ENTITIES{$name}) {
       my $raw = $ENTITIES{$name};
       my $out = '';
       my @hex = split /;/, $raw;
       foreach (@hex) {
          s/^&#x([a-fA-F0-9]+)$/0x$1/;
          $out .= chr(hex($_));
       }
       return $out;
    } else {
       return "&$name;";
    }
    return (exists $ENTITIES{$name}) ?
	$ENTITIES{$name} : "&$name;" ;
}

1;
__END__

=head1 NAME

MathML::Entities - Convert XHTML+MathML Named Entities to Numeric Character References

=head1 SYNOPSIS

 use MathML::Entities;
 
 $html    = '<b>&copy;</b> by me';
 
 # convert named HTML entities to character references:
 $numeric = name2numbered($html);    # <b>&#x000A9;</b> by me

 # convert named HTML entities to character references:
 $utf8 = name2utf8($html);    # utf8 
 
=head1 DESCRIPTION

MathML::Entities a content conversion filter for named
XHTML+MathML entities. There are over two thousand named entities in the
XHTML+MathML DTD. All the Entities defined in the XHTML+MathML DTD 
except the five "safe" ones (C<&lt;>, C<&gt;>, C<&amp;>, C<&quot;>, C<&apos;>),
will be converted to the equivalent numeric character references or to utf-8 characters.
Named entities which are not in the XHTML+MathML DTD are escaped. This makes
the resulting XHTML (or XHTML+MathML) safe for consumption by non-validating
XML parsers.

Unlike, HTML::Entities, the mapping between MathML named entities and codepoints
is many-to-one. Therefore, there's no particular sense in having an inverse
function, which takes codepoints to named entities.

Based on: HTML::Entities by Koichi Taniguchi E<lt>taniguchi@livedoor.jpE<gt>

=head1 FUNCTIONS

The following functions are exported by default.

=over 4

=item * name2numbered

XHTML+MathML named entities (except for the five "safe" XML named entities)
in the argument of C<name2numbered()> are replaced by the corresponding
numbered character references.

=item * name2utf8

XHTML+MathML named entities (except for the five "safe" XML named entities)
in the argument of C<name2utf8()> are replaced by the corresponding
utf-8 characters.

=back

=head1 AUTHOR

Jacques Distler E<lt>distler@golem.ph.utexas.eduE<gt>

=head1 COPYRIGHT

Copyright (c) 2005-2025 Jacques Distler. All rights reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<HTML::Entities>,
L<HTML::Entities::Numbered>,
L<http://www.w3.org/TR/REC-html40/sgml/entities.html>,
L<http://www.w3.org/Math/characters/>

=cut



( run in 2.819 seconds using v1.01-cache-2.11-cpan-9581c071862 )