Initial skeleton.
[av.git] / av / includes / smarty / plugins / block.textformat.php
blob8cd010acb4e29089db5122c40b119c08de316028
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
8 /**
9 * 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 * @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 * @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
31 function smarty_block_textformat($params, $content, &$smarty)
33 if (is_null($content)) {
34 return;
37 $style = null;
38 $indent = 0;
39 $indent_first = 0;
40 $indent_char = ' ';
41 $wrap = 80;
42 $wrap_char = "\n";
43 $wrap_cut = false;
44 $assign = null;
46 foreach ($params as $_key => $_val) {
47 switch ($_key) {
48 case 'style':
49 case 'indent_char':
50 case 'wrap_char':
51 case 'assign':
52 $$_key = (string)$_val;
53 break;
55 case 'indent':
56 case 'indent_first':
57 case 'wrap':
58 $$_key = (int)$_val;
59 break;
61 case 'wrap_cut':
62 $$_key = (bool)$_val;
63 break;
65 default:
66 $smarty->trigger_error("textformat: unknown attribute '$_key'");
70 if ($style == 'email') {
71 $wrap = 72;
74 // split into paragraphs
75 $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
76 $_output = '';
78 for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
79 if ($_paragraphs[$_x] == '') {
80 continue;
82 // convert mult. spaces & special chars to single space
83 $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
84 // indent first line
85 if($indent_first > 0) {
86 $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
88 // wordwrap sentences
89 $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
90 // indent lines
91 if($indent > 0) {
92 $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
95 $_output = implode($wrap_char . $wrap_char, $_paragraphs);
97 return $assign ? $smarty->assign($assign, $_output) : $_output;
101 /* vim: set expandtab: */