help-texinfo
[Top][All Lists]
Advanced

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

Re: Relating multiple index entries to one table item


From: Arsen Arsenović
Subject: Re: Relating multiple index entries to one table item
Date: Thu, 01 Dec 2022 15:29:34 +0100

Hi,

> It looks like gather_previous_items supports @itemx (at least in the
> Non-XS parser), so I might be able to figure that out tomorrow, sadly, I
> am out of time for the day.  Hopefully, it wouldn't be too hard to
> recover the anchors too (people have taken to like them a lot :) ).

So, I took a crack at this.

I managed to implement a tree transform that does a similar association
that relate_index_entries_to_table_entries_in_tree did, and enabled it
only in the HTML backend, to produce [1] (note the presence of
the copyable anchor) from [2].  I'm not sure the approach I took is
correct, though, but it passes tests (I wasn't entirely sure how to add
a test for this, so I left testing copyable anchors out for now).

I already processed the GCC manual to fix up index entries, so other
parts of invoke.texi should also be correct.  I'll go over the rest of
the manual (mostly) by hand to verify other bits and bobs and find
things to improve as this conversation converges on a solid solution.

Thanks, have a wonderful day.

From 84978985fcc57f2c75c362551ace87dda07c2608 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= <arsen@aarsen.me>
Date: Thu, 1 Dec 2022 15:17:48 +0100
Subject: [PATCH] Re-enable copyable anchors in HTML output

* tp/Texinfo/Common.pm: Implement
relate_index_entries_to_table_items, a transform that finds all
table terms and associates their indices with them, so that the
HTML backend generates a copyable anchor.
* 
tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html:
Update with respect to the new transformation.
* tp/texi2any.pl: Invoke the new
relate_index_entries_to_table_items transform.
(formats_table): Enable the relate_index_entries_to_table_items
transformation on HTML.
---
 ChangeLog                                     | 15 +++++
 tp/Texinfo/Common.pm                          | 62 +++++++++++++++++++
 .../indices_in_begin_tables_lists.html        | 14 ++---
 tp/texi2any.pl                                |  6 ++
 4 files changed, 88 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e7d2f57c5f..9bfba11abb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2022-12-01  Arsen Arsenović  <arsen@aarsen.me>
+
+       Re-enable copyable anchors in HTML output
+
+       * tp/Texinfo/Common.pm: Implement
+       relate_index_entries_to_table_items, a transform that finds all
+       table terms and associates their indices with them, so that the
+       HTML backend generates a copyable anchor.
+       * 
tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html:
+       Update with respect to the new transformation.
+       * tp/texi2any.pl: Invoke the new
+       relate_index_entries_to_table_items transform.
+       (formats_table): Enable the relate_index_entries_to_table_items
+       transformation on HTML.
+
 2022-12-01  Patrice Dumas  <pertusus@free.fr>
 
        Protect spaces in space attributes in XML specific code
diff --git a/tp/Texinfo/Common.pm b/tp/Texinfo/Common.pm
index 8db3972286..988f9494b1 100644
--- a/tp/Texinfo/Common.pm
+++ b/tp/Texinfo/Common.pm
@@ -54,6 +54,7 @@ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 collect_commands_in_tree
 collect_commands_list_in_tree
 move_index_entries_after_items_in_tree
+relate_index_entries_to_table_items_in_tree
 protect_colon_in_tree
 protect_comma_in_tree
 protect_first_parenthesis
@@ -525,6 +526,7 @@ foreach my $output_format_command ('info', 'plaintext',
 my %valid_tree_transformations;
 foreach my $valid_transformation ('simple_menus',
     'fill_gaps_in_sectioning', 'move_index_entries_after_items',
+    'relate_index_entries_to_items',
     'insert_nodes_for_sectioning_commands',
     'complete_tree_nodes_menus', 'regenerate_master_menu',
     'indent_menu_descriptions') {
@@ -2176,6 +2178,66 @@ sub move_index_entries_after_items_in_tree($)
   return modify_tree($tree, \&_move_index_entries_after_items);
 }
 
+# Locates all @tables in the tree, and relocates index entriy groups to be
+# related to the @item that immediately follows them.
+
+sub _relate_index_entries_to_table_items_in($)
+{
+  my $table = shift;
+
+  return unless $table->{'contents'};
+
+  $Data::Dumper::Terse = 1;
+  $Data::Dumper::Indent = 3;
+
+  # For each table_term in $table->{'contents'}->[0], relate it's content's
+  # first index_entry_command to the term itself.
+  ENTRY: foreach my $table_entry (@{$table->{'contents'}}) {
+    next unless $table_entry->{'contents'}
+      and $table_entry->{'type'} eq 'table_entry';
+
+    # AFAIU, there's always a unique term in the first position in an entries
+    # contents.
+    my $term = $table_entry->{'contents'}->[0];
+
+    # Now, to discover the related @index and @item entries.
+    my ($item, $index);
+    foreach my $content (@{$term->{'contents'}}) {
+      if ($content->{'extra'}
+          and $content->{'extra'}->{'index_entry'}) {
+        $index = $content->{'extra'}->{'index_entry'} unless $index;
+      } elsif ($content->{'cmdname'} and $content->{'cmdname'} eq 'item') {
+        $item = $content unless $item;
+      }
+      # If we found both, no need to proceed;
+      last if $item and $index;
+    }
+
+    next unless $item and $index;
+    $index->{'entry_element'} = $item;
+  }
+}
+
+sub _relate_index_entries_to_table_items($$)
+{
+  my $type = shift;
+  my $current = shift;
+
+  return ($current) unless $current->{'cmdname'};
+
+  if ($current->{'cmdname'} eq 'table') {
+    _relate_index_entries_to_table_items_in($current);
+  }
+
+  return ($current);
+}
+
+sub relate_index_entries_to_table_items_in_tree($)
+{
+  my $tree = shift;
+  return modify_tree($tree, \&_relate_index_entries_to_table_items);
+}
+
 # Common to different module, but not meant to be used in user-defined
 # codes.
 #
diff --git 
a/tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html
 
b/tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html
index 592b37203f..b5c6eccc1a 100644
--- 
a/tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html
+++ 
b/tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html
@@ -142,8 +142,7 @@ enum
 </dl>
 
 <dl class="table">
-<dt><a class="index-entry-id" id="index-cindex-in-table"></a>
-<code class="code">abb</code></dt>
+<dt><a id='index-cindex-in-table'></a><span><code class="code">abb</code><a 
class="copiable-link" href='#index-cindex-in-table'> &para;</a></span></dt>
 <dd><p>l&ndash;ine
 </p></dd>
 </dl>
@@ -156,8 +155,7 @@ enum
 </dl>
 
 <dl class="table">
-<dt><a class="index-entry-id" id="index-samp-cindex-in-table"></a>
-&lsquo;<samp class="samp">asamp--bb</samp>&rsquo;</dt>
+<dt><a id='index-samp-cindex-in-table'></a><span>&lsquo;<samp 
class="samp">asamp--bb</samp>&rsquo;<a class="copiable-link" 
href='#index-samp-cindex-in-table'> &para;</a></span></dt>
 <dd><p>l&ndash;ine samp
 </p></dd>
 </dl>
@@ -185,15 +183,13 @@ enum
 </dl>
 
 <dl class="table">
-<dt><a class="index-entry-id" id="index-cindex-after-line"></a>
-&lsquo;<samp class="samp">asamp--bb2</samp>&rsquo;</dt>
+<dt><a id='index-cindex-after-line'></a><span>&lsquo;<samp 
class="samp">asamp--bb2</samp>&rsquo;<a class="copiable-link" 
href='#index-cindex-after-line'> &para;</a></span></dt>
 </dl>
 
 <dl class="table">
-<dt><a class="index-entry-id" id="index-cindex-first"></a>
-<a class="index-entry-id" id="index-second"></a>
+<dt><a class="index-entry-id" id="index-second"></a>
 <a class="index-entry-id" id="index-third"></a>
-&lsquo;<samp class="samp">asamp--bb2</samp>&rsquo;</dt>
+<a id='index-cindex-first'></a><span>&lsquo;<samp 
class="samp">asamp--bb2</samp>&rsquo;<a class="copiable-link" 
href='#index-cindex-first'> &para;</a></span></dt>
 </dl>
 
 <hr>
diff --git a/tp/texi2any.pl b/tp/texi2any.pl
index 3ab174d718..e4ab1ead07 100755
--- a/tp/texi2any.pl
+++ b/tp/texi2any.pl
@@ -589,6 +589,7 @@ my %formats_table = (
              'internal_links' => 1,
              'simple_menu' => 1,
              'move_index_entries_after_items' => 1,
+             'relate_index_entries_to_table_items' => 1,
              'no_warn_non_empty_parts' => 1,
              'module' => 'Texinfo::Convert::HTML'
            },
@@ -1477,6 +1478,11 @@ while(@input_files) {
     next;
   }
 
+  if 
($formats_table{$converted_format}->{'relate_index_entries_to_table_items'}
+      or $tree_transformations{'relate_index_entries_to_table_items'}) {
+    Texinfo::Common::relate_index_entries_to_table_items_in_tree($tree);
+  }
+
   if ($formats_table{$converted_format}->{'move_index_entries_after_items'}
       or $tree_transformations{'move_index_entries_after_items'}) {
     Texinfo::Common::move_index_entries_after_items_in_tree($tree);
-- 
2.38.1

[1] https://www.aarsen.me/~arsen/gcc.html/Warning-Options.html#index-Wpedantic
[2] https://git.sr.ht/~arsen/gcc/tree/5dfb79b7/item/gcc/doc/invoke.texi#L5933
-- 
Arsen Arsenović

Attachment: signature.asc
Description: PGP signature


reply via email to

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