Browse Source

macros replacement; output string generation

tags/1.0.0
Robin Thoni 8 years ago
parent
commit
e5d4ce9553
2 changed files with 53 additions and 9 deletions
  1. 51
    7
      app/controllers/home.controller.js
  2. 2
    2
      app/views/home.html

+ 51
- 7
app/controllers/home.controller.js View File

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

+ 2
- 2
app/views/home.html View File

21
                     </ul>
21
                     </ul>
22
                 </div>
22
                 </div>
23
             </div>
23
             </div>
24
-            <!--<div class="form-group">
24
+            <div class="form-group">
25
                 <label for="Output">Output:</label>
25
                 <label for="Output">Output:</label>
26
                 <textarea id="Output" rows="10" class="form-control" ng-model="Data.Output"></textarea>
26
                 <textarea id="Output" rows="10" class="form-control" ng-model="Data.Output"></textarea>
27
-            </div>-->
27
+            </div>
28
         </div>
28
         </div>
29
     </div>
29
     </div>
30
 </div>
30
 </div>

Loading…
Cancel
Save