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\Text;
26 use Friendica\Core\Renderer;
28 use Friendica\Model\Item;
29 use Friendica\Util\XML;
32 * Translates input text into different formats (HTML, BBCode, Markdown)
34 class Babel extends BaseModule
36 public static function content(array $parameters = [])
38 function visible_whitespace($s)
40 $s = str_replace(' ', ' ', $s);
42 return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
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(htmlspecialchars($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(htmlspecialchars($markdown))
84 $html2 = Text\Markdown::convert($markdown);
86 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
87 'content' => visible_whitespace(htmlspecialchars($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)
111 Item::setHashtags($item);
113 'title' => DI::l10n()->t('Item Body'),
114 'content' => visible_whitespace($item['body'])
117 'title' => DI::l10n()->t('Item Tags'),
118 'content' => $item['tag']
122 $markdown = trim($_REQUEST['text']);
124 'title' => DI::l10n()->t('Source input (Diaspora format)'),
125 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
128 $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
130 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
131 'content' => visible_whitespace(htmlspecialchars($html))
135 'title' => DI::l10n()->t('Markdown::convert'),
139 $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
141 'title' => DI::l10n()->t('Markdown::toBBCode'),
142 'content' => '<pre>' . $bbcode . '</pre>'
146 $html = trim($_REQUEST['text']);
148 'title' => DI::l10n()->t('Raw HTML input'),
149 'content' => htmlspecialchars($html)
153 'title' => DI::l10n()->t('HTML Input'),
157 $bbcode = Text\HTML::toBBCode($html);
159 'title' => DI::l10n()->t('HTML::toBBCode'),
160 'content' => visible_whitespace($bbcode)
163 $html2 = Text\BBCode::convert($bbcode);
165 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
170 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
171 'content' => htmlspecialchars($html2)
174 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
176 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
177 'content' => '<pre>' . $bbcode2plain . '</pre>'
180 $markdown = Text\HTML::toMarkdown($html);
182 'title' => DI::l10n()->t('HTML::toMarkdown'),
183 'content' => visible_whitespace($markdown)
186 $text = Text\HTML::toPlaintext($html, 0);
188 'title' => DI::l10n()->t('HTML::toPlaintext'),
189 'content' => '<pre>' . $text . '</pre>'
192 $text = Text\HTML::toPlaintext($html, 0, true);
194 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
195 'content' => '<pre>' . $text . '</pre>'
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