 ## initial_list = list(
 ##    stud_id = NULL,
 ##    stud_name = "FRED",
 ##    has_correct_filename = TRUE,
 ##    correct_filename = correct_file,
 ##    filename = correct_file,
 ##    can_source = TRUE,
 ##    env = NULL,
 ##    has_add_variables = FALSE,
 ##    is_binary = FALSE,
 ##    add_variables = vector(),
 ##    source_error = vector(),
 ##    code_tests = NULL
 ##    )

nl = function() "\\\\newline"
item = function() "\\\\item"

sanitize <- function(str) {
  result <- str
  result <- gsub("&", "\\\\&", result, fixed = TRUE)
  result <- gsub("_", "\\\\_", result, fixed = TRUE)
  result = gsub("{", "\\\\{", result, fixed = TRUE)
  result = gsub("\n", "\\\\newline", result, fixed = TRUE)
  result = gsub("}", "\\\\}", result, fixed = TRUE)  
  result = gsub("$", "\\\\$", result, fixed = TRUE)
  result = gsub(">", "\\$>\\$", result, fixed = TRUE)
  result = gsub("#", "\\\\#", result, fixed = TRUE)
  result = gsub("<", "\\$<\\$", result, fixed = TRUE)
  result = gsub("^", "\\\\^\\{\\}", result, fixed = TRUE)
  result = gsub("%", "\\\\%", result, fixed = TRUE)
  result = paste("\\\\texttt{", result, "}", sep="")
  result
}

sweaveCheckFilename = function(d) {
  if(d$has_correct_filename)
    return("File named correctly")
  comment  = paste("Correct filename was", sanitize(d$correct_filename))
  comment = paste(comment, ". However, your filename was ", sanitize(d$your_filename))
  #comment = paste(comment, ". Lose 10 marks.")
  return(comment)
}

sweaveCheckSource = function(d) {
  if(d$can_source)
    return("File can be loaded.")
  comment  = "Your code can not be loaded."
  comment = paste(comment, "When trying to load your code, we get the error:")
  comment = paste(comment, nl(), sanitize(d$source_error))

  return(comment)
}

sweaveCheckVariables = function(d) {
  if(!d$has_add_variables)return("")

  comment = paste(item(), "Your code included extraneous variables:")
  comment = paste(comment, sanitize(paste(d$add_variables, collapse=" ")))

  return(comment)
}

sweaveGetFunctionCall = function(d) {
#  d = dd[["tests"]][["MyMean"]][[1]]
  fun_args = lapply(d$args, paste, collapse=", ")
  fun_args_vec = unlist(lapply(fun_args,
    function(i) paste("c(", i, ")",sep="")))

  par_args = data.frame(names(fun_args_vec), as.vector(fun_args_vec))
  args = paste(apply(par_args, 1, paste, collapse="="), collapse=", ")
  fun_call = paste(d$fun_name, "(", args, ")", sep="")
  
  return(sanitize(fun_call))
}

sweaveDidFunctionRun = function(d) {
  if(d$can_run) return("Function can be called.")
  comment = paste("Your function could not be called. It returned the error",
    nl())
  comment = paste(comment, sanitize(d$run_error))
  return(comment)
}

sweaveDidFunctionWork = function(d) {
  if(!d$can_run) return("")

  if(d$is_correct) return(paste(item(), "Function gave correct result."))

  comment = paste(item(),"The correct answer was", sanitize(d$correct_answer))
  comment = paste(comment, "However, your answer was", sanitize(d$your_answer))
  return(comment)
}

sweaveEndDocument = function(d) {

  if(d$can_source)
    return("")
  comment = "File can not be run: 0 marks.\n"
  if(!d$is_binary)
    comment = paste(comment, sweavePrintCode(d))
  comment = paste(comment, "\n \\\\end\\{document\\}")
  return(comment)
  
}


sweavePrintCode = function(d) {
  path = paste(d$filename_dir, d$your_filename, sep="/")
  comment = paste("\\\\lstinputlisting\\{", path, "\\}", sep="")

  return(comment)
}



  
 
