3 namespace Friendica\Module\Debug;
5 use Friendica\BaseModule;
6 use Friendica\Content\Text;
7 use Friendica\Core\Renderer;
8 use Friendica\Model\Item;
9 use Friendica\Util\XML;
12 * Translates input text into different formats (HTML, BBCode, Markdown)
14 class Babel extends BaseModule
16 public static function content(array $parameters = [])
18 function visible_whitespace($s)
20 $s = str_replace(' ', ' ', $s);
22 return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
26 if (!empty($_REQUEST['text'])) {
27 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
29 $bbcode = trim($_REQUEST['text']);
31 'title' => DI::l10n()->t('Source input'),
32 'content' => visible_whitespace($bbcode)
35 $plain = Text\BBCode::toPlaintext($bbcode, false);
37 'title' => DI::l10n()->t('BBCode::toPlaintext'),
38 'content' => visible_whitespace($plain)
41 $html = Text\BBCode::convert($bbcode);
43 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'),
44 'content' => visible_whitespace(htmlspecialchars($html))
48 'title' => DI::l10n()->t('BBCode::convert'),
52 $bbcode2 = Text\HTML::toBBCode($html);
54 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
55 'content' => visible_whitespace($bbcode2)
58 $markdown = Text\BBCode::toMarkdown($bbcode);
60 'title' => DI::l10n()->t('BBCode::toMarkdown'),
61 'content' => visible_whitespace(htmlspecialchars($markdown))
64 $html2 = Text\Markdown::convert($markdown);
66 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
67 'content' => visible_whitespace(htmlspecialchars($html2))
70 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
74 $bbcode3 = Text\Markdown::toBBCode($markdown);
76 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
77 'content' => visible_whitespace($bbcode3)
80 $bbcode4 = Text\HTML::toBBCode($html2);
82 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
83 'content' => visible_whitespace($bbcode4)
91 Item::setHashtags($item);
93 'title' => DI::l10n()->t('Item Body'),
94 'content' => visible_whitespace($item['body'])
97 'title' => DI::l10n()->t('Item Tags'),
98 'content' => $item['tag']
102 $markdown = trim($_REQUEST['text']);
104 'title' => DI::l10n()->t('Source input (Diaspora format)'),
105 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
108 $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
110 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
111 'content' => visible_whitespace(htmlspecialchars($html))
115 'title' => DI::l10n()->t('Markdown::convert'),
119 $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
121 'title' => DI::l10n()->t('Markdown::toBBCode'),
122 'content' => '<pre>' . $bbcode . '</pre>'
126 $html = trim($_REQUEST['text']);
128 'title' => DI::l10n()->t('Raw HTML input'),
129 'content' => htmlspecialchars($html)
133 'title' => DI::l10n()->t('HTML Input'),
137 $bbcode = Text\HTML::toBBCode($html);
139 'title' => DI::l10n()->t('HTML::toBBCode'),
140 'content' => visible_whitespace($bbcode)
143 $html2 = Text\BBCode::convert($bbcode);
145 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
150 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
151 'content' => htmlspecialchars($html2)
154 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
156 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
157 'content' => '<pre>' . $bbcode2plain . '</pre>'
160 $markdown = Text\HTML::toMarkdown($html);
162 'title' => DI::l10n()->t('HTML::toMarkdown'),
163 'content' => visible_whitespace($markdown)
166 $text = Text\HTML::toPlaintext($html, 0);
168 'title' => DI::l10n()->t('HTML::toPlaintext'),
169 'content' => '<pre>' . $text . '</pre>'
172 $text = Text\HTML::toPlaintext($html, 0, true);
174 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
175 'content' => '<pre>' . $text . '</pre>'
180 $tpl = Renderer::getMarkupTemplate('babel.tpl');
181 $o = Renderer::replaceMacros($tpl, [
182 '$text' => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
183 '$type_bbcode' => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
184 '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
185 '$type_html' => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
186 '$results' => $results