Category talk:Manuals: Difference between revisions
Jump to navigation
Jump to search
(When I added categories to the main text files I had to change the exit logic) |
No edit summary |
||
Line 6: | Line 6: | ||
import pathlib | import pathlib | ||
path_to_files = '/projects' | path_to_files = '/projects/manuals' | ||
def main(): | def main(): | ||
Line 64: | Line 64: | ||
# Project Structure | # Project Structure | ||
# /projects/AB/_AB.txt | # /projects/manuals/AB/_AB.txt | ||
# /projects/AB/AB_001.txt | # /projects/manuals/AB/AB_001.txt | ||
# ... | # ... | ||
# /projects/AB/AB_028.txt | # /projects/manuals/AB/AB_028.txt | ||
# /projects/RoE/_RoE.txt | # /projects/manuals/RoE/_RoE.txt | ||
# /projects/RoE/RoE_001.txt | # /projects/manuals/RoE/RoE_001.txt | ||
# ... | # ... | ||
# /projects/RoE/RoE_144.txt | # /projects/manuals/RoE/RoE_144.txt | ||
# /projects/SoD/_SoD.txt | # /projects/manuals/SoD/_SoD.txt | ||
# /projects/SoD/SoD_001.txt | # /projects/manuals/SoD/SoD_001.txt | ||
# ... | # ... | ||
# /projects/SoD/SoD_036.txt | # /projects/manuals/SoD/SoD_036.txt | ||
# /projects/Tutorial/_Tutorial.txt | # /projects/manuals/Tutorial/_Tutorial.txt | ||
# /projects/Tutorial/Tutorial_001.txt | # /projects/manuals/Tutorial/Tutorial_001.txt | ||
# ... | # ... | ||
# /projects/Tutorial/Tutorial_012.txt | # /projects/manuals/Tutorial/Tutorial_012.txt | ||
</pre> | </pre> |
Revision as of 06:03, 4 January 2017
#!/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