Skip to content

Commit

Permalink
radius: get page title from markdown/filename
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmarquis committed Dec 27, 2024
1 parent 99f8562 commit a5c1f50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion orbit/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="manifest" href="/assets/site.webmanifest" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>KDLP</title>
<title>TITLE_STR</title>
</head>
<body>
<div class="logo">
Expand Down
43 changes: 28 additions & 15 deletions orbit/radius.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import subprocess
import sys
import secrets
import re
from http import HTTPStatus, cookies
from datetime import datetime, timedelta
from urllib.parse import parse_qs, urlparse
Expand Down Expand Up @@ -252,14 +253,16 @@ def retire(self):
self._session.end()
self.headers += self._session.mk_cookie_header()

def format_html(self, doc):
def format_html(self, doc, title):
# loads cookie if exists
self.session
return html_header + doc + f"""
page_header = html_header.replace("TITLE_STR", title)
return page_header + doc + f"""
<hr>
<code>msg = {self._msg}</code><br>
<code>whoami = {self.username}</code><br>
<code>{config.version_info}</code><br>
<code>dbg = {str(type(html_header))}</code><br>
<hr>
</body>
</html>
Expand All @@ -270,9 +273,11 @@ def raw_respond(self, response_code, body=b''):
self.headers)
return [body]

def respond(self, response_document):
def respond(self, response_document, title=None):
if title is None:
title = "KDLP"
self.headers += [('Content-Type', 'text/html')]
response_document = self.format_html(response_document)
response_document = self.format_html(response_document, title)
return self.raw_respond(HTTPStatus.OK, response_document.encode())


Expand Down Expand Up @@ -329,11 +334,11 @@ def respond(welcome):
rocket.headers += [('Location', target)]
return rocket.raw_respond(HTTPStatus.SEE_OTHER)
elif target:
return rocket.respond(login_form(target_location=target))
return rocket.respond(login_form(target_location=target), "Login")
elif welcome:
return rocket.respond(mk_form_welcome(rocket.session))
return rocket.respond(mk_form_welcome(rocket.session), "Welcome")
else:
return rocket.respond(login_form())
return rocket.respond(login_form(), "Login")

if rocket.session:
rocket.msg(f'{rocket.username} authenticated by token')
Expand Down Expand Up @@ -410,7 +415,7 @@ def submission_fields(sub):
</tr>
<tr>{table_content}</tr>
</table>
""")
""", "Activity Log")


class OopsStatus:
Expand Down Expand Up @@ -560,7 +565,7 @@ def handle_dashboard(rocket):
<button type="submit" name="confirm" value="y">Confirm</button>
<br><br>
</form>
''')
''', "Confirm Oopsie")
try:
db.Oopsie.create(user=rocket.session.username, assignment=asn,
timestamp=int(now))
Expand Down Expand Up @@ -592,7 +597,7 @@ def handle_dashboard(rocket):
final = asn_gradeables.where(grd_tbl.component == 'final').first()
ret += str(AsmtTable(assignment, oopsieness, peer1, peer2, init, rev1,
rev2, final))
return rocket.respond(ret + '</form>')
return rocket.respond(ret + '</form>', "Dashboard")


def find_creds_for_registration(student_id):
Expand All @@ -618,7 +623,7 @@ def form_respond():
<label for="student_id">Student ID:</label>
<input name="student_id" type="text" id="student_id" /><br />
<button type="submit">Submit</button>
</form>''')
</form>''', "Register")

if rocket.method != 'POST':
return form_respond()
Expand All @@ -633,7 +638,7 @@ def form_respond():
return rocket.respond(f'''
<h1>Save these credentials, you will not be able to access them again</h1><br>
<h3>Username: {username}</h3><br>
<h3>Password: {password}</h3><br>''')
<h3>Password: {password}</h3><br>''', "New User")


def determine_cache_entry(cred_str):
Expand Down Expand Up @@ -708,7 +713,7 @@ def cgit_internal_server_error(msg):
if raw_return:
return rocket.raw_respond(status, raw_body)
outstring = raw_body.decode()
return rocket.respond(outstring)
return rocket.respond(outstring, "cgit")
except (UnicodeDecodeError, ValueError, IndexError) as ex:
return cgit_internal_server_error(type(ex))

Expand All @@ -723,7 +728,7 @@ def handle_error(rocket):
return rocket.raw_respond(HTTPStatus.INTERNAL_SERVER_ERROR)
error_description = (f'<h1>HTTP ERROR {error.value}: '
f'{error.name.upper().replace("_", " ")}</h1>')
return rocket.respond(error_description)
return rocket.respond(error_description, f"ERROR {error.value}")


def handle_try_md(rocket):
Expand All @@ -736,7 +741,15 @@ def handle_try_md(rocket):
md = file.read()
html = markdown.markdown(md, extensions=['tables', 'fenced_code',
'footnotes', 'toc'])
return rocket.respond(html)
# Find the first h2 in the markdown file
regex = re.compile(r'\#\# .+\n')
title = regex.search(md)
if title is not None:
title = title.group(0).replace('\n', '').replace('## ', '')
else:
# If the .md file doesn't have a leading h2 title, just use the basename
title = rocket.path_info[rocket.path_info.rfind('/') + 1 : rocket.path_info.rfind('.')]
return rocket.respond(html, title)


def application(env, SR):
Expand Down

0 comments on commit a5c1f50

Please sign in to comment.