| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | angular.module('app')
    .controller('HomeController', ['$scope', '$state',
        function($scope, $state) {
            $scope.Data = {
                Input: "",
                Output: "",
                Filename: "qcm.html"
            };
            $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.saveFile($scope.Data.Filename, $scope.Data.Output, "text/html");
            };
            $scope.setDraggedContent = function(data, file)
            {
                $scope.Data.Filename = file.name.replace(/\.tex$/, ".html");
                $scope.Data.Input = data;
                $scope.convert();
            };
            $scope.saveFile = function(filename, data, mime)
            {
                //this will remove the blank-spaces from the title and replace it with an underscore
                filename = filename.replace(/ /g,"_");
                if (navigator.msSaveBlob) {
                    navigator.msSaveBlob(new Blob(data, { type: mime + ';' }), filename);
                }
                else
                {
                    //Initialize file format you want csv or xls
                    var uri = 'data:' + mime + ',' + escape(data);
                    // Now the little tricky part.
                    // you can use either>> window.open(uri);
                    // but this will not work in some browsers
                    // or you will not get the correct file extension
                    //this trick will generate a temp <a /> tag
                    var link = document.createElement("a");
                    link.href = uri;
                    //set the visibility hidden so it will not effect on your web-layout
                    link.style.visibility = "hidden";
                    link.download = filename;
                    //this part will append the anchor tag and remove it after automatic click
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            };
            $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}", "À");
    }]);
 |