version 1.18, 2011/04/13 10:44:26
|
version 1.19, 2011/04/19 22:30:42
|
Line 1
|
Line 1
|
|
|
# The LearningOnline Network with CAPA |
# The LearningOnline Network with CAPA |
# Generating TeX tables. |
# Generating TeX tables. |
# |
# |
Line 7
|
Line 8
|
# Copyright Michigan State University Board of Trustees |
# Copyright Michigan State University Board of Trustees |
# |
# |
# This file is part of the LearningOnline Network with CAPA (LON-CAPA). |
# This file is part of the LearningOnline Network with CAPA (LON-CAPA). |
# |
|
# LON-CAPA 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 2 of the License, or |
|
# (at your option) any later version. |
# (at your option) any later version. |
# |
# |
# LON-CAPA is distributed in the hope that it will be useful, |
# LON-CAPA is distributed in the hope that it will be useful, |
Line 57 package Apache::lontable;
|
Line 54 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 = 0; # 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. |
|
|
Line 272 sub new {
|
Line 269 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. |
|
|
}; |
}; |
|
|
Line 931 define the column group.
|
Line 929 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 967 sub generate {
|
Line 967 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 1029 sub generate {
|
Line 1030 sub generate {
|
|
|
} |
} |
} |
} |
|
# 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 1055 sub generate {
|
Line 1076 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 1090 sub size_to_cm {
|
Line 1115 sub size_to_cm {
|
} |
} |
|
|
return $size; # Default is cm. |
return $size; # Default is cm. |
|
} |
|
|
|
|
|
|
|
#--------------------------------------------------------------------------- |
|
# |
|
# Private methods: |
|
# |
|
|
|
# |
|
# Convert size with units -> size in cm. |
|
# The resulting size is floating point with no units so that it can be used in |
|
# computation. Note that an illegal or missing unit is treated silently as |
|
# cm for now. |
|
# |
|
sub size_to_cm { |
|
my ($this, $size_spec) = @_; |
|
my ($size, $units) = split(/ /, $size_spec); |
|
if (lc($units) eq 'mm') { |
|
return $size / 10.0; |
|
} |
|
if (lc($units) eq 'in') { |
|
return $size * 2.54; |
|
} |
|
|
|
return $size; # Default is cm. |
} |
} |
|
|
# |
# |
Line 1099 sub size_to_cm {
|
Line 1150 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") }; |
|
|
Line 1110 sub render_part {
|
Line 1162 sub render_part {
|
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 1143 sub render_part {
|
Line 1195 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 1196 sub render_part {
|
Line 1250 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 1222 sub render_part {
|
Line 1294 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); |