]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
Improve Babel code
[friendica.git] / src / Module / Debug / Babel.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Debug;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Text;
26 use Friendica\Core\Renderer;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Util\XML;
30
31 /**
32  * Translates input text into different formats (HTML, BBCode, Markdown)
33  */
34 class Babel extends BaseModule
35 {
36         public static function content(array $parameters = [])
37         {
38                 function visible_whitespace($s)
39                 {
40                         return '<pre>' . htmlspecialchars($s) . '</pre>';
41                 }
42
43                 $results = [];
44                 if (!empty($_REQUEST['text'])) {
45                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
46                                 case 'bbcode':
47                                         $bbcode = trim($_REQUEST['text']);
48                                         $results[] = [
49                                                 'title'   => DI::l10n()->t('Source input'),
50                                                 'content' => visible_whitespace($bbcode)
51                                         ];
52
53                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
54                                         $results[] = [
55                                                 'title'   => DI::l10n()->t('BBCode::toPlaintext'),
56                                                 'content' => visible_whitespace($plain)
57                                         ];
58
59                                         $html = Text\BBCode::convert($bbcode);
60                                         $results[] = [
61                                                 'title'   => DI::l10n()->t('BBCode::convert (raw HTML)'),
62                                                 'content' => visible_whitespace($html)
63                                         ];
64
65                                         $results[] = [
66                                                 'title'   => DI::l10n()->t('BBCode::convert'),
67                                                 'content' => $html
68                                         ];
69
70                                         $bbcode2 = Text\HTML::toBBCode($html);
71                                         $results[] = [
72                                                 'title'   => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
73                                                 'content' => visible_whitespace($bbcode2)
74                                         ];
75
76                                         $markdown = Text\BBCode::toMarkdown($bbcode);
77                                         $results[] = [
78                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown'),
79                                                 'content' => visible_whitespace($markdown)
80                                         ];
81
82                                         $html2 = Text\Markdown::convert($markdown);
83                                         $results[] = [
84                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
85                                                 'content' => visible_whitespace($html2)
86                                         ];
87                                         $results[] = [
88                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
89                                                 'content' => $html2
90                                         ];
91
92                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
93                                         $results[] = [
94                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
95                                                 'content' => visible_whitespace($bbcode3)
96                                         ];
97
98                                         $bbcode4 = Text\HTML::toBBCode($html2);
99                                         $results[] = [
100                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
101                                                 'content' => visible_whitespace($bbcode4)
102                                         ];
103
104                                         $item = [
105                                                 'body' => $bbcode,
106                                                 'tag'  => '',
107                                         ];
108
109                                         Item::setHashtags($item);
110                                         $results[] = [
111                                                 'title'   => DI::l10n()->t('Item Body'),
112                                                 'content' => visible_whitespace($item['body'])
113                                         ];
114                                         $results[] = [
115                                                 'title'   => DI::l10n()->t('Item Tags'),
116                                                 'content' => $item['tag']
117                                         ];
118                                         break;
119                                 case 'diaspora':
120                                         $diaspora = trim($_REQUEST['text']);
121                                         $results[] = [
122                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
123                                                 'content' => visible_whitespace($diaspora),
124                                         ];
125
126                                         $markdown = XML::unescape($diaspora);
127                                 case 'markdown':
128                                         if (!isset($markdown)) {
129                                                 $markdown = trim($_REQUEST['text']);
130                                         }
131
132                                         $results[] = [
133                                                 'title'   => DI::l10n()->t('Source input (Markdown)'),
134                                                 'content' => visible_whitespace($markdown),
135                                         ];
136
137                                         $html = Text\Markdown::convert($markdown);
138                                         $results[] = [
139                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
140                                                 'content' => visible_whitespace($html),
141                                         ];
142
143                                         $results[] = [
144                                                 'title'   => DI::l10n()->t('Markdown::convert'),
145                                                 'content' => $html
146                                         ];
147
148                                         $bbcode = Text\Markdown::toBBCode($markdown);
149                                         $results[] = [
150                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
151                                                 'content' => visible_whitespace($bbcode),
152                                         ];
153                                         break;
154                                 case 'html' :
155                                         $html = trim($_REQUEST['text']);
156                                         $results[] = [
157                                                 'title'   => DI::l10n()->t('Raw HTML input'),
158                                                 'content' => visible_whitespace($html),
159                                         ];
160
161                                         $results[] = [
162                                                 'title'   => DI::l10n()->t('HTML Input'),
163                                                 'content' => $html
164                                         ];
165
166                                         $bbcode = Text\HTML::toBBCode($html);
167                                         $results[] = [
168                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
169                                                 'content' => visible_whitespace($bbcode)
170                                         ];
171
172                                         $html2 = Text\BBCode::convert($bbcode);
173                                         $results[] = [
174                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
175                                                 'content' => $html2
176                                         ];
177
178                                         $results[] = [
179                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
180                                                 'content' => htmlspecialchars($html2)
181                                         ];
182
183                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
184                                         $results[] = [
185                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
186                                                 'content' => visible_whitespace($bbcode2plain),
187                                         ];
188
189                                         $markdown = Text\HTML::toMarkdown($html);
190                                         $results[] = [
191                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
192                                                 'content' => visible_whitespace($markdown)
193                                         ];
194
195                                         $text = Text\HTML::toPlaintext($html, 0);
196                                         $results[] = [
197                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
198                                                 'content' => visible_whitespace($text),
199                                         ];
200
201                                         $text = Text\HTML::toPlaintext($html, 0, true);
202                                         $results[] = [
203                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
204                                                 'content' => visible_whitespace($text),
205                                         ];
206                         }
207                 }
208
209                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
210                 $o = Renderer::replaceMacros($tpl, [
211                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
212                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
213                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
214                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
215                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
216                         '$results'       => $results
217                 ]);
218
219                 return $o;
220         }
221 }