library/smarty/libs/plugins/block.textformat.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin to format text blocks
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsBlock
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty {textformat}{/textformat} block plugin
       
    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  * Params:
       
    16  * <pre>
       
    17  * - style         - string (email)
       
    18  * - indent        - integer (0)
       
    19  * - wrap          - integer (80)
       
    20  * - wrap_char     - string ("\n")
       
    21  * - indent_char   - string (" ")
       
    22  * - wrap_boundary - boolean (true)
       
    23  * </pre>
       
    24  *
       
    25  * @link   http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
       
    26  *         (Smarty online manual)
       
    27  *
       
    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  *
       
    33  * @return string content re-formatted
       
    34  * @author Monte Ohrt <monte at ohrt dot com>
       
    35  */
       
    36 function smarty_block_textformat($params, $content, $template, &$repeat) {
       
    37     if (is_null($content)) {
       
    38         return;
       
    39     }
       
    40 
       
    41     $style = null;
       
    42     $indent = 0;
       
    43     $indent_first = 0;
       
    44     $indent_char = ' ';
       
    45     $wrap = 80;
       
    46     $wrap_char = "\n";
       
    47     $wrap_cut = false;
       
    48     $assign = null;
       
    49 
       
    50     foreach ($params as $_key => $_val) {
       
    51         switch ($_key) {
       
    52             case 'style':
       
    53             case 'indent_char':
       
    54             case 'wrap_char':
       
    55             case 'assign':
       
    56                 $$_key = (string)$_val;
       
    57                 break;
       
    58 
       
    59             case 'indent':
       
    60             case 'indent_first':
       
    61             case 'wrap':
       
    62                 $$_key = (int)$_val;
       
    63                 break;
       
    64 
       
    65             case 'wrap_cut':
       
    66                 $$_key = (bool)$_val;
       
    67                 break;
       
    68 
       
    69             default:
       
    70                 trigger_error("textformat: unknown attribute '$_key'");
       
    71         }
       
    72     }
       
    73 
       
    74     if ($style == 'email') {
       
    75         $wrap = 72;
       
    76     }
       
    77     // split into paragraphs
       
    78     $_paragraphs = preg_split('![\r\n]{2}!', $content);
       
    79 
       
    80     foreach ($_paragraphs as &$_paragraph) {
       
    81         if (!$_paragraph) {
       
    82             continue;
       
    83         }
       
    84         // convert mult. spaces & special chars to single space
       
    85         $_paragraph = preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), array(' ', ''), $_paragraph);
       
    86         // indent first line
       
    87         if ($indent_first > 0) {
       
    88             $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
       
    89         }
       
    90         // wordwrap sentences
       
    91         if (Smarty::$_MBSTRING) {
       
    92             require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
       
    93             $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
       
    94         } else {
       
    95             $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
       
    96         }
       
    97         // indent lines
       
    98         if ($indent > 0) {
       
    99             $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
       
   100         }
       
   101     }
       
   102     $_output = implode($wrap_char . $wrap_char, $_paragraphs);
       
   103 
       
   104     if ($assign) {
       
   105         $template->assign($assign, $_output);
       
   106     } else {
       
   107         return $_output;
       
   108     }
       
   109 }