Changeset 3396


Ignore:
Timestamp:
04/10/09 10:37:08 (3 years ago)
Author:
CrawfordCurrie
Message:

Item1394: fixed colour handling

Location:
trunk/WysiwygPlugin
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/WysiwygPlugin/data/System/WysiwygPlugin.txt

    r3047 r3396  
    166166|  Plugin Version: | %$VERSION% | 
    167167|  Change History: | | 
     168|  10 Apr 2009 | Foswikitask:Item1394: fixed colour handling | 
    168169|  03 Dec 2008 | Foswikitask:Item6041: fixed empty bullet list problem. Foswiki version | 
    169170|  22 Oct 2008 | Fixed TWikibug:Item5961 (emphasis), TWikibug:Item6089 (backslash in verbatim) | 
  • trunk/WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/Constants.pm

    r2957 r3396  
    44 
    55use strict; 
    6  
    7 our ( %ALWAYS_BLOCK, $ALWAYS_BLOCK_S, $STARTWW, $ENDWW, $PROTOCOL ); 
    86 
    97# HTML elements that are strictly block type, as defined by 
     
    119# Block type elements do not require 
    1210# <br /> to be generated for newlines on the boundary - see WC::isInline. 
    13 %ALWAYS_BLOCK = map { $_ => 1 } 
     11our %ALWAYS_BLOCK = map { $_ => 1 } 
    1412  qw( ADDRESS BLOCKQUOTE CENTER DIR DIV DL FIELDSET FORM H1 H2 H3 H4 H5 H6 
    1513  HR ISINDEX MENU NOFRAMES NOSCRIPT OL P PRE TABLE UL ); 
    16 $ALWAYS_BLOCK_S = join( '|', keys %ALWAYS_BLOCK ); 
    17  
    18 $STARTWW  = qr/^|(?<=[ \t\n\(\!])/om; 
    19 $ENDWW    = qr/$|(?=[ \t\n\,\.\;\:\!\?\)])/om; 
    20 $PROTOCOL = qr/^(file|ftp|gopher|https?|irc|news|nntp|telnet|mailto):/; 
    21  
    22 our (%KNOWN_COLOUR); 
    23  
    24 # Colours with colour settings in DefaultPreferences. WTF does Foswiki see 
    25 # fit to *redefine* the standard colors? e.g. ORANGE below is *not* orange. 
    26 # For goodness sakes! 
    27 %KNOWN_COLOUR = ( 
     14our $ALWAYS_BLOCK_S = join( '|', keys %ALWAYS_BLOCK ); 
     15 
     16our $STARTWW  = qr/^|(?<=[ \t\n\(\!])/om; 
     17our $ENDWW    = qr/$|(?=[ \t\n\,\.\;\:\!\?\)])/om; 
     18our $PROTOCOL = qr/^(file|ftp|gopher|https?|irc|news|nntp|telnet|mailto):/; 
     19 
     20# Colours with colour settings in DefaultPreferences. 
     21our @TML_COLOURS = ( 
     22    'BLACK',  'MAROON', 'PURPLE', 'PINK',       'RED',   'ORANGE', 
     23    'YELLOW', 'LIME',   'AQUA',   'AQUAMARINE', 'GREEN', 'OLIVE', 
     24    'BROWN',  'NAVY',   'TEAL',   'BLUE',       'GRAY',  'SILVER', 
     25    'WHITE', 
     26); 
     27 
     28# Map of possible colours back to TML %COLOUR%...%ENDCOLOR% 
     29our %HTML2TML_COLOURMAP = ( 
    2830    BLACK      => 'BLACK', 
    2931    '#000000'  => 'BLACK', 
     
    3234    PURPLE     => 'PURPLE', 
    3335    '#800080'  => 'PURPLE', 
    34     PINK       => 'PINK', 
     36    FUCHSIA    => 'PINK', 
    3537    '#FF00FF'  => 'PINK', 
    3638    RED        => 'RED', 
     
    3840    ORANGE     => 'ORANGE', 
    3941    '#FF6600'  => 'ORANGE', 
    40     '#FFA500'  => 'ORANGE',    # HTML standard 
    4142    YELLOW     => 'YELLOW', 
    4243    '#FFFF00'  => 'YELLOW', 
     
    5253    BROWN      => 'BROWN', 
    5354    '#996633'  => 'BROWN', 
    54     '#A52A2A'  => 'BROWN',     # HTML standard 
    5555    NAVY       => 'NAVY', 
    5656    '#000080'  => 'NAVY', 
  • trunk/WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/HTML2TML/Node.pm

    r2957 r3396  
    12811281 
    12821282    # Try to convert font tags into %COLOUR%..%ENDCOLOR% 
    1283     # First extract the colour 
     1283 
     1284    # First extract the colour from a style= param, if we can. 
    12841285    my $colour; 
    1285     if ( $atts{style} ) { 
    1286         my $style = $atts{style}; 
    1287         if ( $style =~ s/(^|\s|;)color\s*:\s*([^\s;]+);?//i ) { 
    1288             $colour = $2; 
    1289             delete $atts{style} if $style =~ /^[\s;]*$/; 
    1290         } 
    1291     } 
    1292     if ( $atts{color} ) { 
     1286    if ( defined $atts{style} 
     1287        && $atts{style} =~ s/(^|\s|;)color\s*:\s*(#?\w+)\s*(;|$)// ) 
     1288    { 
     1289        $colour = $2; 
     1290    } 
     1291 
     1292    # override it with a color= param, if there is one. 
     1293    if ( defined $atts{color} ) { 
    12931294        $colour = $atts{color}; 
    1294         delete $atts{color}; 
    1295     } 
    1296  
    1297     # The presence of the class forces it to be converted to a 
    1298     # Foswiki variable 
    1299     if ( !_removeClass( \%atts, 'WYSIWYG_COLOUR' ) ) { 
    1300         delete $atts{class}; 
    1301         if (   scalar( keys %atts ) > 0 
    1302             || !$colour 
    1303             || $colour !~ /^([a-z]+|#[0-9A-Fa-f]{6})$/i ) 
    1304         { 
    1305             return ( 0, undef ); 
    1306         } 
    1307     } 
    1308  
    1309     # OK, just the colour 
    1310     $colour = $WC::KNOWN_COLOUR{ uc($colour) }; 
    1311     if ( !$colour ) { 
    1312  
    1313         # Not a recognised colour 
    1314         return ( 0, undef ); 
    1315     } 
    1316     my ( $f, $kids ) = $this->_flatten($options); 
    1317     return ( $f, '%' . uc($colour) . '%' . $kids . '%ENDCOLOR%' ); 
     1295    } 
     1296 
     1297    # The presence of the WYSIWYG_COLOR class _forces_ the tag to be 
     1298    # converted to a Foswiki colour macro, as long as the colour is 
     1299    # recognised. 
     1300    if ( hasClass( \%atts, 'WYSIWYG_COLOR' ) ) { 
     1301        my $percentColour = $WC::HTML2TML_COLOURMAP{ uc($colour) }; 
     1302        if ( defined $percentColour ) { 
     1303 
     1304            # All other font information will be lost. 
     1305            my ( $f, $kids ) = $this->_flatten($options); 
     1306            return ( $f, '%' . $percentColour . '%' . $kids . '%ENDCOLOR%' ); 
     1307        } 
     1308    } 
     1309 
     1310    # May still be able to convert if there is no other font information. 
     1311    delete $atts{class} if defined $atts{class} && $atts{class} =~ /^\s*$/; 
     1312    delete $atts{style} if defined $atts{style} && $atts{style} =~ /^[\s;]*$/; 
     1313    delete $atts{color} if defined $atts{color}; 
     1314    if ( defined $colour && !scalar keys %atts ) { 
     1315        my $percentColour = $WC::HTML2TML_COLOURMAP{ uc($colour) }; 
     1316        if ( defined $percentColour ) { 
     1317            my ( $f, $kids ) = $this->_flatten($options); 
     1318            return ( $f, '%' . $percentColour . '%' . $kids . '%ENDCOLOR%' ); 
     1319        } 
     1320    } 
     1321 
     1322    # Either the colour can't be mapped, or we can't do the conversion 
     1323    # without loss of information 
     1324    return ( 0, undef ); 
    13181325} 
    13191326 
     
    14561463    } 
    14571464 
    1458     # Remove all other classes 
    1459     delete $atts{class}; 
     1465    # If we have WYSIWYG_COLOR and the colour can be mapped, then convert 
     1466    # to a macro. 
     1467    if ( _removeClass( \%atts, 'WYSIWYG_COLOR' ) ) { 
     1468        my $colour; 
     1469        if ( $atts{style} ) { 
     1470            my $style = $atts{style}; 
     1471            if ( $style =~ s/(^|\s|;)color\s*:\s*(#?\w+)\s*(;|$)// ) { 
     1472                $colour = $2; 
     1473            } 
     1474        } 
     1475        my $percentColour = $WC::HTML2TML_COLOURMAP{ uc($colour) }; 
     1476        if ( defined $percentColour ) { 
     1477            my ( $f, $kids ) = $this->_flatten($options); 
     1478            return ( $f, '%' . $percentColour . '%' . $kids . '%ENDCOLOR%' ); 
     1479        } 
     1480    } 
     1481 
     1482    # Remove all other (non foswiki) classes 
     1483    if ( defined $atts{class} && $atts{class} !~ /foswiki/ ) { 
     1484        delete $atts{class}; 
     1485    } 
    14601486 
    14611487    if ( $options & $WC::VERY_CLEAN ) { 
    14621488 
    1463         # remove style attribute if cleaning aggressively. Have to do this 
    1464         # because Foswiki generates these. 
    1465         delete $atts{style} if defined $atts{style}; 
     1489        # remove style attribute if cleaning aggressively. 
     1490        #        delete $atts{style} if defined $atts{style}; 
    14661491    } 
    14671492 
     
    14951520    return ( 0, undef ) 
    14961521      unless $this->_isConvertableTable( $options | $WC::NO_BLOCK_TML, 
    1497               \@table ); 
     1522        \@table ); 
    14981523 
    14991524    my $maxrow = 0; 
  • trunk/WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/TML2HTML.pm

    r2957 r3396  
    260260 
    261261    # Handle colour tags specially (hack, hack, hackity-HACK!) 
    262     my $colourMatch = join( '|', grep( /^[A-Z]/, keys %WC::KNOWN_COLOUR ) ); 
    263     while ( $text =~ 
    264         s#%($colourMatch)%(.*?)%ENDCOLOR%#<font color="\L$1\E">$2</font>#og ) 
    265     { 
    266     } 
     262    my $colourMatch = join( '|', grep( /^[A-Z]/, @WC::TML_COLOURS ) ); 
     263    $text =~ s#%($colourMatch)%(.*?)%ENDCOLOR%# 
     264      _getNamedColour($1, $2)#oge; 
    267265 
    268266    # Convert Foswiki tags to spans outside protected text 
     
    508506} 
    509507 
     508sub _getNamedColour { 
     509    my ( $name, $t ) = @_; 
     510    my $epr = Foswiki::Func::getPreferencesValue($name); 
     511 
     512    # Match <font color="x" and style="color:x" 
     513    if ( 
     514        defined $epr 
     515        && (   $epr =~ /color=["'](#?\w+)['"]/ 
     516            || $epr =~ /color\s*:\s*(#?\w+)/ ) 
     517      ) 
     518    { 
     519        return "<span class='WYSIWYG_COLOR' style='color:$1'>$t</span>"; 
     520    } 
     521 
     522    # Can't map to a 'real' colour; leave the variables 
     523    return '%' . $name . '%' . $t . '%ENDCOLOR%'; 
     524} 
     525 
    510526sub _addClass { 
    511527    if ( $_[0] ) { 
  • trunk/WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm

    r2957 r3396  
    15571557        exec => $HTML2TML, 
    15581558        html => <<HERE, 
    1559 <font color="red" class="WYSIWYG_COLOUR">red</font> 
     1559<font color="red" class="WYSIWYG_COLOR">red</font> 
    15601560<font style="color:green">green</font> 
    15611561<font style="border:1;color:blue">blue</font> 
    1562 <font class="WYSIWYG_COLOUR" style="border:1;color:yellow">yellow</font> 
     1562<font class="WYSIWYG_COLOR" style="border:1;color:yellow">yellow</font> 
    15631563<font color="brown">brown</font> 
    15641564HERE 
  • trunk/WysiwygPlugin/test/unit/WysiwygPlugin/WysiwygPluginTests.pm

    r2996 r3396  
    162162    # Strip ASCII header 
    163163    $this->assert_matches( qr/Content-Type: *text\/plain; *charset=UTF-8/i, 
    164                            $out, 
    165                            anal($out) ); 
     164        $out, anal($out) ); 
    166165    $out =~ s/^.*?\r\n\r\n//s; 
    167166 
     
    217216    # Strip ASCII header 
    218217    $this->assert_matches( qr/Content-Type: *text\/plain; *charset=UTF-8/, 
    219                            $out, 
    220         anal($out) ); 
     218        $out, anal($out) ); 
    221219    $out =~ s/^.*?\r\n\r\n//s; 
    222220 
Note: See TracChangeset for help on using the changeset viewer.