File:  [LON-CAPA] / loncom / xml / lonlatextable.pm
Revision 1.3: download - view: text, annotated - select for diffs
Wed Apr 13 10:08:06 2011 UTC (13 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ 6317 - NOTE This commit contains over-extensive debugging output and should not be used in production.
- This commit includes initial work to support the rule="groups" option in
<table> for printing.  Commit because some drastic re-factoring and some data
structure re-engineering was requuired to capture some of the group information.
Anticipate the next commit will support <thead><tbody><tfoot> properly
both with respect to the order in which they appear in the output and
with respect to rules="groups"...if this version does not already do that
correctly...at that time the debug spew will be removed as well.

    1: #  Generating TeX tables.
    2: #
    3: # $Id: lonlatextable.pm,v 1.3 2011/04/13 10:08:06 foxr Exp $
    4: # 
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   28: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   29: # binary executable programs or libraries distributed by the 
   30: # Michigan State University (the "Licensee"), but any binaries so 
   31: # distributed are hereby licensed only for use in the context
   32: # of a program or computational system for which the Licensee is the 
   33: # primary author or distributor, and which performs substantial 
   34: # additional tasks beyond the translation of (La)TeX into HTML.
   35: # The C source of the Code may not be distributed by the Licensee
   36: # to any other parties under any circumstances.
   37: #
   38: 
   39: # This module is a support packkage that helps londefdef generate
   40: # LaTeX tables using the LaTeX::Table package.  A prerequisite is that
   41: # the print generator must have added the following to the LaTeX 
   42: #
   43: #  \usepackage{xtab}
   44: #  \usepackage{booktabs}
   45: #  \usepackage{array}
   46: #  \usepackage{colortbl}
   47: #  \usepackage{xcolor}
   48: #
   49: #  These packages are installed in the packaged LaTeX distributions we know of as of
   50: #  11/24/2008
   51: #
   52: 
   53: 
   54: # 
   55: #  This module provides a sub-set substitute for LaTeX::table 
   56: #  which is a great module but unfortunately not usable in safe space
   57: #  
   58: 
   59: 
   60: =pod
   61: 
   62: =head1  NAME
   63: 
   64: Apache:lonltextable
   65: 
   66: =head1 SYNOPSIS
   67: 
   68:     use Apache::lonlatextable;
   69: 
   70:     $table = Apache::lonlatextable->new();
   71:     $table->set_caption("some text");
   72:     $table->set_data(cell_descriptions);
   73:     $table->{'coldef'} = column_definitions;
   74:     $table->generate_string();
   75: 
   76: =head1 DETAILS
   77: 
   78: =cut
   79: 
   80: package Apache::lonlatextable;
   81: use Data::Dumper;
   82: use Apache::lonnet;
   83: use strict;
   84: 
   85: =pod
   86: 
   87: =head2 new
   88: 
   89: Creates a new instance of a lonlatextable.  The returned value should be used on the left side of the
   90: -> for further calls to object methods
   91: 
   92: =head3 Example:
   93:    
   94:     my $latexTable = &Apache::lonlatextable->new();
   95: 
   96: =cut
   97: 
   98: 
   99: sub new {
  100:     my ($class) = @_;
  101: 
  102:     my $self = {
  103: 	caption           => '',
  104: 	data              => [],
  105: 	coldef            =>  '',
  106: 	
  107:     };
  108:     bless($self, $class);
  109:     return $self;
  110: }
  111: 
  112: =pod
  113: 
  114: =head2 set_caption(some_text)
  115: 
  116: Set the table caption value.  If this string is empty, the table will be generated without a caption.
  117: some_text will be the  new table caption value.
  118: 
  119: =head3 Example:
  120: 
  121:     $table = Apache::lonlatextable->new();
  122:     $table->set_caption("Some very interesting table");
  123: 
  124: =cut
  125: 
  126: sub set_caption {
  127:     my ($self, $caption) = @_;
  128:     $self->{'caption'} = $caption;
  129: }
  130: 
  131: =pod
  132: 
  133: =head2 set_data(cell_descriptions)
  134: 
  135: Sets the table cell data.  Table data in its simplest is an aray of arrays.  Each outer 
  136: array element is a table row. However:
  137:   
  138: =over 3
  139: 
  140: =item Single column rows sarting with \
  141: 
  142: These are treated as LaTeX/TeX commands and, when the table is generated, 
  143: passed without interpretation.
  144: 
  145: =item Lines with one cell that is empty.
  146: 
  147: These produce a horizontal rule that spans the width of the table.
  148: 
  149: =back
  150: 
  151: Calling this multiple times will continue to append cell data.
  152: 
  153: =head3 Example
  154: 
  155:     $table->set_data([ ['Gnu', '92.52'], [], ['Emu', '33.33'] ]);
  156: 
  157: Produces
  158:     +--------------+--------------+
  159:     |   Gnu        | 92.52        |
  160:     +--------------+--------------+ (due to the empty row).
  161:     |   Emu        | 33.33        |
  162:     +--------------+--------------+
  163: 
  164: 
  165: =cut
  166: 
  167: sub set_data {
  168:     my ($self, $data) = @_;
  169:     &Apache::lonnet::logthis("--------------- set_data ------------");
  170:     &Apache::lonnet::logthis(Dumper($self->{'data'}));
  171:     &Apache::lonnet::logthis("---------------------------------------");
  172:     &Apache::lonnet::logthis(Dumper($data));
  173:     my $current_data = $self->{'data'};
  174:     push (@$current_data, @$data);
  175:     $self->{'data'} = $current_data;
  176: 
  177: 
  178: }
  179: 
  180: =pod
  181: 
  182: =head2 Column  definitions.
  183: 
  184: The hash value 'coldef' can overide the column definitions computed by default for the table.
  185: 
  186: =head3 Example
  187: 
  188:     $table->{'coldef'} = 'lrcr';
  189: 
  190: Produces table header output of:
  191: 
  192:     \begin{tabular}{lrcr}
  193: 
  194: The default is to produce a set of left aligned columns.  The number of columns produced
  195: will be determined by the size of the longest row.
  196: 
  197: =cut
  198: 
  199: =pod
  200: 
  201: =head2   $table->generate_string()
  202: 
  203: Generates the LaTeX for the table as a string that is returned to the caller.
  204: 
  205: =head3 Example
  206: 
  207:     $latex = $table->generate_string();
  208: 
  209: =cut
  210: 
  211: sub generate_string {
  212: 
  213:     my ($self) = @_;
  214:     my $result;
  215: 
  216:     $result  = $self->table_header();
  217:     $result .= $self->table_body();
  218:     $result .= $self->table_trailer();
  219: 
  220: 
  221: 
  222:     return $result;
  223: }
  224:  
  225: 
  226: ###################################################################################
  227: #
  228: #  Private methods:
  229: #
  230: 
  231: #  generate the table header.
  232: #  If coldef is an empty string, the default column definition is computed instead.
  233: #
  234: sub table_header()
  235: {
  236:     my ($self) = @_;
  237:     my $result = '\begin{tabular}{';
  238:     my $coldef = $self->{'coldef'};
  239: 
  240:     if ($coldef eq '') {
  241: 	my @rows   = @$self->{'data'};
  242: 	my $longest = 0;
  243: 	foreach my $row (@rows) {
  244: 	    my $length = scalar @{$row};
  245: 	    if ($length > $longest) {
  246: 		$longest = $length;
  247: 	    }
  248:         } 
  249: 	# $longest cells all left aligned.
  250: 	# There's got to be a better way than this in perl?
  251: 
  252: 	for (my $i =0; $i < $longest; $i++) {
  253: 	    $coldef .= 'l';
  254: 	}
  255:     }
  256:     
  257:     $result .= $coldef . '}';
  258:     $result .= "\n";
  259: 
  260:     return $result;
  261: }
  262: #
  263: #   Generate the table body
  264: #
  265: sub table_body()
  266: {
  267:     my ($self) = @_;
  268:     my $result = '';
  269:     foreach my $row (@{$self->{'data'}}) {
  270: 	#
  271: 	# If a row has only a single cell we need to deal with the two special cases
  272: 	# Pass LaTeX uninterpreted or \hline.
  273: 	#
  274: 	if ((scalar @{$row}) == 1) {
  275: 	    my $cell = $row->[0];
  276: 	    if ($cell eq '') {
  277: 		$result .= '\hline' . "\n";
  278: 	    } elsif (substr($cell, 0, 1) eq "\\") {
  279: 		$result .= $cell . "\n";
  280: 	    } else {
  281: 		# could just be a table with one col...
  282: 
  283: 		$result .= $cell . ' \\\\ ' ."\n";
  284: 	    }
  285: 	} else {
  286: 	    my $line = '';
  287: 	    foreach my $cell (@{$row}) {
  288: 		$line .= $cell . ' & ';
  289: 	    }
  290: 	    #  Replace the last ' & ' with \\ and a line terminator..
  291: 	    #  and append the line to the result.
  292: 
  293: 	    $line =~ s/ & $/ \\\\\n/;
  294: 	    $result .= $line;
  295: 	}
  296:     }
  297:     return $result;
  298: }
  299: 
  300: #
  301: #  Generate the table trailer
  302: #
  303: sub table_trailer {
  304:     my ($self) = @_;
  305: 
  306:     my $result = '\end{tabular}' . "\n";
  307:     if ($self->{'caption'} ne '') {
  308: 	$result .= '\caption{' . $self->{'caption'} . '}' . "\n";
  309:     }
  310:     return $result;
  311: }
  312: 
  313: 
  314: 
  315: 1;
  316: __END__

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