bug-texinfo
[Top][All Lists]
Advanced

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

[PATCH] Re-enable copyable anchors in HTML output


From: Arsen Arsenović
Subject: [PATCH] Re-enable copyable anchors in HTML output
Date: Sat, 24 Dec 2022 19:56:25 +0100

* 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/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.
* tp/tests/indices/Makefile.am (EXTRA_DIST): New test case.
* tp/tests/test_scripts/indices_index_entries_relate_to_item.sh:
Ditto.
* 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.1:
Ditto.
* 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2:
Ditto.
* 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html:
Ditto.
* tp/tests/indices/index_entries_relate_to_item.texi: Ditto.
* tp/tests/indices/list-of-tests: Ditto.
* 
tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html:
Update with respect to the new transformation.
---
Evening,

I cleaned up the anchors patch from a bit ago slightly, and added a test
case for it.  I decided to send this out without the related @[fv]table
patch due to time constraints.  I might be able to finish that one this
year, but I can't promise anything.

I also added some documentation about the transformation, but it's
somewhat stubby, and I'm not sure if I got the syntax right.  I can
elaborate on that more if need be.

Thanks in advance, happy holidays!

 ChangeLog                                     | 26 ++++++++
 tp/Texinfo/Common.pm                          | 65 +++++++++++++++++++
 tp/tests/indices/Makefile.am                  |  1 +
 .../indices/index_entries_relate_to_item.texi | 22 +++++++
 tp/tests/indices/list-of-tests                |  2 +
 .../index_entries_relate_to_item.1            |  0
 .../index_entries_relate_to_item.2            |  4 ++
 .../index_entries_relate_to_item.html         | 52 +++++++++++++++
 .../indices_in_begin_tables_lists.html        | 14 ++--
 .../indices_index_entries_relate_to_item.sh   | 19 ++++++
 tp/texi2any.pl                                |  6 ++
 11 files changed, 202 insertions(+), 9 deletions(-)
 create mode 100644 tp/tests/indices/index_entries_relate_to_item.texi
 create mode 100644 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.1
 create mode 100644 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2
 create mode 100644 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html
 create mode 100755 
tp/tests/test_scripts/indices_index_entries_relate_to_item.sh

diff --git a/ChangeLog b/ChangeLog
index a2afc49c78..d7f42c22a3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2022-12-24  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/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.
+       * tp/tests/indices/Makefile.am (EXTRA_DIST): New test case.
+       * tp/tests/test_scripts/indices_index_entries_relate_to_item.sh:
+       Ditto.
+       * 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.1:
+       Ditto.
+       * 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2:
+       Ditto.
+       * 
tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html:
+       Ditto.
+       * tp/tests/indices/index_entries_relate_to_item.texi: Ditto.
+       * tp/tests/indices/list-of-tests: Ditto.
+       * 
tp/tests/indices/res_parser/indices_in_begin_tables_lists/indices_in_begin_tables_lists.html:
+       Update with respect to the new transformation.
+
 2022-12-24  Patrice Dumas  <pertusus@free.fr>
 
        * doc/Makefile.am (TEXINFO_TEX): use in-source texinfo.tex.
diff --git a/tp/Texinfo/Common.pm b/tp/Texinfo/Common.pm
index ddd17ad6b9..e076bdefa7 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,63 @@ 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'};
+
+  # For each table_term in $table->{'contents'}->[0], relate it's content's
+  # first index_entry_command to the term itself.
+  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.
 #
@@ -2571,6 +2630,12 @@ In C<@enumerate> and C<@itemize> from the tree, move 
index entries
 appearing just before C<@item> after the C<@item>.  Comment lines
 between index entries are moved too.
 
+=item relate_index_entries_to_table_items_in_tree($tree)
+X<C<relate_index_entries_to_table_items_in_tree>>
+
+In tables, relates index entries preceding items with said item, by placing it
+inside the entries C<entry_element>.
+
 =item $normalized_name = normalize_top_node_name($node_string)
 X<C<normalize_top_node_name>>
 
diff --git a/tp/tests/indices/Makefile.am b/tp/tests/indices/Makefile.am
index 2056d1eb53..e26e80580e 100644
--- a/tp/tests/indices/Makefile.am
+++ b/tp/tests/indices/Makefile.am
@@ -9,6 +9,7 @@ EXTRA_DIST = index_table.texi index_split.texi \
  same_doc_nr_split_index_and_element.texi \
  printindex_between_part_chapter.texi \
  indices_in_begin_tables_lists.texi \
+ index_entries_relate_to_item.texi \
  list-of-tests res_parser res_parser_info
 
 DISTCLEANFILES = tests.log tests.out
diff --git a/tp/tests/indices/index_entries_relate_to_item.texi 
b/tp/tests/indices/index_entries_relate_to_item.texi
new file mode 100644
index 0000000000..420c3c7bb5
--- /dev/null
+++ b/tp/tests/indices/index_entries_relate_to_item.texi
@@ -0,0 +1,22 @@
+\input texinfo @c -*-texinfo-*-
+
+@setfilename index_entries_relate_to_items
+
+@table @code
+
+@cindex A
+@cindex B
+@cindex C
+@item A
+@itemx B
+@itemx C
+
+body
+
+@item D
+
+body2
+
+@end table
+
+@bye
diff --git a/tp/tests/indices/list-of-tests b/tp/tests/indices/list-of-tests
index 939ff799f1..7007de834b 100644
--- a/tp/tests/indices/list-of-tests
+++ b/tp/tests/indices/list-of-tests
@@ -24,3 +24,5 @@ index_table index_table.texi -c 'TEXI2HTML 1' --split chapter
 
 indices_in_begin_tables_lists indices_in_begin_tables_lists.texi
 indices_in_begin_tables_lists_latex indices_in_begin_tables_lists.texi --latex
+
+index_entries_relate_to_item index_entries_relate_to_item.texi --html
diff --git 
a/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.1
 
b/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.1
new file mode 100644
index 0000000000..e69de29bb2
diff --git 
a/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2
 
b/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2
new file mode 100644
index 0000000000..ed00112c9e
--- /dev/null
+++ 
b/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.2
@@ -0,0 +1,4 @@
+index_entries_relate_to_item.texi:7: warning: entry for index `cp' outside of 
any node
+index_entries_relate_to_item.texi:8: warning: entry for index `cp' outside of 
any node
+index_entries_relate_to_item.texi:9: warning: entry for index `cp' outside of 
any node
+index_entries_relate_to_item.texi: warning: must specify a title with a title 
command or @top
diff --git 
a/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html
 
b/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html
new file mode 100644
index 0000000000..0c206130c2
--- /dev/null
+++ 
b/tp/tests/indices/res_parser/index_entries_relate_to_item/index_entries_relate_to_item.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<html>
+<!-- Created by texinfo, http://www.gnu.org/software/texinfo/ -->
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+<title>Untitled Document</title>
+
+<meta name="description" content="Untitled Document">
+<meta name="keywords" content="Untitled Document">
+<meta name="resource-type" content="document">
+<meta name="distribution" content="global">
+<meta name="Generator" content="texi2any">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+
+<style type="text/css">
+<!--
+a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
+span.program-in-footer {font-size: smaller}
+span:hover a.copiable-link {visibility: visible}
+-->
+</style>
+
+
+</head>
+
+<body lang="en">
+
+<dl class="table">
+<dt><a class="index-entry-id" id="index-B"></a>
+<a class="index-entry-id" id="index-C"></a>
+<a id='index-A'></a><span><code class="code">A</code><a class="copiable-link" 
href='#index-A'> &para;</a></span></dt>
+<dt><code class="code">B</code></dt>
+<dt><code class="code">C</code></dt>
+<dd>
+<p>body
+</p>
+</dd>
+<dt><code class="code">D</code></dt>
+<dd>
+<p>body2
+</p>
+</dd>
+</dl>
+
+<hr>
+<p>
+  <span class="program-in-footer">This document was generated on <em 
class="emph">a sunny day</em> using <a class="uref" 
href="http://www.gnu.org/software/texinfo/";><em 
class="emph">texi2any</em></a>.</span>
+</p>
+
+
+</body>
+</html>
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/tests/test_scripts/indices_index_entries_relate_to_item.sh 
b/tp/tests/test_scripts/indices_index_entries_relate_to_item.sh
new file mode 100755
index 0000000000..2395e45f39
--- /dev/null
+++ b/tp/tests/test_scripts/indices_index_entries_relate_to_item.sh
@@ -0,0 +1,19 @@
+#! /bin/sh
+# This file generated by maintain/regenerate_cmd_tests.sh
+
+if test z"$srcdir" = "z"; then
+  srcdir=.
+fi
+
+one_test_logs_dir=test_log
+
+
+dir=indices
+name='index_entries_relate_to_item'
+mkdir -p $dir
+
+"$srcdir"/run_parser_all.sh -dir $dir $name
+exit_status=$?
+cat $dir/$one_test_logs_dir/$name.log
+exit $exit_status
+
diff --git a/tp/texi2any.pl b/tp/texi2any.pl
index 398dbc9791..147872ef30 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'
            },
@@ -1485,6 +1486,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.39.0




reply via email to

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