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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. var regex = macro.latex.match(/[a-zA-Z0-9]$/) != null ? "([^a-zA-Z0-9])" : "";
  35. text = text.replace(new RegExp($scope.escapeRegExp(macro.latex) + regex, "g"), function($1, $2)
  36. {
  37. return macro.html + $2;
  38. });
  39. });
  40. return text;
  41. };
  42. $scope.convert = function()
  43. {
  44. $scope.Data.Output = "";
  45. $scope.questions = [];
  46. var input = $scope.Data.Input;
  47. $scope.Data.Output = "";
  48. var questions = input.split("\\section");
  49. questions.splice(0, 1);
  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(/^\*\{([^\}]*)\}/, function($1, $2)
  59. {
  60. return $2 + "\n";
  61. });
  62. responses.splice(0, 1);
  63. responses.forEach(function(answerStr) {
  64. answerStr = answerStr.replace(/\\medskip/, "").trim();
  65. var a = answerStr.match(/^\[[^\]\[]+\]/)[0];
  66. answerStr = answerStr.substring(a.length, answerStr.length);
  67. answerStr = $scope.formatText(answerStr);
  68. question.Answers.push({
  69. Text: answerStr,
  70. Correct: a.length != 4
  71. })
  72. });
  73. $scope.questions.push(question);
  74. });
  75. $scope.questions.forEach(function(question)
  76. {
  77. $scope.Data.Output += question.Text + "\n";
  78. question.Answers.forEach(function(answer)
  79. {
  80. $scope.Data.Output += "\t[" + (answer.Correct ? "x" : "") + "]" + answer.Text + "\n";
  81. });
  82. $scope.Data.Output += "\n";
  83. });
  84. };
  85. $scope.setDraggedContent = function(data, file)
  86. {
  87. $scope.Data.Input = data;
  88. $scope.convert();
  89. };
  90. $scope.addMacro("\\RR", "\\mathbb{R}");
  91. $scope.addMacro("\\CC", "\\mathbb{C}");
  92. $scope.addMacro("\\QQ", "\\mathbb{Q}");
  93. $scope.addMacro("\\NN", "\\mathbb{N}");
  94. $scope.addMacro("\\ZZ", "\\mathbb{Z}");
  95. $scope.addMacro("\\di", "\\displaystyle");
  96. $scope.addMacro("\\dd", "\\text{d}");
  97. $scope.addMacro("<<", "&#171;");
  98. $scope.addMacro(">>", "&#187;");
  99. $scope.addMacro("<", "&#60; ");
  100. $scope.addMacro(">", "&#62;");
  101. $scope.addMacro("\\\'{E}", "&#201;");
  102. $scope.addMacro("\\\'{A}", "&#192;");
  103. }]);