PageRenderTime 72ms CodeModel.GetById 67ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/vendor/smarty2/lib/plugins/block.textformat.php

http://zoop.googlecode.com/
PHP | 103 lines | 56 code | 13 blank | 34 comment | 9 complexity | f4e1cc15997ff132066f5e4e09e92054 MD5 | raw file
  1<?php
  2/**
  3 * Smarty plugin
  4 * @package Smarty
  5 * @subpackage plugins
  6 */
  7
  8/**
  9 * Smarty {textformat}{/textformat} block plugin
 10 *
 11 * Type:     block function<br>
 12 * Name:     textformat<br>
 13 * Purpose:  format text a certain way with preset styles
 14 *           or custom wrap/indent settings<br>
 15 * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
 16 *       (Smarty online manual)
 17 * @param array
 18 * <pre>
 19 * Params:   style: string (email)
 20 *           indent: integer (0)
 21 *           wrap: integer (80)
 22 *           wrap_char string ("\n")
 23 *           indent_char: string (" ")
 24 *           wrap_boundary: boolean (true)
 25 * </pre>
 26 * @author Monte Ohrt <monte at ohrt dot com>
 27 * @param string contents of the block
 28 * @param Smarty clever simulation of a method
 29 * @return string string $content re-formatted
 30 */
 31function smarty_block_textformat($params, $content, &$smarty)
 32{
 33    if (is_null($content)) {
 34        return;
 35    }
 36
 37    $style = null;
 38    $indent = 0;
 39    $indent_first = 0;
 40    $indent_char = ' ';
 41    $wrap = 80;
 42    $wrap_char = "\n";
 43    $wrap_cut = false;
 44    $assign = null;
 45    
 46    foreach ($params as $_key => $_val) {
 47        switch ($_key) {
 48            case 'style':
 49            case 'indent_char':
 50            case 'wrap_char':
 51            case 'assign':
 52                $$_key = (string)$_val;
 53                break;
 54
 55            case 'indent':
 56            case 'indent_first':
 57            case 'wrap':
 58                $$_key = (int)$_val;
 59                break;
 60
 61            case 'wrap_cut':
 62                $$_key = (bool)$_val;
 63                break;
 64
 65            default:
 66                $smarty->trigger_error("textformat: unknown attribute '$_key'");
 67        }
 68    }
 69
 70    if ($style == 'email') {
 71        $wrap = 72;
 72    }
 73
 74    // split into paragraphs
 75    $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
 76    $_output = '';
 77
 78    for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
 79        if ($_paragraphs[$_x] == '') {
 80            continue;
 81        }
 82        // convert mult. spaces & special chars to single space
 83        $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
 84        // indent first line
 85        if($indent_first > 0) {
 86            $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
 87        }
 88        // wordwrap sentences
 89        $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
 90        // indent lines
 91        if($indent > 0) {
 92            $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
 93        }
 94    }
 95    $_output = implode($wrap_char . $wrap_char, $_paragraphs);
 96
 97    return $assign ? $smarty->assign($assign, $_output) : $_output;
 98
 99}
100
101/* vim: set expandtab: */
102
103?>