]> git.mxchange.org Git - friendica.git/blob - mod/babel.php
Merge pull request #6595 from annando/notice
[friendica.git] / mod / babel.php
1 <?php
2 /**
3  * @file mod/babel.php
4  */
5
6 use Friendica\Content\Text;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Renderer;
9
10 function visible_whitespace($s)
11 {
12         $s = str_replace(' ', '&nbsp;', $s);
13
14         return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
15 }
16
17 function babel_content()
18 {
19         $results = [];
20         if (!empty($_REQUEST['text'])) {
21                 switch (defaults($_REQUEST, 'type', 'bbcode')) {
22                         case 'bbcode':
23                                 $bbcode = trim($_REQUEST['text']);
24                                 $results[] = [
25                                         'title' => L10n::t('Source input'),
26                                         'content' => visible_whitespace($bbcode)
27                                 ];
28
29                                 $plain = Text\BBCode::toPlaintext($bbcode, false);
30                                 $results[] = [
31                                         'title' => L10n::t('BBCode::toPlaintext'),
32                                         'content' => visible_whitespace($plain)
33                                 ];
34
35                                 $html = Text\BBCode::convert($bbcode);
36                                 $results[] = [
37                                         'title' => L10n::t('BBCode::convert (raw HTML)'),
38                                         'content' => visible_whitespace(htmlspecialchars($html))
39                                 ];
40
41                                 $results[] = [
42                                         'title' => L10n::t('BBCode::convert'),
43                                         'content' => $html
44                                 ];
45
46                                 $bbcode2 = Text\HTML::toBBCode($html);
47                                 $results[] = [
48                                         'title' => L10n::t('BBCode::convert => HTML::toBBCode'),
49                                         'content' => visible_whitespace($bbcode2)
50                                 ];
51
52                                 $markdown = Text\BBCode::toMarkdown($bbcode);
53                                 $results[] = [
54                                         'title' => L10n::t('BBCode::toMarkdown'),
55                                         'content' => visible_whitespace($markdown)
56                                 ];
57
58                                 $html2 = Text\Markdown::convert($markdown);
59                                 $results[] = [
60                                         'title' => L10n::t('BBCode::toMarkdown => Markdown::convert'),
61                                         'content' => $html2
62                                 ];
63
64                                 $bbcode3 = Text\Markdown::toBBCode($markdown);
65                                 $results[] = [
66                                         'title' => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
67                                         'content' => visible_whitespace($bbcode3)
68                                 ];
69
70                                 $bbcode4 = Text\HTML::toBBCode($html2);
71                                 $results[] = [
72                                         'title' => L10n::t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
73                                         'content' => visible_whitespace($bbcode4)
74                                 ];
75
76                                 $item = [
77                                         'body' => $bbcode,
78                                         'tag' => '',
79                                 ];
80
81                                 \Friendica\Model\Item::setHashtags($item);
82                                 $results[] = [
83                                         'title' => L10n::t('Item Body'),
84                                         'content' => visible_whitespace($item['body'])
85                                 ];
86                                 $results[] = [
87                                         'title' => L10n::t('Item Tags'),
88                                         'content' => $item['tag']
89                                 ];
90                                 break;
91                         case 'markdown':
92                                 $markdown = trim($_REQUEST['text']);
93                                 $results[] = [
94                                         'title' => L10n::t('Source input (Diaspora format)'),
95                                         'content' => '<pre>' . $markdown . '</pre>'
96                                 ];
97
98                                 $html = Text\Markdown::convert($markdown);
99                                 $results[] = [
100                                         'title' => L10n::t('Markdown::convert (raw HTML)'),
101                                         'content' => visible_whitespace(htmlspecialchars($html))
102                                 ];
103
104                                 $results[] = [
105                                         'title' => L10n::t('Markdown::convert'),
106                                         'content' => $html
107                                 ];
108
109                                 $bbcode = Text\Markdown::toBBCode($markdown);
110                                 $results[] = [
111                                         'title' => L10n::t('Markdown::toBBCode'),
112                                         'content' => '<pre>' . $bbcode . '</pre>'
113                                 ];
114                                 break;
115                         case 'html' :
116                                 $html = trim($_REQUEST['text']);
117                                 $results[] = [
118                                         'title' => L10n::t('Raw HTML input'),
119                                         'content' => htmlspecialchars($html)
120                                 ];
121
122                                 $results[] = [
123                                         'title' => L10n::t('HTML Input'),
124                                         'content' => $html
125                                 ];
126
127                                 $bbcode = Text\HTML::toBBCode($html);
128                                 $results[] = [
129                                         'title' => L10n::t('HTML::toBBCode'),
130                                         'content' => visible_whitespace($bbcode)
131                                 ];
132
133                                 $html2 = Text\BBCode::convert($bbcode);
134                                 $results[] = [
135                                         'title' => L10n::t('HTML::toBBCode => BBCode::convert'),
136                                         'content' => $html2
137                                 ];
138
139                                 $results[] = [
140                                         'title' => L10n::t('HTML::toBBCode => BBCode::convert (raw HTML)'),
141                                         'content' => htmlspecialchars($html2)
142                                 ];
143
144                                 $markdown = Text\HTML::toMarkdown($html);
145                                 $results[] = [
146                                         'title' => L10n::t('HTML::toMarkdown'),
147                                         'content' => visible_whitespace($markdown)
148                                 ];
149
150                                 $text = Text\HTML::toPlaintext($html);
151                                 $results[] = [
152                                         'title' => L10n::t('HTML::toPlaintext'),
153                                         'content' => '<pre>' . $text . '</pre>'
154                                 ];
155                 }
156         }
157
158         $tpl = Renderer::getMarkupTemplate('babel.tpl');
159         $o = Renderer::replaceMacros($tpl, [
160                 '$text'          => ['text', L10n::t('Source text'), defaults($_REQUEST, 'text', ''), ''],
161                 '$type_bbcode'   => ['type', L10n::t('BBCode'), 'bbcode', '', defaults($_REQUEST, 'type', 'bbcode') == 'bbcode'],
162                 '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', defaults($_REQUEST, 'type', 'bbcode') == 'markdown'],
163                 '$type_html'     => ['type', L10n::t('HTML'), 'html', '', defaults($_REQUEST, 'type', 'bbcode') == 'html'],
164                 '$results'       => $results
165         ]);
166
167         return $o;
168 }