Diff for /loncom/xml/lontable.pm between versions 1.17 and 1.22

version 1.17, 2011/04/13 10:08:06 version 1.22, 2014/12/15 00:52:40
Line 57  package Apache::lontable; Line 57  package Apache::lontable;
 use strict;  use strict;
 use Apache::lonlatextable;  use Apache::lonlatextable;
 use Apache::lonnet; # for trace logging.  use Apache::lonnet; # for trace logging.
 use Data::Dumper;  
   
 my $tracing = 1; # Set to 1 to enable log tracing. 2 for local sub tracing.  my $tracing = 0; # Set to 1 to enable log tracing. 2 for local sub tracing.
   
 =pod  =pod
   
Line 272  sub new { Line 271  sub new {
     'foot'     => []      'foot'     => []
  },   },
  col_widths      => {},   col_widths      => {},
  part           => 'body'     # one of 'body', 'head', 'foot'.   part           => 'body',     # one of 'body', 'head', 'foot'.
    colgroups      => []      # Stores information about column groups.
   
     };      };
   
     foreach my $key (keys %$configuration) {      foreach my $key (keys(%$configuration)) {
  $self->{$key} = $$configuration{$key};   $self->{$key} = $$configuration{$key};
     }      }
   
Line 500  sub start_row { Line 500  sub start_row {
     # Override the defaults if the config hash is present:      # Override the defaults if the config hash is present:
   
     if (defined($config)) {      if (defined($config)) {
  foreach my $key  (keys %$config) {   foreach my $key  (keys(%$config)) {
     $row_hash->{$key} = $config->{$key};      $row_hash->{$key} = $config->{$key};
  }   }
     }      }
Line 533  sub end_row { Line 533  sub end_row {
  # Mostly we need to determine if this row has the maximum   # Mostly we need to determine if this row has the maximum
  # cell count of any row in existence in the table:   # cell count of any row in existence in the table:
   
  &Apache::lonnet::logthis($self->{'part'});  
  &Apache::lonnet::logthis(Dumper($self->{'rows'}));  
  my $row        = $self->{'rows'}->{$self->{'part'}}->[-1];   my $row        = $self->{'rows'}->{$self->{'part'}}->[-1];
  my $cells      = $row->{'cells'};   my $cells      = $row->{'cells'};
   
Line 584  sub configure_row { Line 583  sub configure_row {
     }      }
           
     my $row = $self->{'rows'}->{$self->{'part'}}->[-1];      my $row = $self->{'rows'}->{$self->{'part'}}->[-1];
     foreach my $config_item (keys %$config) {      foreach my $config_item (keys(%$config)) {
  $row->{$config_item} = $config->{$config_item};   $row->{$config_item} = $config->{$config_item};
     }      }
 }  }
Line 932  define the column group. Line 931  define the column group.
 sub define_colgroup {  sub define_colgroup {
     my ($this, $attributes)  = @_;      my ($this, $attributes)  = @_;
     if ($tracing) { &Apache::lonnet::logthis("col_group"); }      if ($tracing) { &Apache::lonnet::logthis("col_group"); }
           my $colgroups = $this->{'colgroups'};
       push(@$colgroups, $attributes); # Colgroups always add at end.
   
   
 }  }
   
Line 968  sub generate { Line 969  sub generate {
     my $cell_lr_border = (($inner_border == 1) || ($inner_border == 3)) ? 1 : 0;      my $cell_lr_border = (($inner_border == 1) || ($inner_border == 3)) ? 1 : 0;
     my $part_border   = ($inner_border == 4);      my $part_border   = ($inner_border == 4);
     
     # Add the caption if supplied.   
         # Add the caption if supplied.
   
     if ($this->{'caption'} ne "") {      if ($this->{'caption'} ne "") {
  $table->set_caption($this->caption);   $table->set_caption($this->caption);
Line 994  sub generate { Line 996  sub generate {
   
  my $specified_width = 0.0;   my $specified_width = 0.0;
  my $specified_cols   = 0;   my $specified_cols   = 0;
  foreach my $col (keys %$colwidths) {   foreach my $col (keys(%$colwidths)) {
     $specified_width = $specified_width + $colwidths->{$col};      $specified_width = $specified_width + $colwidths->{$col};
     $specified_cols++;      $specified_cols++;
  }   }
Line 1024  sub generate { Line 1026  sub generate {
     $default_width = ($tablewidth/$unspecified_cols);                     # 'fair' default width.      $default_width = ($tablewidth/$unspecified_cols);                     # 'fair' default width.
     my $width_remaining = $tablewidth - $default_width*$unspecified_cols; # What's left for the specified cols.      my $width_remaining = $tablewidth - $default_width*$unspecified_cols; # What's left for the specified cols.
     my $reduction       = $tablewidth/$width_remaining;                    # Reduction fraction for specified cols      my $reduction       = $tablewidth/$width_remaining;                    # Reduction fraction for specified cols
     foreach my $col (keys %$colwidths) {      foreach my $col (keys(%$colwidths)) {
  $colwidths->{$col} = $colwidths->{$col}/$reduction;   $colwidths->{$col} = $colwidths->{$col}/$reduction;
     }      }
           
         }          }
     }      }
       # If rule is groups. we need to have a 
     if ($tracing) { &Apache::lonnet::logthis("rendering head"); }      # list of the column numbers at which a column ends...
     $this->render_part('head', $table, $useP, $default_width);      # and the coldef needs to start with a |
     if ($tracing) { &Apache::lonnet::logthis("rendering body"); }      #
     $this->render_part('body', $table, $useP, $default_width);      my @colgroup_ends;
     if ($tracing) { &Apache::lonnet::logthis("rendering footer"); }      my $colgroup_col = 0;
     $this->render_part('foot', $table, $useP, $default_width);      my $group = 0;
       my $coldef = "";
       if ($outer_border || $cell_lr_border) {
    $coldef .= '|';
       }
       if ($part_border) {
    $coldef .= '|';
    my $colgroup_col = 0;
    my $colgroups = $this->{'colgroups'};
    foreach my $group (@$colgroups) {
       if (defined $group->{'span'}) {
    $colgroup_col += $group->{'span'};
       } else {
    $colgroup_col++;
       }
       push(@colgroup_ends, $colgroup_col);
    }
     
       }
       $this->render_part('head', $table, $useP, $default_width, 
          \@colgroup_ends);
       $this->render_part('body', $table, $useP, $default_width,
    \@colgroup_ends);
       $this->render_part('foot', $table, $useP, $default_width,
    \@colgroup_ends);
   
   
   
   
           
     my $coldef = "";  
     if ($outer_border || $cell_lr_border) {  
  $coldef .= '|';  
     }  
     for (my $i =0; $i < $column_count; $i++) {      for (my $i =0; $i < $column_count; $i++) {
  if ($useP) {   if ($useP) {
     $coldef .= "p{$default_width $colunits}";      $coldef .= "p{$default_width $colunits}";
Line 1056  sub generate { Line 1078  sub generate {
     ($outer_border && ($i == $column_count-1))) {      ($outer_border && ($i == $column_count-1))) {
     $coldef .= '|';      $coldef .= '|';
  }   }
    if ($part_border && ($i == ($colgroup_ends[$group]-1)))  {
       $coldef .= '|';
       $group++;
    }
     }      }
     $table->{'coldef'} = $coldef;      $table->{'coldef'} = $coldef;
   
Line 1100  sub size_to_cm { Line 1126  sub size_to_cm {
 #  respectively.  #  respectively.
 #  #
 sub render_part {  sub render_part {
     my ($this, $part, $table, $useP, $default_width) = @_;      my ($this, $part, $table, $useP,
    $default_width, $colgroup_ends) = @_;
   
     if ($tracing) { &Apache::lonnet::logthis("render_part: $part") };      if ($tracing) { &Apache::lonnet::logthis("render_part: $part") };
   
     # Do nothing if that part of the table is empty:      # Do nothing if that part of the table is empty:
   
     &Apache::lonnet::logthis(Dumper($this->{'rows'}));  
     if ($this->{'rows'}->{$part} == undef) {      if ($this->{'rows'}->{$part} == undef) {
  if ($tracing) {&Apache::lonnet::logthis("$part is empty"); }   if ($tracing) {&Apache::lonnet::logthis("$part is empty"); }
  return;   return;
     }      }
   
       my @cgends = @$colgroup_ends;
     # Build up the data:      # Build up the data:
   
     my @data;      my @data;
     my $colwidths        = $this->{'col_widths'};      my $colwidths        = $this->{'col_widths'};
     my $rows      = $this->{'rows'}->{$part}; # TODO: Render header, body footer as groups.      my $rows      = $this->{'rows'}->{$part}; 
     my $row_count = scalar(@$rows);      my $row_count = scalar(@$rows);
     my $inner_border = $this->{'inner_border'};      my $inner_border = $this->{'inner_border'};
     my $outer_border = $this->{'outer_border'};      my $outer_border = $this->{'outer_border'};
Line 1145  sub render_part { Line 1171  sub render_part {
  my $startcol   = 1;   my $startcol   = 1;
  my @underlines; # Array of \cline cells if cellborder on.   my @underlines; # Array of \cline cells if cellborder on.
   
    my $colgroup_count = @cgends; # Number of column groups.
    my $cgroup         = 0;     # Group we are on.
    my $cgstart        = 0;     # Where the next cgroup starts.
   
  for (my $cell  = 0; $cell < $cell_count; $cell++) {   for (my $cell  = 0; $cell < $cell_count; $cell++) {
     my $contents = $cells->[$cell]->{'contents'};      my $contents = $cells->[$cell]->{'contents'};
       
     #      #
     #  Cell alignment is the default alignment unless      #  Cell alignment is the default alignment unless
     #  explicitly specified in the cell.      #  explicitly specified in the cell.
Line 1198  sub render_part { Line 1226  sub render_part {
     if ($cell_lr_border || ($outer_border && ($cell == ($cell_count -1)))) {      if ($cell_lr_border || ($outer_border && ($cell == ($cell_count -1)))) {
  $col_align = $col_align.'|';   $col_align = $col_align.'|';
     }      }
       if ($part_border)  {
    if ($cell == $cgstart) {
       $col_align = '|' . $col_align;
       if ($cgroup < $colgroup_count) {
    $cgstart = $cgends[$cgroup];
    $cgroup++;
       } else {
    $cgstart = 1000000; # TODO: Get this logic right
       }
       if ($cell == ($cell_count - 1) &&
    ($cell == ($cgstart-1))) {
    $col_align = $col_align . '|'; # last col ends colgrp.
       }
    }
       }
   
     #factor in spans:      #factor in spans:
   
     my $cspan    = $cells->[$cell]->{'colspan'};      my $cspan    = $cells->[$cell]->{'colspan'};
     my $nextcol  = $startcol + $cspan;      my $nextcol  = $startcol + $cspan;
       
       # At this point this col is the start of the span.
       # nextcol is the end of the span.
   
     # If we can avoid the \multicolumn directive that's best as      # If we can avoid the \multicolumn directive that's best as
     # that makes some things like \parpic invalid in LaTeX which      # that makes some things like \parpic invalid in LaTeX which
Line 1224  sub render_part { Line 1270  sub render_part {
  push(@underlines, "\\cline{$startcol-$lastcol}");   push(@underlines, "\\cline{$startcol-$lastcol}");
     }      }
     $startcol = $nextcol;      $startcol = $nextcol;
   
     # Rowspans should take care of themselves.      # Rowspans should take care of themselves.
           
     push(@row, $contents);      push(@row, $contents);

Removed from v.1.17  
changed lines
  Added in v.1.22


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>