#!/usr/bin/env python3
import sys
import re
import pathlib
path_to_files = '/projects'
def main():
fix_pages('Restoration of Erathia', 'RoE', 144)
fix_pages("Armageddon's Blade", 'AB', 28)
fix_pages('Shadow of Death', 'SoD', 36)
fix_pages('Tutorial', 'Tutorial', 12)
def fix_pages(name, short_name, total_pages):
folder = f'{path_to_files}/{short_name}'
path = pathlib.PurePath(f'{folder}/_{short_name}.txt')
file = open(path, encoding='utf-8')
text = file.read()
pages = re.split('\n\n\n\d\d?\d?\d?\n\n\n', text)
page_count = len(pages) - 1
if page_count != total_pages:
print(f'{name} has the wrong number of pages!')
print(f'Expected: {total_pages}')
print(f'Actual: {page_count}')
sys.exit()
page_number = 1
for page in pages:
if page == '':
continue
padding = ''
if page_number < 10:
padding = padding + '0'
if page_number < 100:
padding = padding + '0'
page_name = f'{short_name}_{padding}{page_number}.txt'
if page_number == 1:
previous_page = 'TOC'
else:
previous_page = 'Page ' + str(page_number - 1) + '|Previous Page'
if page_number == len(pages) - 1:
next_page = 'TOC'
else:
next_page = 'Page ' + str(page_number + 1) + '|Next Page'
page_path = pathlib.PurePath(f'{folder}/{page_name}')
text = f'[[{name} Manual {previous_page}]]\n\n[[{name} Manual {next_page}]]\n\n\n[[File:{short_name}1_{page_number}.png|768px]]\n\n\n'
category = f'\n\n\n[[Category:{name} Manual]]'
page_file = open(page_path, 'w', encoding='utf-8')
page_file.write(text + page + category)
page_number = page_number + 1
if __name__ == "__main__":
main()
# Project Structure
# /projects/AB/_AB.txt
# /projects/AB/AB_001.txt
# ...
# /projects/AB/AB_028.txt
# /projects/RoE/_RoE.txt
# /projects/RoE/RoE_001.txt
# ...
# /projects/RoE/RoE_144.txt
# /projects/SoD/_SoD.txt
# /projects/SoD/SoD_001.txt
# ...
# /projects/SoD/SoD_036.txt
# /projects/Tutorial/_Tutorial.txt
# /projects/Tutorial/Tutorial_001.txt
# ...
# /projects/Tutorial/Tutorial_012.txt