/[cvs]/nfo/php/libs/net.php.smarty/plugins/block.textformat.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.smarty/plugins/block.textformat.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Thu Dec 19 16:40:20 2002 UTC revision 1.2 by joko, Wed Jun 16 21:58:16 2004 UTC
# Line 1  Line 1 
1  <?php  <?php
2    /**
 /*  
3   * Smarty plugin   * Smarty plugin
4   * -------------------------------------------------------------   * @package Smarty
5   * Type:     block function   * @subpackage plugins
6   * Name:     textformat   */
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   * Purpose:  format text a certain way with preset styles
14   *           or custom wrap/indent settings   *           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)   * Params:   style: string (email)
20   *           indent: integer (0)   *           indent: integer (0)
21   *           wrap: integer (80)   *           wrap: integer (80)
22   *           wrap_char string ("\n")   *           wrap_char string ("\n")
23   *           indent_char: string (" ")   *           indent_char: string (" ")
24   *           wrap_boundary: boolean (true)   *           wrap_boundary: boolean (true)
25   * -------------------------------------------------------------   * </pre>
26     * @param string contents of the block
27     * @param Smarty clever simulation of a method
28     * @return string string $content re-formatted
29   */   */
30  function smarty_block_textformat($params, $content, &$this)  function smarty_block_textformat($params, $content, &$smarty)
31  {  {
32          $style = null;      if (is_null($content)) {
33          $indent = 0;          return;
34          $indent_first = 0;      }
35          $indent_char = ' ';  
36          $wrap = 80;      $style = null;
37          $wrap_char = "\n";      $indent = 0;
38          $wrap_cut = false;      $indent_first = 0;
39          $assign = null;      $indent_char = ' ';
40                $wrap = 80;
41          if($content == null) {      $wrap_char = "\n";
42                  return true;      $wrap_cut = false;
43          }      $assign = null;
44        
45      extract($params);      foreach ($params as $_key => $_val) {
46            switch ($_key) {
47          if($style == 'email') {              case 'style':
48                  $wrap = 72;              case 'indent_char':
49          }                    case 'wrap_char':
50                        case 'assign':
51          // split into paragraphs                          $$_key = (string)$_val;
52          $paragraphs = preg_split('![\r\n][\r\n]!',$content);                  break;
53            
54          foreach($paragraphs as $paragraph) {              case 'indent':
55                  if($paragraph == '') {              case 'indent_first':
56                          continue;              case 'wrap':
57                  }                  $$_key = (int)$_val;
58                  // convert mult. spaces & special chars to single space                  break;
59                  $paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph);  
60                  // indent first line              case 'wrap_cut':
61                  if($indent_first > 0) {                  $$_key = (bool)$_val;
62                          $paragraph = str_repeat($indent_char,$indent_first) . $paragraph;                  break;
63                  }  
64                  // wordwrap sentences              default:
65                  $paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut);                  $smarty->trigger_error("textformat: unknown attribute '$_key'");
66                  // indent lines          }
67                  if($indent > 0) {      }
68                          $paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph);  
69                  }      if ($style == 'email') {
70                  $output .= $paragraph . $wrap_char . $wrap_char;          $wrap = 72;
71          }      }
72                                    
73          if($assign != null) {      // split into paragraphs
74                  $this->assign($assign,$output);      $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
75          } else {      $_output = '';
76                  echo $output;  
77          }      for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
78          //echo $content;          if ($_paragraphs[$_x] == '') {
79                continue;
80            }
81            // convert mult. spaces & special chars to single space
82            $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
83            // indent first line
84            if($indent_first > 0) {
85                $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
86            }
87            // wordwrap sentences
88            $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
89            // indent lines
90            if($indent > 0) {
91                $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
92            }
93        }
94        $_output = implode($wrap_char . $wrap_char, $_paragraphs);
95    
96        return $assign ? $smarty->assign($assign, $_output) : $_output;
97    
98  }  }
99    
100  /* vim: set expandtab: */  /* vim: set expandtab: */

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed