From 9ccca40189652e529732683abcdf54fcf775c9ec Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 10 May 2016 23:18:04 +0200 Subject: Hashtag system * Hashtag are auto-linked with a filter search * Supports unicode * Compatible with markdown (excluded in code blocks) --- application/LinkUtils.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'application/LinkUtils.php') diff --git a/application/LinkUtils.php b/application/LinkUtils.php index da04ca97..eeca631f 100644 --- a/application/LinkUtils.php +++ b/application/LinkUtils.php @@ -91,5 +91,80 @@ function count_private($links) foreach ($links as $link) { $cpt = $link['private'] == true ? $cpt + 1 : $cpt; } + return $cpt; } + +/** + * In a string, converts URLs to clickable links. + * + * @param string $text input string. + * @param string $redirector if a redirector is set, use it to gerenate links. + * + * @return string returns $text with all links converted to HTML links. + * + * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 + */ +function text2clickable($text, $redirector = '') +{ + $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si'; + + if (empty($redirector)) { + return preg_replace($regex, '$1', $text); + } + // Redirector is set, urlencode the final URL. + return preg_replace_callback( + $regex, + function ($matches) use ($redirector) { + return ''. $matches[1] .''; + }, + $text + ); +} + +/** + * Auto-link hashtags. + * + * @param string $description Given description. + * @param string $indexUrl Root URL. + * + * @return string Description with auto-linked hashtags. + */ +function hashtag_autolink($description, $indexUrl = '') +{ + /* + * To support unicode: http://stackoverflow.com/a/35498078/1484919 + * \p{Pc} - to match underscore + * \p{N} - numeric character in any script + * \p{L} - letter from any language + * \p{Mn} - any non marking space (accents, umlauts, etc) + */ + $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; + $replacement = '$1#$2'; + return preg_replace($regex, $replacement, $description); +} + +/** + * This function inserts   where relevant so that multiple spaces are properly displayed in HTML + * even in the absence of
  (This is used in description to keep text formatting).
+ *
+ * @param string $text input text.
+ *
+ * @return string formatted text.
+ */
+function space2nbsp($text)
+{
+    return preg_replace('/(^| ) /m', '$1 ', $text);
+}
+
+/**
+ * Format Shaarli's description
+ *
+ * @param string $description shaare's description.
+ * @param string $redirector  if a redirector is set, use it to gerenate links.
+ *
+ * @return string formatted description.
+ */
+function format_description($description, $redirector = '', $indexUrl = '') {
+    return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl)));
+}
-- 
cgit v1.2.3


From 7af9a41881ed0b9d44d18a0ce03a123a8441adf5 Mon Sep 17 00:00:00 2001
From: ArthurHoaro 
Date: Thu, 20 Oct 2016 11:31:52 +0200
Subject: Minor code cleanup: PHPDoc, spelling, unused variables, etc.

---
 application/LinkUtils.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

(limited to 'application/LinkUtils.php')

diff --git a/application/LinkUtils.php b/application/LinkUtils.php
index eeca631f..9d9ae3cb 100644
--- a/application/LinkUtils.php
+++ b/application/LinkUtils.php
@@ -81,7 +81,7 @@ function html_extract_charset($html)
 /**
  * Count private links in given linklist.
  *
- * @param array $links Linklist.
+ * @param array|Countable $links Linklist.
  *
  * @return int Number of private links.
  */
@@ -162,6 +162,7 @@ function space2nbsp($text)
  *
  * @param string $description shaare's description.
  * @param string $redirector  if a redirector is set, use it to gerenate links.
+ * @param string $indexUrl    URL to Shaarli's index.
  *
  * @return string formatted description.
  */
-- 
cgit v1.2.3


From d592daea8343bb4dfecff5d97e93699581ccc58c Mon Sep 17 00:00:00 2001
From: ArthurHoaro 
Date: Mon, 28 Nov 2016 18:24:15 +0100
Subject: Add a persistent 'shorturl' key to all links

All existing link will keep their permalinks.
New links will have smallhash generated with date+id.

The purpose of this is to avoid collision between links due to their creation date.
---
 application/LinkUtils.php | 13 +++++++++++++
 1 file changed, 13 insertions(+)

(limited to 'application/LinkUtils.php')

diff --git a/application/LinkUtils.php b/application/LinkUtils.php
index 9d9ae3cb..cf58f808 100644
--- a/application/LinkUtils.php
+++ b/application/LinkUtils.php
@@ -169,3 +169,16 @@ function space2nbsp($text)
 function format_description($description, $redirector = '', $indexUrl = '') {
     return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl)));
 }
+
+/**
+ * Generate a small hash for a link.
+ *
+ * @param DateTime $date Link creation date.
+ * @param int      $id   Link ID.
+ *
+ * @return string the small hash generated from link data.
+ */
+function link_small_hash($date, $id)
+{
+    return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id);
+}
-- 
cgit v1.2.3