qstohtml
Created Tuesday 07 April 2020
#!/bin/bash
# requires 'recode?'
# CHANGE ME AS NEEDED
# --- Title of Quiz ---
title="LIS 5417 Quiz  exercise"
# --- the questions in the right format ---
questionsfile="$1"
# --- the html file that will be created
output="./output.html"
# -- the results page. -- output file above will link to this 
formaction="./results.php"  # NOTE NOTE NOTE -- this will create a file, give it a good name
### THE FORMAT:
#   Do  '!'  for a section header
#    Prepend a new question with a '#',  then do possible answers directly below, line by line -- NO NEWLINES BETWEEN (a newline will "reset" the question
#    Optionally, do '%' for text question, but these will not be "graded" in the system. 
# Set all counts to 0
qcount=0
acount="a"
# function to print the questions and question-headers
function printquestions() {
while read line; do
	newline=$(echo $line | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g' )
	line=$newline
	if  [ "$line" == "" ]; then  # it's a blank line 
		echo "<br><br>"
		acount="a"
	elif grep --quiet '^!' <<< $line ; then # its a heading and should be printed
		linetext=$(echo $line | tr "!" " ")
		echo "<h2> <b> $linetext </b></h2>"
		acount="a"	
	elif grep --quiet '^#' <<< $line ; then # its a NEW question
		((qcount++))
		acount="a"
		qtext="$qcount. ${line:1}"
		echo "<b>$qtext</b><br>"
	elif grep --quiet '^@' <<< $line ; then # it's a fill-in-the-blank NEW QUESTION
		        ((qcount++))
			acount="a"
			qtext="$qcount. ${line:1}"
			echo "<b>$qtext</b><br>"
			echo "<input type='text' name='q$qcount'><br>"
	elif grep --quiet '^%' <<< $line ; then # it's LONGER ANSWER
		        ((qcount++))
			acount="a"
			qtext="$qcount. ${line:1}"
			echo "<b>$qtext</b><br>"
			echo "<textarea rows="4" cols="50" name='q$qcount' ></textarea>"
			
	else 				# its an answer choice BUT IM NOT USING IT NOW
		if [[ $acount == "a" ]]; then
			checked="checked='checked'"
		else
			checked=""
		fi
		echo "<input type='radio' name='q$qcount' value='$acount'>$acount.  $line<br>"	
		acount=$(echo "$acount" | tr "a-y" "b-z")
	fi
done < $1
}
# outputting the actual quiz
# boilerplate
cat "./quizstart.html" > $output
echo "<h1> $title </h1>" >> $output
# start of form
echo "<form action='$formaction' method='post'>" >> $output
# What s your name
echo "<br>FSUID:<input type='text' name="fsuid" ></input><br>" >> $output
# da questions, yall
printquestions "$questionsfile" >> $output
# end of form
echo "<input type='submit' value='Submit'> </form>" >> $output
cat "./quizend.html" >> $output