]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/block.textformat.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / plugins / block.textformat.php
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 {
38     if (is_null($content)) {
39         return;
40     }
41     if (Smarty::$_MBSTRING && !is_callable('smarty_mb_wordwrap')) {
42         require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
43     }
44
45     $style = null;
46     $indent = 0;
47     $indent_first = 0;
48     $indent_char = ' ';
49     $wrap = 80;
50     $wrap_char = "\n";
51     $wrap_cut = false;
52     $assign = null;
53
54     foreach ($params as $_key => $_val) {
55         switch ($_key) {
56             case 'style':
57             case 'indent_char':
58             case 'wrap_char':
59             case 'assign':
60                 $$_key = (string) $_val;
61                 break;
62
63             case 'indent':
64             case 'indent_first':
65             case 'wrap':
66                 $$_key = (int) $_val;
67                 break;
68
69             case 'wrap_cut':
70                 $$_key = (bool) $_val;
71                 break;
72
73             default:
74                 trigger_error("textformat: unknown attribute '$_key'");
75         }
76     }
77
78     if ($style == 'email') {
79         $wrap = 72;
80     }
81     // split into paragraphs
82     $_paragraphs = preg_split('![\r\n]{2}!', $content);
83
84     foreach ($_paragraphs as &$_paragraph) {
85         if (!$_paragraph) {
86             continue;
87         }
88         // convert mult. spaces & special chars to single space
89         $_paragraph =
90             preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER),
91                          array(' ', ''), $_paragraph);
92         // indent first line
93         if ($indent_first > 0) {
94             $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
95         }
96         // wordwrap sentences
97         if (Smarty::$_MBSTRING) {
98             $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
99         } else {
100             $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
101         }
102         // indent lines
103         if ($indent > 0) {
104             $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
105         }
106     }
107     $_output = implode($wrap_char . $wrap_char, $_paragraphs);
108
109     if ($assign) {
110         $template->assign($assign, $_output);
111     } else {
112         return $_output;
113     }
114 }