mirror of https://github.com/dirtbags/moth.git
Add script: header to .moth files
This commit is contained in:
parent
6dfb5618a3
commit
242caa87cb
|
@ -1,5 +1,8 @@
|
||||||
FROM alpine
|
FROM alpine
|
||||||
|
|
||||||
|
ARG http_proxy
|
||||||
|
ENV http_proxy=${http_proxy}
|
||||||
|
|
||||||
RUN apk --no-cache add python3 py3-pillow
|
RUN apk --no-cache add python3 py3-pillow
|
||||||
|
|
||||||
COPY tools/devel-server.py /moth/
|
COPY tools/devel-server.py /moth/
|
||||||
|
|
|
@ -31,12 +31,13 @@ sys.dont_write_bytecode = True
|
||||||
# XXX: This will eventually cause a problem. Do something more clever here.
|
# XXX: This will eventually cause a problem. Do something more clever here.
|
||||||
seed = 1
|
seed = 1
|
||||||
|
|
||||||
def page(title, body):
|
def page(title, body, scripts=[]):
|
||||||
return """<!DOCTYPE html>
|
return """<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
<link rel="stylesheet" href="/files/src/www/res/style.css">
|
<link rel="stylesheet" href="/files/src/www/res/style.css">
|
||||||
|
{scripts}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{title}</h1>
|
<h1>{title}</h1>
|
||||||
|
@ -44,17 +45,21 @@ def page(title, body):
|
||||||
{body}
|
{body}
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</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:
|
try:
|
||||||
title, _ = body.split('\n', 1)
|
title, _ = body.split('\n', 1)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
title = "Result"
|
title = "Result"
|
||||||
title = title.lstrip("#")
|
title = title.lstrip("#")
|
||||||
title = title.strip()
|
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
|
# XXX: What horrors did we unleash with our chdir shenanigans that
|
||||||
|
@ -123,6 +128,7 @@ you are a fool.
|
||||||
body = io.StringIO()
|
body = io.StringIO()
|
||||||
path = self.path.rstrip('/')
|
path = self.path.rstrip('/')
|
||||||
parts = path.split("/")
|
parts = path.split("/")
|
||||||
|
scripts = []
|
||||||
title = None
|
title = None
|
||||||
fpath = None
|
fpath = None
|
||||||
points = None
|
points = None
|
||||||
|
@ -156,6 +162,7 @@ you are a fool.
|
||||||
body.write("</ul>")
|
body.write("</ul>")
|
||||||
elif len(parts) == 4:
|
elif len(parts) == 4:
|
||||||
# Serve up a puzzle
|
# Serve up a puzzle
|
||||||
|
scripts = puzzle.scripts
|
||||||
title = "{} puzzle {}".format(parts[2], parts[3])
|
title = "{} puzzle {}".format(parts[2], parts[3])
|
||||||
body.write("<h2>Body</h2>")
|
body.write("<h2>Body</h2>")
|
||||||
body.write(puzzle.html_body())
|
body.write(puzzle.html_body())
|
||||||
|
@ -200,7 +207,7 @@ you are a fool.
|
||||||
shutil.copyfileobj(pfile.stream, self.wfile)
|
shutil.copyfileobj(pfile.stream, self.wfile)
|
||||||
return
|
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_response(HTTPStatus.OK)
|
||||||
self.send_header("Content-Type", "text/html; charset=utf-8")
|
self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||||
self.send_header("Content-Length", len(payload))
|
self.send_header("Content-Length", len(payload))
|
||||||
|
|
|
@ -73,6 +73,7 @@ class Puzzle:
|
||||||
self.summary = None
|
self.summary = None
|
||||||
self.authors = []
|
self.authors = []
|
||||||
self.answers = []
|
self.answers = []
|
||||||
|
self.scripts = []
|
||||||
self.files = {}
|
self.files = {}
|
||||||
self.body = io.StringIO()
|
self.body = io.StringIO()
|
||||||
self.logs = []
|
self.logs = []
|
||||||
|
@ -112,6 +113,11 @@ class Puzzle:
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
self.files[name] = PuzzleFile(stream, name, not hidden)
|
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:
|
else:
|
||||||
raise ValueError("Unrecognized header field: {}".format(key))
|
raise ValueError("Unrecognized header field: {}".format(key))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -50,6 +50,7 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
|
||||||
''' </ul>
|
''' </ul>
|
||||||
</section>
|
</section>
|
||||||
''')
|
''')
|
||||||
|
scripts = ['<script src="{}"></script>'.format(s) for s in puzzle.scripts]
|
||||||
|
|
||||||
html_content.write(
|
html_content.write(
|
||||||
'''<!DOCTYPE html>
|
'''<!DOCTYPE html>
|
||||||
|
@ -59,6 +60,7 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
<title>{category} {points}</title>
|
<title>{category} {points}</title>
|
||||||
<link rel="stylesheet" href="../../style.css">
|
<link rel="stylesheet" href="../../style.css">
|
||||||
|
{scripts}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{category} for {points} points</h1>
|
<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">
|
<img src="../../images/sandia.png" alt="Sandia National Laboratories">
|
||||||
</section>
|
</section>
|
||||||
</body>
|
</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())
|
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())
|
||||||
|
|
||||||
def build_category(categorydir, outdir):
|
def build_category(categorydir, outdir):
|
||||||
|
|
Loading…
Reference in New Issue