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