angular.module('app') .controller('HomeController', ['$scope', '$state', function($scope, $state) { $scope.Data = { Input: "", Output: "" }; $scope.questions = []; $scope.macros = []; $scope.escapeRegExp = function(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }; $scope.addMacro = function(latex, html) { $scope.macros.push({ latex: latex, html: html }); }; $scope.formatText = function(text) { text = text.replace(/\$([^\$]+)\$/g, function($1, $2) { return "\\(" + $2 + "\\)"; }).replace(/\\medskip/g, "") .replace(/\\begin\{.*\}/g, "") .replace(/\\end\{.*\}/g, "") .replace(/\n|\r|\r\n|\n\r/g, " ") .replace(/ +/g, " ") .trim(); $scope.macros.forEach(function(macro) { var regex = macro.latex.match(/[a-zA-Z0-9]$/) != null ? "([^a-zA-Z0-9])" : ""; text = text.replace(new RegExp($scope.escapeRegExp(macro.latex) + regex, "g"), function($1, $2) { return macro.html + $2; }); }); return text; }; $scope.convert = function() { $scope.Data.Output = ""; $scope.questions = []; var input = $scope.Data.Input; $scope.Data.Output = ""; var questions = input.split("\\section"); questions.splice(0, 1); questions.forEach(function(questionStr) { var question = { Text: "", Answers: [] }; var responses = questionStr.split("\\item"); question.Text = $scope.formatText(responses[0]); question.Text = question.Text.replace(/^\*\{([^\}]*)\}/, function($1, $2) { return $2 + "\n"; }); responses.splice(0, 1); responses.forEach(function(answerStr) { answerStr = answerStr.replace(/\\medskip/, "").trim(); var a = answerStr.match(/^\[[^\]\[]+\]/)[0]; answerStr = answerStr.substring(a.length, answerStr.length); answerStr = $scope.formatText(answerStr); question.Answers.push({ Text: answerStr, Correct: a.length != 4 }) }); $scope.questions.push(question); }); $scope.questions.forEach(function(question) { $scope.Data.Output += question.Text + "\n"; question.Answers.forEach(function(answer) { $scope.Data.Output += "\t[" + (answer.Correct ? "x" : "") + "]" + answer.Text + "\n"; }); $scope.Data.Output += "\n"; }); }; $scope.setDraggedContent = function(data, file) { $scope.Data.Input = data; $scope.convert(); }; $scope.addMacro("\\RR", "\\mathbb{R}"); $scope.addMacro("\\CC", "\\mathbb{C}"); $scope.addMacro("\\QQ", "\\mathbb{Q}"); $scope.addMacro("\\NN", "\\mathbb{N}"); $scope.addMacro("\\ZZ", "\\mathbb{Z}"); $scope.addMacro("\\di", "\\displaystyle"); $scope.addMacro("\\dd", "\\text{d}"); $scope.addMacro("<<", "«"); $scope.addMacro(">>", "»"); $scope.addMacro("<", "< "); $scope.addMacro(">", ">"); $scope.addMacro("\\\'{E}", "É"); $scope.addMacro("\\\'{A}", "À"); }]);