Advertisement
nerdtronn

Untitled

Jul 2nd, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. #!/usr/bin/python
  2. ##############################################################################
  3. # Utility for use with the "Objects in Space" game (http://objectsgame.com/)
  4. # Creates a set of html files from the Infopedia text files for easier
  5. # browsing outside or alongside the game.
  6. #
  7. # TO USE:
  8. # - save this script to the "Objects in Space\assets" directory
  9. # - run the script
  10. # - open any of the html files created in the "assets\info_html" directory
  11. ##############################################################################
  12.  
  13. import sys
  14. import shutil
  15. import os
  16. import glob
  17. import re
  18.  
  19. html_dir_name = "info_html"
  20. info_files_glob_string = "info_*.txt"
  21.  
  22. html_header = """<!DOCTYPE html>
  23. <html>
  24.    <head>
  25.        <meta charset="utf-8" />
  26.        <title>$TITLE</title>
  27.        <style>
  28.            * {
  29.                font-family: "Courier New", sans-serif;
  30.            }
  31.            .nav_menu {
  32.                width: 200px;
  33.                height: 100vh;
  34.                overflow-y: auto;
  35.                float: left;
  36.                margin-right: 12px;
  37.            }
  38.            .nav_menu a {
  39.                background-color: #eee;
  40.                color: black;
  41.                display: block;
  42.                padding: 12px;
  43.                text-decoration: none;
  44.            }
  45.            .nav_menu a:hover {
  46.                background-color: #ccc;
  47.            }
  48.            .nav_menu a.active {
  49.                background-color: #000;
  50.                color: #fff;
  51.            }
  52.            .topic_content {
  53.                width: auto;
  54.                height: 100vh;
  55.                overflow-y: auto;
  56.            }
  57.        </style>
  58.        <script>
  59.            function OnLoad() {
  60.                var ele = document.getElementById("active_element");
  61.                ele.scrollIntoView();
  62.            }
  63.        </script>
  64.    </head>
  65.    <body onload="OnLoad();">
  66.        <div class="nav_menu">
  67. $NAVMENU
  68.        </div>
  69.        <div class="topic_content">
  70. """
  71.  
  72. html_footer = """        </div>
  73.    </body>
  74. </html>
  75. """
  76.  
  77. reSubject = re.compile("^Subject: (.*)$", re.IGNORECASE)
  78. reSummary = re.compile("^Summary: (.*)$", re.IGNORECASE)
  79.  
  80. files_by_subject = {}
  81. created_html_files = []
  82.  
  83.  
  84. def main():
  85.     script_dir = os.path.dirname(__file__)
  86.     html_dir = os.path.join(script_dir, html_dir_name)
  87.  
  88.     if os.path.exists(html_dir):
  89.         shutil.rmtree(html_dir)
  90.     os.makedirs(html_dir)
  91.  
  92.     glob_str = os.path.join(script_dir, info_files_glob_string)
  93.     info_text_files = glob.glob(glob_str)
  94.     for info_text_file in info_text_files:
  95.         info_file = os.path.basename(info_text_file)
  96.         print(f"processing {info_file}")
  97.         info_html_file = os.path.join(html_dir,
  98.                                       info_file.replace(".txt", ".html"))
  99.         with open(info_text_file) as fin:
  100.             with open(info_html_file, "w") as fout:
  101.                 process_info_file(fin, fout, os.path.basename(info_html_file))
  102.                 created_html_files.append(info_html_file)
  103.  
  104.     print("adding nav menus to generated html files")
  105.     for info_html_file in created_html_files:
  106.  
  107.         nav_menu_lines = []
  108.         subjects = list(files_by_subject.keys())
  109.         subjects.sort()
  110.         for subject in subjects:
  111.             target_file = files_by_subject[subject]
  112.             classid_str = ""
  113.             if os.path.basename(info_html_file) == target_file:
  114.                 classid_str = ' class="active" id="active_element"'
  115.             nav_str = '            ' + \
  116.                 f'<a href="{target_file}"{classid_str}>{subject}</a>'
  117.             nav_menu_lines.append(nav_str)
  118.         nav_menu_string = "\n".join(nav_menu_lines)
  119.  
  120.         lines = []
  121.         replaced_nav_menu = False
  122.         with open(info_html_file) as fin:
  123.             lines = fin.readlines()
  124.         with open(info_html_file, "w") as fout:
  125.             for line in lines:
  126.                 if "$NAVMENU" in line:
  127.                     line = line.replace("$NAVMENU", nav_menu_string)
  128.                 fout.write(line)
  129.  
  130.  
  131. def process_info_file(fin, fout, fout_basename):
  132.     global files_by_subject
  133.     in_par = False
  134.     got_subject = False
  135.     got_summary = False
  136.     lines = fin.readlines()
  137.     for line in lines:
  138.         line = line.strip()
  139.         if not got_subject:
  140.             m = reSubject.match(line)
  141.             if m:
  142.                 got_subject = True
  143.                 subject = m.group(1)
  144.         elif not got_summary:
  145.             m = reSummary.match(line)
  146.             if m:
  147.                 got_summary = True
  148.                 summary = m.group(1)
  149.                 fout.write(html_header.replace("$TITLE", summary))
  150.                 fout.write(f"            <h1>{summary}</h1>\n")
  151.         elif got_subject and got_summary and line:
  152.             fout.write(f"            <p>{line}</p>\n")
  153.     if got_subject and got_summary:
  154.         fout.write(html_footer)
  155.         files_by_subject[subject] = fout_basename
  156.  
  157.  
  158. if __name__ == '__main__':
  159.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement