Reduce code duplication for tag highlighting

- Breaks "unreadable date" regression test for unknown reason
This commit is contained in:
Aaron Lichtman 2019-11-06 23:07:11 +01:00
parent baa3ffbebd
commit c3b18181d6
No known key found for this signature in database
GPG key ID: 22368077DE9F9903

View file

@ -215,7 +215,7 @@ def colorize(string, color, bold=False):
def highlight_tags_maintain_background_color(entry, text, color, bold=False): def highlight_tags_maintain_background_color(entry, text, color, bold=False):
""" """
Takes a string and colorizes the tags in it based upon the config value for Takes a string and colorizes the tags in it based upon the config value for
color.tags, while colorizing the rest of the text based on color. color.tags, while colorizing the rest of the text based on `color`.
:param entry: Entry object, for access to journal config :param entry: Entry object, for access to journal config
:param text: Text to be colorized :param text: Text to be colorized
:param color: Color for non-tag text, passed to colorize() :param color: Color for non-tag text, passed to colorize()
@ -225,29 +225,26 @@ def highlight_tags_maintain_background_color(entry, text, color, bold=False):
config = entry.journal.config config = entry.journal.config
if config['highlight']: # highlight tags if config['highlight']: # highlight tags
if entry.journal.search_tags: if entry.journal.search_tags:
search_texts = []
for tag in entry.search_tags: for tag in entry.search_tags:
text_split = re.split(re.compile(re.escape(tag), re.IGNORECASE), search_texts.append(re.split(re.compile(re.escape(tag), re.IGNORECASE),
text, text,
flags=re.UNICODE) flags=re.UNICODE))
text = [colorize(part.strip(),
color,
bold)
if len(part) > 0 and part[0] not in config['tagsymbols']
else colorize(part.strip(),
config['colors']['tags'],
not bold)
for part in text_split]
else: else:
text_split = re.split(entry.tag_regex(config['tagsymbols']), text) search_texts = re.split(entry.tag_regex(config['tagsymbols']), text)
text = [colorize(part.strip(),
# TODO: Condense this all into a list comprehension once the broken regression test is fixed.
pretty_printed_entries = []
for text in search_texts:
pretty_printed_entries.append(" ".join([colorize(part.strip(),
color, color,
bold) bold)
if len(part) > 0 and part[0] not in config['tagsymbols'] if len(part) > 0 and part[0] not in config['tagsymbols']
else colorize(part.strip(), else colorize(part.strip(),
config['colors']['tags'], config['colors']['tags'],
not bold) not bold)
for part in text_split] for part in text.split()]))
return " ".join(text) return " ".join(pretty_printed_entries)
def slugify(string): def slugify(string):