You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

home.controller.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. angular.module('app')
  2. .controller('HomeController', ['$scope', '$state',
  3. function($scope, $state) {
  4. $scope.Data = {
  5. Input: "",
  6. Output: ""
  7. };
  8. $scope.questions = [];
  9. $scope.macros = [];
  10. $scope.escapeRegExp = function(str)
  11. {
  12. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  13. };
  14. $scope.addMacro = function(latex, html)
  15. {
  16. $scope.macros.push({
  17. latex: latex,
  18. html: html
  19. });
  20. };
  21. $scope.formatText = function(text)
  22. {
  23. text = text.replace(/\$([^\$]+)\$/g, function($1, $2)
  24. {
  25. return "\\(" + $2 + "\\)";
  26. }).replace(/\\medskip/g, "")
  27. .replace(/\\begin\{.*\}/g, "")
  28. .replace(/\\end\{.*\}/g, "")
  29. .replace(/\n|\r|\r\n|\n\r/g, " ")
  30. .replace(/ +/g, " ")
  31. .trim();
  32. $scope.macros.forEach(function(macro)
  33. {
  34. text = text.replace(new RegExp($scope.escapeRegExp(macro.latex) + "([^a-zA-Z])", "g"), function($1, $2)
  35. {
  36. return macro.html + $2;
  37. });
  38. });
  39. return text;
  40. };
  41. $scope.convert = function()
  42. {
  43. $scope.Data.Output = "";
  44. $scope.questions = [];
  45. var input = $scope.Data.Input;
  46. $scope.Data.Output = "";
  47. var questions = input.split("\\section");
  48. questions.splice(0, 1);
  49. var firstQuestion = null;
  50. questions.forEach(function(questionStr)
  51. {
  52. var question = {
  53. Text: "",
  54. Answers: []
  55. };
  56. var responses = questionStr.split("\\item");
  57. question.Text = $scope.formatText(responses[0]);
  58. question.Text = question.Text.replace(/^\*\{[^\}]* ([0-9]+)[^\}]*\}/, function($1, $2)
  59. {
  60. $2 = parseInt($2);
  61. if (firstQuestion == null) {
  62. firstQuestion = $2;
  63. }
  64. return "Question " + ($2 - firstQuestion + 1) + "\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. var goodAnswersCount = 0;
  83. question.Answers.forEach(function(answer)
  84. {
  85. if (answer.Correct) {
  86. ++goodAnswersCount;
  87. }
  88. });
  89. question.Answers.forEach(function(answer)
  90. {
  91. $scope.Data.Output += "\t" + (goodAnswersCount > 1 ? "[" : "(") + (answer.Correct ? "x" : "") +
  92. (goodAnswersCount > 1 ? "]" : ")") + answer.Text + "\n";
  93. });
  94. $scope.Data.Output += "\n";
  95. });
  96. };
  97. $scope.setDraggedContent = function(data, file)
  98. {
  99. $scope.Data.Input = data;
  100. $scope.convert();
  101. };
  102. $scope.addMacro("\\RR", "\\mathbb{R}");
  103. $scope.addMacro("\\CC", "\\mathbb{C}");
  104. $scope.addMacro("\\QQ", "\\mathbb{Q}");
  105. $scope.addMacro("\\di", "\\displaystyle");
  106. $scope.addMacro("\\dd", "\\text{d}");
  107. $scope.addMacro("<<", "&#171;");
  108. $scope.addMacro(">>", "&#187;");
  109. $scope.addMacro("<", "&#60;");
  110. $scope.addMacro(">", "&#62;");
  111. $scope.addMacro("\\\'{E}", "&#201;");
  112. $scope.addMacro("\\\'{A}", "&#192;");
  113. }]);