texinfo-commits
[Top][All Lists]
Advanced

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

texinfo/tp/Texinfo/Convert Info.pm


From: Patrice Dumas
Subject: texinfo/tp/Texinfo/Convert Info.pm
Date: Sat, 18 Dec 2010 17:15:57 +0000

CVSROOT:        /sources/texinfo
Module name:    texinfo
Changes by:     Patrice Dumas <pertusus>        10/12/18 17:15:57

Added files:
        tp/Texinfo/Convert: Info.pm 

Log message:
        Add the Info converter.  Mostly inherits from the Plaintext converter.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/Texinfo/Convert/Info.pm?cvsroot=texinfo&rev=1.1

Patches:
Index: Info.pm
===================================================================
RCS file: Info.pm
diff -N Info.pm
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ Info.pm     18 Dec 2010 17:15:57 -0000      1.1
@@ -0,0 +1,214 @@
+# Info.pm: output tree as Info.
+#
+# Copyright 2010 Free Software Foundation, Inc.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License,
+# or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# 
+# Original author: Patrice Dumas <address@hidden>
+
+package Texinfo::Convert::Info;
+
+use 5.00405;
+use strict;
+
+use Texinfo::Convert::Plaintext;
+
+require Exporter;
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
address@hidden = qw(Texinfo::Convert::Plaintext);
+
+# Items to export into callers namespace by default. Note: do not export
+# names by default without a very good reason. Use EXPORT_OK instead.
+# Do not simply export all your public functions/methods/constants.
+
+# This allows declaration       use Texinfo::Covert::Text ':all';
+# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
+# will save memory.
+%EXPORT_TAGS = ( 'all' => [ qw(
+  convert
+) ] );
+
address@hidden = ( @{ $EXPORT_TAGS{'all'} } );
+
address@hidden = qw(
+);
+
+$VERSION = '0.01';
+
+sub convert($)
+{
+  my $self = shift;
+  my $root = shift;
+
+  my ($result) = $self->_convert($root);
+  my ($footnotes) = $self->_footnotes();
+  return $result.$footnotes;
+}
+
+sub count_bytes($$) 
+{
+  my $self = shift;
+  my $string = shift;
+
+  if ($self->{'output_encoding'} ne 'us-ascii') {
+    return length(Encode::encode($self->{'output_encoding'}, $string));
+  } else {
+    return length($string);
+  }
+}
+
+sub update_counts ($$$$$$)
+{
+  my $self = shift;
+  my $main_bytes_count = shift;
+  my $main_lines_count = shift;
+  my $main_locations = shift;
+  my $counts = shift;
+  my $locations = shift;
+  if ($locations) {
+    foreach my $line_location (@{$locations->{'lines'}}) {
+      $line_location->{'lines_count'} += $$main_lines_count;
+    }
+    foreach my $byte_location (@{$locations->{'bytes'}}) {
+      $byte_location->{'bytes_count'} += $$main_bytes_count;
+    }
+    push @{$main_locations->{'lines'}}, @{$locations->{'lines'}};
+    push @{$main_locations->{'bytes'}}, @{$locations->{'bytes'}};
+  }
+  if ($counts) {
+    $$main_bytes_count += $counts->{'bytes'};
+    $$main_lines_count += $counts->{'lines'} if ($counts->{'lines'});
+  }
+}
+
+my $footnote_indent = 3;
+sub _footnotes($)
+{
+  my $self = shift;
+  my $bytes_count = 0;
+  my $lines_count = 0;
+  my $locations = {};
+  my $result = '';
+  if (scalar(@{$self->{'pending_footnotes'}})) {
+    unless ($self->{'empty_lines_count'}) {
+      $result .= "\n";
+      $lines_count++;
+    }
+    $result .= "   ---------- Footnotes ----------\n\n";
+    $lines_count += 2;
+    $bytes_count = $self->count_bytes($result);
+    while (@{$self->{'pending_footnotes'}}) {
+      my $footnote = shift (@{$self->{'pending_footnotes'}});
+      # this pushes on 'context', 'format_context' and 'formatters'
+      $self->push_top_formatter('footnote');
+      my $footnote_text = ' ' x $footnote_indent 
+               . "($footnote->{'number'}) ";
+      $result .= $footnote_text;
+      $self->{'format_context'}->[-1]->{'counter'} += 
+         Texinfo::Convert::Unicode::string_width($footnote_text);
+      $bytes_count += $self->count_bytes($footnote_text);
+
+      $self->advance_count_text(\$result, \$bytes_count, \$lines_count,
+               $locations, 0, 
+               $self->_convert($footnote->{'root'}->{'args'}->[0]));      
+      unless ($self->{'empty_lines_count'}) {
+        $result .= "\n";
+        $bytes_count += $self->count_bytes("\n");
+        $lines_count++;
+      }
+      
+      pop @{$self->{'context'}};
+      pop @{$self->{'format_context'}};
+      pop @{$self->{'formatters'}};
+    }
+  }
+  return ($result, {'lines' => $lines_count, 'bytes' => $bytes_count}, 
+          $locations);
+}
+
+sub _align_lines($$$)
+{
+  my $text = shift;
+  my $max_column = shift;
+  my $direction = shift;
+  my $result = '';
+  foreach my $line (split /^/, $text) {
+    chomp($line);
+    $line =~ s/^\s*//;
+    $line =~ s/\s*$//;
+    my $line_width = Texinfo::Convert::Unicode::string_width($line);
+    if ($line_width == 0) {
+      $result .= "\n";
+    } else {
+      my $spaces_prepended;
+      if ($line_width > $max_column) {
+        $spaces_prepended = 0;
+      } elsif ($direction eq 'center') {
+        $spaces_prepended = (($max_column -1 - $line_width) /2);
+      } else {
+        $spaces_prepended = ($max_column -1 - $line_width);
+      }
+      $result .= ' ' x$spaces_prepended . $line ."\n";
+    }
+  }
+  return $result;
+}
+
+#sub _flush_paragraph($$)
+#{
+#  my $self = shift;
+#  my $text = shift;
+#
+#  my $index = -1;
+#  $index--
+#    while 
(!$flush_commands{$self->{'format_context'}->[$index]->{'cmdname'}});
+#  # nothing to do in case of flushleft
+#  if ($self->{'format_context'}->[$index]->{'cmdname'} eq 'flushleft') {
+#    return $text;
+#  }
+#  return _align_lines($text, $self->{'format_context'}->[$index]->{'max'},
+#                          'right');
+#}
+
+sub _contents($$$)
+{
+  my $self = shift;
+  my $section_root = shift;
+  my $contents_or_shortcontents = shift;
+
+  return '';
+}
+
+sub _printindex($$)
+{
+  my $self = shift;
+  my $printindex = shift;
+  return '';
+}
+
+sub _node($$)
+{
+  my $self = shift;
+  my $node = shift;
+  return '';
+}
+
+sub _anchor($$)
+{
+  my $self = shift;
+  my $anchor = shift;
+  return '';
+}
+
+1;



reply via email to

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