您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

home.controller.js 6.0KB

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