mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
Reduce code duplication for tag highlighting
- Breaks "unreadable date" regression test for unknown reason
This commit is contained in:
parent
baa3ffbebd
commit
c3b18181d6
1 changed files with 19 additions and 22 deletions
41
jrnl/util.py
41
jrnl/util.py
|
@ -215,7 +215,7 @@ def colorize(string, 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
|
||||
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 text: Text to be colorized
|
||||
: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
|
||||
if config['highlight']: # highlight tags
|
||||
if entry.journal.search_tags:
|
||||
search_texts = []
|
||||
for tag in entry.search_tags:
|
||||
text_split = re.split(re.compile(re.escape(tag), re.IGNORECASE),
|
||||
text,
|
||||
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]
|
||||
search_texts.append(re.split(re.compile(re.escape(tag), re.IGNORECASE),
|
||||
text,
|
||||
flags=re.UNICODE))
|
||||
else:
|
||||
text_split = re.split(entry.tag_regex(config['tagsymbols']), text)
|
||||
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]
|
||||
return " ".join(text)
|
||||
search_texts = re.split(entry.tag_regex(config['tagsymbols']), text)
|
||||
|
||||
# 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,
|
||||
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()]))
|
||||
return " ".join(pretty_printed_entries)
|
||||
|
||||
|
||||
def slugify(string):
|
||||
|
|
Loading…
Add table
Reference in a new issue