Progress towards proper tag colorization and body formatting

This commit is contained in:
Aaron Lichtman 2019-11-08 20:53:05 +01:00
parent 95b23d433c
commit 77472261de
No known key found for this signature in database
GPG key ID: 22368077DE9F9903
2 changed files with 11 additions and 7 deletions

View file

@ -51,7 +51,8 @@ class Entry:
@staticmethod @staticmethod
def tag_regex(tagsymbols): def tag_regex(tagsymbols):
pattern = r'(?:^|\s)([{tags}][-+*#/\w]+)'.format(tags=tagsymbols) # pattern = r'(?:^|\s)([{tags}][-+*#/\w]+)'.format(tags=tagsymbols)
pattern = r'(?<!\S)([{tags}][-+*#/\w]+)'.format(tags=tagsymbols)
return re.compile(pattern, re.UNICODE) return re.compile(pattern, re.UNICODE)
def _parse_tags(self): def _parse_tags(self):

View file

@ -228,8 +228,8 @@ def highlight_tags_with_background_color(entry, text, color, bold=False):
:rtype: List of tuples :rtype: List of tuples
:returns [(colorized_str, original_str)]""" :returns [(colorized_str, original_str)]"""
for fragment in fragments: for fragment in fragments:
for part in fragment.strip().split(" "): for part in fragment.lstrip().split(" "):
part = part.strip() # part = part.strip()
if part and part[0] not in config['tagsymbols']: if part and part[0] not in config['tagsymbols']:
yield (colorize(part, color, bold), part) yield (colorize(part, color, bold), part)
elif part: elif part:
@ -240,7 +240,7 @@ def highlight_tags_with_background_color(entry, text, color, bold=False):
if entry.journal.search_tags: if entry.journal.search_tags:
text_fragments = [] text_fragments = []
for tag in entry.search_tags: for tag in entry.search_tags:
text_fragments.append(re.split(re.compile(re.escape(tag), re.IGNORECASE), text_fragments.extend(re.split(re.compile(re.escape(tag), re.IGNORECASE),
text, text,
flags=re.UNICODE)) flags=re.UNICODE))
else: else:
@ -248,19 +248,22 @@ def highlight_tags_with_background_color(entry, text, color, bold=False):
final_text = "" final_text = ""
previous_piece = "" previous_piece = ""
# [print("FRAG: '" + repr(frag) + "'") for frag in text_fragments]
for colorized_piece, piece in colorized_text_generator(text_fragments): for colorized_piece, piece in colorized_text_generator(text_fragments):
# If it's punctuation and the previous word was a tag, add it directly after the tag. # If it's punctuation and the previous word was a tag, add it directly after the tag.
# TODO: This logic seems flawed... # TODO: This logic seems flawed...
if piece in punctuation and previous_piece[0] in config['tagsymbols']: if piece in punctuation and previous_piece[0] in config['tagsymbols']:
final_text = final_text.lstrip() + colorized_piece # print("PUNCT, POST TAG: '" + colorized_piece + "'")
final_text += colorized_piece
else: else:
# Otherwise just append it. # Otherwise just append it.
# print("NOT PUNCT OR NOT POST TAG: '" + colorized_piece + "'")
final_text += " " + colorized_piece final_text += " " + colorized_piece
final_text = final_text.lstrip() final_text = final_text.lstrip()
previous_piece = piece previous_piece = piece
# print("DONE")
return final_text return final_text.lstrip()
else: else:
return text return text