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