Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ##############################################################################
- # Utility for use with the "Objects in Space" game (http://objectsgame.com/)
- # Creates a set of html files from the Infopedia text files for easier
- # browsing outside or alongside the game.
- #
- # TO USE:
- # - save this script to the "Objects in Space\assets" directory
- # - run the script
- # - open any of the html files created in the "assets\info_html" directory
- ##############################################################################
- import sys
- import shutil
- import os
- import glob
- import re
- html_dir_name = "info_html"
- info_files_glob_string = "info_*.txt"
- html_header = """<!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>$TITLE</title>
- <style>
- * {
- font-family: "Courier New", sans-serif;
- }
- .nav_menu {
- width: 200px;
- height: 100vh;
- overflow-y: auto;
- float: left;
- margin-right: 12px;
- }
- .nav_menu a {
- background-color: #eee;
- color: black;
- display: block;
- padding: 12px;
- text-decoration: none;
- }
- .nav_menu a:hover {
- background-color: #ccc;
- }
- .nav_menu a.active {
- background-color: #000;
- color: #fff;
- }
- .topic_content {
- width: auto;
- height: 100vh;
- overflow-y: auto;
- }
- </style>
- <script>
- function OnLoad() {
- var ele = document.getElementById("active_element");
- ele.scrollIntoView();
- }
- </script>
- </head>
- <body onload="OnLoad();">
- <div class="nav_menu">
- $NAVMENU
- </div>
- <div class="topic_content">
- """
- html_footer = """ </div>
- </body>
- </html>
- """
- reSubject = re.compile("^Subject: (.*)$", re.IGNORECASE)
- reSummary = re.compile("^Summary: (.*)$", re.IGNORECASE)
- files_by_subject = {}
- created_html_files = []
- def main():
- script_dir = os.path.dirname(__file__)
- html_dir = os.path.join(script_dir, html_dir_name)
- if os.path.exists(html_dir):
- shutil.rmtree(html_dir)
- os.makedirs(html_dir)
- glob_str = os.path.join(script_dir, info_files_glob_string)
- info_text_files = glob.glob(glob_str)
- for info_text_file in info_text_files:
- info_file = os.path.basename(info_text_file)
- print(f"processing {info_file}")
- info_html_file = os.path.join(html_dir,
- info_file.replace(".txt", ".html"))
- with open(info_text_file) as fin:
- with open(info_html_file, "w") as fout:
- process_info_file(fin, fout, os.path.basename(info_html_file))
- created_html_files.append(info_html_file)
- print("adding nav menus to generated html files")
- for info_html_file in created_html_files:
- nav_menu_lines = []
- subjects = list(files_by_subject.keys())
- subjects.sort()
- for subject in subjects:
- target_file = files_by_subject[subject]
- classid_str = ""
- if os.path.basename(info_html_file) == target_file:
- classid_str = ' class="active" id="active_element"'
- nav_str = ' ' + \
- f'<a href="{target_file}"{classid_str}>{subject}</a>'
- nav_menu_lines.append(nav_str)
- nav_menu_string = "\n".join(nav_menu_lines)
- lines = []
- replaced_nav_menu = False
- with open(info_html_file) as fin:
- lines = fin.readlines()
- with open(info_html_file, "w") as fout:
- for line in lines:
- if "$NAVMENU" in line:
- line = line.replace("$NAVMENU", nav_menu_string)
- fout.write(line)
- def process_info_file(fin, fout, fout_basename):
- global files_by_subject
- in_par = False
- got_subject = False
- got_summary = False
- lines = fin.readlines()
- for line in lines:
- line = line.strip()
- if not got_subject:
- m = reSubject.match(line)
- if m:
- got_subject = True
- subject = m.group(1)
- elif not got_summary:
- m = reSummary.match(line)
- if m:
- got_summary = True
- summary = m.group(1)
- fout.write(html_header.replace("$TITLE", summary))
- fout.write(f" <h1>{summary}</h1>\n")
- elif got_subject and got_summary and line:
- fout.write(f" <p>{line}</p>\n")
- if got_subject and got_summary:
- fout.write(html_footer)
- files_by_subject[subject] = fout_basename
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement