<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">##---------------------------------------------------------------------------##
##  File:
##	$Id: MapUTF8.pm,v 1.2 2003/03/05 22:17:15 ehood Exp $
##  Author:
##      Earl Hood       earl@earlhood.com
##  Description:
##	POD after __END__.
##---------------------------------------------------------------------------##
##    Copyright (C) 2002	Earl Hood, earl@earlhood.com
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
##    02111-1307, USA
##---------------------------------------------------------------------------##

package MHonArc::UTF8::MapUTF8;

use strict;
use Unicode::String;
use Unicode::MapUTF8;
use MHonArc::CharMaps;

sub clip {
    use utf8;
    my $str      = \shift;  # Prevent unnecessary copy.
    my $len      = shift;   # Clip length
    my $is_html  = shift;   # If entity references should be considered
    my $has_tags = shift;   # If html tags should be stripped

    my $u = Unicode::String::utf8($$str);

    if (!$is_html) {
      return $u-&gt;substr(0, $len);
    }

    my $text = Unicode::String::utf8('');
    my $subtext;
    my $html_len = $u-&gt;length;
    my($pos, $sublen, $real_len, $semi);
    my $er_len = 0;
    
    for ( $pos=0, $sublen=$len; $pos &lt; $html_len; ) {
	$subtext = $u-&gt;substr($pos, $sublen);
	$pos += $sublen;

	# strip tags
	if ($has_tags) {
	    # Strip full tags
	    $subtext =~ s/&lt;[^&gt;]*&gt;//g;
	    # Check if clipped part of a tag
	    if ($subtext =~ s/&lt;[^&gt;]*\Z//) {
		my $gt = $u-&gt;index('&gt;', $pos);
		$pos = ($gt &lt; 0) ? $html_len : ($gt+1);
	    }
	}

	# check for clipped entity reference
	if (($pos &lt; $html_len) &amp;&amp; ($subtext =~ /\&amp;[^;]*\Z/)) {
	    my $semi = $u-&gt;index(';', $pos);
	    if ($semi &lt; 0) {
		# malformed entity reference
		$subtext .= $u-&gt;substr($pos);
		$pos = $html_len;
	    } else {
		$subtext .= $u-&gt;substr($pos, $semi-$pos+1);
		$pos = $semi+1;
	    }
	}

	# compute entity reference lengths to determine "real" character
	# count and not raw character count.
	while ($subtext =~ /(\&amp;[^;]+);/g) {
	    $er_len += length($1);
	}

	$text .= $subtext;

	# done if we have enough
	$real_len = $text-&gt;length - $er_len;
	if ($real_len &gt;= $len) {
	    last;
	}
	$sublen = $len - ($text-&gt;length - $er_len);
    }
    $text;
}

sub to_utf8 {
    my $text	= shift;
    my $charset = lc shift;
    my $text_r  = ref($text) ? $text : \$text;

    if (Unicode::MapUTF8::utf8_supported_charset($charset)) {
	return Unicode::MapUTF8::to_utf8(
		    {-string =&gt; $$text_r, -charset =&gt; $charset});
    }
    # Invoke fallback implementation.
    require MHonArc::UTF8::MhaEncode;
    return MHonArc::UTF8::MhaEncode::to_utf8($text_r, $charset);
}

sub str2sgml{
    my $text	= shift;
    my $charset = lc shift;
    my $text_r  = ref($text) ? $text : \$text;

    if ($charset eq 'us-ascii') {
	if ($$text_r =~ /[\x80-\xFF]/) {
	    $charset = 'iso-8859-1';
	} else {
	    $$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
	    return $$text_r;
	}
    }
    if ($charset eq 'utf-8' || $charset eq 'utf8') {
	$$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
	return $$text_r;
    }
    if ($charset eq 'utf-8' || $charset eq 'utf8') {
	$$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
	return $$text_r;
    }
    if (Unicode::MapUTF8::utf8_supported_charset($charset)) {
	$$text_r = Unicode::MapUTF8::to_utf8(
	    {-string =&gt; $$text_r, -charset =&gt; $charset});
	$$text_r =~ s/([$HTMLSpecials])/$HTMLSpecials{$1}/go;
	return $$text_r;
    }
    # Invoke fallback implementation.
    require MHonArc::UTF8::MhaEncode;
    return MHonArc::UTF8::MhaEncode::str2sgml($text_r, $charset);
}

##---------------------------------------------------------------------------##
1;
__END__

=head1 NAME

MHonArc::UTF8::MapUTF8 - UTF-8 Unicode::MapUTF8-based routines for MHonArc

=head1 SYNOPSIS

  use MHonArc::UTF8::MapUTF8;

=head1 DESCRIPTION

MHonArc::UTF8::MapUTF8 provides UTF-8 related routines for use in MHonArc
by use Perl's v5.6, or later, Unicode::MapUTF8 module, which is available
via CPAN.

This module is generally not accessed directly since it is used by
MHonArc::UTF8 when determining what encoding routines it can use based
on your perl installation.

=head1 FUNCTIONS

=over

=item C&lt;to_utf8($data, $from_charset, $to_charset)&gt;

Converts C&lt;$data&gt; encoded in C&lt;$from_charset&gt; into UTF-8.
C&lt;$to_charset&gt; is ignored since it assumed to be C&lt;utf-8&gt;.

=item C&lt;str2sgml($data, $charset)&gt;

All data passed in is converted to utf-8 with HTML specials
converted into entity references.

=item C&lt;clip($text, $clip_len, $is_html, $has_tags)&gt;

Clip C&lt;$text&gt; to C&lt;$clip_len&gt; number of characters.

=back

=head1 SEE ALSO

L&lt;MHonArc::UTF8|MHonArc::UTF8&gt;

=head1 VERSION

C&lt;$Id: MapUTF8.pm,v 1.2 2003/03/05 22:17:15 ehood Exp $&gt;

=head1 AUTHOR

Earl Hood, earl@earlhood.com

MHonArc comes with ABSOLUTELY NO WARRANTY and MHonArc may be copied only
under the terms of the GNU General Public License, which may be found in
the MHonArc distribution.

=cut

</pre></body></html>