選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

home.controller.js 5.8KB

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