diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-05-10 23:18:04 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-06-06 21:04:43 +0200 |
commit | 9ccca40189652e529732683abcdf54fcf775c9ec (patch) | |
tree | 9deda85d287dcba664bbba2f0bf9228e6118fbad /application/LinkFilter.php | |
parent | bb9ca54838e2f877635197541e8439171c83d5dc (diff) | |
download | Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.tar.gz Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.tar.zst Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.zip |
Hashtag system
* Hashtag are auto-linked with a filter search
* Supports unicode
* Compatible with markdown (excluded in code blocks)
Diffstat (limited to 'application/LinkFilter.php')
-rw-r--r-- | application/LinkFilter.php | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/application/LinkFilter.php b/application/LinkFilter.php index e693b284..d4fe28df 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php | |||
@@ -28,6 +28,11 @@ class LinkFilter | |||
28 | public static $FILTER_DAY = 'FILTER_DAY'; | 28 | public static $FILTER_DAY = 'FILTER_DAY'; |
29 | 29 | ||
30 | /** | 30 | /** |
31 | * @var string Allowed characters for hashtags (regex syntax). | ||
32 | */ | ||
33 | public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}'; | ||
34 | |||
35 | /** | ||
31 | * @var array all available links. | 36 | * @var array all available links. |
32 | */ | 37 | */ |
33 | private $links; | 38 | private $links; |
@@ -263,8 +268,10 @@ class LinkFilter | |||
263 | for ($i = 0 ; $i < count($searchtags) && $found; $i++) { | 268 | for ($i = 0 ; $i < count($searchtags) && $found; $i++) { |
264 | // Exclusive search, quit if tag found. | 269 | // Exclusive search, quit if tag found. |
265 | // Or, tag not found in the link, quit. | 270 | // Or, tag not found in the link, quit. |
266 | if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags)) | 271 | if (($searchtags[$i][0] == '-' |
267 | || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags) | 272 | && $this->searchTagAndHashTag(substr($searchtags[$i], 1), $linktags, $link['description'])) |
273 | || ($searchtags[$i][0] != '-') | ||
274 | && ! $this->searchTagAndHashTag($searchtags[$i], $linktags, $link['description']) | ||
268 | ) { | 275 | ) { |
269 | $found = false; | 276 | $found = false; |
270 | } | 277 | } |
@@ -307,6 +314,28 @@ class LinkFilter | |||
307 | } | 314 | } |
308 | 315 | ||
309 | /** | 316 | /** |
317 | * Check if a tag is found in the taglist, or as an hashtag in the link description. | ||
318 | * | ||
319 | * @param string $tag Tag to search. | ||
320 | * @param array $taglist List of tags for the current link. | ||
321 | * @param string $description Link description. | ||
322 | * | ||
323 | * @return bool True if found, false otherwise. | ||
324 | */ | ||
325 | protected function searchTagAndHashTag($tag, $taglist, $description) | ||
326 | { | ||
327 | if (in_array($tag, $taglist)) { | ||
328 | return true; | ||
329 | } | ||
330 | |||
331 | if (preg_match('/(^| )#'. $tag .'([^'. self::$HASHTAG_CHARS .']|$)/mui', $description) > 0) { | ||
332 | return true; | ||
333 | } | ||
334 | |||
335 | return false; | ||
336 | } | ||
337 | |||
338 | /** | ||
310 | * Convert a list of tags (str) to an array. Also | 339 | * Convert a list of tags (str) to an array. Also |
311 | * - handle case sensitivity. | 340 | * - handle case sensitivity. |
312 | * - accepts spaces commas as separator. | 341 | * - accepts spaces commas as separator. |