Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 48 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | /**
|
8 | /**
|
9 | * Smarty {textformat}{/textformat} block plugin
|
9 | * Smarty {textformat}{/textformat} block plugin
|
10 | *
|
10 | *
|
11 | * Type: block function<br>
|
11 | * Type: block function<br>
|
12 | * Name: textformat<br>
|
12 | * Name: textformat<br>
|
13 | * Purpose: format text a certain way with preset styles
|
13 | * Purpose: format text a certain way with preset styles
|
14 | * or custom wrap/indent settings<br>
|
14 | * or custom wrap/indent settings<br>
|
15 | * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
|
15 | * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
|
16 | * (Smarty online manual)
|
16 | * (Smarty online manual)
|
17 | * @param array
|
17 | * @param array
|
18 | * <pre>
|
18 | * <pre>
|
19 | * Params: style: string (email)
|
19 | * Params: style: string (email)
|
20 | * indent: integer (0)
|
20 | * indent: integer (0)
|
21 | * wrap: integer (80)
|
21 | * wrap: integer (80)
|
22 | * wrap_char string ("\n")
|
22 | * wrap_char string ("\n")
|
23 | * indent_char: string (" ")
|
23 | * indent_char: string (" ")
|
24 | * wrap_boundary: boolean (true)
|
24 | * wrap_boundary: boolean (true)
|
25 | * </pre>
|
25 | * </pre>
|
26 | * @author Monte Ohrt <monte at ohrt dot com>
|
26 | * @author Monte Ohrt <monte at ohrt dot com>
|
27 | * @param string contents of the block
|
27 | * @param string contents of the block
|
28 | * @param Smarty clever simulation of a method
|
28 | * @param Smarty clever simulation of a method
|
29 | * @return string string $content re-formatted
|
29 | * @return string string $content re-formatted
|
30 | */
|
30 | */
|
31 | function smarty_block_textformat($params, $content, &$smarty) |
31 | function smarty_block_textformat($params, $content, &$smarty) |
32 | {
|
32 | {
|
33 | if (is_null($content)) { |
33 | if (is_null($content)) { |
34 | return; |
34 | return; |
35 | }
|
35 | }
|
36 | 36 | ||
37 | $style = null; |
37 | $style = null; |
38 | $indent = 0; |
38 | $indent = 0; |
39 | $indent_first = 0; |
39 | $indent_first = 0; |
40 | $indent_char = ' '; |
40 | $indent_char = ' '; |
41 | $wrap = 80; |
41 | $wrap = 80; |
42 | $wrap_char = "\n"; |
42 | $wrap_char = "\n"; |
43 | $wrap_cut = false; |
43 | $wrap_cut = false; |
44 | $assign = null; |
44 | $assign = null; |
45 | 45 | ||
46 | foreach ($params as $_key => $_val) { |
46 | foreach ($params as $_key => $_val) { |
47 | switch ($_key) { |
47 | switch ($_key) { |
48 | case 'style': |
48 | case 'style': |
49 | case 'indent_char': |
49 | case 'indent_char': |
50 | case 'wrap_char': |
50 | case 'wrap_char': |
51 | case 'assign': |
51 | case 'assign': |
52 | $$_key = (string)$_val; |
52 | $$_key = (string)$_val; |
53 | break; |
53 | break; |
54 | 54 | ||
55 | case 'indent': |
55 | case 'indent': |
56 | case 'indent_first': |
56 | case 'indent_first': |
57 | case 'wrap': |
57 | case 'wrap': |
58 | $$_key = (int)$_val; |
58 | $$_key = (int)$_val; |
59 | break; |
59 | break; |
60 | 60 | ||
61 | case 'wrap_cut': |
61 | case 'wrap_cut': |
62 | $$_key = (bool)$_val; |
62 | $$_key = (bool)$_val; |
63 | break; |
63 | break; |
64 | 64 | ||
65 | default: |
65 | default: |
66 | $smarty->trigger_error("textformat: unknown attribute '$_key'"); |
66 | $smarty->trigger_error("textformat: unknown attribute '$_key'"); |
67 | }
|
67 | }
|
68 | }
|
68 | }
|
69 | 69 | ||
70 | if ($style == 'email') { |
70 | if ($style == 'email') { |
71 | $wrap = 72; |
71 | $wrap = 72; |
72 | }
|
72 | }
|
73 | 73 | ||
74 | // split into paragraphs
|
74 | // split into paragraphs
|
75 | $_paragraphs = preg_split('![\r\n][\r\n]!',$content); |
75 | $_paragraphs = preg_split('![\r\n][\r\n]!',$content); |
76 | $_output = ''; |
76 | $_output = ''; |
77 | 77 | ||
78 | for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) { |
78 | for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) { |
79 | if ($_paragraphs[$_x] == '') { |
79 | if ($_paragraphs[$_x] == '') { |
80 | continue; |
80 | continue; |
81 | }
|
81 | }
|
82 | // convert mult. spaces & special chars to single space
|
82 | // convert mult. spaces & special chars to single space
|
83 | $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]); |
83 | $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]); |
84 | // indent first line
|
84 | // indent first line
|
85 | if($indent_first > 0) { |
85 | if($indent_first > 0) { |
86 | $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x]; |
86 | $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x]; |
87 | }
|
87 | }
|
88 | // wordwrap sentences
|
88 | // wordwrap sentences
|
89 | $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut); |
89 | $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut); |
90 | // indent lines
|
90 | // indent lines
|
91 | if($indent > 0) { |
91 | if($indent > 0) { |
92 | $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]); |
92 | $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]); |
93 | }
|
93 | }
|
94 | }
|
94 | }
|
95 | $_output = implode($wrap_char . $wrap_char, $_paragraphs); |
95 | $_output = implode($wrap_char . $wrap_char, $_paragraphs); |
96 | 96 | ||
97 | return $assign ? $smarty->assign($assign, $_output) : $_output; |
97 | return $assign ? $smarty->assign($assign, $_output) : $_output; |
98 | 98 | ||
99 | }
|
99 | }
|
100 | 100 | ||
101 | /* vim: set expandtab: */
|
101 | /* vim: set expandtab: */
|
102 | 102 | ||
103 | ?>
|
103 | ?>
|
104 | 104 |