texinfo-commits
[Top][All Lists]
Advanced

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

[no subject]


From: Patrice Dumas
Date: Thu, 14 Dec 2023 16:39:30 -0500 (EST)

branch: master
commit 7ebfc7b0f22663cb2c20488249d24a5c5d704295
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Thu Dec 14 16:02:31 2023 +0100

    * tp/Texinfo/XS/main/convert_utils.c (add_heading_number): implement
    the case of translations, with a converter with translations support
    present.
    
    * tp/t/convert_to_text.t: test a call of
    Texinfo::Convert::Utils::add_heading_number from
    Texinfo::Convert::Text with a converter associated to the text
    options.  This cannot happen otherwise, as either there is a converter
    with translations support associated but no conversion of sectioning
    commands, or the converter is the simple Text converter without
    support for translations.
---
 ChangeLog                            |  14 +++++
 tp/Texinfo/Convert/Utils.pm          |   6 ++
 tp/Texinfo/XS/main/convert_to_text.c |   4 +-
 tp/Texinfo/XS/main/convert_utils.c   |  86 +++++++++++++++-----------
 tp/t/convert_to_text.t               | 113 +++++++++++++++++++++++++++++++++++
 5 files changed, 186 insertions(+), 37 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e107a09759..6a9aae0d72 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2023-12-13  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/XS/main/convert_utils.c (add_heading_number): implement
+       the case of translations, with a converter with translations support
+       present.
+
+       * tp/t/convert_to_text.t: test a call of
+       Texinfo::Convert::Utils::add_heading_number from
+       Texinfo::Convert::Text with a converter associated to the text
+       options.  This cannot happen otherwise, as either there is a converter
+       with translations support associated but no conversion of sectioning
+       commands, or the converter is the simple Text converter without
+       support for translations.
+
 2023-12-13  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/convert/convert_html.c (convert_today_command)
diff --git a/tp/Texinfo/Convert/Utils.pm b/tp/Texinfo/Convert/Utils.pm
index 47b7be8c28..24e0eaa2f4 100644
--- a/tp/Texinfo/Convert/Utils.pm
+++ b/tp/Texinfo/Convert/Utils.pm
@@ -348,6 +348,12 @@ sub add_heading_number($$$;$)
 
   my $result;
   if ($self) {
+    # NOTE we reach here when called from Texinfo::Convert::Text
+    # only if associated with a converter.  This should not happen
+    # for sectioning commands, so in practice this code is not reached
+    # from Texinfo::Convert::Text when called from texi2any.pl.
+    # There is a test especially crafted to reach that point in
+    # convert_to_text.t
     if (defined($number)) {
       if ($current->{'cmdname'} eq 'appendix'
           and $current->{'extra'}->{'section_level'} == 1) {
diff --git a/tp/Texinfo/XS/main/convert_to_text.c 
b/tp/Texinfo/XS/main/convert_to_text.c
index 88b53cf3c0..98369e9156 100644
--- a/tp/Texinfo/XS/main/convert_to_text.c
+++ b/tp/Texinfo/XS/main/convert_to_text.c
@@ -424,13 +424,13 @@ convert_to_text_internal (const ELEMENT *element, 
TEXT_OPTIONS *text_options,
     {
       if (element->type == ET_untranslated && text_options->converter)
         {
-/*
+       /*
        the tree documentlanguage corresponds to the documentlanguage
        at the place of the tree, but the converter may want to use
        another documentlanguage, for instance the documentlanguage at
        the end of the preamble, so we let the converter set it.
           documentlanguage = lookup_extra_string (element, "documentlanguage");
-*/
+        */
           char *translation_context
             = lookup_extra_string (element, "translation_context");
           ELEMENT *tree = gdt_tree (element->text.text,
diff --git a/tp/Texinfo/XS/main/convert_utils.c 
b/tp/Texinfo/XS/main/convert_utils.c
index 8068e83ed5..2f1491be73 100644
--- a/tp/Texinfo/XS/main/convert_utils.c
+++ b/tp/Texinfo/XS/main/convert_utils.c
@@ -187,42 +187,58 @@ add_heading_number (OPTIONS *options, const ELEMENT 
*current, char *text,
   if (numbered != 0)
     number = lookup_extra_string (current, "section_number");
 
-  /* TODO translate code to use options as $self translation
-     to be done when this can be tested, so when Text converter
-     is called from another converter
-  if ($self) {
-    if (defined($number)) {
-      if ($current->{'cmdname'} eq 'appendix'
-          and $current->{'extra'}->{'section_level'} == 1) {
-        $result = $self->gdt_string('Appendix {number} {section_title}',
-                   {'number' => $number, 'section_title' => $text});
-      } else {
-        $result = $self->gdt_string('{number} {section_title}',
-                   {'number' => $number, 'section_title' => $text});
-      }
-    } else {
-      $result = $text;
-    }
-  } else
-*/
+  text_init (&result);
 
-  {
-    text_init (&result);
-    if (current->cmd == CM_appendix)
-      {
-        int status;
-        int section_level = lookup_extra_integer (current, "section_level",
-                                                  &status);
-        if (section_level == 1)
-          text_append (&result, "Appendix ");
-      }
-    if (number)
-      {
-        text_append (&result, number);
-        text_append (&result, " ");
-      }
-    text_append (&result, text);
-   }
+  if (options)
+    {
+      if (number)
+        {
+          char *numbered_heading = 0;
+          NAMED_STRING_ELEMENT_LIST *substrings
+                                       = new_named_string_element_list ();
+          add_string_to_named_string_element_list (substrings,
+                                                  "number", number);
+          add_string_to_named_string_element_list (substrings,
+                                             "section_title", text);
+          if (current->cmd == CM_appendix)
+            {
+              int status;
+              int section_level
+                    = lookup_extra_integer (current, "section_level",
+                                            &status);
+              if (section_level == 1)
+                {
+                  numbered_heading
+                   = gdt_string ("Appendix {number} {section_title}",
+                                 options, substrings, 0, 0);
+                }
+            }
+          if (!numbered_heading)
+            numbered_heading = gdt_string ("{number} {section_title}",
+                                          options, substrings, 0, 0);
+          text_append (&result, numbered_heading);
+          free (numbered_heading);
+        }
+      else
+        text_append (&result, text);
+    }
+  else
+    {
+      if (current->cmd == CM_appendix)
+        {
+          int status;
+          int section_level = lookup_extra_integer (current, "section_level",
+                                                    &status);
+          if (section_level == 1)
+            text_append (&result, "Appendix ");
+        }
+      if (number)
+        {
+          text_append (&result, number);
+          text_append (&result, " ");
+        }
+      text_append (&result, text);
+    }
   return result.text;
 }
 
diff --git a/tp/t/convert_to_text.t b/tp/t/convert_to_text.t
new file mode 100644
index 0000000000..56a4095bdb
--- /dev/null
+++ b/tp/t/convert_to_text.t
@@ -0,0 +1,113 @@
+use strict;
+
+use lib '.';
+use Texinfo::ModulePath (undef, undef, undef, 'updirs' => 2);
+
+use Test::More;
+
+BEGIN { plan tests => 2; }
+
+use Texinfo::Parser;
+use Texinfo::Document;
+use Texinfo::Config;
+use Texinfo::Convert::Text;
+use Texinfo::Structuring;
+use Texinfo::Convert::HTML;
+
+
+# This tests a call of Texinfo::Convert::Utils::add_heading_number
+# from Texinfo::Convert::Text with a converter associated to the
+# text options (and similar XS functions).  This cannot happen
+# outside of an especially crafted case because either
+# * conversion to text is called from a another converter (to Plaintext,
+#   HTML...), such that a converter is present, but the Texinfo tree
+#   given to the conversion to text never includes parts of the tree
+#   directly in the root of the tree, so no sectioning commands.
+# * conversion to text is called for a whole document, so with
+#   sectioning commands converted, but the converter associated
+#   is the simple Texinfo::Convert::Text converter, which does not
+#   provides translations.
+#
+# Therefore in this test, a converter is setup, which does nothing
+# except to be used by text conversion for translations, and the
+# Texinfo tree passed in text conversion argument contains sectioning
+# commands.
+#
+# Note that Texinfo::Convert::Utils::add_heading_number is called
+# from Texinfo::Convert::Plaintext converter for sectioning commands
+# with translations support.
+
+
+# setup translated strings
+my $locales_dir = File::Spec->catdir($Texinfo::ModulePath::builddir,
+                                     'LocaleData');
+my $strings_textdomain = 'texinfo_document';
+if (-d $locales_dir) {
+  Texinfo::Translations::configure($locales_dir, $strings_textdomain);
+} else {
+  warn "Locales dir for document strings not found\n";
+}
+
+ok(1);
+
+my $parser = Texinfo::Parser::parser();
+
+my $document = $parser->parse_texi_piece ('
+@documentlanguage fr
+
+@node top
+@top A top
+
+
+@node chap
+@chapter Chap
+
+@node appendix
+@appendix Conclusion
+');
+
+my $tree = $document->tree();
+
+#use Texinfo::DebugTree;
+#print STDERR Texinfo::DebugTree->convert_tree ($tree);
+
+my $registrar = $parser->registered_errors();
+my $main_configuration = Texinfo::MainConfig::new();
+# Setup sectioning commands numbers
+my $sections_list
+            = Texinfo::Structuring::sectioning_structure($tree, $registrar,
+                                                       $main_configuration);
+if ($sections_list) {
+  Texinfo::Document::register_document_sections_list($document,
+                                                     $sections_list);
+}
+$tree = Texinfo::Document::rebuild_tree($tree);
+
+# a converter only used for translation in text conversion, not
+# to convert anything.
+my $converter = Texinfo::Convert::HTML->converter({'document' => $document});
+# note that parse_texi_piece is used, so there is no preamble
+$converter->set_global_document_commands('preamble_or_first',
+                                         ['documentlanguage']);
+#print STDERR "DOCUMENTLANGUAGE 
".$converter->get_conf('documentlanguage')."\n";
+
+my $text_options
+ = {Texinfo::Convert::Text::copy_options_for_convert_text($converter)};
+
+my $result_text
+ = Texinfo::Convert::Text::convert_to_text($document->tree(), $text_options);
+
+is ($result_text, '
+
+A top
+*****
+
+
+1 Chap
+******
+
+Annexe A Conclusion
+*******************
+', 'test add_heading_number translations with a converter');
+
+#print STDERR "RRR '$result_text'\n";



reply via email to

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