texinfo-commits
[Top][All Lists]
Advanced

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

[no subject]


From: Gavin D. Smith
Date: Sat, 23 Dec 2023 16:30:47 -0500 (EST)

branch: master
commit 4ead71a3c2ff8a97fae2d6e135c64bf8984fa346
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Sat Dec 23 21:29:53 2023 +0000

    Headings in Plaintext.pm
    
    * tp/Texinfo/Convert/Plaintext.pm (_text_heading): Incorporate
    code from 'text_heading' in Texinfo/Convert/Text.pm and
    'add_heading_number' in Texinfo/Convert/Utils.pm.  Make $self
    the first argument.  Take an element argument for the heading
    text rather than an already-converted string, and convert it
    with convert_line_new_context.  Centralise code for getting the
    width of the heading text in screen columns.
    * tp/Texinfo/Translations.pm (gdt_string_columns): New function.
    
    This has no effect at present, but is in preparation for encoding
    the output throughout the conversion in Plaintext.pm in order to
    keep track of byte offsets in a simpler and more reliable way.
---
 ChangeLog                       | 17 +++++++++
 tp/Texinfo/Convert/Plaintext.pm | 83 ++++++++++++++++++++++++++++++++++++-----
 tp/Texinfo/Translations.pm      | 17 +++++++++
 3 files changed, 107 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 344b7c96bd..84a265513a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2023-12-23  Gavin Smith <gavinsmith0123@gmail.com>
+
+       Headings in Plaintext.pm
+
+       * tp/Texinfo/Convert/Plaintext.pm (_text_heading): Incorporate
+       code from 'text_heading' in Texinfo/Convert/Text.pm and
+       'add_heading_number' in Texinfo/Convert/Utils.pm.  Make $self
+       the first argument.  Take an element argument for the heading
+       text rather than an already-converted string, and convert it
+       with convert_line_new_context.  Centralise code for getting the
+       width of the heading text in screen columns.
+       * tp/Texinfo/Translations.pm (gdt_string_columns): New function.
+
+       This has no effect at present, but is in preparation for encoding
+       the output throughout the conversion in Plaintext.pm in order to
+       keep track of byte offsets in a simpler and more reliable way.
+
 2023-12-23  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/main/build_perl_info.c (output_unit_to_perl_hash): add
diff --git a/tp/Texinfo/Convert/Plaintext.pm b/tp/Texinfo/Convert/Plaintext.pm
index 51801283a3..08758f62fb 100644
--- a/tp/Texinfo/Convert/Plaintext.pm
+++ b/tp/Texinfo/Convert/Plaintext.pm
@@ -1690,6 +1690,74 @@ sub format_image($$)
   return ('', 0);
 }
 
+my %underline_symbol = (
+  0 => '*',
+  1 => '*',
+  2 => '=',
+  3 => '-',
+  4 => '.'
+);
+
+# Return the text of an underlined heading, possibly indented.
+sub _text_heading($$$;$$)
+{
+  my $self = shift;
+  my $current = shift;
+  my $heading_element = shift;
+  my $numbered = shift;
+  my $indent_length = shift;
+
+  my $number;
+  if ($current->{'extra'}
+      and defined($current->{'extra'}->{'section_number'})
+      and ($numbered or !defined($numbered))) {
+    $number = $current->{'extra'}->{'section_number'};
+  }
+
+  my $heading = $self->convert_line_new_context (
+                         {'type' => 'frenchspacing',
+                          'contents' => [$heading_element]});
+  my ($text, $columns);
+  if (defined($number)) {
+    if ($current->{'cmdname'} eq 'appendix'
+        and $current->{'extra'}->{'section_level'} == 1) {
+      ($text, $columns) = $self->gdt_string_columns(
+                 'Appendix {number} {section_title}',
+                 {'number' => $number, 'section_title' => $heading});
+    } else {
+      ($text, $columns) = $self->gdt_string_columns(
+                 '{number} {section_title}',
+                 {'number' => $number, 'section_title' => $heading});
+    }
+  } else {
+    $text = $heading;
+    $columns = Texinfo::Convert::Unicode::string_width($heading);
+  }
+
+  return '' if ($text !~ /\S/);
+  my $result = $text ."\n";
+  if (defined($indent_length)) {
+    if ($indent_length < 0) {
+      $indent_length = 0;
+    }
+    $result .= (' ' x $indent_length);
+  } else {
+    $indent_length = 0;
+  }
+  my $section_level;
+  if (!defined($current->{'extra'})
+      or !defined($current->{'extra'}->{'section_level'})) {
+    $section_level = Texinfo::Common::section_level($current);
+  } else {
+    $section_level = $current->{'extra'}->{'section_level'};
+  }
+  # $text is indented if indent_length is set, so $indent_length needs to
+  # be subtracted to have the width of the heading only.
+  $result .= ($underline_symbol{$section_level}
+                x ($columns - $indent_length))."\n";
+  return $result;
+}
+
 sub _get_form_feeds($)
 {
   my $form_feeds = shift;
@@ -2562,13 +2630,11 @@ sub _convert($$)
         die if ($old_context ne $command);
         return $result;
       } elsif ($command eq 'titlefont') {
-        $result = $self->convert_line_new_context (
-                     {'type' => 'frenchspacing',
-                      'contents' => [$element->{'args'}->[0]]});
-        $result = Texinfo::Convert::Text::text_heading(
+        $result = $self->_text_heading(
                           {'extra' => {'section_level' => 0},
                            'cmdname' => 'titlefont'},
-                            $result, $self, $self->get_conf('NUMBER_SECTIONS'),
+                            $element->{'args'}->[0],
+                            $self->get_conf('NUMBER_SECTIONS'),
           ($self->{'format_context'}->[-1]->{'indent_level'}) *$indent_length);
         $result =~ s/\n$//; # final newline has its own tree element
         $self->{'empty_lines_count'} = 0 unless ($result eq '');
@@ -2800,14 +2866,11 @@ sub _convert($$)
       }
 
       if ($heading_element) {
-        my $heading = $self->convert_line_new_context (
-                        {'type' => 'frenchspacing',
-                         'contents' => [$heading_element]});
         # @* leads to an end of line, underlying appears on the line below
         # over one line
         my $heading_underlined =
-             Texinfo::Convert::Text::text_heading($element, $heading, $self,
-                                             
$self->get_conf('NUMBER_SECTIONS'),
+             $self->_text_heading($element, $heading_element,
+                           $self->get_conf('NUMBER_SECTIONS'),
                            ($self->{'format_context'}->[-1]->{'indent_level'})
                                            * $indent_length);
         $result .= _add_newline_if_needed($self);
diff --git a/tp/Texinfo/Translations.pm b/tp/Texinfo/Translations.pm
index cac3f6dd10..f4a3e2f271 100644
--- a/tp/Texinfo/Translations.pm
+++ b/tp/Texinfo/Translations.pm
@@ -42,6 +42,8 @@ use Texinfo::Parser;
 
 use Texinfo::DocumentXS;
 
+use Texinfo::Convert::Unicode;
+
 # we want a reliable way to switch locale for the document
 # strings translations so we don't use the system gettext.
 Locale::Messages->select_package ('gettext_pp');
@@ -338,6 +340,21 @@ sub gdt_string($$;$$$)
                              $replaced_substrings);
 }
 
+# Like gdt_string, but additionally return the width of the result in
+# screen columns.
+# TODO: In the future, this function may return an encoded string, and
+# take encoded arguments.  The plan is to save the width in columns before
+# encoding the string.
+sub gdt_string_columns($$;$$$)
+{
+  my ($customization_information, $string, $replaced_substrings,
+      $translation_context, $lang) = @_;
+
+  my $result = gdt_string($customization_information, $string,
+                          $replaced_substrings, $translation_context, $lang);
+  return ($result, Texinfo::Convert::Unicode::string_width($result));
+}
+
 sub replace_substrings($$;$)
 {
   my $customization_information = shift;



reply via email to

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