mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-15 10:13:15 -05:00
changed up the highlight fragment formatter
This commit is contained in:
parent
273c474e3f
commit
789abb3bbb
15
docs/api.rst
15
docs/api.rst
@ -221,21 +221,16 @@ Each fragment contains a list of strings, and some of them are marked as a highl
|
|||||||
|
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
{"text": "This is a sample text with a "},
|
{"text": "This is a sample text with a ", "highlight": false},
|
||||||
{"text": "highlighted", "term": 0},
|
{"text": "highlighted", "highlight": true},
|
||||||
{"text": " word."}
|
{"text": " word.", "highlight": false}
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
{"text": "Another", "term": 1},
|
{"text": "Another", "highlight": true},
|
||||||
{"text": " fragment with a highlight."}
|
{"text": " fragment with a highlight.", "highlight": false}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
When ``term`` is present within a string, the word within ``text`` should be highlighted.
|
|
||||||
The term index groups multiple matches together and words with the same index
|
|
||||||
should get identical highlighting.
|
|
||||||
A client may use this example to produce the following output:
|
A client may use this example to produce the following output:
|
||||||
|
|
||||||
... This is a sample text with a **highlighted** word. ... **Another** fragment with a highlight. ...
|
... This is a sample text with a **highlighted** word. ... **Another** fragment with a highlight. ...
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
... <span *ngFor="let fragment of highlights">
|
... <span *ngFor="let fragment of highlights">
|
||||||
<span *ngFor="let token of fragment" [ngClass]="token.term != null ? 'match term'+ token.term : ''">{{token.text}}</span> ...
|
<span *ngFor="let token of fragment" [class.match]="token.highlight">{{token.text}}</span> ...
|
||||||
</span>
|
</span>
|
@ -20,32 +20,37 @@ class JsonFormatter(Formatter):
|
|||||||
self.seen = {}
|
self.seen = {}
|
||||||
|
|
||||||
def format_token(self, text, token, replace=False):
|
def format_token(self, text, token, replace=False):
|
||||||
seen = self.seen
|
|
||||||
ttext = self._text(get_text(text, token, replace))
|
ttext = self._text(get_text(text, token, replace))
|
||||||
if ttext in seen:
|
return {'text': ttext, 'highlight': 'true'}
|
||||||
termnum = seen[ttext]
|
|
||||||
else:
|
|
||||||
termnum = len(seen)
|
|
||||||
seen[ttext] = termnum
|
|
||||||
|
|
||||||
return {'text': ttext, 'term': termnum}
|
|
||||||
|
|
||||||
def format_fragment(self, fragment, replace=False):
|
def format_fragment(self, fragment, replace=False):
|
||||||
output = []
|
output = []
|
||||||
index = fragment.startchar
|
index = fragment.startchar
|
||||||
text = fragment.text
|
text = fragment.text
|
||||||
|
amend_token = None
|
||||||
for t in fragment.matches:
|
for t in fragment.matches:
|
||||||
if t.startchar is None:
|
if t.startchar is None:
|
||||||
continue
|
continue
|
||||||
if t.startchar < index:
|
if t.startchar < index:
|
||||||
continue
|
continue
|
||||||
if t.startchar > index:
|
if t.startchar > index:
|
||||||
output.append({'text': text[index:t.startchar]})
|
text_inbetween = text[index:t.startchar]
|
||||||
output.append(self.format_token(text, t, replace))
|
if amend_token and t.startchar - index < 10:
|
||||||
|
amend_token['text'] += text_inbetween
|
||||||
|
else:
|
||||||
|
output.append({'text': text_inbetween,
|
||||||
|
'highlight': False})
|
||||||
|
amend_token = None
|
||||||
|
token = self.format_token(text, t, replace)
|
||||||
|
if amend_token:
|
||||||
|
amend_token['text'] += token['text']
|
||||||
|
else:
|
||||||
|
output.append(token)
|
||||||
|
amend_token = token
|
||||||
index = t.endchar
|
index = t.endchar
|
||||||
if index < fragment.endchar:
|
if index < fragment.endchar:
|
||||||
output.append({'text': text[index:fragment.endchar]})
|
output.append({'text': text[index:fragment.endchar],
|
||||||
|
'highlight': False})
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def format(self, fragments, replace=False):
|
def format(self, fragments, replace=False):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user