]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
Add PageInfo result panels to Debug\Babel
[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\PageInfo;
26 use Friendica\Content\Text;
27 use Friendica\Core\Renderer;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Tag;
31 use Friendica\Util\XML;
32
33 /**
34  * Translates input text into different formats (HTML, BBCode, Markdown)
35  */
36 class Babel extends BaseModule
37 {
38         public static function content(array $parameters = [])
39         {
40                 function visible_whitespace($s)
41                 {
42                         return '<pre>' . htmlspecialchars($s) . '</pre>';
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($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($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($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                                         $tags = Text\BBCode::getTags($bbcode);
107
108                                         $body = Item::setHashtags($bbcode);
109                                         $results[] = [
110                                                 'title'   => DI::l10n()->t('Item Body'),
111                                                 'content' => visible_whitespace($body)
112                                         ];
113                                         $results[] = [
114                                                 'title'   => DI::l10n()->t('Item Tags'),
115                                                 'content' => visible_whitespace(var_export($tags, true)),
116                                         ];
117
118                                         $body2 = PageInfo::appendToBody($bbcode, true);
119                                         $results[] = [
120                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody'),
121                                                 'content' => visible_whitespace($body2)
122                                         ];
123                                         $html3 = Text\BBCode::convert($body2);
124                                         $results[] = [
125                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
126                                                 'content' => visible_whitespace($html3)
127                                         ];
128                                         $results[] = [
129                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
130                                                 'content' => $html3
131                                         ];
132                                         break;
133                                 case 'diaspora':
134                                         $diaspora = trim($_REQUEST['text']);
135                                         $results[] = [
136                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
137                                                 'content' => visible_whitespace($diaspora),
138                                         ];
139
140                                         $markdown = XML::unescape($diaspora);
141                                 case 'markdown':
142                                         $markdown = $markdown ?? trim($_REQUEST['text']);
143
144                                         $results[] = [
145                                                 'title'   => DI::l10n()->t('Source input (Markdown)'),
146                                                 'content' => visible_whitespace($markdown),
147                                         ];
148
149                                         $html = Text\Markdown::convert($markdown);
150                                         $results[] = [
151                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
152                                                 'content' => visible_whitespace($html),
153                                         ];
154
155                                         $results[] = [
156                                                 'title'   => DI::l10n()->t('Markdown::convert'),
157                                                 'content' => $html
158                                         ];
159
160                                         $bbcode = Text\Markdown::toBBCode($markdown);
161                                         $results[] = [
162                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
163                                                 'content' => visible_whitespace($bbcode),
164                                         ];
165                                         break;
166                                 case 'html' :
167                                         $html = trim($_REQUEST['text']);
168                                         $results[] = [
169                                                 'title'   => DI::l10n()->t('Raw HTML input'),
170                                                 'content' => visible_whitespace($html),
171                                         ];
172
173                                         $results[] = [
174                                                 'title'   => DI::l10n()->t('HTML Input'),
175                                                 'content' => $html
176                                         ];
177
178                                         $bbcode = Text\HTML::toBBCode($html);
179                                         $results[] = [
180                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
181                                                 'content' => visible_whitespace($bbcode)
182                                         ];
183
184                                         $html2 = Text\BBCode::convert($bbcode);
185                                         $results[] = [
186                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
187                                                 'content' => $html2
188                                         ];
189
190                                         $results[] = [
191                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
192                                                 'content' => htmlspecialchars($html2)
193                                         ];
194
195                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
196                                         $results[] = [
197                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
198                                                 'content' => visible_whitespace($bbcode2plain),
199                                         ];
200
201                                         $markdown = Text\HTML::toMarkdown($html);
202                                         $results[] = [
203                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
204                                                 'content' => visible_whitespace($markdown)
205                                         ];
206
207                                         $text = Text\HTML::toPlaintext($html, 0);
208                                         $results[] = [
209                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
210                                                 'content' => visible_whitespace($text),
211                                         ];
212
213                                         $text = Text\HTML::toPlaintext($html, 0, true);
214                                         $results[] = [
215                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
216                                                 'content' => visible_whitespace($text),
217                                         ];
218                         }
219                 }
220
221                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
222                 $o = Renderer::replaceMacros($tpl, [
223                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
224                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
225                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
226                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
227                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
228                         '$results'       => $results
229                 ]);
230
231                 return $o;
232         }
233 }