Category talk:Manuals

From Heroes 3 wiki
Revision as of 06:03, 4 January 2017 by Imahero (talk | contribs)
Jump to navigation Jump to search
#!/usr/bin/env python3

import sys
import re
import pathlib

path_to_files = '/projects/manuals'

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:
        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 page_number > page_count:
            break

if __name__ == "__main__":
    main()

# Project Structure

# /projects/manuals/AB/_AB.txt
# /projects/manuals/AB/AB_001.txt
# ...
# /projects/manuals/AB/AB_028.txt

# /projects/manuals/RoE/_RoE.txt
# /projects/manuals/RoE/RoE_001.txt
# ...
# /projects/manuals/RoE/RoE_144.txt

# /projects/manuals/SoD/_SoD.txt
# /projects/manuals/SoD/SoD_001.txt
# ...
# /projects/manuals/SoD/SoD_036.txt

# /projects/manuals/Tutorial/_Tutorial.txt
# /projects/manuals/Tutorial/Tutorial_001.txt
# ...
# /projects/manuals/Tutorial/Tutorial_012.txt