]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
Merge pull request #8751 from annando/notice
[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\Model\Tag;
30 use Friendica\Util\XML;
31
32 /**
33  * Translates input text into different formats (HTML, BBCode, Markdown)
34  */
35 class Babel extends BaseModule
36 {
37         public static function content(array $parameters = [])
38         {
39                 function visible_whitespace($s)
40                 {
41                         return '<pre>' . htmlspecialchars($s) . '</pre>';
42                 }
43
44                 $results = [];
45                 if (!empty($_REQUEST['text'])) {
46                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
47                                 case 'bbcode':
48                                         $bbcode = trim($_REQUEST['text']);
49                                         $results[] = [
50                                                 'title'   => DI::l10n()->t('Source input'),
51                                                 'content' => visible_whitespace($bbcode)
52                                         ];
53
54                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
55                                         $results[] = [
56                                                 'title'   => DI::l10n()->t('BBCode::toPlaintext'),
57                                                 'content' => visible_whitespace($plain)
58                                         ];
59
60                                         $html = Text\BBCode::convert($bbcode);
61                                         $results[] = [
62                                                 'title'   => DI::l10n()->t('BBCode::convert (raw HTML)'),
63                                                 'content' => visible_whitespace($html)
64                                         ];
65
66                                         $results[] = [
67                                                 'title'   => DI::l10n()->t('BBCode::convert'),
68                                                 'content' => $html
69                                         ];
70
71                                         $bbcode2 = Text\HTML::toBBCode($html);
72                                         $results[] = [
73                                                 'title'   => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
74                                                 'content' => visible_whitespace($bbcode2)
75                                         ];
76
77                                         $markdown = Text\BBCode::toMarkdown($bbcode);
78                                         $results[] = [
79                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown'),
80                                                 'content' => visible_whitespace($markdown)
81                                         ];
82
83                                         $html2 = Text\Markdown::convert($markdown);
84                                         $results[] = [
85                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
86                                                 'content' => visible_whitespace($html2)
87                                         ];
88                                         $results[] = [
89                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
90                                                 'content' => $html2
91                                         ];
92
93                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
94                                         $results[] = [
95                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
96                                                 'content' => visible_whitespace($bbcode3)
97                                         ];
98
99                                         $bbcode4 = Text\HTML::toBBCode($html2);
100                                         $results[] = [
101                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
102                                                 'content' => visible_whitespace($bbcode4)
103                                         ];
104
105                                         $tags = Text\BBCode::getTags($bbcode);
106
107                                         $body = Item::setHashtags($bbcode);
108                                         $results[] = [
109                                                 'title'   => DI::l10n()->t('Item Body'),
110                                                 'content' => visible_whitespace($body)
111                                         ];
112                                         $results[] = [
113                                                 'title'   => DI::l10n()->t('Item Tags'),
114                                                 'content' => visible_whitespace(var_export($tags, true)),
115                                         ];
116                                         break;
117                                 case 'diaspora':
118                                         $diaspora = trim($_REQUEST['text']);
119                                         $results[] = [
120                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
121                                                 'content' => visible_whitespace($diaspora),
122                                         ];
123
124                                         $markdown = XML::unescape($diaspora);
125                                 case 'markdown':
126                                         $markdown = $markdown ?? trim($_REQUEST['text']);
127
128                                         $results[] = [
129                                                 'title'   => DI::l10n()->t('Source input (Markdown)'),
130                                                 'content' => visible_whitespace($markdown),
131                                         ];
132
133                                         $html = Text\Markdown::convert($markdown);
134                                         $results[] = [
135                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
136                                                 'content' => visible_whitespace($html),
137                                         ];
138
139                                         $results[] = [
140                                                 'title'   => DI::l10n()->t('Markdown::convert'),
141                                                 'content' => $html
142                                         ];
143
144                                         $bbcode = Text\Markdown::toBBCode($markdown);
145                                         $results[] = [
146                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
147                                                 'content' => visible_whitespace($bbcode),
148                                         ];
149                                         break;
150                                 case 'html' :
151                                         $html = trim($_REQUEST['text']);
152                                         $results[] = [
153                                                 'title'   => DI::l10n()->t('Raw HTML input'),
154                                                 'content' => visible_whitespace($html),
155                                         ];
156
157                                         $results[] = [
158                                                 'title'   => DI::l10n()->t('HTML Input'),
159                                                 'content' => $html
160                                         ];
161
162                                         $bbcode = Text\HTML::toBBCode($html);
163                                         $results[] = [
164                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
165                                                 'content' => visible_whitespace($bbcode)
166                                         ];
167
168                                         $html2 = Text\BBCode::convert($bbcode);
169                                         $results[] = [
170                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
171                                                 'content' => $html2
172                                         ];
173
174                                         $results[] = [
175                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
176                                                 'content' => htmlspecialchars($html2)
177                                         ];
178
179                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
180                                         $results[] = [
181                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
182                                                 'content' => visible_whitespace($bbcode2plain),
183                                         ];
184
185                                         $markdown = Text\HTML::toMarkdown($html);
186                                         $results[] = [
187                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
188                                                 'content' => visible_whitespace($markdown)
189                                         ];
190
191                                         $text = Text\HTML::toPlaintext($html, 0);
192                                         $results[] = [
193                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
194                                                 'content' => visible_whitespace($text),
195                                         ];
196
197                                         $text = Text\HTML::toPlaintext($html, 0, true);
198                                         $results[] = [
199                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
200                                                 'content' => visible_whitespace($text),
201                                         ];
202                         }
203                 }
204
205                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
206                 $o = Renderer::replaceMacros($tpl, [
207                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
208                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
209                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
210                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
211                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
212                         '$results'       => $results
213                 ]);
214
215                 return $o;
216         }
217 }