Skip to content

Commit

Permalink
Item2254: Wrap tables and associated macros in divs, and provide root…
Browse files Browse the repository at this point in the history
… blocks for other text, to keep TMCE happy. This means we no longer need the { force_root_block: false } setting with TMCE.

The change to Leaf.pm is needed for correct in-browser behaviour - it does not affect the TranslatorTests

I also (re-)discovered the reason why _processTags was splitting on /(\n?%)/ in TML2HTML.pm - it is part of the mechanism for making sure macros at the start of a line stay at the start of a line.


git-svn-id: http://svn.foswiki.org/trunk@7432 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelTempest authored and MichaelTempest committed May 15, 2010
1 parent 711213f commit 05cc5da
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sub generate {
$t =~ s/\t/ /g;
$t =~ s/\n/$WC::CHECKw/g;
$t =~ s/ +/ /g;
$t =~ s/ $/$WC::CHECKw/g;
}
if ( $options & $WC::NOP_ALL ) {

Expand Down
28 changes: 24 additions & 4 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/HTML2TML/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,19 @@ generate TML)
sub rootGenerate {
my ( $this, $opts ) = @_;

#print STDERR "Raw [", WC::debugEncode($this->stringify()), "\n\n";
$this->cleanParseTree();

#print STDERR "Cleaned [", WC::debugEncode($this->stringify()), "]\n\n";
# Perform some transformations on the parse tree
$this->_collapse();

#print STDERR "Collapsed [", WC::debugEncode($this->stringify()), "]\n\n";

my ( $f, $text ) = $this->generate($opts);

# Debug support
#print STDERR "Converted ",WC::debugEncode($text),"\n";
#print STDERR "Converted [",WC::debugEncode($text),"]\n";

# Move leading \n out of protected region. Delicate hack fix required to
# maintain Foswiki variables at the start of lines.
Expand Down Expand Up @@ -1491,11 +1495,27 @@ sub _handleP {

my ( $f, $kids ) = $this->_flatten($options);
return ( $f, '<p>' . $kids . '</p>' ) if ( $options & $WC::NO_BLOCK_TML );
my $pre = '';
if ( $this->prevIsInline() ) {
my $prevNode = $this->{prev};
if ($prevNode and not $prevNode->{tag}) {
$prevNode = $prevNode->{prev};
}
my $afterTable = ($prevNode and uc( $prevNode->{tag} ) eq 'TABLE');
my $nextNode = $this->{next};
if ($nextNode and not $nextNode->{tag}) {
$nextNode = $nextNode->{next};
}
my $beforeTable = ($nextNode and uc( $nextNode->{tag} ) eq 'TABLE');
my $pre;
if ( $afterTable and not $beforeTable) {
$pre = '';
}
elsif ( $this->prevIsInline() ) {
$pre = $WC::NBBR . $WC::NBBR;
}
else {
$pre = $WC::NBBR;
}
return ( $f | $WC::BLOCK_TML, $pre . $WC::NBBR . $kids . $WC::NBBR );
return ( $f | $WC::BLOCK_TML, $pre . $kids . $WC::NBBR );
}

# PARAM
Expand Down
110 changes: 98 additions & 12 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/TML2HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,18 @@ sub _processTags {

return '' unless defined($text);

#SMELL: why put a \n into the split? (at least document it..)
# my @queue = split( /(\n?%)/s, $text );
#SVEN: I've removed the extra \n capture, and am seeing what the tests tell me..
# doing so removes an (so far) unexplaind <br /> that was put into the WYSIWYG_PROTECTED bloc..
# eg. <span class="WYSIWYG_PROTECTED"><br />%TABLESEP%</span>
#SMELL: having removed the \n from the split, the code below now _should_ be rewritten to remove the
# other processing of the \n. For now (Feb2010), I've left it as is, so that we can revert to the old
# code if/when someone figures out why it was in the split in the first place.
my @queue = split( /(%)/s, $text );
# Macros at the start of a line must *stay* at the start of a line.
# The newline preceding the mcro must be preserved.
# This is important for macros like %SEARCH that can emit
# line-oriented TML.
#
# This split captures the preceding newline along with the %,
# if present, as that is a convenient way to include the newline
# in the protected span.
#
# The result is something like this:
# <span class="WYSIWYG_PROTECTED"><br />%TABLESEP%</span>
my @queue = split( /(\n?%)/s, $text );
my @stack;
my $stackTop = '';

Expand Down Expand Up @@ -308,7 +311,8 @@ sub _getRenderedVersion {
$text =~ s/(<\/?(?!(?i:$PALATABLE_HTML)\b)[A-Z]+(\s[^>]*)?>)/
$this->_liftOut($1, 'PROTECTED')/gei;

$text =~ s/\\\n//gs; # Join lines ending in '\'
# SMELL: This was just done, about 25 lines above! Commenting out to see what breaks...
#$text =~ s/\\\n//gs; # Join lines ending in '\'

# Blockquoted email (indented with '> ')
# Could be used to provide different colours for different numbers of '>'
Expand Down Expand Up @@ -350,15 +354,70 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
my $hr = CGI::hr( { class => 'TMLhr' } );
$text =~ s/^---+$/$hr/gm;

# Wrap tables with macros before or after them in a <div>,
# together with the macros,
# so that TMCE may be used without the force_root_block option
my @lines = split( /\n/, $text );
my $divableStartLine = undef;
my $hasTable = 0;
my $hasMacro = 0;
my @divIndexes = ();
for my $lineNumber ( 0 .. $#lines ) {
# Table: | cell | cell |
# allow trailing white space after the last |
if ($lines[$lineNumber] =~ m/^\s*\|.*\|\s*$/) {
$divableStartLine = $lineNumber
if not defined $divableStartLine;
$hasTable = 1;
}

# Macro, after it was lifted out by _processTags
elsif ($lines[$lineNumber] =~ m/$TT1(\d+)$TT2/
and $this->{refs}->[$1]->{text} =~ /^\n?%/) {
$divableStartLine = $lineNumber
if not defined $divableStartLine;
$hasMacro = 1;
}

# Neither table line nor macro
else {
if (defined $divableStartLine) {
if ($hasMacro and $hasTable) {
push @divIndexes, { start => $divableStartLine, end => $lineNumber };
}
undef $divableStartLine;
$hasMacro = 0;
$hasTable = 0;
}
}
}
if (defined $divableStartLine) {
if ($hasMacro and $hasTable) {
push @divIndexes, { start => $divableStartLine, end => $#lines };
}
}
my $tableAndMacrosDivStart = '<div class="foswikiTableAndMacros">';
my $tableAndMacrosDivEnd = '</div><!--foswikiTableAndMacros-->';
while (@divIndexes) {
# Work backwards from the end,
# so that the indexes are correct as they are processed
my $set = pop @divIndexes;
splice @lines, $set->{end}+1, 0, $tableAndMacrosDivEnd;
splice @lines, $set->{start}, 0, $tableAndMacrosDivStart;
}
$text = join( "\n", @lines );

# Now we really _do_ need a line loop, to process TML
# line-oriented stuff.
my $inList = 0; # True when within a list type
my $inTable = 0; # True when within a table type
my %table = ();
my $inParagraph = 1; # True when within a P
my @result = ('<p>');
my $inParagraph = 0; # True when within a P
my $inDiv = 0; # True when within a foswikiTableAndMacros div
my @result = ();

foreach my $line ( split( /\n/, $text ) ) {
my $tableEnded = 0;

# Table: | cell | cell |
# allow trailing white space after the last |
Expand All @@ -375,6 +434,7 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
if ($inTable) {
push( @result, _emitTable( \%table ) );
$inTable = 0;
$tableEnded = 1;
}

if ( $line =~ /$Foswiki::regex{headerPatternDa}/o ) {
Expand Down Expand Up @@ -462,11 +522,34 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
# Extend text of previous list item by dropping through

}
elsif ($line eq $hr) {
push( @result, '</p>' ) if $inParagraph;
$inParagraph = 0;
}
elsif ($line eq $tableAndMacrosDivStart) {
push( @result, '</p>' ) if $inParagraph;
$inParagraph = 0;
$this->_addListItem( \@result, '', '', '' ) if $inList;
$inList = 0;
$inDiv = 1;
}
elsif ($line eq $tableAndMacrosDivEnd) {
$this->_addListItem( \@result, '', '', '' ) if $inList;
$inList = 0;
$inDiv = 0;
# The comment was only needed for this test,
# and it must be removed to prevent it ending up in TML
$line = '</div>';
}
else {

# Other line
$this->_addListItem( \@result, '', '', '' ) if $inList;
$inList = 0;
unless ($inParagraph or $inDiv) {
push( @result, '<p>' );
$inParagraph = 1;
}
}

push( @result, $line );
Expand All @@ -481,6 +564,9 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
elsif ($inParagraph) {
push( @result, '</p>' );
}
elsif ($inDiv) {
push( @result, '</div>' );
}

$text = join( "\n", @result );

Expand Down
Loading

0 comments on commit 05cc5da

Please sign in to comment.