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\Protocol;
28 use Friendica\Core\Renderer;
30 use Friendica\Model\Conversation;
31 use Friendica\Model\Item;
32 use Friendica\Protocol\Activity;
33 use Friendica\Model\Tag;
34 use Friendica\Util\XML;
37 * Translates input text into different formats (HTML, BBCode, Markdown)
39 class Babel extends BaseModule
41 public static function content(array $parameters = [])
43 function visible_whitespace($s)
45 return '<pre>' . htmlspecialchars($s) . '</pre>';
49 if (!empty($_REQUEST['text'])) {
50 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
52 $bbcode = trim($_REQUEST['text']);
54 'title' => DI::l10n()->t('Source input'),
55 'content' => visible_whitespace($bbcode)
58 $plain = Text\BBCode::toPlaintext($bbcode, false);
60 'title' => DI::l10n()->t('BBCode::toPlaintext'),
61 'content' => visible_whitespace($plain)
64 $html = Text\BBCode::convert($bbcode);
66 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'),
67 'content' => visible_whitespace($html)
71 'title' => DI::l10n()->t('BBCode::convert'),
75 $bbcode2 = Text\HTML::toBBCode($html);
77 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
78 'content' => visible_whitespace($bbcode2)
81 $markdown = Text\BBCode::toMarkdown($bbcode);
83 'title' => DI::l10n()->t('BBCode::toMarkdown'),
84 'content' => visible_whitespace($markdown)
87 $html2 = Text\Markdown::convert($markdown);
89 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
90 'content' => visible_whitespace($html2)
93 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
97 $bbcode3 = Text\Markdown::toBBCode($markdown);
99 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
100 'content' => visible_whitespace($bbcode3)
103 $bbcode4 = Text\HTML::toBBCode($html2);
105 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
106 'content' => visible_whitespace($bbcode4)
109 $tags = Text\BBCode::getTags($bbcode);
111 $body = Item::setHashtags($bbcode);
113 'title' => DI::l10n()->t('Item Body'),
114 'content' => visible_whitespace($body)
117 'title' => DI::l10n()->t('Item Tags'),
118 'content' => visible_whitespace(var_export($tags, true)),
121 $body2 = PageInfo::searchAndAppendToBody($bbcode, true);
123 'title' => DI::l10n()->t('PageInfo::appendToBody'),
124 'content' => visible_whitespace($body2)
126 $html3 = Text\BBCode::convert($body2);
128 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
129 'content' => visible_whitespace($html3)
132 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
137 $diaspora = trim($_REQUEST['text']);
139 'title' => DI::l10n()->t('Source input (Diaspora format)'),
140 'content' => visible_whitespace($diaspora),
143 $markdown = XML::unescape($diaspora);
145 $markdown = $markdown ?? trim($_REQUEST['text']);
148 'title' => DI::l10n()->t('Source input (Markdown)'),
149 'content' => visible_whitespace($markdown),
152 $html = Text\Markdown::convert($markdown);
154 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
155 'content' => visible_whitespace($html),
159 'title' => DI::l10n()->t('Markdown::convert'),
163 $bbcode = Text\Markdown::toBBCode($markdown);
165 'title' => DI::l10n()->t('Markdown::toBBCode'),
166 'content' => visible_whitespace($bbcode),
170 $html = trim($_REQUEST['text']);
172 'title' => DI::l10n()->t('Raw HTML input'),
173 'content' => visible_whitespace($html),
177 'title' => DI::l10n()->t('HTML Input'),
181 $bbcode = Text\HTML::toBBCode($html);
183 'title' => DI::l10n()->t('HTML::toBBCode'),
184 'content' => visible_whitespace($bbcode)
187 $html2 = Text\BBCode::convert($bbcode);
189 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
194 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
195 'content' => htmlspecialchars($html2)
198 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
200 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
201 'content' => visible_whitespace($bbcode2plain),
204 $markdown = Text\HTML::toMarkdown($html);
206 'title' => DI::l10n()->t('HTML::toMarkdown'),
207 'content' => visible_whitespace($markdown)
210 $text = Text\HTML::toPlaintext($html, 0);
212 'title' => DI::l10n()->t('HTML::toPlaintext'),
213 'content' => visible_whitespace($text),
216 $text = Text\HTML::toPlaintext($html, 0, true);
218 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
219 'content' => visible_whitespace($text),
223 $json = trim($_REQUEST['text']);
225 $status = json_decode($json);
228 'title' => DI::l10n()->t('Decoded post'),
229 'content' => visible_whitespace(var_export($status, true)),
233 $postarray['object-type'] = Activity\ObjectType::NOTE;
235 if (!empty($status->full_text)) {
236 $postarray['body'] = $status->full_text;
238 $postarray['body'] = $status->text;
241 // When the post contains links then use the correct object type
242 if (count($status->entities->urls) > 0) {
243 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
246 if (file_exists('addon/twitter/twitter.php')) {
247 require_once 'addon/twitter/twitter.php';
249 $picture = \twitter_media_entities($status, $postarray);
252 'title' => DI::l10n()->t('Post array before expand entities'),
253 'content' => visible_whitespace(var_export($postarray, true)),
256 $converted = \twitter_expand_entities($postarray['body'], $status, $picture);
259 'title' => DI::l10n()->t('Post converted'),
260 'content' => visible_whitespace(var_export($converted, true)),
264 'title' => DI::l10n()->t('Converted body'),
265 'content' => visible_whitespace($converted['body']),
269 'title' => DI::l10n()->t('Error'),
270 'content' => DI::l10n()->t('Twitter addon is absent from the addon/ folder.'),
278 $tpl = Renderer::getMarkupTemplate('babel.tpl');
279 $o = Renderer::replaceMacros($tpl, [
280 '$text' => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
281 '$type_bbcode' => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
282 '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
283 '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
284 '$type_html' => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
285 '$flag_twitter' => file_exists('addon/twitter/twitter.php'),
286 '$type_twitter' => ['type', DI::l10n()->t('Twitter Source'), 'twitter', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'twitter'],
287 '$results' => $results