Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

home.controller.js 4.2KB

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