From d443dffc599f0f858068bd22dcf289fcbe4d64f3 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 12 Jan 2022 00:20:40 -0800 Subject: [PATCH] Abstract out the filename_from_name function 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 | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/html_generator.py b/html_generator.py index b944391..c16f033 100644 --- a/html_generator.py +++ b/html_generator.py @@ -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 += [ '