]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/formatter/BookmarkDefaultFormatter.php
d58a5e39dde46ca5f5f0c71ac80e1f60fde8e55b
3 namespace Shaarli\Formatter
;
6 * Class BookmarkDefaultFormatter
8 * Default bookmark formatter.
9 * Escape values for HTML display and automatically add link to URL and hashtags.
11 * @package Shaarli\Formatter
13 class BookmarkDefaultFormatter
extends BookmarkFormatter
15 const SEARCH_HIGHLIGHT_OPEN
= '|@@HIGHLIGHT';
16 const SEARCH_HIGHLIGHT_CLOSE
= 'HIGHLIGHT@@|';
21 protected function formatTitle($bookmark)
23 return escape($bookmark->getTitle());
29 protected function formatTitleHtml($bookmark)
31 $title = $this->tokenizeSearchHighlightField(
32 $bookmark->getTitle() ?? '',
33 $bookmark->getAdditionalContentEntry('search_highlight')['title'] ?? []
36 return $this->replaceTokens(escape($title));
42 protected function formatDescription($bookmark)
44 $indexUrl = ! empty($this->contextData
['index_url']) ? $this->contextData
['index_url'] : '';
45 $description = $this->tokenizeSearchHighlightField(
46 $bookmark->getDescription() ?? '',
47 $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? []
50 return $this->replaceTokens(format_description(escape($description), $indexUrl));
56 protected function formatTagList($bookmark)
58 return escape(parent
::formatTagList($bookmark));
64 protected function formatTagListHtml($bookmark)
66 if (empty($bookmark->getAdditionalContentEntry('search_highlight')['tags'])) {
67 return $this->formatTagList($bookmark);
70 $tags = $this->tokenizeSearchHighlightField(
71 $bookmark->getTagsString(),
72 $bookmark->getAdditionalContentEntry('search_highlight')['tags']
74 $tags = $this->filterTagList(explode(' ', $tags));
75 $tags = escape($tags);
76 $tags = $this->replaceTokensArray($tags);
84 protected function formatTagString($bookmark)
86 return implode(' ', $this->formatTagList($bookmark));
92 protected function formatUrl($bookmark)
94 if ($bookmark->isNote() && isset($this->contextData
['index_url'])) {
95 return rtrim($this->contextData
['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/'));
98 return escape($bookmark->getUrl());
104 protected function formatRealUrl($bookmark)
106 if ($bookmark->isNote()) {
107 if (isset($this->contextData
['index_url'])) {
108 $prefix = rtrim($this->contextData
['index_url'], '/') . '/';
111 if (isset($this->contextData
['base_path'])) {
112 $prefix = rtrim($this->contextData
['base_path'], '/') . '/';
115 return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/'));
118 return escape($bookmark->getUrl());
124 protected function formatUrlHtml($bookmark)
126 $url = $this->tokenizeSearchHighlightField(
127 $bookmark->getUrl() ?? '',
128 $bookmark->getAdditionalContentEntry('search_highlight')['url'] ?? []
131 return $this->replaceTokens(escape($url));
137 protected function formatThumbnail($bookmark)
139 return escape($bookmark->getThumbnail());
143 * Insert search highlight token in provided field content based on a list of search result positions
145 * @param string $fieldContent
146 * @param array|null $positions List of of search results with 'start' and 'end' positions.
148 * @return string Updated $fieldContent.
150 protected function tokenizeSearchHighlightField(string $fieldContent, ?array $positions): string
152 if (empty($positions)) {
153 return $fieldContent;
157 $tokenLength = strlen(static::SEARCH_HIGHLIGHT_OPEN
);
158 foreach ($positions as $position) {
160 'start' => $position['start'] +
($insertedTokens * $tokenLength),
161 'end' => $position['end'] +
($insertedTokens * $tokenLength),
164 $content = mb_substr($fieldContent, 0, $position['start']);
165 $content .= static::SEARCH_HIGHLIGHT_OPEN
;
166 $content .= mb_substr($fieldContent, $position['start'], $position['end'] - $position['start']);
167 $content .= static::SEARCH_HIGHLIGHT_CLOSE
;
168 $content .= mb_substr($fieldContent, $position['end']);
170 $fieldContent = $content;
172 $insertedTokens +
= 2;
175 return $fieldContent;
179 * Replace search highlight tokens with HTML highlighted span.
181 * @param string $fieldContent
183 * @return string updated content.
185 protected function replaceTokens(string $fieldContent): string
188 [static::SEARCH_HIGHLIGHT_OPEN
, static::SEARCH_HIGHLIGHT_CLOSE
],
189 ['<span class="search-highlight">', '</span>'],
195 * Apply replaceTokens to an array of content strings.
197 * @param string[] $fieldContents
201 protected function replaceTokensArray(array $fieldContents): array
203 foreach ($fieldContents as &$entry) {
204 $entry = $this->replaceTokens($entry);
207 return $fieldContents;