data-workers-publication/create_stories_layout.py
2019-03-25 08:35:09 +01:00

80 lines
2.5 KiB
Python

from functions import insert_linebreaks, insert_text_block, apply_zigzag, language
def check_if_page_is_not_empty(string):
new_page = False
lines = string.split('\n')
print('... Checking if this page is not empty ...')
print(lines)
for line in lines:
if line.strip() != '':
new_page = True
print('>>> new_page:', new_page)
return new_page
def create_stories_layout(stories_txt):
if language == 'en':
columnwidth = 50
else:
columnwidth = 48
stories = insert_linebreaks(stories_txt, 50, type='word')
stories_lines = stories.split('\n')
# insert empty lines for header space
column_count = 0
lines = ''
line_number = 1
for line in stories_lines:
if line_number % 69 == 0:
lines += '\n' * 5
line_number += 5
lines += line + '\n'
line_number += 1
column_count += 1
else:
lines += line + '\n'
line_number += 1
# make zigzag columns
zigzag = apply_zigzag(lines, 5)
zigzag_lines = zigzag.split('\n')
# make two-column page(s)
count = 1
pages = ''
current_page_first_column = ''
current_page_second_column = ''
for i, line in enumerate(zigzag_lines):
if count < 68:
current_page_first_column += line + '\n'
count += 1
elif count < 69:
current_page_first_column += line
count += 1
elif count < 137:
current_page_second_column += line + '\n'
count += 1
elif count < 138:
current_page_second_column += line
count += 1
else:
print('>>> length first column:', len(current_page_first_column.split('\n')))
print('>>> length second column:', len(current_page_second_column.split('\n')))
current_page_two_columns = insert_text_block(current_page_first_column, current_page_second_column, 56, 0)
pages += current_page_two_columns + '\n'
print('--> Page added to this stories page, with length:', len(current_page_two_columns.split('\n')))
current_page_first_column = line + '\n'
current_page_second_column = ''
count = 1
# Check if the current line is the very last one
# if so, append a new page
if i + 1 == len(zigzag_lines):
print('... Ready to add a stories page, with length:', len(current_page_first_column.split('\n')))
new_page = check_if_page_is_not_empty(current_page_first_column)
if new_page == True:
current_page_two_columns = insert_text_block(current_page_first_column, current_page_second_column, 56, 48)
pages += current_page_two_columns + '\n'
print('--> Page added to this stories page, with length:', len(current_page_two_columns.split('\n')))
return pages