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