]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Markdown.php
Remove deprecated code
[friendica.git] / src / Content / Text / Markdown.php
1 <?php
2
3 /**
4  * @file src/Content/Text/Markdown.php
5  */
6
7 namespace Friendica\Content\Text;
8
9 use Friendica\Core\System;
10 use Friendica\DI;
11 use Friendica\Model\Contact;
12
13 /**
14  * Friendica-specific usage of Markdown
15  *
16  * @author Hypolite Petovan <hypolite@mrpetovan.com>
17  */
18 class Markdown
19 {
20         /**
21          * Converts a Markdown string into HTML. The hardwrap parameter maximizes
22          * compatibility with Diaspora in spite of the Markdown standard.
23          *
24          * @param string $text
25          * @param bool   $hardwrap
26          * @return string
27          * @throws \Exception
28          */
29         public static function convert($text, $hardwrap = true) {
30                 $stamp1 = microtime(true);
31
32                 $MarkdownParser = new MarkdownParser();
33                 $MarkdownParser->code_class_prefix  = 'language-';
34                 $MarkdownParser->hard_wrap          = $hardwrap;
35                 $MarkdownParser->hashtag_protection = true;
36                 $MarkdownParser->url_filter_func    = function ($url) {
37                         if (strpos($url, '#') === 0) {
38                                 $url = ltrim($_SERVER['REQUEST_URI'], '/') . $url;
39                         }
40                         return  $url;
41                 };
42
43                 $html = $MarkdownParser->transform($text);
44
45                 DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack());
46
47                 return $html;
48         }
49
50         /**
51          * Callback function to replace a Diaspora style mention in a mention for Friendica
52          *
53          * @param array $match Matching values for the callback
54          *                     [1] = mention type (@ or !)
55          *                     [2] = name (optional)
56          *                     [3] = address
57          * @return string Replaced mention
58          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
59          * @throws \ImagickException
60          */
61         private static function diasporaMention2BBCodeCallback($match)
62         {
63                 if ($match[3] == '') {
64                         return;
65                 }
66
67                 $data = Contact::getDetailsByAddr($match[3]);
68
69                 if (empty($data)) {
70                         return;
71                 }
72
73                 $name = $match[2];
74
75                 if ($name == '') {
76                         $name = $data['name'];
77                 }
78
79                 return $match[1] . '[url=' . $data['url'] . ']' . $name . '[/url]';
80         }
81
82         /*
83          * we don't want to support a bbcode specific markdown interpreter
84          * and the markdown library we have is pretty good, but provides HTML output.
85          * So we'll use that to convert to HTML, then convert the HTML back to bbcode,
86          * and then clean up a few Diaspora specific constructs.
87          */
88         public static function toBBCode($s)
89         {
90                 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
91
92                 // The parser cannot handle paragraphs correctly
93                 $s = str_replace(['</p>', '<p>', '<p dir="ltr">'], ['<br>', '<br>', '<br>'], $s);
94
95                 // Escaping hashtags that could be titles
96                 $s = preg_replace('/^\#([^\s\#])/im', '\#$1', $s);
97
98                 $s = self::convert($s);
99
100                 $regexp = "/([@!])\{(?:([^\}]+?); ?)?([^\} ]+)\}/";
101                 $s = preg_replace_callback($regexp, ['self', 'diasporaMention2BBCodeCallback'], $s);
102
103                 $s = HTML::toBBCode($s);
104
105                 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
106                 $s = str_replace('&#x2672;', html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8'), $s);
107
108                 // Convert everything that looks like a link to a link
109                 $s = preg_replace('/([^\]=]|^)(https?\:\/\/)([a-zA-Z0-9:\/\-?&;.=_~#%$!+,@]+(?<!,))/ism', '$1[url=$2$3]$2$3[/url]', $s);
110
111                 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
112                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
113                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism'   , '[youtube]$1[/youtube]', 'url', $s);
114                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism'        , '[vimeo]$2[/vimeo]'    , 'url', $s);
115                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism'              , '[vimeo]$1[/vimeo]'    , 'url', $s);
116
117                 // remove duplicate adjacent code tags
118                 $s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
119
120                 // Don't show link to full picture (until it is fixed)
121                 $s = BBCode::scaleExternalImages($s);
122
123                 return $s;
124         }
125 }