]>
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 protected const SEARCH_HIGHLIGHT_OPEN
= '|@@HIGHLIGHT';
16 protected 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 $tagsSeparator = $this->conf
->get('general.tags_separator', ' ');
72 if (empty($bookmark->getAdditionalContentEntry('search_highlight')['tags'])) {
73 return $this->formatTagList($bookmark);
76 $tags = $this->tokenizeSearchHighlightField(
77 $bookmark->getTagsString($tagsSeparator),
78 $bookmark->getAdditionalContentEntry('search_highlight')['tags']
80 $tags = $this->filterTagList(tags_str2array($tags, $tagsSeparator));
81 $tags = escape($tags);
82 $tags = $this->replaceTokensArray($tags);
90 protected function formatTagString($bookmark)
92 return implode($this->conf
->get('general.tags_separator'), $this->formatTagList($bookmark));
98 protected function formatUrl($bookmark)
100 if ($bookmark->isNote() && isset($this->contextData
['index_url'])) {
101 return rtrim($this->contextData
['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/'));
104 return escape($bookmark->getUrl());
110 protected function formatRealUrl($bookmark)
112 if ($bookmark->isNote()) {
113 if (isset($this->contextData
['index_url'])) {
114 $prefix = rtrim($this->contextData
['index_url'], '/') . '/';
117 if (isset($this->contextData
['base_path'])) {
118 $prefix = rtrim($this->contextData
['base_path'], '/') . '/';
121 return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/'));
124 return escape($bookmark->getUrl());
130 protected function formatUrlHtml($bookmark)
132 $url = $this->tokenizeSearchHighlightField(
133 $bookmark->getUrl() ?? '',
134 $bookmark->getAdditionalContentEntry('search_highlight')['url'] ?? []
137 return $this->replaceTokens(escape($url));
143 protected function formatThumbnail($bookmark)
145 return escape($bookmark->getThumbnail());
149 * Insert search highlight token in provided field content based on a list of search result positions
151 * @param string $fieldContent
152 * @param array|null $positions List of of search results with 'start' and 'end' positions.
154 * @return string Updated $fieldContent.
156 protected function tokenizeSearchHighlightField(string $fieldContent, ?array $positions): string
158 if (empty($positions)) {
159 return $fieldContent;
163 $tokenLength = strlen(static::SEARCH_HIGHLIGHT_OPEN
);
164 foreach ($positions as $position) {
166 'start' => $position['start'] +
($insertedTokens * $tokenLength),
167 'end' => $position['end'] +
($insertedTokens * $tokenLength),
170 $content = mb_substr($fieldContent, 0, $position['start']);
171 $content .= static::SEARCH_HIGHLIGHT_OPEN
;
172 $content .= mb_substr($fieldContent, $position['start'], $position['end'] - $position['start']);
173 $content .= static::SEARCH_HIGHLIGHT_CLOSE
;
174 $content .= mb_substr($fieldContent, $position['end']);
176 $fieldContent = $content;
178 $insertedTokens +
= 2;
181 return $fieldContent;
185 * Replace search highlight tokens with HTML highlighted span.
187 * @param string $fieldContent
189 * @return string updated content.
191 protected function replaceTokens(string $fieldContent): string
194 [static::SEARCH_HIGHLIGHT_OPEN
, static::SEARCH_HIGHLIGHT_CLOSE
],
195 ['<span class="search-highlight">', '</span>'],
201 * Apply replaceTokens to an array of content strings.
203 * @param string[] $fieldContents
207 protected function replaceTokensArray(array $fieldContents): array
209 foreach ($fieldContents as &$entry) {
210 $entry = $this->replaceTokens($entry);
213 return $fieldContents;