texinfo-commits
[Top][All Lists]
Advanced

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

branch master updated: * tp/Texinfo/Convert/HTML.pm (shared_conversion_s


From: Patrice Dumas
Subject: branch master updated: * tp/Texinfo/Convert/HTML.pm (shared_conversion_state): add shared_conversion_state() for conversion state information shared among conversion functions. It is only used in code that can be customized by the user.
Date: Tue, 25 Jan 2022 17:25:50 -0500

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new e241fba160 * tp/Texinfo/Convert/HTML.pm (shared_conversion_state): add 
shared_conversion_state() for conversion state information shared among 
conversion functions.  It is only used in code that can be customized by the 
user.
e241fba160 is described below

commit e241fba16009f55679d0494e4cc566070d976ca6
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Tue Jan 25 23:25:37 2022 +0100

    * tp/Texinfo/Convert/HTML.pm (shared_conversion_state): add
    shared_conversion_state() for conversion state information
    shared among conversion functions.  It is only used in code
    that can be customized by the user.
---
 ChangeLog                  |   7 +++
 tp/TODO                    |  14 +-----
 tp/Texinfo/Convert/HTML.pm | 107 +++++++++++++++++++++++++++------------------
 3 files changed, 72 insertions(+), 56 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7563ba043c..2336d59d7c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2022-01-25  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/Convert/HTML.pm (shared_conversion_state): add
+       shared_conversion_state() for conversion state information
+       shared among conversion functions.  It is only used in code
+       that can be customized by the user.
+
 2022-01-25  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/Convert/HTML.pm
diff --git a/tp/TODO b/tp/TODO
index 403d3b8c3d..6c443d4eaa 100644
--- a/tp/TODO
+++ b/tp/TODO
@@ -28,19 +28,6 @@ Some private function used in conversion
  _convert_printindex_command
   _new_document_context
 
-Some private variables/private state used in conversion
- _convert_explained_command (actually internal to the function)
-  $self->{'explained_commands'}
-  $self->{'element_explanation_contents'}
- _convert_footnote_command:
-  $foot_num, %footnote_id_numbers
- _convert_printindex_command
-  %formatted_index_entries
- _convert_menu_command
- _convert_menu_entry_type
-  $html_menu_entry_index
-
-
 To document:
 -----------
 
@@ -55,6 +42,7 @@ convert* call
 
 args in *convert*, in %default_commands_args
 
+shared_conversion_state
 
 register_file_information
 
diff --git a/tp/Texinfo/Convert/HTML.pm b/tp/Texinfo/Convert/HTML.pm
index a580319b27..044ee81612 100644
--- a/tp/Texinfo/Convert/HTML.pm
+++ b/tp/Texinfo/Convert/HTML.pm
@@ -1334,6 +1334,24 @@ sub get_value($$)
   }
 }
 
+# $INITIALIZATION_VALUE is only used for the initialization.
+# If it is not a reference, it is turned into a scalar reference.
+sub shared_conversion_state($$;$)
+{
+  my $self = shift;
+  my $state_name = shift;
+  my $initialization_value = shift;
+
+  if (not defined($self->{'shared_conversion_state'}->{$state_name})) {
+    if (not ref($initialization_value)) {
+      $self->{'shared_conversion_state'}->{$state_name} = 
\$initialization_value;
+    } else {
+      $self->{'shared_conversion_state'}->{$state_name} = 
$initialization_value;
+    }
+  }
+  return $self->{'shared_conversion_state'}->{$state_name};
+}
+
 sub register_footnote($$$$$$$)
 {
   my ($self, $command, $footnote_text, $footid, $docid, $number_in_doc,
@@ -2556,8 +2574,6 @@ sub _convert_email_command($$$$)
 
 $default_commands_conversion{'email'} = \&_convert_email_command;
 
-# FIXME set and use 'explained_commands' and 'element_explanation_contents'
-# converter state.  Should there be an API instead?
 sub _convert_explained_command($$$$)
 {
   my $self = shift;
@@ -2572,6 +2588,10 @@ sub _convert_explained_command($$$$)
     = Texinfo::Convert::NodeNameNormalization::normalize_node(
     {'contents' => $command->{'args'}->[0]->{'contents'}});
 
+  my $explained_commands
+    = $self->shared_conversion_state('explained_commands', {});
+  my $element_explanation_contents
+    = $self->shared_conversion_state('element_explanation_contents', {});
   if ($args->[1] and defined($args->[1]->{'string'})
                  and $args->[1]->{'string'} =~ /\S/) {
     $with_explanation = 1;
@@ -2582,31 +2602,30 @@ sub _convert_explained_command($$$$)
     # for recursively-defined acronyms.
     $explanation_result = $self->convert_tree($args->[1]->{'tree'},
                                               "convert $cmdname explanation");
-
-    $self->{'explained_commands'}->{$cmdname}->{$normalized_type} =
+    $explained_commands->{$cmdname}->{$normalized_type} =
        $command->{'args'}->[1]->{'contents'};
-  } elsif ($self->{'element_explanation_contents'}->{$command}) {
+  } elsif ($element_explanation_contents->{$command}) {
     # if an acronym element is formatted more than once, this ensures that
     # only the first explanation (including a lack of explanation) is reused.
     # Note that this means that acronyms converted first on a sectioning
     # command line for a direction text may not get the explanation
     # from acronyms appearing later on in the document but before
     # the sectioning command.
-    if (@{$self->{'element_explanation_contents'}->{$command}}) {
+    if (@{$element_explanation_contents->{$command}}) {
       $explanation_string = $self->convert_tree_new_formatting_context(
         {'type' => '_string',
-         'contents' => $self->{'element_explanation_contents'}->{$command}},
+         'contents' => $element_explanation_contents->{$command}},
         $cmdname, $cmdname);
     }
-  } elsif ($self->{'explained_commands'}->{$cmdname}->{$normalized_type}) {
+  } elsif ($explained_commands->{$cmdname}->{$normalized_type}) {
     $explanation_string = $self->convert_tree_new_formatting_context(
                       {'type' => '_string',
-                       'contents' => $self->{'explained_commands'}
+                       'contents' => $explained_commands
                                      ->{$cmdname}->{$normalized_type}},
                                                    $cmdname, $cmdname);
 
-    $self->{'element_explanation_contents'}->{$command}
-       = $self->{'explained_commands'}->{$cmdname}->{$normalized_type};
+    $element_explanation_contents->{$command}
+       = $explained_commands->{$cmdname}->{$normalized_type};
   } else {
     # Avoid ever giving an explanation for this element, even if an
     # explanation could appear later on, for instance if acronym is
@@ -2616,7 +2635,7 @@ sub _convert_explained_command($$$$)
     # @acronym within the explanation could end up referring to the
     # containing @acronym.
 
-    $self->{'element_explanation_contents'}->{$command} = [];
+    $element_explanation_contents->{$command} = [];
   }
   my $result = $args->[0]->{'normal'};
   if (!$self->in_string()) {
@@ -2661,9 +2680,6 @@ sub _convert_anchor_command($$$$)
 
 $default_commands_conversion{'anchor'} = \&_convert_anchor_command;
 
-my $foot_num;
-
-my %footnote_id_numbers;
 sub _convert_footnote_command($$$$)
 {
   my $self = shift;
@@ -2672,9 +2688,10 @@ sub _convert_footnote_command($$$$)
   my $args = shift;
 
   my $number_in_doc;
-  $foot_num++;
+  my $foot_num = $self->shared_conversion_state('footnote_number', 0);
+  ${$foot_num}++;
   if ($self->get_conf('NUMBER_FOOTNOTES')) {
-    $number_in_doc = $foot_num;
+    $number_in_doc = $$foot_num;
   } else {
     $number_in_doc = $self->get_conf('NO_NUMBER_FOOTNOTE_SYMBOL');
   }
@@ -2704,19 +2721,21 @@ sub _convert_footnote_command($$$$)
   if (defined($multi_expanded_region)) {
     # to avoid duplicate names, use a prefix that cannot happen in anchors
     my $target_prefix = "t_f";
-    $footid = $target_prefix.$multi_expanded_region.'_'.$footid.'_'.$foot_num;
-    $docid = $target_prefix.$multi_expanded_region.'_'.$docid.'_'.$foot_num;
+    $footid = $target_prefix.$multi_expanded_region.'_'.$footid.'_'.$$foot_num;
+    $docid = $target_prefix.$multi_expanded_region.'_'.$docid.'_'.$$foot_num;
   } else {
-    if (!defined($footnote_id_numbers{$footid})) {
-      $footnote_id_numbers{$footid} = $foot_num;
+    my $footnote_id_numbers
+      = $self->shared_conversion_state('footnote_id_numbers', {});
+    if (!defined($footnote_id_numbers->{$footid})) {
+      $footnote_id_numbers->{$footid} = $$foot_num;
     } else {
       # This should rarely happen, except for @footnote in @copying and
       # multiple @insertcopying...
       # Here it is not checked that there is no clash with another anchor. 
       # However, unless there are more than 1000 footnotes this should not 
       # happen.
-      $footid .= '_'.$foot_num;
-      $docid .= '_'.$foot_num;
+      $footid .= '_'.$$foot_num;
+      $docid .= '_'.$$foot_num;
       $multiple_expanded_footnote = 1;
     }
   }
@@ -3751,7 +3770,6 @@ sub _indent_with_table($$$;$)
                 ."</td></tr></table>\n";
 }
 
-my $html_menu_entry_index = 0;
 sub _convert_preformatted_command($$$$)
 {
   my $self = shift;
@@ -4123,7 +4141,10 @@ sub _convert_menu_command($$$$)
 
   return $content if ($cmdname eq 'detailmenu');
 
-  $html_menu_entry_index = 0;
+  my $html_menu_entry_index
+    = $self->shared_conversion_state('html_menu_entry_index', 0);
+  $$html_menu_entry_index = 0;
+
   if ($content !~ /\S/) {
     return '';
   }
@@ -4796,8 +4817,6 @@ sub _convert_index_command($$$$)
 }
 $default_commands_conversion{'cindex'} = \&_convert_index_command;
 
-my %formatted_index_entries;
-
 sub _convert_printindex_command($$$$)
 {
   my $self = shift;
@@ -4895,6 +4914,8 @@ sub _convert_printindex_command($$$$)
     .  $self->convert_tree($self->gdt('Section'))
     ."</th></tr>\n" . "<tr><td colspan=\"4\"> ".$self->get_conf('DEFAULT_RULE')
     ."</td></tr>\n";
+  my $formatted_index_entries
+    = $self->shared_conversion_state('formatted_index_entries', {});
   foreach my $letter_entry (@{$index_entries_by_letter->{$index_name}}) {
     my $letter = $letter_entry->{'letter'};
     my $entries_text = '';
@@ -4904,10 +4925,10 @@ sub _convert_printindex_command($$$$)
       # to avoid double error messages set ignore_notice if an entry was
       # already formatted once, for example if there are multiple printindex.
       my $already_formatted;
-      if (!$formatted_index_entries{$index_entry_ref}) {
-        $formatted_index_entries{$index_entry_ref} = 1;
+      if (!$formatted_index_entries->{$index_entry_ref}) {
+        $formatted_index_entries->{$index_entry_ref} = 1;
       } else {
-        $formatted_index_entries{$index_entry_ref}++;
+        $formatted_index_entries->{$index_entry_ref}++;
       }
 
       my $subentries_tree = 
$self->comma_index_subentries_tree($index_entry_ref);
@@ -4918,10 +4939,10 @@ sub _convert_printindex_command($$$$)
       $entry_tree->{'type'} = '_code' if ($index_entry_ref->{'in_code'});
 
       my $entry;
-      if ($formatted_index_entries{$index_entry_ref} > 1) {
+      if ($formatted_index_entries->{$index_entry_ref} > 1) {
         $entry = $self->convert_tree_new_formatting_context($entry_tree,
                        "index $index_name l $letter index entry $entry_nr",
-                   "index formatted 
$formatted_index_entries{$index_entry_ref}")
+                   "index formatted 
$formatted_index_entries->{$index_entry_ref}")
       } else {
         $entry = $self->convert_tree($entry_tree,
                             "index $index_name l $letter index entry 
$entry_nr");
@@ -5473,10 +5494,12 @@ sub _convert_menu_entry_type($$$)
     }
   }
 
-  $html_menu_entry_index++;
+  my $html_menu_entry_index
+    = $self->shared_conversion_state('html_menu_entry_index', 0);
+  ${$html_menu_entry_index}++;
   my $accesskey = '';
-  $accesskey = " accesskey=\"$html_menu_entry_index\"" 
-    if ($self->get_conf('USE_ACCESSKEY') and $html_menu_entry_index < 10);
+  $accesskey = " accesskey=\"$$html_menu_entry_index\"" 
+    if ($self->get_conf('USE_ACCESSKEY') and $$html_menu_entry_index < 10);
 
   my $MENU_SYMBOL = $self->get_conf('MENU_SYMBOL');
   my $MENU_ENTRY_COLON = $self->get_conf('MENU_ENTRY_COLON');
@@ -6556,6 +6579,9 @@ sub _load_htmlxref_files {
 #  output_init_conf
 #
 #     API exists
+#  shared_conversion_state
+#
+#     API exists
 #  current_filename
 #  document_name
 #  destination_directory
@@ -6651,10 +6677,6 @@ sub converter_initialize($)
 {
   my $self = shift;
 
-  $foot_num = 0;
-  %formatted_index_entries = ();
-  %footnote_id_numbers = ();
-
   %{$self->{'css_element_class_styles'}} = %css_element_class_styles;
 
   _load_htmlxref_files($self);
@@ -8899,10 +8921,7 @@ sub convert($$)
 
   my $result = '';
 
-  # FIXME the document_name and destination_directory arguments are undef.
-  # If a converter is reused, it could be possible to set before and reuse
-  # here something like $self->{'document_name'}
-  # but it is unclear if it is correct or not.
+  $self->{'shared_conversion_state'} = {};
 
   # call before _prepare_conversion_tree_units, which calls _translate_names.
   # Some informations are not set yet.
@@ -9106,6 +9125,8 @@ sub output($$)
 
   $self->{'current_filename'} = undef;
 
+  $self->{'shared_conversion_state'} = {};
+
   # setup informations once here, to have some information for
   # run_stage_handlers.  Some informations are not set yet.
   $self->_reset_infos();



reply via email to

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