Add script: header to .moth files

This commit is contained in:
Neale Pickett 2017-10-23 10:01:11 -06:00
parent 6dfb5618a3
commit 242caa87cb
4 changed files with 32 additions and 7 deletions

View File

@ -1,5 +1,8 @@
FROM alpine
ARG http_proxy
ENV http_proxy=${http_proxy}
RUN apk --no-cache add python3 py3-pillow
COPY tools/devel-server.py /moth/

View File

@ -31,12 +31,13 @@ sys.dont_write_bytecode = True
# XXX: This will eventually cause a problem. Do something more clever here.
seed = 1
def page(title, body):
def page(title, body, scripts=[]):
return """<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<link rel="stylesheet" href="/files/src/www/res/style.css">
{scripts}
</head>
<body>
<h1>{title}</h1>
@ -44,17 +45,21 @@ def page(title, body):
{body}
</div>
</body>
</html>""".format(title=title, body=body)
</html>""".format(
title=title,
body=body,
scripts="\n".join('<script src="{}"></script>'.format(s) for s in scripts),
)
def mdpage(body):
def mdpage(body, scripts=[]):
try:
title, _ = body.split('\n', 1)
except ValueError:
title = "Result"
title = title.lstrip("#")
title = title.strip()
return page(title, mistune.markdown(body, escape=False))
return page(title, mistune.markdown(body, escape=False), scripts=scripts)
# XXX: What horrors did we unleash with our chdir shenanigans that
@ -123,6 +128,7 @@ you are a fool.
body = io.StringIO()
path = self.path.rstrip('/')
parts = path.split("/")
scripts = []
title = None
fpath = None
points = None
@ -156,6 +162,7 @@ you are a fool.
body.write("</ul>")
elif len(parts) == 4:
# Serve up a puzzle
scripts = puzzle.scripts
title = "{} puzzle {}".format(parts[2], parts[3])
body.write("<h2>Body</h2>")
body.write(puzzle.html_body())
@ -200,7 +207,7 @@ you are a fool.
shutil.copyfileobj(pfile.stream, self.wfile)
return
payload = page(title, body.getvalue()).encode('utf-8')
payload = page(title, body.getvalue(), scripts=scripts).encode('utf-8')
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", len(payload))

View File

@ -73,6 +73,7 @@ class Puzzle:
self.summary = None
self.authors = []
self.answers = []
self.scripts = []
self.files = {}
self.body = io.StringIO()
self.logs = []
@ -112,6 +113,11 @@ class Puzzle:
except IndexError:
pass
self.files[name] = PuzzleFile(stream, name, not hidden)
elif key == 'script':
stream = open(val, 'rb')
# Make sure this shows up in the header block of the HTML output.
self.files[val] = PuzzleFile(stream, val, visible=False)
self.scripts.append(val)
else:
raise ValueError("Unrecognized header field: {}".format(key))
else:

View File

@ -50,6 +50,7 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
''' </ul>
</section>
''')
scripts = ['<script src="{}"></script>'.format(s) for s in puzzle.scripts]
html_content.write(
'''<!DOCTYPE html>
@ -59,6 +60,7 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
<meta name="viewport" content="width=device-width">
<title>{category} {points}</title>
<link rel="stylesheet" href="../../style.css">
{scripts}
</head>
<body>
<h1>{category} for {points} points</h1>
@ -80,7 +82,14 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
<img src="../../images/sandia.png" alt="Sandia National Laboratories">
</section>
</body>
</html>'''.format(category=category, points=points, body=puzzle.html_body(), file_content=file_content.getvalue(), authors=', '.join(authors)))
</html>'''.format(
category=category,
points=points,
body=puzzle.html_body(),
file_content=file_content.getvalue(),
authors=', '.join(authors)),
scripts='\n'.join(scripts),
)
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())
def build_category(categorydir, outdir):