import os

from pathlib import Path

PROJECT_PATH = Path(__file__).resolve().parent.parent.parent.absolute()
DATA_PATH = os.path.join(PROJECT_PATH, 'data')

def split_string(text, max_length):
    """Splits a string into chunks, each with a maximum length.

    Args:
        text (str): The string to split.
        max_length (int): The maximum length of each chunk.

    Returns:
        list: A list of strings, each with a length no more than max_length.
    """
    result = []
    start = 0
    while start < len(text):
        end = min(start + max_length, len(text))
        result.append(text[start:end])
        start = end
    return result

novel_path = os.path.join(DATA_PATH, '如果，宅.txt')

with open(novel_path, 'r', encoding='ANSI') as f:
    novel = f.readlines()

new_novel = ''
for novel_line in novel:
    line_list = split_string(novel_line, 30)
    new_novel += '\n'.join(line_list)
    new_novel += '\n'

with open(os.path.join(DATA_PATH, 'target.txt'), 'w', encoding='utf-8') as f:
    f.write(new_novel)


print('done')