fix post wrapping

This commit is contained in:
olari
2021-06-27 18:48:47 +03:00
parent 26232c60ae
commit 6181fa29ea

View File

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