]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
cf1f869552e6116b0c3bb097c97120bdafdb5851
[friendica.git] / src / Module / Debug / Babel.php
1 <?php
2
3 namespace Friendica\Module\Debug;
4
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;
11
12 /**
13  * Translates input text into different formats (HTML, BBCode, Markdown)
14  */
15 class Babel extends BaseModule
16 {
17         public static function content()
18         {
19                 function visible_whitespace($s)
20                 {
21                         $s = str_replace(' ', '&nbsp;', $s);
22
23                         return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
24                 }
25
26                 $results = [];
27                 if (!empty($_REQUEST['text'])) {
28                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
29                                 case 'bbcode':
30                                         $bbcode = trim($_REQUEST['text']);
31                                         $results[] = [
32                                                 'title'   => L10n::t('Source input'),
33                                                 'content' => visible_whitespace($bbcode)
34                                         ];
35
36                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
37                                         $results[] = [
38                                                 'title'   => L10n::t('BBCode::toPlaintext'),
39                                                 'content' => visible_whitespace($plain)
40                                         ];
41
42                                         $html = Text\BBCode::convert($bbcode);
43                                         $results[] = [
44                                                 'title'   => L10n::t('BBCode::convert (raw HTML)'),
45                                                 'content' => visible_whitespace(htmlspecialchars($html))
46                                         ];
47
48                                         $results[] = [
49                                                 'title'   => L10n::t('BBCode::convert'),
50                                                 'content' => $html
51                                         ];
52
53                                         $bbcode2 = Text\HTML::toBBCode($html);
54                                         $results[] = [
55                                                 'title'   => L10n::t('BBCode::convert => HTML::toBBCode'),
56                                                 'content' => visible_whitespace($bbcode2)
57                                         ];
58
59                                         $markdown = Text\BBCode::toMarkdown($bbcode);
60                                         $results[] = [
61                                                 'title'   => L10n::t('BBCode::toMarkdown'),
62                                                 'content' => visible_whitespace($markdown)
63                                         ];
64
65                                         $html2 = Text\Markdown::convert($markdown);
66                                         $results[] = [
67                                                 'title'   => L10n::t('BBCode::toMarkdown => Markdown::convert'),
68                                                 'content' => $html2
69                                         ];
70
71                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
72                                         $results[] = [
73                                                 'title'   => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
74                                                 'content' => visible_whitespace($bbcode3)
75                                         ];
76
77                                         $bbcode4 = Text\HTML::toBBCode($html2);
78                                         $results[] = [
79                                                 'title'   => L10n::t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
80                                                 'content' => visible_whitespace($bbcode4)
81                                         ];
82
83                                         $item = [
84                                                 'body' => $bbcode,
85                                                 'tag'  => '',
86                                         ];
87
88                                         Item::setHashtags($item);
89                                         $results[] = [
90                                                 'title'   => L10n::t('Item Body'),
91                                                 'content' => visible_whitespace($item['body'])
92                                         ];
93                                         $results[] = [
94                                                 'title'   => L10n::t('Item Tags'),
95                                                 'content' => $item['tag']
96                                         ];
97                                         break;
98                                 case 'markdown':
99                                         $markdown = trim($_REQUEST['text']);
100                                         $results[] = [
101                                                 'title'   => L10n::t('Source input (Diaspora format)'),
102                                                 'content' => '<pre>' . htmlspecialchars($markdown) . '</pre>'
103                                         ];
104
105                                         $html = Text\Markdown::convert(html_entity_decode($markdown,ENT_COMPAT, 'UTF-8'));
106                                         $results[] = [
107                                                 'title'   => L10n::t('Markdown::convert (raw HTML)'),
108                                                 'content' => visible_whitespace(htmlspecialchars($html))
109                                         ];
110
111                                         $results[] = [
112                                                 'title'   => L10n::t('Markdown::convert'),
113                                                 'content' => $html
114                                         ];
115
116                                         $bbcode = Text\Markdown::toBBCode(XML::unescape($markdown));
117                                         $results[] = [
118                                                 'title'   => L10n::t('Markdown::toBBCode'),
119                                                 'content' => '<pre>' . $bbcode . '</pre>'
120                                         ];
121                                         break;
122                                 case 'html' :
123                                         $html = trim($_REQUEST['text']);
124                                         $results[] = [
125                                                 'title'   => L10n::t('Raw HTML input'),
126                                                 'content' => htmlspecialchars($html)
127                                         ];
128
129                                         $results[] = [
130                                                 'title'   => L10n::t('HTML Input'),
131                                                 'content' => $html
132                                         ];
133
134                                         $bbcode = Text\HTML::toBBCode($html);
135                                         $results[] = [
136                                                 'title'   => L10n::t('HTML::toBBCode'),
137                                                 'content' => visible_whitespace($bbcode)
138                                         ];
139
140                                         $html2 = Text\BBCode::convert($bbcode);
141                                         $results[] = [
142                                                 'title'   => L10n::t('HTML::toBBCode => BBCode::convert'),
143                                                 'content' => $html2
144                                         ];
145
146                                         $results[] = [
147                                                 'title'   => L10n::t('HTML::toBBCode => BBCode::convert (raw HTML)'),
148                                                 'content' => htmlspecialchars($html2)
149                                         ];
150
151                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
152                                         $results[] = [
153                                                 'title'   => L10n::t('HTML::toBBCode => BBCode::toPlaintext'),
154                                                 'content' => '<pre>' . $bbcode2plain . '</pre>'
155                                         ];
156
157                                         $markdown = Text\HTML::toMarkdown($html);
158                                         $results[] = [
159                                                 'title'   => L10n::t('HTML::toMarkdown'),
160                                                 'content' => visible_whitespace($markdown)
161                                         ];
162
163                                         $text = Text\HTML::toPlaintext($html, 0);
164                                         $results[] = [
165                                                 'title'   => L10n::t('HTML::toPlaintext'),
166                                                 'content' => '<pre>' . $text . '</pre>'
167                                         ];
168
169                                         $text = Text\HTML::toPlaintext($html, 0, true);
170                                         $results[] = [
171                                                 'title'   => L10n::t('HTML::toPlaintext (compact)'),
172                                                 'content' => '<pre>' . $text . '</pre>'
173                                         ];
174                         }
175                 }
176
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
184                 ]);
185
186                 return $o;
187         }
188 }