3 namespace Friendica\Module\Debug;
5 use Friendica\BaseModule;
6 use Friendica\Content\Text;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Renderer;
9 use Friendica\Model\Item;
10 use Friendica\Util\XML;
13 * Translates input text into different formats (HTML, BBCode, Markdown)
15 class Babel extends BaseModule
17 public static function content(array $parameters = [])
19 function visible_whitespace($s)
21 $s = str_replace(' ', ' ', $s);
23 return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
27 if (!empty($_REQUEST['text'])) {
28 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
30 $bbcode = trim($_REQUEST['text']);
32 'title' => L10n::t('Source input'),
33 'content' => visible_whitespace($bbcode)
36 $plain = Text\BBCode::toPlaintext($bbcode, false);
38 'title' => L10n::t('BBCode::toPlaintext'),
39 'content' => visible_whitespace($plain)
42 $html = Text\BBCode::convert($bbcode);
44 'title' => L10n::t('BBCode::convert (raw HTML)'),
45 'content' => visible_whitespace(htmlspecialchars($html))
49 'title' => L10n::t('BBCode::convert'),
53 $bbcode2 = Text\HTML::toBBCode($html);
55 'title' => L10n::t('BBCode::convert => HTML::toBBCode'),
56 'content' => visible_whitespace($bbcode2)
59 $markdown = Text\BBCode::toMarkdown($bbcode);
61 'title' => L10n::t('BBCode::toMarkdown'),
62 'content' => visible_whitespace(htmlspecialchars($markdown))
65 $html2 = Text\Markdown::convert($markdown);
67 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
68 'content' => visible_whitespace(htmlspecialchars($html2))
71 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert'),
75 $bbcode3 = Text\Markdown::toBBCode($markdown);
77 'title' => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
78 'content' => visible_whitespace($bbcode3)
81 $bbcode4 = Text\HTML::toBBCode($html2);
83 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
84 'content' => visible_whitespace($bbcode4)
92 Item::setHashtags($item);
94 'title' => L10n::t('Item Body'),
95 'content' => visible_whitespace($item['body'])
98 'title' => L10n::t('Item Tags'),
99 'content' => $item['tag']
103 $markdown = trim($_REQUEST['text']);
105 'title' => L10n::t('Source input (Diaspora format)'),
106 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
109 $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
111 'title' => L10n::t('Markdown::convert (raw HTML)'),
112 'content' => visible_whitespace(htmlspecialchars($html))
116 'title' => L10n::t('Markdown::convert'),
120 $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
122 'title' => L10n::t('Markdown::toBBCode'),
123 'content' => '<pre>' . $bbcode . '</pre>'
127 $html = trim($_REQUEST['text']);
129 'title' => L10n::t('Raw HTML input'),
130 'content' => htmlspecialchars($html)
134 'title' => L10n::t('HTML Input'),
138 $bbcode = Text\HTML::toBBCode($html);
140 'title' => L10n::t('HTML::toBBCode'),
141 'content' => visible_whitespace($bbcode)
144 $html2 = Text\BBCode::convert($bbcode);
146 'title' => L10n::t('HTML::toBBCode => BBCode::convert'),
151 'title' => L10n::t('HTML::toBBCode => BBCode::convert (raw HTML)'),
152 'content' => htmlspecialchars($html2)
155 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
157 'title' => L10n::t('HTML::toBBCode => BBCode::toPlaintext'),
158 'content' => '<pre>' . $bbcode2plain . '</pre>'
161 $markdown = Text\HTML::toMarkdown($html);
163 'title' => L10n::t('HTML::toMarkdown'),
164 'content' => visible_whitespace($markdown)
167 $text = Text\HTML::toPlaintext($html, 0);
169 'title' => L10n::t('HTML::toPlaintext'),
170 'content' => '<pre>' . $text . '</pre>'
173 $text = Text\HTML::toPlaintext($html, 0, true);
175 'title' => L10n::t('HTML::toPlaintext (compact)'),
176 'content' => '<pre>' . $text . '</pre>'
181 $tpl = Renderer::getMarkupTemplate('babel.tpl');
182 $o = Renderer::replaceMacros($tpl, [
183 '$text' => ['text', L10n::t('Source text'), $_REQUEST['text'] ?? '', ''],
184 '$type_bbcode' => ['type', L10n::t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
185 '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
186 '$type_html' => ['type', L10n::t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
187 '$results' => $results