mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 21:18: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):
|
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(),
|
|
||||||
color,
|
# TODO: Condense this all into a list comprehension once the broken regression test is fixed.
|
||||||
bold)
|
pretty_printed_entries = []
|
||||||
if len(part) > 0 and part[0] not in config['tagsymbols']
|
for text in search_texts:
|
||||||
else colorize(part.strip(),
|
pretty_printed_entries.append(" ".join([colorize(part.strip(),
|
||||||
config['colors']['tags'],
|
color,
|
||||||
not bold)
|
bold)
|
||||||
for part in text_split]
|
if len(part) > 0 and part[0] not in config['tagsymbols']
|
||||||
return " ".join(text)
|
else colorize(part.strip(),
|
||||||
|
config['colors']['tags'],
|
||||||
|
not bold)
|
||||||
|
for part in text.split()]))
|
||||||
|
return " ".join(pretty_printed_entries)
|
||||||
|
|
||||||
|
|
||||||
def slugify(string):
|
def slugify(string):
|
||||||
|
|
Loading…
Add table
Reference in a new issue