import os def read_adventure_file(filename): with open(filename, 'r') as file: content = file.read() return content def parse_adventure(content): #two \n\n to separate pages pages = content.split('\n\n') adventure = {} for page in pages: print(f'') lines = page.split('\n') print(f'') #split Page 1 to 1 page_id = lines[0].split()[1] print(f'page_id{page_id}') title = lines[0] text = '\n'.join(lines[1:-1]) choices = lines[-1].split(';') print(f'') adventure[page_id] = {'title': title, 'text': text, 'choices': choices} return adventure def generate_html(adventure): html = ''' Backfire ''' for page_id, page in adventure.items(): html += f'''

{page['title']}

{page['text']}

    ''' for choice in page['choices']: if choice is not '': choice_text, choice_page = choice.split(' -> ') html += f'
  • {choice_text}
  • ' html += f'''
''' html += ''' ''' return html def main(): content = read_adventure_file('backfire.txt') adventure = parse_adventure(content) html = generate_html(adventure) with open('backfire.html', 'w') as file: file.write(html) # Convert the merged text file to .mobi format using Calibre's ebook-convert os.system('ebook-convert backfire.html backfire.mobi --chapter-mark=pagebreak --title="Backfire" --authors="Omar Khan"') if __name__ == '__main__': main()