[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Encoding problem in Gtk2::Ex::PodViewer



On Sun, Jul 27, 2008 at 3:34 PM, Roderich Schupp
<roderich schupp googlemail com> wrote:
> It's a bug in Gtk2::Ex::PodViewer::Parser:

Looking over Gtk2::Ex::PodViewer::Parser, I can't see a reason to
"use bytes" - simply discard it and your example works.
The attached patch also does the following:

- Change parse_from_file: it reads the file line by line into
  a variable and passes it to parse_from_string; but the
  latter just wraps the string into an IO::Handle and then
  calls parse_from_filehandle on it. Instead make parse_from_file
  open the file as an IO::File (in :utf8 mode) and pass that
  directly to parse_from_filehandle.

- Make the pod entity decoder (e.g. E<auml> => "\xE4") understand
  numeric entities, e.g. E<123> or E<0xE4>.

Cheers, Roderich
--- /usr/share/perl5/Gtk2/Ex/PodViewer/Parser.pm	2006-07-11 14:47:36.000000000 +0200
+++ Gtk2/Ex/PodViewer/Parser.pm	2008-07-27 20:27:36.000000000 +0200
@@ -5,11 +5,11 @@
 package Gtk2::Ex::PodViewer::Parser;
 use base 'Pod::Parser';
 use Carp;
+use IO::File;
 use IO::Scalar;
 use vars qw(%ENTITIES $LINK_TEXT_TEMPLATE);
 use Exporter;
 use Locale::gettext;
-use bytes;
 use strict;
 
 require 5.8.0;
@@ -234,7 +234,7 @@
 						my $text = $sequence->parse_tree->raw_text;
 
 						if ($command eq 'E') {
-							$text = $ENTITIES{$text} || $text;
+							$text = _entity($text) || $text;
 
 						} elsif ($command eq 'L') {
 							push(@{$parser->{links}}, [$text, $parser->{iter}->get_offset]);
@@ -299,17 +299,11 @@
 
 sub parse_from_file {
 	my ($self, $file) = @_;
-	if (!open(FILE, '<:utf8', $file)) {
+	if (my $fh = IO::File->new($file, '<:utf8')) {
+		return  $self->parse_from_filehandle($fh);
+	} else {
 		carp("Cannot open '$file': $!");
 		return undef;
-
-	} else {
-		my $data;
-		while (<FILE>) {
-			$data .= $_;
-		}
-		close(FILE);
-		return $self->parse_from_string($data);
 	}
 }
 
@@ -345,11 +339,18 @@
 
 sub decode_entities {
 	my $text = shift;
-	$text =~ s/E<([^<]*)>/$ENTITIES{$1}/g;
+	$text =~ s/E<([^<]*)>/_entity($1)/eg;
 	$text =~ s/\w{1}<([^<]*)>/$1/g;
 	return $text;
 }
 
+sub _entity {
+	my $ent = shift;
+	return $ent =~ /^(0x?)?\d+$/ 
+	    ? chr(defined $1 ? oct($ent) : $ent) 
+	    : $ENTITIES{$ent};
+}
+
 sub source {
 	my $self = shift;
 	return $self->{_source};


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]