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