diff options
author | yude <yudesleepy@gmail.com> | 2021-01-04 18:51:10 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-04 18:51:10 +0900 |
commit | e6754f2154a79abd8e5e64bd923f6984aa9ad44b (patch) | |
tree | f074119530bb59ef155938ea367f719f1e4b70f1 /application/bookmark/LinkUtils.php | |
parent | 5256b4287021342a9f8868967b2a77e481314331 (diff) | |
parent | ed4ee8f0297941ac83300389b7de6a293312d20e (diff) | |
download | Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.gz Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.zst Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.zip |
Merge pull request #2 from shaarli/master
Merge fork source
Diffstat (limited to 'application/bookmark/LinkUtils.php')
-rw-r--r-- | application/bookmark/LinkUtils.php | 74 |
1 files changed, 64 insertions, 10 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php index faf5dbfd..0ab2d213 100644 --- a/application/bookmark/LinkUtils.php +++ b/application/bookmark/LinkUtils.php | |||
@@ -67,17 +67,20 @@ function html_extract_tag($tag, $html) | |||
67 | $propertiesKey = ['property', 'name', 'itemprop']; | 67 | $propertiesKey = ['property', 'name', 'itemprop']; |
68 | $properties = implode('|', $propertiesKey); | 68 | $properties = implode('|', $propertiesKey); |
69 | // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"' | 69 | // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"' |
70 | $orCondition = '["\']?(?:og:)?'. $tag .'["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]'; | 70 | $orCondition = '["\']?(?:og:)?' . $tag . '["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]'; |
71 | // Try to retrieve OpenGraph image. | 71 | // Support quotes in double quoted content, and the other way around |
72 | $ogRegex = '#<meta[^>]+(?:'. $properties .')=(?:'. $orCondition .')[^>]*content=["\'](.*?)["\'].*?>#'; | 72 | $content = 'content=(["\'])((?:(?!\1).)*)\1'; |
73 | // Try to retrieve OpenGraph tag. | ||
74 | $ogRegex = '#<meta[^>]+(?:' . $properties . ')=(?:' . $orCondition . ')[^>]*' . $content . '.*?>#'; | ||
73 | // If the attributes are not in the order property => content (e.g. Github) | 75 | // If the attributes are not in the order property => content (e.g. Github) |
74 | // New regex to keep this readable... more or less. | 76 | // New regex to keep this readable... more or less. |
75 | $ogRegexReverse = '#<meta[^>]+content=["\'](.*?)["\'][^>]+(?:'. $properties .')=(?:'. $orCondition .').*?>#'; | 77 | $ogRegexReverse = '#<meta[^>]+' . $content . '[^>]+(?:' . $properties . ')=(?:' . $orCondition . ').*?>#'; |
76 | 78 | ||
77 | if (preg_match($ogRegex, $html, $matches) > 0 | 79 | if ( |
80 | preg_match($ogRegex, $html, $matches) > 0 | ||
78 | || preg_match($ogRegexReverse, $html, $matches) > 0 | 81 | || preg_match($ogRegexReverse, $html, $matches) > 0 |
79 | ) { | 82 | ) { |
80 | return $matches[1]; | 83 | return $matches[2]; |
81 | } | 84 | } |
82 | 85 | ||
83 | return false; | 86 | return false; |
@@ -116,7 +119,7 @@ function hashtag_autolink($description, $indexUrl = '') | |||
116 | * \p{Mn} - any non marking space (accents, umlauts, etc) | 119 | * \p{Mn} - any non marking space (accents, umlauts, etc) |
117 | */ | 120 | */ |
118 | $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; | 121 | $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; |
119 | $replacement = '$1<a href="'. $indexUrl .'./add-tag/$2" title="Hashtag $2">#$2</a>'; | 122 | $replacement = '$1<a href="' . $indexUrl . './add-tag/$2" title="Hashtag $2">#$2</a>'; |
120 | return preg_replace($regex, $replacement, $description); | 123 | return preg_replace($regex, $replacement, $description); |
121 | } | 124 | } |
122 | 125 | ||
@@ -138,12 +141,17 @@ function space2nbsp($text) | |||
138 | * | 141 | * |
139 | * @param string $description shaare's description. | 142 | * @param string $description shaare's description. |
140 | * @param string $indexUrl URL to Shaarli's index. | 143 | * @param string $indexUrl URL to Shaarli's index. |
141 | 144 | * @param bool $autolink Turn on/off automatic linkifications of URLs and hashtags | |
145 | * | ||
142 | * @return string formatted description. | 146 | * @return string formatted description. |
143 | */ | 147 | */ |
144 | function format_description($description, $indexUrl = '') | 148 | function format_description($description, $indexUrl = '', $autolink = true) |
145 | { | 149 | { |
146 | return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl))); | 150 | if ($autolink) { |
151 | $description = hashtag_autolink(text2clickable($description), $indexUrl); | ||
152 | } | ||
153 | |||
154 | return nl2br(space2nbsp($description)); | ||
147 | } | 155 | } |
148 | 156 | ||
149 | /** | 157 | /** |
@@ -171,3 +179,49 @@ function is_note($linkUrl) | |||
171 | { | 179 | { |
172 | return isset($linkUrl[0]) && $linkUrl[0] === '?'; | 180 | return isset($linkUrl[0]) && $linkUrl[0] === '?'; |
173 | } | 181 | } |
182 | |||
183 | /** | ||
184 | * Extract an array of tags from a given tag string, with provided separator. | ||
185 | * | ||
186 | * @param string|null $tags String containing a list of tags separated by $separator. | ||
187 | * @param string $separator Shaarli's default: ' ' (whitespace) | ||
188 | * | ||
189 | * @return array List of tags | ||
190 | */ | ||
191 | function tags_str2array(?string $tags, string $separator): array | ||
192 | { | ||
193 | // For whitespaces, we use the special \s regex character | ||
194 | $separator = $separator === ' ' ? '\s' : $separator; | ||
195 | |||
196 | return preg_split('/\s*' . $separator . '+\s*/', trim($tags) ?? '', -1, PREG_SPLIT_NO_EMPTY); | ||
197 | } | ||
198 | |||
199 | /** | ||
200 | * Return a tag string with provided separator from a list of tags. | ||
201 | * Note that given array is clean up by tags_filter(). | ||
202 | * | ||
203 | * @param array|null $tags List of tags | ||
204 | * @param string $separator | ||
205 | * | ||
206 | * @return string | ||
207 | */ | ||
208 | function tags_array2str(?array $tags, string $separator): string | ||
209 | { | ||
210 | return implode($separator, tags_filter($tags, $separator)); | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * Clean an array of tags: trim + remove empty entries | ||
215 | * | ||
216 | * @param array|null $tags List of tags | ||
217 | * @param string $separator | ||
218 | * | ||
219 | * @return array | ||
220 | */ | ||
221 | function tags_filter(?array $tags, string $separator): array | ||
222 | { | ||
223 | $trimDefault = " \t\n\r\0\x0B"; | ||
224 | return array_values(array_filter(array_map(function (string $entry) use ($separator, $trimDefault): string { | ||
225 | return trim($entry, $trimDefault . $separator); | ||
226 | }, $tags ?? []))); | ||
227 | } | ||