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