|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Markdown; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\Bundle\MarkdownBundle\Parser\MarkdownParser; |
|
6
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
7
|
|
|
|
|
8
|
|
|
class PointParser extends MarkdownParser |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var RouterInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $router; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $features = array( |
|
19
|
|
|
'header' => true, |
|
20
|
|
|
'list' => true, |
|
21
|
|
|
'horizontal_rule' => true, |
|
22
|
|
|
'table' => false, |
|
23
|
|
|
'foot_note' => true, |
|
24
|
|
|
'fenced_code_block' => true, |
|
25
|
|
|
'abbreviation' => true, |
|
26
|
|
|
'definition_list' => true, |
|
27
|
|
|
'inline_link' => true, // [link text](url "optional title") |
|
|
|
|
|
|
28
|
|
|
'reference_link' => true, // [link text] [id] |
|
|
|
|
|
|
29
|
|
|
'shortcut_link' => true, // [link text] |
|
30
|
|
|
'images' => true, |
|
31
|
|
|
'block_quote' => true, |
|
32
|
|
|
'code_block' => true, |
|
33
|
|
|
'html_block' => false, |
|
34
|
|
|
'auto_link' => true, |
|
35
|
|
|
'auto_mailto' => true, |
|
36
|
|
|
'entities' => true, |
|
37
|
|
|
'no_html' => true, |
|
38
|
|
|
'point_usernames' => true, // @user |
|
39
|
|
|
'point_posts' => true, // #post |
|
40
|
|
|
'point_tags' => false, // @todo implement |
|
41
|
|
|
'point_inline_images' => true, // https://i.skobk.in/i/facepalm.jpg |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
12 |
|
public function __construct(array $features, RouterInterface $router) |
|
45
|
|
|
{ |
|
46
|
12 |
|
$this->router = $router; |
|
47
|
|
|
|
|
48
|
12 |
|
parent::__construct($features); |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Point.im breaks Markdown rule about quotation on next line |
|
53
|
|
|
* |
|
54
|
|
|
* @param $text |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
1 |
|
protected function doBlockQuotes($text) { |
|
59
|
1 |
|
$text = preg_replace_callback('/ |
|
60
|
|
|
( # Wrap whole match in $1 |
|
61
|
|
|
(?> |
|
62
|
|
|
^[ ]*>[ ]? # ">" at the start of a line |
|
63
|
|
|
.+\n # rest of the first line |
|
64
|
|
|
\n* # blanks |
|
65
|
|
|
)+ |
|
66
|
|
|
) |
|
67
|
1 |
|
/xm', |
|
68
|
1 |
|
array($this, '_doBlockQuotes_callback'), $text |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
1 |
|
return $text; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Point.im breaks Markdown double line wrap rule |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
1 |
|
protected function formParagraphs($text, $wrap_in_p = true) { |
|
79
|
|
|
// Strip leading and trailing lines: |
|
80
|
1 |
|
$text = preg_replace('/\A\n+|\n+\z/', '', $text); |
|
81
|
|
|
|
|
82
|
1 |
|
$grafs = preg_split('/\n+/', $text, -1, PREG_SPLIT_NO_EMPTY); |
|
83
|
|
|
|
|
84
|
|
|
// Wrap <p> tags and unhashify HTML blocks |
|
85
|
1 |
|
foreach ($grafs as $key => $value) { |
|
86
|
1 |
|
if (!preg_match('/^B\x1A[0-9]+B$/', $value)) { |
|
87
|
|
|
// Is a paragraph. |
|
88
|
1 |
|
$value = $this->runSpanGamut($value); |
|
89
|
1 |
|
if ($wrap_in_p) { |
|
90
|
1 |
|
$value = preg_replace('/^([ ]*)/', "<p>", $value); |
|
91
|
1 |
|
$value .= "</p>"; |
|
92
|
|
|
} |
|
93
|
1 |
|
$grafs[$key] = $this->unhash($value); |
|
94
|
|
|
} else { |
|
95
|
|
|
// Is a block. |
|
96
|
|
|
// Modify elements of @grafs in-place... |
|
97
|
|
|
$graf = $value; |
|
98
|
|
|
$block = $this->html_hashes[$graf]; |
|
99
|
|
|
$graf = $block; |
|
100
|
1 |
|
$grafs[$key] = $graf; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return implode("\n\n", $grafs); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $text |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function doAutoLinks($text) |
|
113
|
|
|
{ |
|
114
|
1 |
|
if ($this->features['point_inline_images']) { |
|
115
|
1 |
|
return preg_replace_callback('{((https?):\/\/[^\'">\s]+\.(jpg|jpeg|png|gif))}i', array(&$this, 'doAutoLinksInlineImageCallback'), $text); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return parent::doAutoLinks($text); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
public function doAnchors($text) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$text = parent::doAnchors($text); |
|
124
|
|
|
|
|
125
|
|
|
# |
|
126
|
|
|
# Turn Markdown link shortcuts into XHTML <a> tags. |
|
127
|
|
|
# |
|
128
|
1 |
|
if ($this->in_anchor) { |
|
129
|
|
|
return $text; |
|
130
|
|
|
} |
|
131
|
1 |
|
$this->in_anchor = true; |
|
132
|
|
|
|
|
133
|
|
|
# |
|
134
|
|
|
# Handling username links: @some-point-username |
|
135
|
|
|
# |
|
136
|
1 |
|
if ($this->features['point_usernames']) { |
|
137
|
1 |
|
$text = preg_replace_callback('{ |
|
138
|
|
|
( # wrap whole match in $1 |
|
139
|
|
|
@ |
|
140
|
|
|
( |
|
141
|
|
|
[a-zA-Z0-9\-]+? # username = $2 |
|
142
|
|
|
) |
|
143
|
|
|
) |
|
144
|
|
|
([^a-zA-Z0-9\-]|$) # Tail of username |
|
145
|
1 |
|
}xs', |
|
146
|
1 |
|
array(&$this, 'doAnchorsPointUsernameCallback'), $text); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
# |
|
150
|
|
|
# Handling post links: #somepost |
|
151
|
|
|
# |
|
152
|
1 |
|
if ($this->features['point_posts']) { |
|
153
|
1 |
|
$text = preg_replace_callback('{ |
|
154
|
|
|
( # wrap whole match in $1 |
|
155
|
|
|
\# |
|
156
|
|
|
( |
|
157
|
|
|
[a-zA-Z]+? # post id = $2 |
|
158
|
|
|
) |
|
159
|
|
|
) |
|
160
|
|
|
([^a-zA-Z]|$) # Tail of post |
|
161
|
1 |
|
}xs', |
|
162
|
1 |
|
array(&$this, 'doAnchorsPointPostCallback'), $text); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
$this->in_anchor = false; |
|
166
|
|
|
|
|
167
|
1 |
|
return $text; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function doAutoLinksInlineImageCallback($matches) |
|
171
|
|
|
{ |
|
172
|
|
|
$url = trim($matches[1]); |
|
173
|
|
|
$ext = $matches[2]; |
|
174
|
|
|
|
|
175
|
|
|
return $this->hashPart('<a href="'.$url.'" class="post-image ' |
|
176
|
|
|
.('gif' === $ext ? ' img-gif':'').'"><img src="'.$url.'" class="img-thumbnail" alt="Inline image"></a>'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
View Code Duplication |
protected function doAnchorsPointUsernameCallback($matches) |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
//$wholeMatch = $matches[1]; |
|
|
|
|
|
|
182
|
|
|
$username = $matches[2]; |
|
183
|
|
|
$href = $this->router->generate('user_show', ['login' => $username]); |
|
184
|
|
|
$tail = htmlspecialchars($matches[3]); |
|
185
|
|
|
|
|
186
|
|
|
return $this->hashPart('<a href="'.$href.'" class="user">@'.$username.'</a>'.$tail); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
View Code Duplication |
protected function doAnchorsPointPostCallback($matches) |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
//$wholeMatch = $matches[1]; |
|
|
|
|
|
|
192
|
|
|
$postId = $matches[2]; |
|
193
|
|
|
$href = $this->router->generate('post_show', ['id' => $postId]); |
|
194
|
|
|
$tail = htmlspecialchars($matches[3]); |
|
195
|
|
|
|
|
196
|
|
|
return $this->hashPart( |
|
197
|
|
|
'<a href="'.$href.'" class="post">#'.$postId.'</a>' |
|
198
|
|
|
//.'<a href="https://point.im/'.$postId.'"><span class="glyphicon glyphicon-link"></span></a>' |
|
|
|
|
|
|
199
|
|
|
.$tail |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.