mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
Replace list comprehension with generator
This commit is contained in:
parent
82d3c65d82
commit
6ce83c7aa1
1 changed files with 19 additions and 17 deletions
36
jrnl/util.py
36
jrnl/util.py
|
@ -222,30 +222,32 @@ def highlight_tags_with_background_color(entry, text, color, bold=False):
|
|||
:param bold: Bold flag text, passed to colorize()
|
||||
:return: Colorized str
|
||||
"""
|
||||
def colorized_text_generator(fragments):
|
||||
"""Efficiently generate colorized tags / text from text fragments.
|
||||
:param fragments: List of strings representing parts of entry (tag or word)."""
|
||||
# Taken from @shobrook. Thanks, buddy :)
|
||||
for fragment in fragments:
|
||||
for part in fragment.strip().split(" "):
|
||||
if part and part[0] not in config['tagsymbols']:
|
||||
yield colorize(part.strip(), color, bold)
|
||||
else:
|
||||
yield colorize(part.strip(), config['colors']['tags'], not bold)
|
||||
|
||||
config = entry.journal.config
|
||||
if config['highlight']: # highlight tags
|
||||
if entry.journal.search_tags:
|
||||
search_texts = []
|
||||
text_fragments = []
|
||||
for tag in entry.search_tags:
|
||||
search_texts.append(re.split(re.compile(re.escape(tag), re.IGNORECASE),
|
||||
text,
|
||||
flags=re.UNICODE))
|
||||
text_fragments.append(re.split(re.compile(re.escape(tag), re.IGNORECASE),
|
||||
text,
|
||||
flags=re.UNICODE))
|
||||
else:
|
||||
search_texts = re.split(entry.tag_regex(config['tagsymbols']), text)
|
||||
text_fragments = re.split(entry.tag_regex(config['tagsymbols']), text)
|
||||
|
||||
pretty_printed_entries = []
|
||||
for text in search_texts:
|
||||
colorized_parts = (colorize(part.strip(),
|
||||
color,
|
||||
bold)
|
||||
if part and part[0] not in config['tagsymbols']
|
||||
else colorize(part.strip(),
|
||||
config['colors']['tags'],
|
||||
not bold)
|
||||
for part in text.strip().split(" "))
|
||||
return " ".join(colorized_text_generator(text_fragments))
|
||||
|
||||
pretty_printed_entries.append(" ".join(colorized_parts))
|
||||
return " ".join(pretty_printed_entries)
|
||||
else:
|
||||
return text
|
||||
|
||||
|
||||
def slugify(string):
|
||||
|
|
Loading…
Add table
Reference in a new issue