| 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 | * @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, &$smarty) |
| 31 | { |
| 32 | $style = null; |
| 33 | $indent = 0; |
| 34 | $indent_first = 0; |
| 35 | $indent_char = ' '; |
| 36 | $wrap = 80; |
| 37 | $wrap_char = "\n"; |
| 38 | $wrap_cut = false; |
| 39 | $assign = null; |
| 40 | |
| 41 | if($content == null) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | extract($params); |
| 46 | |
| 47 | if($style == 'email') { |
| 48 | $wrap = 72; |
| 49 | } |
| 50 | |
| 51 | // split into paragraphs |
| 52 | $paragraphs = preg_split('![\r\n][\r\n]!',$content); |
| 53 | $output = ''; |
| 54 | |
| 55 | foreach($paragraphs as $paragraph) { |
| 56 | if($paragraph == '') { |
| 57 | continue; |
| 58 | } |
| 59 | // convert mult. spaces & special chars to single space |
| 60 | $paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph); |
| 61 | // indent first line |
| 62 | if($indent_first > 0) { |
| 63 | $paragraph = str_repeat($indent_char,$indent_first) . $paragraph; |
| 64 | } |
| 65 | // wordwrap sentences |
| 66 | $paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut); |
| 67 | // indent lines |
| 68 | if($indent > 0) { |
| 69 | $paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph); |
| 70 | } |
| 71 | $output .= $paragraph . $wrap_char . $wrap_char; |
| 72 | } |
| 73 | |
| 74 | if($assign != null) { |
| 75 | $smarty->assign($assign,$output); |
| 76 | } else { |
| 77 | return $output; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* vim: set expandtab: */ |
| 82 | |
| 83 | ?> |