Changeset 1107:3943962d69b8 for inc/public/lib.tpl.context.php
- Timestamp:
- 02/27/13 11:48:32 (12 years ago)
- Branch:
- sexy
- Parents:
- 880:02c78f56f430 (diff), 1105:ce855d61f9ce (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/public/lib.tpl.context.php
r852 r1107 127 127 $str = self::lower_case($str); 128 128 } elseif ($upper_case) { 129 $str = self::upper_case($str); 129 if ($upper_case == 2) { 130 $str = self::capitalize($str); 131 } else { 132 $str = self::upper_case($str); 133 } 130 134 } 131 135 … … 160 164 { 161 165 return mb_strtoupper($str); 166 } 167 168 public static function capitalize($str) 169 { 170 if ($str != '') { 171 $str[0] = mb_strtoupper($str[0]); 172 } 173 return $str; 162 174 } 163 175 … … 314 326 return $str; 315 327 } 316 317 return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str); 318 } 319 328 329 # Process part adapted from SmartyPants engine (J. Gruber et al.) : 330 331 $tokens = self::tokenizeHTML($str); 332 $result = ''; 333 $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags. 334 335 foreach ($tokens as $cur_token) { 336 if ($cur_token[0] == "tag") { 337 # Don't mess with quotes inside tags. 338 $result .= $cur_token[1]; 339 if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) { 340 $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; 341 } 342 } else { 343 $t = $cur_token[1]; 344 if (!$in_pre) { 345 $t = preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$t); 346 } 347 $result .= $t; 348 } 349 } 350 351 return $result; 352 } 353 354 private static function tokenizeHTML($str) 355 { 356 # Function from SmartyPants engine (J. Gruber et al.) 357 # 358 # Parameter: String containing HTML markup. 359 # Returns: An array of the tokens comprising the input 360 # string. Each token is either a tag (possibly with nested, 361 # tags contained therein, such as <a href="<MTFoo>">, or a 362 # run of text between tags. Each element of the array is a 363 # two-element array; the first is either 'tag' or 'text'; 364 # the second is the actual value. 365 # 366 # 367 # Regular expression derived from the _tokenize() subroutine in 368 # Brad Choate's MTRegex plugin. 369 # <http://www.bradchoate.com/past/mtregex.php> 370 # 371 $index = 0; 372 $tokens = array(); 373 374 $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment 375 '(?s:<\?.*?\?>)|'. # processing instruction 376 # regular tags 377 '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; 378 379 $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE); 380 381 foreach ($parts as $part) { 382 if (++$index % 2 && $part != '') 383 $tokens[] = array('text', $part); 384 else 385 $tokens[] = array('tag', $part); 386 } 387 return $tokens; 388 } 389 320 390 } 321 391 ?> -
inc/public/lib.tpl.context.php
r975 r1107 172 172 } 173 173 return $str; 174 }175 176 public static function categoryPostParam(&$p)177 {178 $not = substr($p['cat_url'],0,1) == '!';179 if ($not) {180 $p['cat_url'] = substr($p['cat_url'],1);181 }182 183 $p['cat_url'] = preg_split('/\s*,\s*/',$p['cat_url'],-1,PREG_SPLIT_NO_EMPTY);184 185 foreach ($p['cat_url'] as &$v)186 {187 if ($not) {188 $v .= ' ?not';189 }190 if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/',$v)) {191 $v = preg_replace('/#self/',$GLOBALS['_ctx']->categories->cat_url,$v);192 } elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/',$v)) {193 $v = preg_replace('/#self/',$GLOBALS['_ctx']->posts->cat_url,$v);194 }195 }196 174 } 197 175 … … 410 388 } 411 389 412 413 # First post image helpers414 public static function EntryFirstImageHelper($size,$with_category,$class="",$no_tag=false,$content_only=false,$cat_only=false)415 {416 global $core, $_ctx;417 418 $media = new dcMedia($core);419 $sizes = implode('|',array_keys($media->thumb_sizes)).'|o';420 if (!preg_match('/^'.$sizes.'$/',$size)) {421 $size = 's';422 }423 $p_url = $core->blog->settings->system->public_url;424 $p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url);425 $p_root = $core->blog->public_path;426 427 $pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/');428 $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|jpeg|gif|png))"[^>]+/msui',$pattern);429 430 $src = '';431 $alt = '';432 433 # We first look in post content434 if (!$cat_only && $_ctx->posts)435 {436 $subject = ($content_only ? '' : $_ctx->posts->post_excerpt_xhtml).$_ctx->posts->post_content_xhtml;437 if (preg_match_all($pattern,$subject,$m) > 0)438 {439 foreach ($m[1] as $i => $img) {440 if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {441 $dirname = str_replace('\\', '/', dirname($img));442 $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;443 if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {444 $alt = $malt[1];445 }446 break;447 }448 }449 }450 }451 452 # No src, look in category description if available453 if (!$src && $with_category && $_ctx->categories)454 {455 if (preg_match_all($pattern,$_ctx->categories->cat_desc,$m) > 0)456 {457 foreach ($m[1] as $i => $img) {458 if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {459 $dirname = str_replace('\\', '/', dirname($img));460 $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;461 if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {462 $alt = $malt[1];463 }464 break;465 }466 }467 };468 }469 470 if ($src) {471 if ($no_tag) {472 return $src;473 } else {474 return '<img alt="'.$alt.'" src="'.$src.'" class="'.$class.'" />';475 }476 }477 }478 479 private static function ContentFirstImageLookup($root,$img,$size)480 {481 global $core;482 483 # Get base name and extension484 $info = path::info($img);485 $base = $info['base'];486 487 $media = new dcMedia($core);488 $sizes = implode('|',array_keys($media->thumb_sizes));489 if (preg_match('/^\.(.+)_('.$sizes.')$/',$base,$m)) {490 $base = $m[1];491 }492 493 $res = false;494 if ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg'))495 {496 $res = '.'.$base.'_'.$size.'.jpg';497 }498 else499 {500 $f = $root.'/'.$info['dirname'].'/'.$base;501 if (file_exists($f.'.'.$info['extension'])) {502 $res = $base.'.'.$info['extension'];503 } elseif (file_exists($f.'.jpg')) {504 $res = $base.'.jpg';505 } elseif (file_exists($f.'.jpeg')) {506 $res = $base.'.jpeg';507 } elseif (file_exists($f.'.png')) {508 $res = $base.'.png';509 } elseif (file_exists($f.'.gif')) {510 $res = $base.'.gif';511 } elseif (file_exists($f.'.JPG')) {512 $res = $base.'.JPG';513 } elseif (file_exists($f.'.JPEG')) {514 $res = $base.'.JPEG';515 } elseif (file_exists($f.'.PNG')) {516 $res = $base.'.PNG';517 } elseif (file_exists($f.'.GIF')) {518 $res = $base.'.GIF';519 }520 }521 522 if ($res) {523 return $res;524 }525 return false;526 }527 390 } 528 391 ?>
Note: See TracChangeset
for help on using the changeset viewer.