Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {textformat}{/textformat} block plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     block function
Chris PeBenito 696b41
 * Name:     textformat
Chris PeBenito 696b41
 * Purpose:  format text a certain way with preset styles
Chris PeBenito 696b41
 *           or custom wrap/indent settings
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
Chris PeBenito 696b41
 *       (Smarty online manual)
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * Params:   style: string (email)
Chris PeBenito 696b41
 *           indent: integer (0)
Chris PeBenito 696b41
 *           wrap: integer (80)
Chris PeBenito 696b41
 *           wrap_char string ("\n")
Chris PeBenito 696b41
 *           indent_char: string (" ")
Chris PeBenito 696b41
 *           wrap_boundary: boolean (true)
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @param string contents of the block
Chris PeBenito 696b41
 * @param Smarty clever simulation of a method
Chris PeBenito 696b41
 * @return string string $content re-formatted
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_block_textformat($params, $content, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    if (is_null($content)) {
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $style = null;
Chris PeBenito 696b41
    $indent = 0;
Chris PeBenito 696b41
    $indent_first = 0;
Chris PeBenito 696b41
    $indent_char = ' ';
Chris PeBenito 696b41
    $wrap = 80;
Chris PeBenito 696b41
    $wrap_char = "\n";
Chris PeBenito 696b41
    $wrap_cut = false;
Chris PeBenito 696b41
    $assign = null;
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    foreach ($params as $_key => $_val) {
Chris PeBenito 696b41
        switch ($_key) {
Chris PeBenito 696b41
            case 'style':
Chris PeBenito 696b41
            case 'indent_char':
Chris PeBenito 696b41
            case 'wrap_char':
Chris PeBenito 696b41
            case 'assign':
Chris PeBenito 696b41
                $$_key = (string)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'indent':
Chris PeBenito 696b41
            case 'indent_first':
Chris PeBenito 696b41
            case 'wrap':
Chris PeBenito 696b41
                $$_key = (int)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'wrap_cut':
Chris PeBenito 696b41
                $$_key = (bool)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                $smarty->trigger_error("textformat: unknown attribute '$_key'");
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($style == 'email') {
Chris PeBenito 696b41
        $wrap = 72;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    // split into paragraphs
Chris PeBenito 696b41
    $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
Chris PeBenito 696b41
    $_output = '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
Chris PeBenito 696b41
        if ($_paragraphs[$_x] == '') {
Chris PeBenito 696b41
            continue;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        // convert mult. spaces & special chars to single space
Chris PeBenito 696b41
        $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
Chris PeBenito 696b41
        // indent first line
Chris PeBenito 696b41
        if($indent_first > 0) {
Chris PeBenito 696b41
            $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        // wordwrap sentences
Chris PeBenito 696b41
        $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
Chris PeBenito 696b41
        // indent lines
Chris PeBenito 696b41
        if($indent > 0) {
Chris PeBenito 696b41
            $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $_output = implode($wrap_char . $wrap_char, $_paragraphs);
Chris PeBenito 696b41
Chris PeBenito 696b41
    return $assign ? $smarty->assign($assign, $_output) : $_output;
Chris PeBenito 696b41
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>