Ignore:
Timestamp:
12/17/09 19:28:55 (2 years ago)
Author:
CrawfordCurrie
Message:

Item2511: split macros out of the Foswiki.pm monster

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/lib/Foswiki/Render.pm

    r5792 r5810  
    340340                           # calc COLSPAN 
    341341    $row =~ s/(\|\|+)/ 
    342       'colspan'.$Foswiki::TranslationToken.length($1).'|'/ge; 
     342      'colspan'.$REMARKER.length($1).'|'/ge; 
    343343    my $cells = ''; 
    344344    my $containsTableHeader; 
     
    348348 
    349349        # Avoid matching single columns 
    350         if (s/colspan$Foswiki::TranslationToken([0-9]+)//o) { 
     350        if (s/colspan$REMARKER([0-9]+)//o) { 
    351351            push( @attr, colspan => $1 ); 
    352352        } 
     
    566566 
    567567sub internalLink { 
    568     my ( $this, $web, $topic, $linkText, $anchor, $linkIfAbsent, $keepWebPrefix, 
    569         $hasExplicitLinkLabel ) 
     568    my ( $this, $web, $topic, $linkText, $anchor, 
     569         $linkIfAbsent, $keepWebPrefix, $hasExplicitLinkLabel, $params ) 
    570570      = @_; 
    571571 
     
    612612    $linkText =~ s/(?<=[\s\(])([$Foswiki::regex{upperAlpha}])/<nop>$1/go; 
    613613    return _renderWikiWord( $this, $web, $topic, $linkText, $anchor, 
    614         $linkIfAbsent, $keepWebPrefix ); 
     614        $linkIfAbsent, $keepWebPrefix, $params ); 
    615615} 
    616616 
     
    618618sub _renderWikiWord { 
    619619    my ( $this, $web, $topic, $linkText, $anchor, $linkIfAbsent, 
    620         $keepWebPrefix ) = @_; 
     620        $keepWebPrefix, $params ) = @_; 
    621621    my $session = $this->{session}; 
    622622    my $topicExists = $session->topicExists( $web, $topic ); 
     
    641641 
    642642        return _renderExistingWikiWord( $this, $web, $topic, $linkText, 
    643             $anchor ); 
     643            $anchor, $params ); 
    644644    } 
    645645    if ($linkIfAbsent) { 
     
    652652        # add a dependency so that the page gets invalidated as soon as the 
    653653        # WikiWord comes into existance 
     654        # Note we *ignore* the params if the target topic does not exist 
    654655        $this->{session}->{cache}->addDependency($web, $topic) 
    655656          if $Foswiki::cfg{Cache}{Enabled}; 
     
    665666 
    666667sub _renderExistingWikiWord { 
    667     my ( $this, $web, $topic, $text, $anchor ) = @_; 
     668    my ( $this, $web, $topic, $text, $anchor, $params ) = @_; 
    668669 
    669670    my @cssClasses; 
     
    678679    my @attrs; 
    679680    my $href = $this->{session}->getScriptUrl( 0, 'view', $web, $topic ); 
     681    if ($params) { 
     682        $href .= $params; 
     683    } 
     684 
    680685    if ($anchor) { 
    681686        $anchor = $this->_makeAnchorName($anchor); 
     
    778783    $link =~ s/\s+$//; 
    779784 
    780     my $hasExplicitLinkLabel = $text ? 1 : undef; 
    781  
    782     # Explicit external [[$link][$text]]-style can be handled directly 
     785    my $hasExplicitLinkLabel = $text ? 1 : 0; 
     786 
     787    # Explicit external [[http://$link][$text]]-style can be handled directly 
    783788    if ( $link =~ m!^($Foswiki::regex{linkProtocolPattern}\:|/)! ) { 
    784789        if ( defined $text ) { 
     
    808813    $text ||= $link; 
    809814 
     815    # Extract '?params' 
     816    # $link =~ s/(\?.*?)(?>#|$)//; 
     817    my $params = ''; 
     818    if ( $link =~ s/(\?.*$)// ) { 
     819        $params = $1; 
     820        my $p = quotemeta($params); 
     821        $text =~ s/$p//; 
     822    } 
     823 
    810824    # Extract '#anchor' 
    811825    # $link =~ s/(\#[a-zA-Z_0-9\-]*$)//; 
     
    813827    if ( $link =~ s/($Foswiki::regex{anchorRegex}$)// ) { 
    814828        $anchor = $1; 
     829        #$text =~ s/#$anchor//; 
    815830    } 
    816831 
     
    845860      $this->{session}->normalizeWebTopicName( $topicObject->web, $link ); 
    846861    return $this->internalLink( $web, $topic, $text, $anchor, 1, undef, 
    847         $hasExplicitLinkLabel ); 
     862        $hasExplicitLinkLabel, $params ); 
    848863} 
    849864 
     
    10101025 
    10111026    # verbatim before literal - see Item3431 
    1012     $text = $this->takeOutBlocks( $text, 'verbatim', $removed ); 
    1013     $text = $this->takeOutBlocks( $text, 'literal',  $removed ); 
    1014     $text = $this->takeOutBlocks( $text, 'dirtyarea', $removed ) 
     1027    $text = Foswiki::takeOutBlocks( $text, 'verbatim', $removed ); 
     1028    $text = Foswiki::takeOutBlocks( $text, 'literal',  $removed ); 
     1029    $text = Foswiki::takeOutBlocks( $text, 'dirtyarea', $removed ) 
    10151030      if $Foswiki::cfg{Cache}{Enabled}; 
    10161031 
     
    10421057        $topicObject->topic ); 
    10431058 
    1044     $text = $this->takeOutBlocks( $text, 'pre', $removed ); 
     1059    $text = Foswiki::takeOutBlocks( $text, 'pre', $removed ); 
    10451060 
    10461061    # Join lines ending in '\' (don't need \r?, it was removed already) 
     
    11001115    # locate isolated < and > and translate to entities 
    11011116    # Protect isolated <!-- and --> 
    1102     $text =~ s/<!--/{$Foswiki::TranslationToken!--/g; 
    1103     $text =~ s/-->/--}$Foswiki::TranslationToken/g; 
     1117    $text =~ s/<!--/{$REMARKER!--/g; 
     1118    $text =~ s/-->/--}$REMARKER/g; 
    11041119 
    11051120    # SMELL: this next fragment does not handle the case where HTML tags 
     
    11081123    # they have been escaped. 
    11091124    $text =~ 
    1110 s/<(\/?\w+(:\w+)?)>/{$Foswiki::TranslationToken$1}$Foswiki::TranslationToken/g; 
     1125s/<(\/?\w+(:\w+)?)>/{$REMARKER$1}$REMARKER/g; 
    11111126    $text =~ 
    1112 s/<(\w+(:\w+)?(\s+.*?|\/)?)>/{$Foswiki::TranslationToken$1}$Foswiki::TranslationToken/g; 
     1127s/<(\w+(:\w+)?(\s+.*?|\/)?)>/{$REMARKER$1}$REMARKER/g; 
    11131128 
    11141129    # XML processing instruction only valid at start of text 
    11151130    $text =~ 
    1116 s/^<(\?\w.*?\?)>/{$Foswiki::TranslationToken$1}$Foswiki::TranslationToken/g; 
     1131s/^<(\?\w.*?\?)>/{$REMARKER$1}$REMARKER/g; 
    11171132 
    11181133    # entitify lone < and >, praying that we haven't screwed up :-( 
     
    11201135    $text =~ s/<(?!\!\[CDATA\[)/&lt\;/g; 
    11211136    $text =~ s/(?<!\]\])>/&gt\;/g; 
    1122     $text =~ s/{$Foswiki::TranslationToken/</go; 
    1123     $text =~ s/}$Foswiki::TranslationToken/>/go; 
     1137    $text =~ s/{$REMARKER/</go; 
     1138    $text =~ s/}$REMARKER/>/go; 
    11241139 
    11251140    # standard URI - don't modify if url(http://as) form 
     
    11281143 
    11291144    # other entities 
    1130     $text =~ s/&(\w+);/$Foswiki::TranslationToken$1;/g;              # "&abc;" 
    1131     $text =~ s/&(#x?[0-9a-f]+);/$Foswiki::TranslationToken$1;/gi;    # "&#123;" 
     1145    $text =~ s/&(\w+);/$REMARKER$1;/g;              # "&abc;" 
     1146    $text =~ s/&(#x?[0-9a-f]+);/$REMARKER$1;/gi;    # "&#123;" 
    11321147    $text =~ s/&/&amp;/g;    # escape standalone "&" 
    1133     $text =~ s/$Foswiki::TranslationToken(#x?[0-9a-f]+;)/&$1/goi; 
    1134     $text =~ s/$Foswiki::TranslationToken(\w+;)/&$1/go; 
     1148    $text =~ s/$REMARKER(#x?[0-9a-f]+;)/&$1/goi; 
     1149    $text =~ s/$REMARKER(\w+;)/&$1/go; 
    11351150 
    11361151    # clear the set of unique anchornames in order to inhibit 
     
    12961311 
    12971312        # Handle WikiWords 
    1298         $text = $this->takeOutBlocks( $text, 'noautolink', $removed ); 
     1313        $text = Foswiki::takeOutBlocks( $text, 'noautolink', $removed ); 
    12991314        $text =~ 
    13001315s/$STARTWW(?:($Foswiki::regex{webNameRegex})\.)?($Foswiki::regex{wikiWordRegex}|$Foswiki::regex{abbrevRegex})($Foswiki::regex{anchorRegex})?/_handleWikiWord( $this, $topicObject, $1, $2, $3)/geom; 
    1301         $this->putBackBlocks( \$text, $removed, 'noautolink' ); 
    1302     } 
    1303  
    1304     $this->putBackBlocks( \$text, $removed, 'pre' ); 
     1316        Foswiki::putBackBlocks( \$text, $removed, 'noautolink' ); 
     1317    } 
     1318 
     1319    Foswiki::putBackBlocks( \$text, $removed, 'pre' ); 
    13051320 
    13061321    # DEPRECATED plugins hook after PRE re-inserted 
     
    13081323 
    13091324    # replace verbatim with pre in the final output 
    1310     $this->putBackBlocks( \$text, $removed, 'verbatim', 'pre', 
     1325    Foswiki::putBackBlocks( \$text, $removed, 'verbatim', 'pre', 
    13111326        \&verbatimCallBack ); 
    13121327    $text =~ s|\n?<nop>\n$||o;    # clean up clutch 
    13131328 
    13141329    $this->_putBackProtected( \$text, 'script', $removed, \&_filterScript ); 
    1315     $this->putBackBlocks( \$text, $removed, 'literal', '', \&_filterLiteral ); 
     1330    Foswiki::putBackBlocks( \$text, $removed, 'literal', '', \&_filterLiteral ); 
    13161331    $this->_putBackProtected( \$text, 'literal',  $removed ); 
    1317     $this->putBackBlocks( \$text, $removed, 'dirtyarea' ) 
     1332    Foswiki::putBackBlocks( \$text, $removed, 'dirtyarea' ) 
    13181333      if $Foswiki::cfg{Cache}{Enabled}; 
    13191334    $this->_putBackProtected( \$text, 'comment',  $removed ); 
     
    15261541    return 
    15271542        '<!--' 
    1528       . $Foswiki::TranslationToken 
     1543      . $REMARKER 
    15291544      . $id 
    15301545      . $placeholder 
    1531       . $Foswiki::TranslationToken . '-->'; 
     1546      . $REMARKER . '-->'; 
    15321547} 
    15331548 
     
    15491564        $val = &$callback($val) if ( defined($callback) ); 
    15501565        $$text =~ 
    1551 s/<!--$Foswiki::TranslationToken$placeholder$Foswiki::TranslationToken-->/$val/; 
     1566s/<!--$REMARKER$placeholder$REMARKER-->/$val/; 
    15521567        delete( $map->{$placeholder} ); 
    1553     } 
    1554 } 
    1555  
    1556 =begin TML 
    1557  
    1558 ---++ ObjectMethod takeOutBlocks( \$text, $tag, \%map ) -> $text 
    1559  
    1560    * =$text= - Text to process 
    1561    * =$tag= - XHTML-style tag. 
    1562    * =\%map= - Reference to a hash to contain the removed blocks 
    1563  
    1564 Return value: $text with blocks removed 
    1565  
    1566 Searches through $text and extracts blocks delimited by a tag, appending each 
    1567 onto the end of the @buffer and replacing with a token 
    1568 string which is not affected by TML rendering.  The text after these 
    1569 substitutions is returned. 
    1570  
    1571 Parameters to the open tag are recorded. 
    1572  
    1573 This is _different_ to takeOutProtected, because it requires tags 
    1574 to be on their own line. it also supports a callback for post- 
    1575 processing the data before re-insertion. 
    1576  
    1577 =cut 
    1578  
    1579 sub takeOutBlocks { 
    1580     my ( $this, $intext, $tag, $map ) = @_; 
    1581  
    1582     return $intext unless ( $intext =~ m/<$tag\b/i ); 
    1583  
    1584     my $out   = ''; 
    1585     my $depth = 0; 
    1586     my $scoop; 
    1587     my $tagParams; 
    1588  
    1589     foreach my $token ( split /(<\/?$tag[^>]*>)/i, $intext ) { 
    1590         if ( $token =~ /<$tag\b([^>]*)?>/i ) { 
    1591             $depth++; 
    1592             if ( $depth eq 1 ) { 
    1593                 $tagParams = $1; 
    1594                 next; 
    1595             } 
    1596         } 
    1597         elsif ( $token =~ /<\/$tag>/i ) { 
    1598             if ( $depth > 0 ) { 
    1599                 $depth--; 
    1600                 if ( $depth eq 0 ) { 
    1601                     my $placeholder = $tag . $placeholderMarker; 
    1602                     $placeholderMarker++; 
    1603                     $map->{$placeholder}{text}   = $scoop; 
    1604                     $map->{$placeholder}{params} = $tagParams; 
    1605                     $out .= '<!--' 
    1606                       . $Foswiki::TranslationToken 
    1607                       . $placeholder 
    1608                       . $Foswiki::TranslationToken . '-->'; 
    1609                     $scoop = ''; 
    1610                     next; 
    1611                 } 
    1612             } 
    1613         } 
    1614         if ( $depth > 0 ) { 
    1615             $scoop .= $token; 
    1616         } 
    1617         else { 
    1618             $out .= $token; 
    1619         } 
    1620     } 
    1621  
    1622     # unmatched tags 
    1623     if ( defined($scoop) && ( $scoop ne '' ) ) { 
    1624         my $placeholder = $tag . $placeholderMarker; 
    1625         $placeholderMarker++; 
    1626         $map->{$placeholder}{text}   = $scoop; 
    1627         $map->{$placeholder}{params} = $tagParams; 
    1628         $out .= '<!--' 
    1629           . $Foswiki::TranslationToken 
    1630           . $placeholder 
    1631           . $Foswiki::TranslationToken . '-->'; 
    1632     } 
    1633  
    1634     return $out; 
    1635 } 
    1636  
    1637 =begin TML 
    1638  
    1639 ---++ ObjectMethod putBackBlocks( \$text, \%map, $tag, $newtag, $callBack ) -> $text 
    1640  
    1641 Return value: $text with blocks added back 
    1642    * =\$text= - reference to text to process 
    1643    * =\%map= - map placeholders to blocks removed by takeOutBlocks 
    1644    * =$tag= - Tag name processed by takeOutBlocks 
    1645    * =$newtag= - Tag name to use in output, in place of $tag. If undefined, uses $tag. 
    1646    * =$callback= - Reference to function to call on each block being inserted (optional) 
    1647  
    1648 Reverses the actions of takeOutBlocks. 
    1649  
    1650 Each replaced block is processed by the callback (if there is one) before 
    1651 re-insertion. 
    1652  
    1653 Parameters to the outermost cut block are replaced into the open tag, 
    1654 even if that tag is changed. This allows things like 
    1655 &lt;verbatim class=''> 
    1656 to be mapped to 
    1657 &lt;pre class=''> 
    1658  
    1659 Cool, eh what? Jolly good show. 
    1660  
    1661 And if you set $newtag to '', we replace the taken out block with the value itself 
    1662    * which i'm using to stop the rendering process, but then at the end put in the html directly 
    1663    (for &lt;literal> tag. 
    1664  
    1665 =cut 
    1666  
    1667 sub putBackBlocks { 
    1668     my ( $this, $text, $map, $tag, $newtag, $callback ) = @_; 
    1669  
    1670     $newtag = $tag if ( !defined($newtag) ); 
    1671  
    1672     foreach my $placeholder ( keys %$map ) { 
    1673         if ( $placeholder =~ /^$tag\d+$/ ) { 
    1674             my $params = $map->{$placeholder}{params} || ''; 
    1675             my $val = $map->{$placeholder}{text}; 
    1676             $val = &$callback($val) if ( defined($callback) ); 
    1677             if ( $newtag eq '' ) { 
    1678                 $$text =~ 
    1679 s(<!--$Foswiki::TranslationToken$placeholder$Foswiki::TranslationToken-->)($val); 
    1680             } 
    1681             else { 
    1682                 $$text =~ 
    1683 s(<!--$Foswiki::TranslationToken$placeholder$Foswiki::TranslationToken-->) 
    1684                 (<$newtag$params>$val</$newtag>); 
    1685             } 
    1686             delete( $map->{$placeholder} ); 
    1687         } 
    16881568    } 
    16891569} 
     
    20971977    # throw away <verbatim> and <pre> blocks 
    20981978    my %junk; 
    2099     $text = $this->takeOutBlocks( $text, 'verbatim', \%junk ); 
    2100     $text = $this->takeOutBlocks( $text, 'pre',      \%junk ); 
     1979    $text = Foswiki::takeOutBlocks( $text, 'verbatim', \%junk ); 
     1980    $text = Foswiki::takeOutBlocks( $text, 'pre',      \%junk ); 
    21011981 
    21021982    my $maxDepth = $params->{depth}; 
     
    21161996    my $result   = ''; 
    21171997    my $verbatim = {}; 
    2118     $text = $this->takeOutBlocks( $text, 'verbatim', $verbatim ); 
    2119     $text = $this->takeOutBlocks( $text, 'pre',      $verbatim ); 
     1998    $text = Foswiki::takeOutBlocks( $text, 'verbatim', $verbatim ); 
     1999    $text = Foswiki::takeOutBlocks( $text, 'pre',      $verbatim ); 
    21202000 
    21212001    # Find URL parameters 
Note: See TracChangeset for help on using the changeset viewer.