texinfo-commits
[Top][All Lists]
Advanced

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

texinfo/tp Texinfo/Parser.pm Texinfo/Structurin...


From: Patrice Dumas
Subject: texinfo/tp Texinfo/Parser.pm Texinfo/Structurin...
Date: Fri, 03 Dec 2010 00:55:10 +0000

CVSROOT:        /sources/texinfo
Module name:    texinfo
Changes by:     Patrice Dumas <pertusus>        10/12/03 00:55:10

Modified files:
        tp/Texinfo     : Parser.pm Structuring.pm 
        tp/t           : test_sort.t 

Log message:
        New function to sort index entries.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/Texinfo/Parser.pm?cvsroot=texinfo&r1=1.156&r2=1.157
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/Texinfo/Structuring.pm?cvsroot=texinfo&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/t/test_sort.t?cvsroot=texinfo&r1=1.1&r2=1.2

Patches:
Index: Texinfo/Parser.pm
===================================================================
RCS file: /sources/texinfo/texinfo/tp/Texinfo/Parser.pm,v
retrieving revision 1.156
retrieving revision 1.157
diff -u -b -r1.156 -r1.157
--- Texinfo/Parser.pm   1 Dec 2010 22:39:43 -0000       1.156
+++ Texinfo/Parser.pm   3 Dec 2010 00:55:07 -0000       1.157
@@ -227,8 +227,9 @@
   'novalidate' => 0,         # same as setting @novalidate.
   'encoding' => 'us-ascii',  # Current encoding set by @documentencoding
                              # and normalized
-  'documentlanguage' => 'en' # Current documentlanguage set by 
@documentlanguage
-                             # or at initialization
+  'documentlanguage' => 'en', # Current documentlanguage set by 
+                              # @documentlanguage or at initialization
+  'enable_encoding' => 1      # corresponds to --enable-encoding.
 );
 
 # The commands in initialization_overrides are not set in the document if
@@ -521,7 +522,7 @@
 
 # enter all the commands associated with an index name using the prefix
 # list
-sub _enter_index_commands ($$)
+sub _register_index_commands ($$)
 {
   my $self = shift;
   my $index_name = shift;
@@ -607,7 +608,7 @@
     }
   }
   foreach my $index (keys (%{$parser->{'index_names'}})) {
-    $parser->_enter_index_commands($index);
+    $parser->_register_index_commands($index);
   }
   $parser->{'errors_warnings'} = [];
   $parser->{'errors_nrs'} = 0;
@@ -1651,7 +1652,7 @@
                     };
   $index_entry->{'def'} = 1 if ($def_commands{$command});
   $index_entry->{'node'} = $self->{'current_node'} if 
($self->{'current_node'});
-  push @{$self->{'index_entries'}->{'index_name'}}, $index_entry;
+  push @{$self->{'index_entries'}->{$index_name}}, $index_entry;
   $current->{'extra'}->{'index_entry'} = $index_entry;
 }
 
@@ -3648,7 +3649,7 @@
         $in_code = 1 if ($command eq 'defcodeindex');
         $args = [$name];
         $self->{'index_names'}->{$name} = {$name => $in_code};
-        $self->_enter_index_commands($name);
+        $self->_register_index_commands($name);
       }
     } else {
       _line_error ($self, sprintf($self->

Index: Texinfo/Structuring.pm
===================================================================
RCS file: /sources/texinfo/texinfo/tp/Texinfo/Structuring.pm,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- Texinfo/Structuring.pm      1 Dec 2010 22:39:44 -0000       1.20
+++ Texinfo/Structuring.pm      3 Dec 2010 00:55:07 -0000       1.21
@@ -24,7 +24,7 @@
 use 5.00405;
 use strict;
 
-# for debugging only
+# for debugging.  Also for index entries sorting.
 use Texinfo::Convert::Text;
 # for error messages 
 use Texinfo::Convert::Texinfo;
@@ -44,6 +44,7 @@
   sectioning_structure  
   nodes_tree
   number_floats
+  sort_indices
 ) ] );
 
 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
@@ -482,4 +483,46 @@
   }
 }
 
+sub _sort_subroutine($$)
+{
+  my $key1 = shift;
+  my $key2 = shift;
+  my $a = uc($key1->{'key'});
+  my $b = uc($key2->{'key'});
+  my $res = ((($a =~ /^[[:alpha:]]/ and $b =~ /^[[:alpha:]]/) or
+            ($a !~ /^[[:alpha:]]/ and $b !~ /^[[:alpha:]]/)) && $a cmp $b)
+             || ($a =~ /^[[:alpha:]]/ && 1) || -1;
+  return $res;
+}
+
+sub _do_index_keys($$)
+{
+  my $self = shift;
+  my $index_entries = shift;
+  my $options = {'sort_string' => 1};
+  if ($self->{'enable_encoding'} and $self->{'encoding'}) {
+    $options->{'enable_encoding'} = $self->{'encoding'};
+  }
+  foreach my $index_name (keys(%$index_entries)) {
+    foreach my $entry (@{$index_entries->{$index_name}}) {
+      $entry->{'key'} = Texinfo::Convert::Text::convert(
+                              {'contents' => $entry->{'content'}},
+                              $options);
+    }
+  }
+}
+
+sub sort_indices ($$)
+{
+  my $self = shift;
+  my $index_entries = shift;
+  my $sorted_index_entries;
+  _do_index_keys($self, $index_entries);
+  foreach my $index_name (keys(%$index_entries)) {
+    @{$sorted_index_entries->{$index_name}} = 
+        sort _sort_subroutine @{$index_entries->{$index_name}};
+  }
+  return $sorted_index_entries;
+}
+
 1;

Index: t/test_sort.t
===================================================================
RCS file: /sources/texinfo/texinfo/tp/t/test_sort.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- t/test_sort.t       2 Dec 2010 09:17:32 -0000       1.1
+++ t/test_sort.t       3 Dec 2010 00:55:08 -0000       1.2
@@ -1,11 +1,13 @@
 use strict;
 
 use Test::More;
-BEGIN { plan tests => 4 };
+BEGIN { plan tests => 5 };
 
 use lib '../texi2html/lib/Unicode-EastAsianWidth/lib/';
 use Texinfo::Convert::Text;
 use Texinfo::Parser;
+use Texinfo::Structuring;
+use Test::Deep;
 
 ok(1, "modules loading");
 
@@ -22,3 +24,30 @@
                                       'enable_encoding' => 'iso-8859-1'});
 
 is ($result, "\x{00A9} ,,", 'sort iso-8859-1');
+
+my $parser = Texinfo::Parser::parser();
+$tree = $parser->parse_texi_text('@node Top
+
address@hidden !
address@hidden e
address@hidden E
address@hidden ``
address@hidden @~e
address@hidden
address@hidden aaaaaaaaaaaa
+');
+
+my ($index_names, $merged_indices, $index_entries) = 
+   $parser->indices_information();
+my $sorted_index_entries = Texinfo::Structuring::sort_indices($tree, 
$index_entries);
+
+my @entries = ();
+foreach my $entry (@{$sorted_index_entries->{'cp'}}) {
+  push @entries, $entry->{'key'};
+}
+
+my @entries_ref = ('!', '``', 'aaaaaaaaaaaa', 'E', 'e', 'e~');
+
+cmp_deeply (address@hidden, address@hidden, 'sorted index entries');
+
+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]