#!/bin/python3
# $Id: outline2page.py,v 1.3 2023/10/30 11:29:29 leavens Exp leavens $
import csv
import sys

USAGE='Usage: outline2page filename'
NUM_ARGS = 1

WCOURSE="https://webcourses.ucf.edu/courses/1437151/"
WCFILES=WCOURSE+"files/100640713/"
APIFILES=WCOURSE + '/api/v1/courses/1437151/' + WCFILES

def usage():
    """Print a usage message on stderr"""
    sys.stderr.write(USAGE+'\n')

def headingpattern(ln):
    if ln.startswith('*'):
          lvl = ln.count('*')
          return lvl < len(ln) and ln[lvl] == ' '
    else:
          return False

def heading_level(ln):
    return ln.count('*')

# Requires ln is a heading
# Return the text of ln after the first blank
def heading_text(ln):
    firstBlank = ln.find(' ')
    return ln[firstBlank+1:len(ln)-1]

def print_heading(level, text):
    if level <= 4:
        level_end = "h" + str(level) + ">"
        print("<" + level_end + text + "</" + level_end)
    else:
        print("<p><strong>" + text + "</strong></p>")

def print_question(qtext):
    print('<p><img id="98820219" src="' + str(WCFILES) +
          'preview" alt="Start of important question" ' +
          'width="42" height="50" data-api-endpoint="' +
          str(APIFILES) + '" data-api-returntype="File" /> <strong>' +
          str(qtext) + '</strong></p> <p>')

def slidepattern(ln):
    return ln.startswith('-------------')

def blanklinepattern(ln):
    for c in ln:
        if c != ' ':
           return False
    return True
    
# does ln contain a question?
def questionpattern(ln):
    qidx = ln.find('Q: ')
    return qidx != -1

# Requires: ln contains 'Q: '          
def question_text(ln):
    qidx = ln.find('Q: ')
    if qidx != -1:
        return ln[qidx+3:len(ln)-1]
    else:
        return ln
          
def sanitize(str):
    ret=''
    for c in str:
       if c == '<':
            ret=ret+'&lt;'
       elif c == '>':
            ret=ret+'&gt;'
       else:
            ret=ret+c
    return ret

def processIt(inputfile):
    inSlide = False
    for ln in inputfile.readlines():
        if inSlide:
            if slidepattern(ln):
                # if seeing the slide pattern, signals end of slide
                print("</pre>")
                inSlide = False
            else:
                # inside slides, print the lines
                print(sanitize(ln), end='')
        elif slidepattern(ln):
           print("<pre>")
           inSlide = True
        elif headingpattern(ln):
           print_heading(heading_level(ln)+1, heading_text(ln))
        elif blanklinepattern(ln):
           print("</p> <p>")
        elif questionpattern(ln):
           print_question(question_text(ln))
        else:
           # nothing special, print the line
           print(sanitize(ln), end='')

# The following happens when this module is run as main
if __name__ == '__main__':
    if len(sys.argv) == NUM_ARGS+1:
        # generate the output
        processIt(open(sys.argv[1], "r"))
        exit(0)
    else:
        # give a usage message and exit
        usage()
        exit(2)
