Dotclear

Changeset 975:8d003494d2f5 for inc


Ignore:
Timestamp:
11/07/12 11:04:09 (13 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Smileys are not more converted in image in pre,code,kbd,script and math contents, fixes #1029

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/public/lib.tpl.context.php

    r937 r975  
    348348               return $str; 
    349349          } 
    350            
    351           return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str); 
    352      } 
     350 
     351          # Process part adapted from SmartyPants engine (J. Gruber et al.) : 
     352 
     353          $tokens = self::tokenizeHTML($str); 
     354          $result = ''; 
     355          $in_pre = 0;  # Keep track of when we're inside <pre> or <code> tags. 
     356 
     357          foreach ($tokens as $cur_token) { 
     358               if ($cur_token[0] == "tag") { 
     359                    # Don't mess with quotes inside tags. 
     360                    $result .= $cur_token[1]; 
     361                    if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) { 
     362                         $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; 
     363                    } 
     364               } else { 
     365                    $t = $cur_token[1]; 
     366                    if (!$in_pre) { 
     367                         $t = preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$t); 
     368                    } 
     369                    $result .= $t; 
     370               } 
     371          } 
     372 
     373          return $result; 
     374     } 
     375 
     376     private static function tokenizeHTML($str) 
     377     { 
     378          # Function from SmartyPants engine (J. Gruber et al.) 
     379          # 
     380          #   Parameter:  String containing HTML markup. 
     381          #   Returns:    An array of the tokens comprising the input 
     382          #               string. Each token is either a tag (possibly with nested, 
     383          #               tags contained therein, such as <a href="<MTFoo>">, or a 
     384          #               run of text between tags. Each element of the array is a 
     385          #               two-element array; the first is either 'tag' or 'text'; 
     386          #               the second is the actual value. 
     387          # 
     388          # 
     389          #   Regular expression derived from the _tokenize() subroutine in  
     390          #   Brad Choate's MTRegex plugin. 
     391          #   <http://www.bradchoate.com/past/mtregex.php> 
     392          # 
     393          $index = 0; 
     394          $tokens = array(); 
     395 
     396          $match = '(?s:<!(?:--.*?--\s*)+>)|'.    # comment 
     397                     '(?s:<\?.*?\?>)|'.                # processing instruction 
     398                                                            # regular tags 
     399                     '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';  
     400 
     401          $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE); 
     402 
     403          foreach ($parts as $part) { 
     404               if (++$index % 2 && $part != '')  
     405                    $tokens[] = array('text', $part); 
     406               else 
     407                    $tokens[] = array('tag', $part); 
     408          } 
     409          return $tokens; 
     410     } 
     411 
    353412 
    354413     # First post image helpers 
Note: See TracChangeset for help on using the changeset viewer.

Sites map