From 334e581818d613a92a2f931d09ea631b907fd87c Mon Sep 17 00:00:00 2001 From: Aaron Scott Pope Date: Thu, 19 Jan 2017 16:50:21 -0700 Subject: [PATCH] Hacky puzzle html generation. I make no promises. --- tools/package-puzzles.py | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tools/package-puzzles.py b/tools/package-puzzles.py index 90b248b..f852d6d 100755 --- a/tools/package-puzzles.py +++ b/tools/package-puzzles.py @@ -31,6 +31,57 @@ def write_kv_pairs(ziphandle, filename, kv): filehandle.write("%s: %s%s" % (key, kv[key], os.linesep)) filehandle.seek(0) ziphandle.writestr(filename, filehandle.read()) + +def escape(s): + return s.replace('&', '&').replace('<', '<').replace('>', '>') + +def generate_html(ziphandle, puzzle, puzzledir, category, points, author, files): + html_content = io.StringIO() + file_content = io.StringIO() + if files: + file_content.write( +'''
+

Associated files:

+ +
+''') + + html_content.write( +''' + + + + + {category} {points} + + + +

{category} for {points} points

+
+{body}
+{file_content}
+
+ + +
Team hash:
+
Answer:
+ +
+
+
Puzzle by {author}
+
+ Los Alamos National Laboratory + US Department Of Energy + Sandia National Laboratories +
+ +'''.format(category=category, points=points, body=puzzle.html_body(), file_content=file_content.getvalue(), author=author)) + ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue()) def build_category(categorydir, outdir): zipfileraw = tempfile.NamedTemporaryFile(delete=False) @@ -88,6 +139,7 @@ def build_category(categorydir, outdir): } puzzlejson = json.dumps(puzzledict) zf.writestr(os.path.join(puzzledir, 'puzzle.json'), puzzlejson) + generate_html(zf, puzzle, puzzledir, categoryname, puzzle.points, puzzle.author, files) write_kv_pairs(zf, 'map.txt', mapping) write_kv_pairs(zf, 'answers.txt', answers)