]> git.mxchange.org Git - friendica.git/blob - mod/babel.php
Merge pull request #5762 from JonnyTischbein/2018.08-rc
[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
9 function visible_whitespace($s)
10 {
11         $s = str_replace(' ', '&nbsp;', $s);
12
13         return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
14 }
15
16 function babel_content()
17 {
18         $results = [];
19         if (!empty($_REQUEST['text'])) {
20                 switch (defaults($_REQUEST, 'type', 'bbcode')) {
21                         case 'bbcode':
22                                 $bbcode = trim($_REQUEST['text']);
23                                 $results[] = [
24                                         'title' => L10n::t('Source input'),
25                                         'content' => visible_whitespace($bbcode)
26                                 ];
27
28                                 $plain = Text\BBCode::toPlaintext($bbcode, false);
29                                 $results[] = [
30                                         'title' => L10n::t('BBCode::toPlaintext'),
31                                         'content' => visible_whitespace($plain)
32                                 ];
33
34                                 $html = Text\BBCode::convert($bbcode);
35                                 $results[] = [
36                                         'title' => L10n::t('BBCode::convert (raw HTML)'),
37                                         'content' => visible_whitespace(htmlspecialchars($html))
38                                 ];
39
40                                 $results[] = [
41                                         'title' => L10n::t('BBCode::convert'),
42                                         'content' => $html
43                                 ];
44
45                                 $bbcode2 = Text\HTML::toBBCode($html);
46                                 $results[] = [
47                                         'title' => L10n::t('BBCode::convert => HTML::toBBCode'),
48                                         'content' => visible_whitespace($bbcode2)
49                                 ];
50
51                                 $markdown = Text\BBCode::toMarkdown($bbcode);
52                                 $results[] = [
53                                         'title' => L10n::t('BBCode::toMarkdown'),
54                                         'content' => visible_whitespace($markdown)
55                                 ];
56
57                                 $html2 = Text\Markdown::convert($markdown);
58                                 $results[] = [
59                                         'title' => L10n::t('BBCode::toMarkdown => Markdown::convert'),
60                                         'content' => $html2
61                                 ];
62
63                                 $bbcode3 = Text\Markdown::toBBCode($markdown);
64                                 $results[] = [
65                                         'title' => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
66                                         'content' => visible_whitespace($bbcode3)
67                                 ];
68
69                                 $bbcode4 = Text\HTML::toBBCode($html2);
70                                 $results[] = [
71                                         'title' => L10n::t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
72                                         'content' => visible_whitespace($bbcode4)
73                                 ];
74                                 break;
75                         case 'markdown':
76                                 $markdown = trim($_REQUEST['text']);
77                                 $results[] = [
78                                         'title' => L10n::t('Source input (Diaspora format)'),
79                                         'content' => '<pre>' . $markdown . '</pre>'
80                                 ];
81
82                                 $html = Text\Markdown::convert($markdown);
83                                 $results[] = [
84                                         'title' => L10n::t('Markdown::convert (raw HTML)'),
85                                         'content' => htmlspecialchars($html)
86                                 ];
87
88                                 $results[] = [
89                                         'title' => L10n::t('Markdown::convert'),
90                                         'content' => $html
91                                 ];
92
93                                 $bbcode = Text\Markdown::toBBCode($markdown);
94                                 $results[] = [
95                                         'title' => L10n::t('Markdown::toBBCode'),
96                                         'content' => '<pre>' . $bbcode . '</pre>'
97                                 ];
98                                 break;
99                         case 'html' :
100                                 $html = trim($_REQUEST['text']);
101                                 $results[] = [
102                                         'title' => L10n::t('Raw HTML input'),
103                                         'content' => htmlspecialchars($html)
104                                 ];
105
106                                 $results[] = [
107                                         'title' => L10n::t('HTML Input'),
108                                         'content' => $html
109                                 ];
110
111                                 $bbcode = Text\HTML::toBBCode($html);
112                                 $results[] = [
113                                         'title' => L10n::t('HTML::toBBCode'),
114                                         'content' => visible_whitespace($bbcode)
115                                 ];
116
117                                 $markdown = Text\HTML::toMarkdown($html);
118                                 $results[] = [
119                                         'title' => L10n::t('HTML::toMarkdown'),
120                                         'content' => visible_whitespace($markdown)
121                                 ];
122
123                                 $text = Text\HTML::toPlaintext($html);
124                                 $results[] = [
125                                         'title' => L10n::t('HTML::toPlaintext'),
126                                         'content' => '<pre>' . $text . '</pre>'
127                                 ];
128                 }
129         }
130
131         $tpl = get_markup_template('babel.tpl');
132         $o = replace_macros($tpl, [
133                 '$text'          => ['text', L10n::t('Source text'), htmlentities(defaults($_REQUEST, 'text', '')), ''],
134                 '$type_bbcode'   => ['type', L10n::t('BBCode'), 'bbcode', '', defaults($_REQUEST, 'type', 'bbcode') == 'bbcode'],
135                 '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', defaults($_REQUEST, 'type', 'bbcode') == 'markdown'],
136                 '$type_html'     => ['type', L10n::t('HTML'), 'html', '', defaults($_REQUEST, 'type', 'bbcode') == 'html'],
137                 '$results'       => $results
138         ]);
139
140         return $o;
141 }