]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/formatter/BookmarkDefaultFormatter.php
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'] ?? []
49 $description = format_description(
52 $this->conf
->get('formatter_settings.autolink', true)
55 return $this->replaceTokens($description);
61 protected function formatTagList($bookmark)
63 return escape(parent
::formatTagList($bookmark));
69 protected function formatTagListHtml($bookmark)
71 if (empty($bookmark->getAdditionalContentEntry('search_highlight')['tags'])) {
72 return $this->formatTagList($bookmark);
75 $tags = $this->tokenizeSearchHighlightField(
76 $bookmark->getTagsString(),
77 $bookmark->getAdditionalContentEntry('search_highlight')['tags']
79 $tags = $this->filterTagList(explode(' ', $tags));
80 $tags = escape($tags);
81 $tags = $this->replaceTokensArray($tags);
89 protected function formatTagString($bookmark)
91 return implode(' ', $this->formatTagList($bookmark));
97 protected function formatUrl($bookmark)
99 if ($bookmark->isNote() && isset($this->contextData
['index_url'])) {
100 return rtrim($this->contextData
['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/'));
103 return escape($bookmark->getUrl());
109 protected function formatRealUrl($bookmark)
111 if ($bookmark->isNote()) {
112 if (isset($this->contextData
['index_url'])) {
113 $prefix = rtrim($this->contextData
['index_url'], '/') . '/';
116 if (isset($this->contextData
['base_path'])) {
117 $prefix = rtrim($this->contextData
['base_path'], '/') . '/';
120 return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/'));
123 return escape($bookmark->getUrl());
129 protected function formatUrlHtml($bookmark)
131 $url = $this->tokenizeSearchHighlightField(
132 $bookmark->getUrl() ?? '',
133 $bookmark->getAdditionalContentEntry('search_highlight')['url'] ?? []
136 return $this->replaceTokens(escape($url));
142 protected function formatThumbnail($bookmark)
144 return escape($bookmark->getThumbnail());
148 * Insert search highlight token in provided field content based on a list of search result positions
150 * @param string $fieldContent
151 * @param array|null $positions List of of search results with 'start' and 'end' positions.
153 * @return string Updated $fieldContent.
155 protected function tokenizeSearchHighlightField(string $fieldContent, ?array $positions): string
157 if (empty($positions)) {
158 return $fieldContent;
162 $tokenLength = strlen(static::SEARCH_HIGHLIGHT_OPEN
);
163 foreach ($positions as $position) {
165 'start' => $position['start'] +
($insertedTokens * $tokenLength),
166 'end' => $position['end'] +
($insertedTokens * $tokenLength),
169 $content = mb_substr($fieldContent, 0, $position['start']);
170 $content .= static::SEARCH_HIGHLIGHT_OPEN
;
171 $content .= mb_substr($fieldContent, $position['start'], $position['end'] - $position['start']);
172 $content .= static::SEARCH_HIGHLIGHT_CLOSE
;
173 $content .= mb_substr($fieldContent, $position['end']);
175 $fieldContent = $content;
177 $insertedTokens +
= 2;
180 return $fieldContent;
184 * Replace search highlight tokens with HTML highlighted span.
186 * @param string $fieldContent
188 * @return string updated content.
190 protected function replaceTokens(string $fieldContent): string
193 [static::SEARCH_HIGHLIGHT_OPEN
, static::SEARCH_HIGHLIGHT_CLOSE
],
194 ['<span class="search-highlight">', '</span>'],
200 * Apply replaceTokens to an array of content strings.
202 * @param string[] $fieldContents
206 protected function replaceTokensArray(array $fieldContents): array
208 foreach ($fieldContents as &$entry) {
209 $entry = $this->replaceTokens($entry);
212 return $fieldContents;