]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
cleanup namespace usages for L10n
[friendica.git] / src / Module / Debug / Babel.php
1 <?php
2
3 namespace Friendica\Module\Debug;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Text;
7 use Friendica\Core\Renderer;
8 use Friendica\Model\Item;
9 use Friendica\Util\XML;
10
11 /**
12  * Translates input text into different formats (HTML, BBCode, Markdown)
13  */
14 class Babel extends BaseModule
15 {
16         public static function content(array $parameters = [])
17         {
18                 function visible_whitespace($s)
19                 {
20                         $s = str_replace(' ', '&nbsp;', $s);
21
22                         return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
23                 }
24
25                 $results = [];
26                 if (!empty($_REQUEST['text'])) {
27                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
28                                 case 'bbcode':
29                                         $bbcode = trim($_REQUEST['text']);
30                                         $results[] = [
31                                                 'title'   => DI::l10n()->t('Source input'),
32                                                 'content' => visible_whitespace($bbcode)
33                                         ];
34
35                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
36                                         $results[] = [
37                                                 'title'   => DI::l10n()->t('BBCode::toPlaintext'),
38                                                 'content' => visible_whitespace($plain)
39                                         ];
40
41                                         $html = Text\BBCode::convert($bbcode);
42                                         $results[] = [
43                                                 'title'   => DI::l10n()->t('BBCode::convert (raw HTML)'),
44                                                 'content' => visible_whitespace(htmlspecialchars($html))
45                                         ];
46
47                                         $results[] = [
48                                                 'title'   => DI::l10n()->t('BBCode::convert'),
49                                                 'content' => $html
50                                         ];
51
52                                         $bbcode2 = Text\HTML::toBBCode($html);
53                                         $results[] = [
54                                                 'title'   => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
55                                                 'content' => visible_whitespace($bbcode2)
56                                         ];
57
58                                         $markdown = Text\BBCode::toMarkdown($bbcode);
59                                         $results[] = [
60                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown'),
61                                                 'content' => visible_whitespace(htmlspecialchars($markdown))
62                                         ];
63
64                                         $html2 = Text\Markdown::convert($markdown);
65                                         $results[] = [
66                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
67                                                 'content' => visible_whitespace(htmlspecialchars($html2))
68                                         ];
69                                         $results[] = [
70                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
71                                                 'content' => $html2
72                                         ];
73
74                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
75                                         $results[] = [
76                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
77                                                 'content' => visible_whitespace($bbcode3)
78                                         ];
79
80                                         $bbcode4 = Text\HTML::toBBCode($html2);
81                                         $results[] = [
82                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
83                                                 'content' => visible_whitespace($bbcode4)
84                                         ];
85
86                                         $item = [
87                                                 'body' => $bbcode,
88                                                 'tag'  => '',
89                                         ];
90
91                                         Item::setHashtags($item);
92                                         $results[] = [
93                                                 'title'   => DI::l10n()->t('Item Body'),
94                                                 'content' => visible_whitespace($item['body'])
95                                         ];
96                                         $results[] = [
97                                                 'title'   => DI::l10n()->t('Item Tags'),
98                                                 'content' => $item['tag']
99                                         ];
100                                         break;
101                                 case 'markdown':
102                                         $markdown = trim($_REQUEST['text']);
103                                         $results[] = [
104                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
105                                                 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
106                                         ];
107
108                                         $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
109                                         $results[] = [
110                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
111                                                 'content' => visible_whitespace(htmlspecialchars($html))
112                                         ];
113
114                                         $results[] = [
115                                                 'title'   => DI::l10n()->t('Markdown::convert'),
116                                                 'content' => $html
117                                         ];
118
119                                         $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
120                                         $results[] = [
121                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
122                                                 'content' => '<pre>' . $bbcode . '</pre>'
123                                         ];
124                                         break;
125                                 case 'html' :
126                                         $html = trim($_REQUEST['text']);
127                                         $results[] = [
128                                                 'title'   => DI::l10n()->t('Raw HTML input'),
129                                                 'content' => htmlspecialchars($html)
130                                         ];
131
132                                         $results[] = [
133                                                 'title'   => DI::l10n()->t('HTML Input'),
134                                                 'content' => $html
135                                         ];
136
137                                         $bbcode = Text\HTML::toBBCode($html);
138                                         $results[] = [
139                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
140                                                 'content' => visible_whitespace($bbcode)
141                                         ];
142
143                                         $html2 = Text\BBCode::convert($bbcode);
144                                         $results[] = [
145                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
146                                                 'content' => $html2
147                                         ];
148
149                                         $results[] = [
150                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
151                                                 'content' => htmlspecialchars($html2)
152                                         ];
153
154                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
155                                         $results[] = [
156                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
157                                                 'content' => '<pre>' . $bbcode2plain . '</pre>'
158                                         ];
159
160                                         $markdown = Text\HTML::toMarkdown($html);
161                                         $results[] = [
162                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
163                                                 'content' => visible_whitespace($markdown)
164                                         ];
165
166                                         $text = Text\HTML::toPlaintext($html, 0);
167                                         $results[] = [
168                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
169                                                 'content' => '<pre>' . $text . '</pre>'
170                                         ];
171
172                                         $text = Text\HTML::toPlaintext($html, 0, true);
173                                         $results[] = [
174                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
175                                                 'content' => '<pre>' . $text . '</pre>'
176                                         ];
177                         }
178                 }
179
180                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
181                 $o = Renderer::replaceMacros($tpl, [
182                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
183                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
184                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
185                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
186                         '$results'       => $results
187                 ]);
188
189                 return $o;
190         }
191 }