aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/LinkUtils.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-05-10 23:18:04 +0200
committerArthurHoaro <arthur@hoa.ro>2016-06-06 21:04:43 +0200
commit9ccca40189652e529732683abcdf54fcf775c9ec (patch)
tree9deda85d287dcba664bbba2f0bf9228e6118fbad /application/LinkUtils.php
parentbb9ca54838e2f877635197541e8439171c83d5dc (diff)
downloadShaarli-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/LinkUtils.php')
-rw-r--r--application/LinkUtils.php75
1 files changed, 75 insertions, 0 deletions
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)
91 foreach ($links as $link) { 91 foreach ($links as $link) {
92 $cpt = $link['private'] == true ? $cpt + 1 : $cpt; 92 $cpt = $link['private'] == true ? $cpt + 1 : $cpt;
93 } 93 }
94
94 return $cpt; 95 return $cpt;
95} 96}
97
98/**
99 * In a string, converts URLs to clickable links.
100 *
101 * @param string $text input string.
102 * @param string $redirector if a redirector is set, use it to gerenate links.
103 *
104 * @return string returns $text with all links converted to HTML links.
105 *
106 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
107 */
108function text2clickable($text, $redirector = '')
109{
110 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si';
111
112 if (empty($redirector)) {
113 return preg_replace($regex, '<a href="$1">$1</a>', $text);
114 }
115 // Redirector is set, urlencode the final URL.
116 return preg_replace_callback(
117 $regex,
118 function ($matches) use ($redirector) {
119 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>';
120 },
121 $text
122 );
123}
124
125/**
126 * Auto-link hashtags.
127 *
128 * @param string $description Given description.
129 * @param string $indexUrl Root URL.
130 *
131 * @return string Description with auto-linked hashtags.
132 */
133function hashtag_autolink($description, $indexUrl = '')
134{
135 /*
136 * To support unicode: http://stackoverflow.com/a/35498078/1484919
137 * \p{Pc} - to match underscore
138 * \p{N} - numeric character in any script
139 * \p{L} - letter from any language
140 * \p{Mn} - any non marking space (accents, umlauts, etc)
141 */
142 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
143 $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>';
144 return preg_replace($regex, $replacement, $description);
145}
146
147/**
148 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
149 * even in the absence of <pre> (This is used in description to keep text formatting).
150 *
151 * @param string $text input text.
152 *
153 * @return string formatted text.
154 */
155function space2nbsp($text)
156{
157 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
158}
159
160/**
161 * Format Shaarli's description
162 *
163 * @param string $description shaare's description.
164 * @param string $redirector if a redirector is set, use it to gerenate links.
165 *
166 * @return string formatted description.
167 */
168function format_description($description, $redirector = '', $indexUrl = '') {
169 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl)));
170}