Example #1
0
def getExercise(exercise):
    '''returns an html-representation of an exercise'''
    result = '<h1>%s</h1>\n' % exercise['title']
    result += mdTeX2html.convert(exercise['exercise'])
    if exercise['solution'] != '':
        result += '<hr />\n'
        result += '<h1>Lösung</h1>\n'
        result += mdTeX2html.convert(exercise['solution'])
    if exercise['origin'] != '':
        result += '<p style="text-align:right;">Quelle: %s</p>\n' % exercise['origin']
    if exercise['comment'] != '':
        result += '<hr />\n'
        result += '<h2>Bemerkungen</h2>\n'
        result += mdTeX2html.convert(exercise['comment'])
    result += '<p style="text-align:right;" class="no-print"><a href="../exerciseEdit/%s">Aufgabe bearbeiten</a></p>\n' % exercise['id']
    return result
Example #2
0
    def do_POST(self):
        ''' all operations on an exercise like editing is done via POST-Requests '''
        self._set_headers()
        postvars = self.parse_POST()

        # path-switch
        if self.path == '/mdtex2html':
            try:
                result = mdTeX2html.convert(postvars)
            except Exception as e:
                result = 'ERROR: Could not convert the mdTeX to HTML:' + str(e)
        elif self.path == '/getExerciseJson':
            e = db.getExercise(postvars)
            result = json.dumps(e)
        elif self.path == '/getTopicsJson':
            t = db.getTopics()
            result = json.dumps(t)
        elif self.path == '/getLicensesJson':
            l = db.getLicenses()
            result = json.dumps(l)
        elif self.path == '/saveExercise':
            print(postvars)
            if postvars['id'] == '':
                result = str(db.insertExercise(postvars))
            elif postvars['id'].isnumeric():
                result = db.editExercise(postvars)
        self.wfile.write(bytes(result, "utf8"))
        return
async def register(websocket):
    USERS.add(websocket)
    print('user added, ' + str(len(USERS))+ ' user(s) online')
    html = mdTeX2html.convert(mdtex.value)
    replyd = {'mdtex': mdtex.value, 'html': html}
    replyj = json.dumps(replyd)
    await asyncio.wait([user.send(replyj) for user in USERS])
Example #4
0
def getTest():
    '''returns a Testpage, rendered from the test.mdtex in test folder'''
    with open('test/test.mdtex', 'r') as f:
        mdtex = f.read()
    result = mdTeX2html.convert(mdtex)
    result += '<br>\n'
    result += 'Webrequest-Test:<br>\n'
    result += '<div id="requestbox">Ursprünglicher Requestbox-Testinhalt</div>\n'
    result += '<button onClick="request();">request-test</button>\n'
    return result
Example #5
0
def getSheet(title, exercises, option):
    ''' returns a sheet with exercises/solutions/source according to options '''
    result = '<h1>%s</h1>' % title
    i = 1
    if option=='exercises' or option=='':
        for ex in exercises:
            result += '<h2>Aufgabe %s: %s</h2>' % (str(i), ex['title'])
            result += mdTeX2html.convert(ex['exercise'])
            i+=1
    elif option == 'solutions':
        for ex in exercises:
            result += '<h2>Aufgabe %s: %s</h2>' % (str(i), ex['title'])
            result += mdTeX2html.convert(ex[5])
            i+=1
    elif option == 'source':
        for ex in exercises:
            result += '<pre>'
            result += '# '+ex['title']+'<br />\n'
            result += ex['exercise']
            result += '</pre><br />\n'
    else:
        result = '<p style="color:red;">ERROR 404: Illegal URL, I didn\'t understand your option!</p>'
    return result
async def wsHandler(websocket, path):
    # Register.
    await register(websocket)
    try:
        async for message in websocket:
            mdtex.value = message
            html = mdTeX2html.convert(message)
            replyd = {'mdtex': message, 'html': html}
            replyj = json.dumps(replyd)
            if USERS:  # asyncio.wait doesn't accept an empty list
                await asyncio.wait([user.send(replyj) for user in USERS])
    except:
        pass
    finally:
        await unregister(websocket)