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($markdown)
65 $html2 = Text\Markdown::convert($markdown);
67 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert'),
71 $bbcode3 = Text\Markdown::toBBCode($markdown);
73 'title' => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
74 'content' => visible_whitespace($bbcode3)
77 $bbcode4 = Text\HTML::toBBCode($html2);
79 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
80 'content' => visible_whitespace($bbcode4)
88 Item::setHashtags($item);
90 'title' => L10n::t('Item Body'),
91 'content' => visible_whitespace($item['body'])
94 'title' => L10n::t('Item Tags'),
95 'content' => $item['tag']
99 $markdown = trim($_REQUEST['text']);
101 'title' => L10n::t('Source input (Diaspora format)'),
102 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
105 $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
107 'title' => L10n::t('Markdown::convert (raw HTML)'),
108 'content' => visible_whitespace(htmlspecialchars($html))
112 'title' => L10n::t('Markdown::convert'),
116 $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
118 'title' => L10n::t('Markdown::toBBCode'),
119 'content' => '<pre>' . $bbcode . '</pre>'
123 $html = trim($_REQUEST['text']);
125 'title' => L10n::t('Raw HTML input'),
126 'content' => htmlspecialchars($html)
130 'title' => L10n::t('HTML Input'),
134 $bbcode = Text\HTML::toBBCode($html);
136 'title' => L10n::t('HTML::toBBCode'),
137 'content' => visible_whitespace($bbcode)
140 $html2 = Text\BBCode::convert($bbcode);
142 'title' => L10n::t('HTML::toBBCode => BBCode::convert'),
147 'title' => L10n::t('HTML::toBBCode => BBCode::convert (raw HTML)'),
148 'content' => htmlspecialchars($html2)
151 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
153 'title' => L10n::t('HTML::toBBCode => BBCode::toPlaintext'),
154 'content' => '<pre>' . $bbcode2plain . '</pre>'
157 $markdown = Text\HTML::toMarkdown($html);
159 'title' => L10n::t('HTML::toMarkdown'),
160 'content' => visible_whitespace($markdown)
163 $text = Text\HTML::toPlaintext($html, 0);
165 'title' => L10n::t('HTML::toPlaintext'),
166 'content' => '<pre>' . $text . '</pre>'
169 $text = Text\HTML::toPlaintext($html, 0, true);
171 'title' => L10n::t('HTML::toPlaintext (compact)'),
172 'content' => '<pre>' . $text . '</pre>'
177 $tpl = Renderer::getMarkupTemplate('babel.tpl');
178 $o = Renderer::replaceMacros($tpl, [
179 '$text' => ['text', L10n::t('Source text'), $_REQUEST['text'] ?? '', ''],
180 '$type_bbcode' => ['type', L10n::t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
181 '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
182 '$type_html' => ['type', L10n::t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
183 '$results' => $results