Add error page generator

This commit is contained in:
schmelczerandras 2020-07-16 16:35:41 +02:00
parent 29879100bf
commit 0390c74061
2 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,43 @@
from typing import NamedTuple, Dict, List, Type
from os import makedirs, path
template_path = 'template.html'
result_path = 'built'
class Substitutions(NamedTuple):
title: str
description: str
error_messages: Dict[str, Substitutions] = {
'403': Substitutions(
title='403 - Forbidden',
description='You are not allowed to view this resource.'
),
'404': Substitutions(
title='404 - Not found',
description='The requested resource cannot be found.'
),
'50x': Substitutions(
title='50x - Server error',
description='It\'s my fault.'
),
}
def substitute(source: str, substitutions: Substitutions) -> str:
for i, property_name in enumerate(Substitutions._fields):
source = source.replace(f'{{{property_name}}}', str(substitutions[i]))
return source
if __name__ == '__main__':
with open(template_path) as f:
template = f.read()
makedirs(result_path, mode=0o440, exist_ok=True)
for name, substitutions in error_messages.items():
with open(path.join(result_path, f'{name}.html'), 'w') as f:
html = substitute(template, substitutions)
f.write(html)

View file

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{title}</title>
<meta name="theme-color" content="#b7455e" />
<meta name="viewport" content="initial-scale=1.0" />
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #b7455e;
}
h1 {
font-family: 'Roboto', 'Helvetica Neue', sans-serif;
font-weight: 100;
font-size: 4rem;
color: white;
text-align: center;
padding: 0.5rem;
}
</style>
</head>
<body>
<h1>{description}</h1>
</body>
</html>