3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Debug;
24 use Friendica\BaseModule;
25 use Friendica\Content\PageInfo;
26 use Friendica\Content\Text;
27 use Friendica\Core\Renderer;
29 use Friendica\Model\Item;
30 use Friendica\Model\Tag;
31 use Friendica\Util\XML;
34 * Translates input text into different formats (HTML, BBCode, Markdown)
36 class Babel extends BaseModule
38 public static function content(array $parameters = [])
40 function visible_whitespace($s)
42 return '<pre>' . htmlspecialchars($s) . '</pre>';
46 if (!empty($_REQUEST['text'])) {
47 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
49 $bbcode = trim($_REQUEST['text']);
51 'title' => DI::l10n()->t('Source input'),
52 'content' => visible_whitespace($bbcode)
55 $plain = Text\BBCode::toPlaintext($bbcode, false);
57 'title' => DI::l10n()->t('BBCode::toPlaintext'),
58 'content' => visible_whitespace($plain)
61 $html = Text\BBCode::convert($bbcode);
63 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'),
64 'content' => visible_whitespace($html)
68 'title' => DI::l10n()->t('BBCode::convert'),
72 $bbcode2 = Text\HTML::toBBCode($html);
74 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
75 'content' => visible_whitespace($bbcode2)
78 $markdown = Text\BBCode::toMarkdown($bbcode);
80 'title' => DI::l10n()->t('BBCode::toMarkdown'),
81 'content' => visible_whitespace($markdown)
84 $html2 = Text\Markdown::convert($markdown);
86 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
87 'content' => visible_whitespace($html2)
90 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
94 $bbcode3 = Text\Markdown::toBBCode($markdown);
96 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
97 'content' => visible_whitespace($bbcode3)
100 $bbcode4 = Text\HTML::toBBCode($html2);
102 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
103 'content' => visible_whitespace($bbcode4)
106 $tags = Text\BBCode::getTags($bbcode);
108 $body = Item::setHashtags($bbcode);
110 'title' => DI::l10n()->t('Item Body'),
111 'content' => visible_whitespace($body)
114 'title' => DI::l10n()->t('Item Tags'),
115 'content' => visible_whitespace(var_export($tags, true)),
118 $body2 = PageInfo::searchAndAppendToBody($bbcode, true);
120 'title' => DI::l10n()->t('PageInfo::appendToBody'),
121 'content' => visible_whitespace($body2)
123 $html3 = Text\BBCode::convert($body2);
125 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
126 'content' => visible_whitespace($html3)
129 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
134 $diaspora = trim($_REQUEST['text']);
136 'title' => DI::l10n()->t('Source input (Diaspora format)'),
137 'content' => visible_whitespace($diaspora),
140 $markdown = XML::unescape($diaspora);
142 $markdown = $markdown ?? trim($_REQUEST['text']);
145 'title' => DI::l10n()->t('Source input (Markdown)'),
146 'content' => visible_whitespace($markdown),
149 $html = Text\Markdown::convert($markdown);
151 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
152 'content' => visible_whitespace($html),
156 'title' => DI::l10n()->t('Markdown::convert'),
160 $bbcode = Text\Markdown::toBBCode($markdown);
162 'title' => DI::l10n()->t('Markdown::toBBCode'),
163 'content' => visible_whitespace($bbcode),
167 $html = trim($_REQUEST['text']);
169 'title' => DI::l10n()->t('Raw HTML input'),
170 'content' => visible_whitespace($html),
174 'title' => DI::l10n()->t('HTML Input'),
178 $bbcode = Text\HTML::toBBCode($html);
180 'title' => DI::l10n()->t('HTML::toBBCode'),
181 'content' => visible_whitespace($bbcode)
184 $html2 = Text\BBCode::convert($bbcode);
186 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
191 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
192 'content' => htmlspecialchars($html2)
195 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
197 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
198 'content' => visible_whitespace($bbcode2plain),
201 $markdown = Text\HTML::toMarkdown($html);
203 'title' => DI::l10n()->t('HTML::toMarkdown'),
204 'content' => visible_whitespace($markdown)
207 $text = Text\HTML::toPlaintext($html, 0);
209 'title' => DI::l10n()->t('HTML::toPlaintext'),
210 'content' => visible_whitespace($text),
213 $text = Text\HTML::toPlaintext($html, 0, true);
215 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
216 'content' => visible_whitespace($text),
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