Skip to content

Commit

Permalink
Item13782: Update to Foswiki 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Oct 2, 2015
1 parent 198175c commit 1a7cb35
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 80 deletions.
4 changes: 2 additions & 2 deletions lib/Foswiki/Contrib/TablesContrib.pm
Expand Up @@ -11,8 +11,8 @@ package Foswiki::Contrib::TablesContrib;
use strict;
use warnings;

our $VERSION = '1.04';
our $RELEASE = '08 Sep 2015';
our $VERSION = '1.05';
our $RELEASE = '01 Oct 2015';

our $SHORTDESCRIPTION =
'Tables module for use with 1.1.x Foswiki versions that don\'t have the tables handling';
Expand Down
77 changes: 32 additions & 45 deletions lib/Foswiki/Tables/Parser.pm
Expand Up @@ -85,8 +85,9 @@ interact with tables during a static parse e.g. special macros such
as %EDITTABLE.
If early_line returns a positive result, then the parser will open a
table on the next line, whether or not it is a table line,
and any non-whitespace left in $line will be inserted as text.
table on the next line, whether or not it is a table line, *BUT ONLY
IF* the early_line handler for that next line returns 0. Any
non-whitespace left in $line will be inserted as text.
If it returns a negative result, then any non-whitespace left in
$line will be inserted as text, but no other processing will be performed.
Expand Down Expand Up @@ -115,7 +116,7 @@ sub parse {
my $in_table = 0;

# Are we to create a table even if the next line isn't a table line?
my $always_create_table = 0;
my $require_new_table = 0;

# Depth of tag scopes
my %scope = ( verbatim => 0, literal => 0, include => 0 );
Expand Down Expand Up @@ -153,52 +154,38 @@ sub parse {
next LINE;
}

my $analysis = 0;
my $analysis = 0;
my $dont_change_table = 0;

if ( !_in_blocking_scope( \%scope ) ) {
print STDERR "Processing $line\n" if TRACE;
my $origline = $line;

# Call the per-line event. This handles macros.
$analysis = &$dispatch( 'early_line', $line, $in_table );
if ($analysis) {

print STDERR "early_line returned $analysis\n" if TRACE;
print STDERR
"early_line $analysis, it=$in_table, rnt=$require_new_table, dct=$dont_change_table, line '$line' => "
if TRACE && $analysis;

# If early_line returns > 0, then a new table is to be
# created *even if the next line isn't a table line*
# (delimited by |). This is so EDITTABLE and
# equivalent can either latch on to a following table
# or create a new table just by virtue of the macro
# being present.
if ( $analysis > 0 ) {

# If early_line returns < 0, then the text remaining on
# the line after early_line processing will *not* close
# any open table and will . This is what happens
# when a TABLE macro is seen.
# A macro, such as EDITTABLE, is forcing creation
# of a new table.
print STDERR "open new table\n" if TRACE;

if ( $analysis > 0 ) {
print STDERR "open new table, line is '$line'\n" if TRACE;
if ($in_table) {

# open table has been terminated
print STDERR "Close TABLE\n" if TRACE;
$in_table = 0;
&$dispatch('close_table');
# fall through to allow dispatch of line event,
# which will close the current table and open a
# new one (because $require_new_table is true)
$require_new_table = 1;
}
elsif ( $analysis < 0 ) {
print STDERR "ignore macro\n" if TRACE;

# fall through to allow dispatch of line event
}
$always_create_table = 1;

# Skip blank lines that are inhabited only by
# macros that early_line as > 0. Thus a macro
# like EDITTABLE don't imply a blank line
# (whereas a TABLE macro on a line of it's own
# *does* imply a blank line)
next LINE unless $line =~ /\S/;
}
elsif (TRACE) {
print STDERR "ignore macro, line is '$line'\n" if TRACE;
}
# Don't handle $require_new_table yet if this is a
# blank line (or just contains a TABLE macro)
$require_new_table = 1 unless $in_table;
$dont_change_table = 1 unless $line =~ /\S/;
}

if ( $line =~ m/^\s*\|.*(\|\s*|\\)$/ ) {
Expand All @@ -207,7 +194,7 @@ sub parse {

# A table has been encountered, we don't need to
# force it.
$always_create_table = 0;
$require_new_table = 0;

if ( $line =~ s/\\$// ) {

Expand Down Expand Up @@ -287,7 +274,7 @@ sub parse {
elsif (TRACE) {
print STDERR "blocked: $line\n";
}
if ($in_table) {
if ( $in_table && !$dont_change_table ) {

# open table has been terminated
print STDERR "Close TABLE\n" if TRACE;
Expand All @@ -296,17 +283,17 @@ sub parse {

# fall through to allow dispatch of line event
}
if ($always_create_table) {
if ( $require_new_table && !$dont_change_table ) {

# Something encountered by the early_line handler
# requires the immediate creation of a new table.
print STDERR "*Force* Open TABLE\n" if TRACE;
&$dispatch('open_table');
&$dispatch('close_table');
$always_create_table = 0;
$in_table = 1;
$require_new_table = 0;
}

unless ( $analysis < 0 && $line !~ /\S/ ) {
unless ( $analysis && $line !~ /\S/ ) {
print STDERR "Dispatch $line\n" if TRACE;
&$dispatch( 'line', _rewrite( $line, \@comments ) );
}
Expand All @@ -317,7 +304,7 @@ sub parse {
print STDERR "Close TABLE (mop-up)\n" if TRACE;
&$dispatch('close_table'); #
}
if ($always_create_table) {
if ($require_new_table) {

print STDERR "*Force* Open TABLE (mop-up)\n" if TRACE;
&$dispatch('open_table');
Expand Down
39 changes: 24 additions & 15 deletions lib/Foswiki/Tables/Reader.pm
Expand Up @@ -130,12 +130,13 @@ for table decorators (such as %EDITTABLE) will be unavailable.
sub parse {
my ( $this, $text, $meta ) = @_;

$this->{meta} = $meta;
$this->{active_table} = undef; # Open table
$this->{active_row} = undef; # Open row
$this->{pending_spec} = []; # attributes
$this->{nTables} = 0; # number of tables read so far
$this->{result} = []; # tables and lines of text
$this->{meta} = $meta;
$this->{active_table} = undef; # Open table
$this->{active_row} = undef; # Open row
$this->{on_open_spec} = []; # attributes
$this->{on_close_spec} = []; # attributes
$this->{nTables} = 0; # number of tables read so far
$this->{result} = []; # tables and lines of text

# Dispatch Foswiki::Parser::Table events to this "class"
my $dispatch = sub {
Expand Down Expand Up @@ -222,11 +223,19 @@ sub _early_line {
my $make_table = $this->adjustSpec( $macro, $attrs );

# Remember what we just discovered for when the next table is
# encountered.
push(
@{ $this->{pending_spec} },
{ raw => $spec, tag => $macro, attrs => $attrs }
);
# opened or closed
if ( $make_table > 0 ) {
push(
@{ $this->{on_open_spec} },
{ raw => $spec, tag => $macro, attrs => $attrs }
);
}
else {
push(
@{ $this->{on_close_spec} },
{ raw => $spec, tag => $macro, attrs => $attrs }
);
}

return $make_table; # processing complete, goto next line
}
Expand All @@ -247,15 +256,15 @@ sub line {
sub open_table {
my ( $this, $line ) = @_;

$this->{active_table} = $this->{table_class}->new( $this->{pending_spec} );

# Throw away the params
$this->{pending_spec} = [];
$this->{active_table} = $this->{table_class}->new( $this->{on_open_spec} );
$this->{on_open_spec} = [];
}

# Parser event handler
sub close_table {
my ($this) = @_;
$this->{active_table}->addTagSpecs( $this->{on_close_spec} );
$this->{on_close_spec} = [];
push( @{ $this->{result} }, $this->{active_table} );
$this->{active_table}->number( $this->{nTables}++ );
undef $this->{active_table};
Expand Down
2 changes: 1 addition & 1 deletion lib/Foswiki/Tables/Row.pm
Expand Up @@ -54,7 +54,7 @@ sub new {
# pad out the cols to the width of the format
my $ncols = scalar( @{ $table->{colTypes} } );
while ( defined $cols && scalar(@$cols) < $ncols ) {
push( @$cols, '' );
push( @$cols, ' ' );
}
$this->{cols} = [];
$this->setRow($cols) if $cols;
Expand Down
61 changes: 44 additions & 17 deletions lib/Foswiki/Tables/Table.pm
Expand Up @@ -43,16 +43,17 @@ BEGIN {

=begin TML
---++ ClassMethod new($spec)
---++ ClassMethod new($specs [, $supertag])
Constructor
* =$specs= - array of macro spec hashes for the macros that may
* =$specs= - array of tag specs that
affect this table. Each spec is defined as follows:
* =raw= is the string representation of the macro that
apply to this table. Only required so that the table can be
accurately serialised to TML.
* =tag= - simple string name of the tag
* =attrs= - Foswiki::Attrs for the tag. Note that these may have been
heavily modified due to expansion of =include= parameters.
* =$supertag= - optional tag that overrides all other tags
The following entries in attrs are used:
* =format= - The format of the cells in a row of the table. The format is
defined like a table row, where the cell data specify the type for each
Expand All @@ -74,36 +75,62 @@ The following entries in attrs are used:
* =headerrows= - integer number of rows in the thead
* =footerrows= - integer number of rows in the tfoot
* =extras= - optional Foswiki::Attrs hash of extra attributes
* =initsort=, =sort=, =disableallsort=
=cut

sub new {
my ( $class, $specs ) = @_;
my ( $class, $specs, $supertag ) = @_;
my $this = bless(
{
rows => [],
number => undef,
specs => $specs
rows => [],
number => undef,
specs => [],
supertag => $supertag || 'TABLE',
attrs => {} # combined attributes from all tags
},
$class
);

$this->{colTypes} = [];
foreach my $spec ( @{ $this->{specs} } ) {
if ( $spec->{attrs}->{format} ) {
$this->{colTypes} = $this->parseFormat( $spec->{attrs}->{format} );
}

if ( defined $spec->{attrs}->{headerrows} ) {
$this->{headerrows} = $spec->{attrs}->{headerrows};
}
$this->addTagSpecs($specs);

return $this;
}

sub addTagSpecs {
my ( $this, $specs ) = @_;

# Collapse tag attributes into a single attribute block.
my $attrs = $this->{attrs};
foreach my $spec (@$specs) {

# Record the tag for stringification
push( @{ $this->{specs} }, $spec );
while ( my ( $k, $v ) = each %{ $spec->{attrs} } ) {
next if $k =~ /^_/;

if ( defined $spec->{attrs}->{footerrows} ) {
$this->{footerrows} = $spec->{attrs}->{footerrows};
# $supertag attributes trump all other tags
if ( $spec->{tag} eq $this->{supertag}
|| !defined $this->{attrs}->{$k} )
{
$this->{attrs}->{$k} = $v;
}
}
}

return $this;
if ( $attrs->{format} ) {
$this->{colTypes} = $this->parseFormat( $attrs->{format} );
}

if ( defined $attrs->{headerrows} ) {
$this->{headerrows} = $attrs->{headerrows};
}

if ( defined $attrs->{footerrows} ) {
$this->{footerrows} = $attrs->{footerrows};
}
}

=begin TML
Expand Down Expand Up @@ -458,7 +485,7 @@ sub addRow {
if ( $this->totalRows() ) {
my $count = scalar( @{ $this->{rows}->[0]->{cols} } );
while ( scalar(@vals) < $count ) {
push( @vals, '' );
push( @vals, ' ' );
}
}
push( @vals, '' ) unless scalar(@vals);
Expand Down

0 comments on commit 1a7cb35

Please sign in to comment.