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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.addMacro = function(latex, html)
  11. {
  12. $scope.macros.push({
  13. latex: latex,
  14. html: html
  15. });
  16. };
  17. $scope.formatText = function(text)
  18. {
  19. text = text.replace(/\$([^\$]+)\$/g, function($1, $2)
  20. {
  21. return "\\(" + $2 + "\\)";
  22. }).replace("\n", "");
  23. $scope.macros.forEach(function(macro)
  24. {
  25. text = text.replace(macro.latex, macro.html);
  26. });
  27. return text;
  28. };
  29. $scope.convert = function()
  30. {
  31. $scope.Data.Output = "";
  32. $scope.questions = [];
  33. var input = $scope.Data.Input;
  34. $scope.Data.Output = "";
  35. var questions = input.split("\\section");
  36. questions.splice(0, 1);
  37. questions.forEach(function(questionStr)
  38. {
  39. var question = {
  40. Text: "",
  41. Answers: []
  42. };
  43. var responses = questionStr.split("\\item");
  44. question.Text = responses[0].replace(/^\*\{.*\}/, "")
  45. .replace(/\\medskip/g, "")
  46. .replace(/\\begin\{.*\}/g, "")
  47. .trim();
  48. question.Text = $scope.formatText(question.Text);
  49. responses.splice(0, 1);
  50. responses.forEach(function(answerStr) {
  51. answerStr = answerStr.replace(/\\medskip/, "")
  52. .replace(/\\end\{.*\}/g, "")
  53. .trim();
  54. var a = answerStr.match(/^\[[^\]\[]+\]/)[0];
  55. answerStr = answerStr.substring(a.length, answerStr.length);
  56. answerStr = $scope.formatText(answerStr);
  57. question.Answers.push({
  58. Text: answerStr,
  59. Correct: a.length != 4
  60. })
  61. });
  62. $scope.questions.push(question);
  63. });
  64. $scope.questions.forEach(function(question)
  65. {
  66. $scope.Data.Output += question.Text + "\n";
  67. var goodAnswersCount = 0;
  68. question.Answers.forEach(function(answer)
  69. {
  70. if (answer.Correct) {
  71. ++goodAnswersCount;
  72. }
  73. });
  74. question.Answers.forEach(function(answer)
  75. {
  76. $scope.Data.Output += "\t" + (goodAnswersCount > 1 ? "[" : "(") + (answer.Correct ? "x" : "") +
  77. (goodAnswersCount > 1 ? "]" : ")") + answer.Text + "\n";
  78. });
  79. $scope.Data.Output += "\n";
  80. });
  81. };
  82. $scope.addMacro("\\RR", "\\mathbb{R}");
  83. $scope.addMacro("\\CC", "\\mathbb{C}");
  84. $scope.addMacro("\\QQ", "\\mathbb{Q}");
  85. $scope.addMacro("\\di", "\\displaystyle");
  86. $scope.addMacro("\\dd", "\\text{d}");
  87. $scope.addMacro("<<", "&#171");
  88. $scope.addMacro(">>", "&#187");
  89. $scope.addMacro("<", "&#60");
  90. $scope.addMacro(">", "&#62");
  91. $scope.addMacro("\\\'{E}", "&#201");
  92. $scope.addMacro("\\\'{A}", "&#192");
  93. }]);