Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

home.controller.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. angular.module('app')
  2. .controller('HomeController', ['$scope', '$state',
  3. function($scope, $state) {
  4. $scope.Data = {
  5. Input: "",
  6. Output: "",
  7. Filename: "qcm.html"
  8. };
  9. $scope.questions = [];
  10. $scope.macros = [];
  11. $scope.escapeRegExp = function(str)
  12. {
  13. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  14. };
  15. $scope.addMacro = function(latex, html)
  16. {
  17. $scope.macros.push({
  18. latex: latex,
  19. html: html
  20. });
  21. };
  22. $scope.formatText = function(text)
  23. {
  24. text = text.replace(/\$\$([^\$]+)\$\$/g, function($1, $2)
  25. {
  26. return "\\(" + $2 + "\\)";
  27. }).replace(/\$([^\$]+)\$/g, function($1, $2)
  28. {
  29. return "\\(" + $2 + "\\)";
  30. }).replace(/\\medskip/g, "")
  31. .replace(/\\begin\{.*\}/g, "")
  32. .replace(/\\end\{.*\}/g, "")
  33. .replace(/\n|\r|\r\n|\n\r/g, " ")
  34. .replace(/ +/g, " ")
  35. .trim();
  36. $scope.macros.forEach(function(macro)
  37. {
  38. var regex = macro.latex.match(/[a-zA-Z0-9]$/) != null ? "([^a-zA-Z0-9])" : "()";
  39. text = text.replace(new RegExp($scope.escapeRegExp(macro.latex) + regex, "g"), function($1, $2)
  40. {
  41. return macro.html + $2;
  42. });
  43. });
  44. return text;
  45. };
  46. $scope.convert = function()
  47. {
  48. $scope.Data.Output = "";
  49. $scope.questions = [];
  50. var input = $scope.Data.Input;
  51. $scope.Data.Output = "";
  52. var questions = input.split("\\section");
  53. questions.splice(0, 1);
  54. questions.forEach(function(questionStr)
  55. {
  56. var question = {
  57. Text: "",
  58. Answers: []
  59. };
  60. var responses = questionStr.split("\\item");
  61. question.Text = $scope.formatText(responses[0]);
  62. question.Text = question.Text.replace(/^\*\{([^\}]*)\}/, function($1, $2)
  63. {
  64. return $2 + "\n";
  65. });
  66. responses.splice(0, 1);
  67. responses.forEach(function(answerStr) {
  68. answerStr = answerStr.replace(/\\medskip/, "").trim();
  69. var a = answerStr.match(/^\[[^\]\[]+\]/)[0];
  70. answerStr = answerStr.substring(a.length, answerStr.length);
  71. answerStr = $scope.formatText(answerStr);
  72. question.Answers.push({
  73. Text: answerStr,
  74. Correct: a.length != 4
  75. })
  76. });
  77. $scope.questions.push(question);
  78. });
  79. $scope.questions.forEach(function(question)
  80. {
  81. $scope.Data.Output += question.Text + "\n";
  82. question.Answers.forEach(function(answer)
  83. {
  84. $scope.Data.Output += "\t[" + (answer.Correct ? "x" : "") + "]" + answer.Text + "\n";
  85. });
  86. $scope.Data.Output += "\n";
  87. });
  88. $scope.saveFile($scope.Data.Filename, $scope.Data.Output, "text/html");
  89. };
  90. $scope.setDraggedContent = function(data, file)
  91. {
  92. $scope.Data.Filename = file.name.replace(/\.tex$/, ".html");
  93. $scope.Data.Input = data;
  94. $scope.convert();
  95. };
  96. $scope.saveFile = function(filename, data, mime)
  97. {
  98. //this will remove the blank-spaces from the title and replace it with an underscore
  99. filename = filename.replace(/ /g,"_");
  100. if (navigator.msSaveBlob) {
  101. navigator.msSaveBlob(new Blob(data, { type: mime + ';' }), filename);
  102. }
  103. else
  104. {
  105. //Initialize file format you want csv or xls
  106. var uri = 'data:' + mime + ',' + escape(data);
  107. // Now the little tricky part.
  108. // you can use either>> window.open(uri);
  109. // but this will not work in some browsers
  110. // or you will not get the correct file extension
  111. //this trick will generate a temp <a /> tag
  112. var link = document.createElement("a");
  113. link.href = uri;
  114. //set the visibility hidden so it will not effect on your web-layout
  115. link.style.visibility = "hidden";
  116. link.download = filename;
  117. //this part will append the anchor tag and remove it after automatic click
  118. document.body.appendChild(link);
  119. link.click();
  120. document.body.removeChild(link);
  121. }
  122. };
  123. $scope.addMacro("\\RR", "\\mathbb{R}");
  124. $scope.addMacro("\\CC", "\\mathbb{C}");
  125. $scope.addMacro("\\QQ", "\\mathbb{Q}");
  126. $scope.addMacro("\\NN", "\\mathbb{N}");
  127. $scope.addMacro("\\ZZ", "\\mathbb{Z}");
  128. $scope.addMacro("\\di", "\\displaystyle");
  129. $scope.addMacro("\\dd", "\\text{d}");
  130. $scope.addMacro("<<", "&#171;");
  131. $scope.addMacro(">>", "&#187;");
  132. $scope.addMacro("<", "&#60; ");
  133. $scope.addMacro(">", "&#62;");
  134. $scope.addMacro("\\\'{E}", "&#201;");
  135. $scope.addMacro("\\`{A}", "&#192;");
  136. }]);