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