Yield highlighted text lines (with line returns) for the whole of the matched and unmatched text of a ``query``.
(
match,
query,
stopwords=STOPWORDS,
trace=TRACE_HIGHLIGHTED_TEXT,
)
| 3329 | |
| 3330 | |
| 3331 | def get_highlighted_lines( |
| 3332 | match, |
| 3333 | query, |
| 3334 | stopwords=STOPWORDS, |
| 3335 | trace=TRACE_HIGHLIGHTED_TEXT, |
| 3336 | ): |
| 3337 | """ |
| 3338 | Yield highlighted text lines (with line returns) for the whole of the matched and unmatched text of a ``query``. |
| 3339 | """ |
| 3340 | tokens = tokenize_matched_text( |
| 3341 | location=query.location, |
| 3342 | query_string=query.query_string, |
| 3343 | dictionary=query.idx.dictionary, |
| 3344 | start_line=match.query.start_line, |
| 3345 | _cache={}, |
| 3346 | ) |
| 3347 | tokens = tag_matched_tokens(tokens=tokens, match_qspan=match.qspan) |
| 3348 | |
| 3349 | if trace: |
| 3350 | tokens = list(tokens) |
| 3351 | print() |
| 3352 | logger_debug('get_highlighted_lines: tokens:') |
| 3353 | for t in tokens: |
| 3354 | print(' ', t) |
| 3355 | print() |
| 3356 | header = '''<style> |
| 3357 | pre.log {color: #f1f1f1; background-color: #222; font-family: monospace;} |
| 3358 | pre.wrap {white-space: pre-wrap;} |
| 3359 | span.not-matched {color:#f5bf3c;} |
| 3360 | span.matched {color:#000000;} |
| 3361 | </style> |
| 3362 | |
| 3363 | <div class="license-match"><pre>''' |
| 3364 | footer = '''</pre></div>''' |
| 3365 | |
| 3366 | yield header |
| 3367 | highlight_matched = '<span class="matched">{}</span>' |
| 3368 | highlight_not_matched = '<span class="not-matched">{}</span>' |
| 3369 | for token in tokens: |
| 3370 | val = token.value |
| 3371 | if token.is_text and val.lower() not in stopwords: |
| 3372 | if token.is_matched: |
| 3373 | yield highlight_matched.format(val) |
| 3374 | else: |
| 3375 | yield highlight_not_matched.format(val) |
| 3376 | else: |
| 3377 | # we do not highlight punctuation and stopwords. |
| 3378 | yield highlight_not_matched.format(val) |
| 3379 | |
| 3380 | yield footer |
| 3381 | |
| 3382 | |
| 3383 | def tag_matched_tokens(tokens, match_qspan): |
no test coverage detected