]> git.cworth.org Git - turbot-web/commitdiff
Abstract out the filename_from_name function
authorCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 08:20:40 +0000 (00:20 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 08:39:47 +0000 (00:39 -0800)
Previously, there were many, many places i nthe code performing a
calculation of:

"_".join(name.split())

in order to replace all spaces with underscores before using a name as
a filename.

But as we determined recently, this is insufficient as we at least
need to also replaces all '/' with an '_' as well (to ensure that the
filename looks like a filename and not a directory plus a filename).

The previous fix for the '/' in one place, (immediately before writing
the file), meant that all the other places that were emitting links to
that file were now wrong.

We can ensure everything is consistent by having a single function for
all instances of this filename generation which we do here with the
new filename_from_name() function.

html_generator.py

index b944391ed2cd15745f57b1c29ac563d889b2893e..c16f033552cb80094befa995b04645fa1d612ba6 100644 (file)
@@ -21,6 +21,11 @@ import re
 website = "https://halibut.cworth.org/"
 #change this if we're using AWS or some other subdomain instead
 
+def filename_from_name(name):
+    """Returns a string derived from name, but with all spaces and slashes
+       replaced with underscores, (for making a clean filename)"""
+    return re.sub(r'[ /]', '_', name)
+
 def channel_url(channel_id):
     """Given a channel ID, return the URL for that channel."""
 
@@ -167,7 +172,7 @@ def overview(puzzles, rounds):
         expanding += [
         '  <div id="b{}" class="containerTab {}" style="display:none;">\n'.format(i, status),
         '    <span onclick="this.parentElement.style.display=\'none\'" class="closebtn">x</span>\n',
-        '    <h2>{}</h2>\n'.format(link(website + "_".join(rnd.split()) + "_round.html", rnd)),
+        '    <h2>{}</h2>\n'.format(link(website + filename_from_name(rnd) + "_round.html", rnd)),
         '    <table class="sortable">\n',
         '      <tr>\n',
         '        <th><u>Puzzle</u></th>\n',
@@ -181,12 +186,12 @@ def overview(puzzles, rounds):
                 meta = ''
             if puzzle['status'] == 'solved':
                 expanding += ['      <tr class=\'solved\';>\n',
-                '        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
+                '        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + ".html", puzzle['name']+meta)),
                 '        <td>{}</td>\n'.format(puzzle['solution']),
                 '      </tr>\n']
             else:
                 expanding += ['      <tr class=\'unsolved\';>\n',
-                '        <td><b>{}</b></td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
+                '        <td><b>{}</b></td>\n'.format(link(website + filename_from_name(puzzle['name']) + ".html", puzzle['name']+meta)),
                 '        <td></td>\n',
                 '      </tr>\n']
         expanding.append('    </table>\n')
@@ -248,7 +253,7 @@ def round_overview(rnd, puzzles):
              '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
              '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
              '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
-             '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
+             '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
              '                        <td>{}</td>\n'.format(puzzle['solution']),
             # '                        <td></td>\n',
              '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
@@ -258,7 +263,7 @@ def round_overview(rnd, puzzles):
              '                        <td><b>{}</b></td>\n'.format(elink(slack_url, puzzle['name']+meta)),
              '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
              '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
-             '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
+             '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
              '                        <td></td>\n',
             # '                        <td></td>\n',
              '                        <td>{}</td>\n'.format(" ".join(puzzle.get('tags',[]))),
@@ -269,7 +274,7 @@ def round_overview(rnd, puzzles):
     '    </body>\n',
     '</html>\n']
     html = start + puzzle_list + end
-    file = "{}_round.html".format('_'.join(rnd.split()))
+    file = "{}_round.html".format(filename_from_name(rnd))
     f = open(file, "w")
     for line in html:
         f.write(line)
@@ -286,7 +291,7 @@ def puzzle_overview(puzzle):
         meta = ''
     slack_url = channel_url(puzzle['channel_id'])
     if 'rounds' in puzzle:
-        round_url = [link(website + "_".join(rnd.split()) + "_round.html", rnd) for rnd in puzzle['rounds']]
+        round_url = [link(website + filename_from_name(rnd) + "_round.html", rnd) for rnd in puzzle['rounds']]
     else:
         round_url = ''
     if puzzle['status'] == 'solved':
@@ -330,9 +335,7 @@ def puzzle_overview(puzzle):
      '\n',
      '</body>\n',
      '</html>\n']
-    # Replace all spaces and slashes in the name with underscores
-    underscored = re.sub(r'[ /]', '_', name)
-    file = "{}.html".format(underscored)
+    file = "{}.html".format(filename_from_name(name))
     f = open(file, "w")
     for line in html:
         f.write(line)
@@ -382,7 +385,7 @@ def puzzle_lists(puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -391,7 +394,7 @@ def puzzle_lists(puzzles, filt):
          '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
          '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
          '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
-         '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
+         '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
          '                        <td>{}</td>\n'.format(puzzle['solution']),
          '                        <td>{}</td>\n'.format(round_url),
          '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
@@ -403,7 +406,7 @@ def puzzle_lists(puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -412,7 +415,7 @@ def puzzle_lists(puzzles, filt):
          '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
          '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
          '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
-         '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
+         '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
          '                        <td></td>\n',
          '                        <td>{}</td>\n'.format(round_url),
          '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),