You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
box-o-sand/mathpractice/html_formatter.py

74 lines
1.7 KiB

import textwrap
class HTMLFormatter(object):
STYLE = textwrap.dedent("""\
body {
color: black;
font-size: 18px;
font-family: Arial, sans-serif;
}
.ex {
display: inline;
text-align: right;
padding: 3px;
margin: 24px;
float: left;
}
.op {
padding-right: 18px;
}
.eq {
min-width: 48px;
border-bottom: 1px solid black;
}
""")
def __init__(self, style=''):
self.style = style if style != '' else self.STYLE
def format(self, sheet):
gen = [self._format_header(sheet.seed)]
for (f0, f1), answer in sheet.entries:
gen.append(self._format_entry(f0, f1, answer))
gen.append(self._format_footer())
return '\n'.join(gen)
def _format_header(self, seed):
return '\n'.join([
'<!DOCTYPE html>',
'<html>',
'<head>',
'<title>MATHS</title>',
'<!-- SEED={} -->'.format(seed),
'<style type="text/css">',
self.style,
'</style>',
'</head>',
'<body>'
])
def _format_entry(self, f0, f1, answer):
return '\n'.join([
'<div class="ex">',
'<p>',
'<span class="fac">{}</span>'.format(f0),
'</p>',
'<p class="eq">',
'<span class="op">x</span>',
'<span class="fac">{}</span>'.format(f1),
'</p>',
'<p>',
'<span class="ans" style="display:none">{}</span>'.format(answer),
'</p>',
'</div>'
])
def _format_footer(self):
return '</body></html>'