71d1a7b849c92dd7aab25bf8e7de105ea42ba1e2
[friendica.git/.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\Model\Contact;
11 use Michelf\MarkdownExtra;
12 use Friendica\Content\Text\HTML;
13
14 /**
15  * Friendica-specific usage of Markdown
16  *
17  * @author Hypolite Petovan <mrpetovan@gmail.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          */
30         public static function convert($text, $hardwrap = true) {
31                 $stamp1 = microtime(true);
32
33                 $MarkdownParser = new MarkdownExtra();
34                 $MarkdownParser->hard_wrap = $hardwrap;
35                 $html = $MarkdownParser->transform($text);
36
37                 self::getApp()->save_timestamp($stamp1, "parser");
38
39                 return $html;
40         }
41
42         /**
43          * @brief Callback function to replace a Diaspora style mention in a mention for Friendica
44          *
45          * @param array $match Matching values for the callback
46          * @return string Replaced mention
47          */
48         private static function diasporaMention2BBCodeCallback($match)
49         {
50                 if ($match[2] == '') {
51                         return;
52                 }
53
54                 $data = Contact::getDetailsByAddr($match[2]);
55
56                 if (empty($data)) {
57                         return;
58                 }
59
60                 $name = $match[1];
61
62                 if ($name == '') {
63                         $name = $data['name'];
64                 }
65
66                 return '@[url=' . $data['url'] . ']' . $name . '[/url]';
67         }
68
69         /*
70          * we don't want to support a bbcode specific markdown interpreter
71          * and the markdown library we have is pretty good, but provides HTML output.
72          * So we'll use that to convert to HTML, then convert the HTML back to bbcode,
73          * and then clean up a few Diaspora specific constructs.
74          */
75         public static function toBBCode($s)
76         {
77                 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
78
79                 // Handles single newlines
80                 $s = str_replace("\r\n", "\n", $s);
81                 $s = str_replace("\n", " \n", $s);
82                 $s = str_replace("\r", " \n", $s);
83
84                 // Replace lonely stars in lines not starting with it with literal stars
85                 $s = preg_replace('/^([^\*]+)\*([^\*]*)$/im', '$1\*$2', $s);
86
87                 // The parser cannot handle paragraphs correctly
88                 $s = str_replace(['</p>', '<p>', '<p dir="ltr">'], ['<br>', '<br>', '<br>'], $s);
89
90                 // Escaping the hash tags
91                 $s = preg_replace('/\#([^\s\#])/', '&#35;$1', $s);
92
93                 $s = self::convert($s);
94
95                 $regexp = "/@\{(?:([^\}]+?); )?([^\} ]+)\}/";
96                 $s = preg_replace_callback($regexp, ['self', 'diasporaMention2BBCodeCallback'], $s);
97
98                 $s = str_replace('&#35;', '#', $s);
99
100                 $s = HTML::toBBCode($s);
101
102                 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
103                 $s = str_replace('&#x2672;', html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8'), $s);
104
105                 // Convert everything that looks like a link to a link
106                 $s = preg_replace('/([^\]=]|^)(https?\:\/\/)([a-zA-Z0-9:\/\-?&;.=_~#%$!+,@]+(?<!,))/ism', '$1[url=$2$3]$2$3[/url]', $s);
107
108                 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
109                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
110                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism'   , '[youtube]$1[/youtube]', 'url', $s);
111                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism'        , '[vimeo]$2[/vimeo]'    , 'url', $s);
112                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism'              , '[vimeo]$1[/vimeo]'    , 'url', $s);
113
114                 // remove duplicate adjacent code tags
115                 $s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
116
117                 // Don't show link to full picture (until it is fixed)
118                 $s = BBCode::scaleExternalImages($s, false);
119
120                 return $s;
121         }
122 }