diff --git a/journal.py b/journal.py index d88facc..fa9a6ce 100644 --- a/journal.py +++ b/journal.py @@ -540,6 +540,7 @@ def parse_entry(entry): def merge_wrapped_lines(l): TIMESTAMP_LENGTH = len('2020-02-02 02:02:02 ') + POST_BLOCK_LENGTH = len('@post 2020-02-02 02:02:02 ') COLUMN_LIMIT = 80 res = [] @@ -550,6 +551,9 @@ def parse_entry(entry): prev = l[i-1] if i > 0 else None next = l[i+1] if i+1 < len(l) else None + before_prev = l[i-2] if i-2 >= 0 else None + before_before_prev = l[i-3] if i-3 >= 0 else None + # ['aoeu', '\n', 'aoeu'] if prev and curr == '\n' and next: len_prev = len(prev) @@ -558,6 +562,9 @@ def parse_entry(entry): if i - 1 == 0: len_prev += TIMESTAMP_LENGTH + if before_prev and before_before_prev and before_prev.startswith('@post') and all(c == '\n' for c in before_before_prev): + len_prev += POST_BLOCK_LENGTH + # do not wrap indented lines if not next[0].isspace(): next_word = next.split()[0] @@ -633,27 +640,37 @@ def parse_entry(entry): } def generate_entry(entry): - def format_block(block, is_first): + def format_block(curr, prev, before_prev): def format_text(text): - if all(c == '\n' for c in block): + if all(c == '\n' for c in curr): return text DUMMY_TS = '2020-02-02 02:02:02 ' + DUMMY_POST = '@post 2020-02-02 02:02:02 ' + + is_first = not prev + is_post = before_prev and all(c == '\n' for c in before_prev) and isinstance(prev, dict) and prev['type'] == 'post' if is_first: text = DUMMY_TS + text + if is_post: + text = DUMMY_POST + text + length = len(text) if length > 80: text = wrap_text(text) + if is_post: + text = text.removeprefix(DUMMY_POST) + if is_first: text = text.removeprefix(DUMMY_TS) return text - formatted = format_text(block) if isinstance(block, str) else generate_entry_module(block) + formatted = format_text(curr) if isinstance(curr, str) else generate_entry_module(curr) if result[-1] != '\n' and not all(c == '\n' for c in formatted): formatted = ' ' + formatted @@ -662,8 +679,15 @@ def generate_entry(entry): result = f'\n\n{format_timestamp(entry["timestamp"])}' - for i, block in enumerate(entry['blocks']): - result += format_block(block, i == 0) + i = 0 + while i < len(entry['blocks']): + curr = entry['blocks'][i] + prev = entry['blocks'][i-1] if i-1 >= 0 else None + before_prev = entry['blocks'][i-2] if i-2 >= 0 else None + + result += format_block(curr, prev, before_prev) + + i += 1 return result @@ -854,7 +878,6 @@ def handle_test(args): else: print('Test passed!') - def handle_summary(args): def generate_food_summary(page): result = ''