Ignore:
Timestamp:
Jun 20, 2013, 5:38:47 AM (11 years ago)
Author:
rvelices
Message:

smarty 3 - first pass for tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/smarty/libs/plugins/block.textformat.php

    r3282 r23384  
    11<?php
    22/**
    3  * Smarty plugin
     3 * Smarty plugin to format text blocks
     4 *
    45 * @package Smarty
    5  * @subpackage plugins
     6 * @subpackage PluginsBlock
    67 */
    78
     
    1314 * Purpose:  format text a certain way with preset styles
    1415 *           or custom wrap/indent settings<br>
    15  * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
     16 * Params:
     17 * <pre>
     18 * - style         - string (email)
     19 * - indent        - integer (0)
     20 * - wrap          - integer (80)
     21 * - wrap_char     - string ("\n")
     22 * - indent_char   - string (" ")
     23 * - wrap_boundary - boolean (true)
     24 * </pre>
     25 *
     26 * @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
    1627 *       (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>
     28 * @param array                    $params   parameters
     29 * @param string                   $content  contents of the block
     30 * @param Smarty_Internal_Template $template template object
     31 * @param boolean                  &$repeat  repeat flag
     32 * @return string content re-formatted
    2633 * @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
    3034 */
    31 function smarty_block_textformat($params, $content, &$smarty)
     35function smarty_block_textformat($params, $content, $template, &$repeat)
    3236{
    3337    if (is_null($content)) {
     
    4347    $wrap_cut = false;
    4448    $assign = null;
    45    
     49
    4650    foreach ($params as $_key => $_val) {
    4751        switch ($_key) {
     
    6468
    6569            default:
    66                 $smarty->trigger_error("textformat: unknown attribute '$_key'");
     70                trigger_error("textformat: unknown attribute '$_key'");
    6771        }
    6872    }
     
    7175        $wrap = 72;
    7276    }
    73 
    7477    // split into paragraphs
    75     $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
     78    $_paragraphs = preg_split('![\r\n]{2}!', $content);
    7679    $_output = '';
    7780
    78     for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
    79         if ($_paragraphs[$_x] == '') {
     81
     82    foreach ($_paragraphs as &$_paragraph) {
     83        if (!$_paragraph) {
    8084            continue;
    8185        }
    8286        // convert mult. spaces & special chars to single space
    83         $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
     87        $_paragraph = preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), array(' ', ''), $_paragraph);
    8488        // indent first line
    85         if($indent_first > 0) {
    86             $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
     89        if ($indent_first > 0) {
     90            $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
    8791        }
    8892        // wordwrap sentences
    89         $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
     93        if (Smarty::$_MBSTRING) {
     94            require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
     95            $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
     96        } else {
     97            $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
     98        }
    9099        // indent lines
    91         if($indent > 0) {
    92             $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
     100        if ($indent > 0) {
     101            $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
    93102        }
    94103    }
    95104    $_output = implode($wrap_char . $wrap_char, $_paragraphs);
    96 
    97     return $assign ? $smarty->assign($assign, $_output) : $_output;
    98 
     105   
     106    if ($assign) {
     107        $template->assign($assign, $_output);
     108    } else {
     109        return $_output;
     110    }
    99111}
    100112
    101 /* vim: set expandtab: */
    102 
    103113?>
Note: See TracChangeset for help on using the changeset viewer.