From e45b23f373c9ba41a31aa9d72d5b5fa69428499c Mon Sep 17 00:00:00 2001
From: "Tate, Hongliang Tian" <tatetian@gmail.com>
Date: Wed, 11 Mar 2015 16:40:56 +0800
Subject: [PATCH] Move call into open/close text

---
 src/Parser.js          | 30 ++++++++++++++--------------
 src/Renderer.js        | 44 ++++++++++++++++++++++--------------------
 static/test-suite.html | 10 ----------
 3 files changed, 38 insertions(+), 46 deletions(-)

diff --git a/src/Parser.js b/src/Parser.js
index 96a2bbb..7382674 100644
--- a/src/Parser.js
+++ b/src/Parser.js
@@ -20,8 +20,8 @@
  *     <require>       :== \REQUIRE + <open-text>
  *     <ensure>        :== \ENSURE + <open-text>
  *
- *     <block>         :== ( <control> | <function>
- *                         | <statement> | <comment> | <call> )[0..n]
+ *     <block>         :== ( <control> | <function> | <statement> |
+ *                           <comment> )[0..n]
  *
  *     <control>       :== <if> | <for> | <while>
  *     <if>            :== \IF{<cond>} + <block>
@@ -41,16 +41,17 @@
  *
  *     <comment>       :== \COMMENT{<close-text>}
  *
- *     <call>          :== \CALL{<name>}({<close-text>})
- *
  *     <cond>          :== <close-text>
- *     <open-text>     :== <atom> + <open-text> | { <close-text> } | <empty>
- *     <close-text>    :== <atom> + <close-text> | { <close-text> } | <empty>
+ *     <open-text>     :== ( <atom> | <call> ) + <open-text> |
+ *                         { <close-text> } | <empty>
+ *     <close-text>    :== ( <atom> | <call> ) + <close-text> |
+ *                         { <close-text> } | <empty>
  *
  *     <atom>          :== <ordinary>[1..n] | <special> | <symbol>
  *                         | <size> | <font> | <bool> | <math>
  *     <name>          :== <ordinary>
  *
+ *     <call>          :== \CALL{<name>}({<close-text>})
  *     <special>       :== \\ | \{ | \} | \$ | \& | \# | \% | \_
  *     <cond-symbol>   :== \AND | \OR | \NOT | \TRUE | \FALSE | \TO
  *     <text-symbol>   :== \textbackslash
@@ -239,9 +240,6 @@ Parser.prototype._parseBlock = function() {
         var commentNode = this._parseComment();
         if (commentNode) { blockNode.addChild(commentNode); continue; }
 
-        var callNode = this._parseCall();
-        if (callNode) { blockNode.addChild(callNode); continue; }
-
         break;
     }
 
@@ -390,17 +388,19 @@ Parser.prototype._parseText = function(openOrClose) {
     var textNode = new ParseNode(openOrClose + '-text');
     // any whitespace between Atom and CloseText
     var anyWhitespace = false;
-    var atomNode;
+    var subTextNode;
     while (true) {
-        atomNode = this._parseAtom();
-        if (atomNode) {
-            if (anyWhitespace) atomNode.whitespace |= anyWhitespace;
-            textNode.addChild(atomNode);
+        // atom or call
+        subTextNode = this._parseAtom() || this._parseCall();
+        if (subTextNode) {
+            if (anyWhitespace) subTextNode.whitespace |= anyWhitespace;
+            textNode.addChild(subTextNode);
             continue;
         }
 
+        // or close text
         if (this._lexer.accept('open')) {
-            var subTextNode = this._parseCloseText();
+            subTextNode = this._parseCloseText();
 
             anyWhitespace = this._lexer.get().whitespace;
             subTextNode.whitespace = anyWhitespace;
diff --git a/src/Renderer.js b/src/Renderer.js
index 139e28a..4ecee03 100644
--- a/src/Renderer.js
+++ b/src/Renderer.js
@@ -139,6 +139,14 @@ function TextEnvironment(nodes, textStyle) {
     this._textStyle = textStyle;
 }
 
+TextEnvironment.prototype._renderCloseText = function(node) {
+    var newTextStyle = new TextStyle(this._textStyle.fontSize());
+    var closeTextEnv = new TextEnvironment(
+                            node.children, newTextStyle);
+    if (node.whitespace) this._html.putText(' ');
+    this._html.putSpan(closeTextEnv.renderToHTML());
+}
+
 TextEnvironment.prototype.renderToHTML = function() {
     this._html = new HTMLBuilder();
 
@@ -197,12 +205,18 @@ TextEnvironment.prototype.renderToHTML = function() {
             var realQuote = quoteReplace[text];
             this._html.putText(realQuote);
             break;
+        case 'call':
+            // \CALL{funcName}{funcArgs}
+            // ==>
+            // funcName(funcArgs)
+            this._html.beginSpan('ps-funcname').putText(text).endSpan()
+            this._html.write('(');
+            var argsTextNode = node.children[0];
+            this._renderCloseText(argsTextNode);
+            this._html.write(')');
+            break;
         case 'close-text':
-            var newTextStyle = new TextStyle(this._textStyle.fontSize());
-            var closeTextEnv = new TextEnvironment(
-                                    node.children, newTextStyle);
-            if (node.whitespace) this._html.putText(' ');
-            this._html.putSpan(closeTextEnv.renderToHTML());
+            this._renderCloseText(node);
             break;
         // There are two kinds of typestyle commands:
         //      command (e.g. \textrm{...}).
@@ -721,10 +735,10 @@ Renderer.prototype._buildTree = function(node) {
         var cmdName = node.value;
         var displayName = {
             'STATE': '',
-            'ENSURE': 'Ensure:',
-            'REQUIRE': 'Require:',
-            'PRINT': 'print',
-            'RETURN': 'return'
+            'ENSURE': 'Ensure: ',
+            'REQUIRE': 'Require: ',
+            'PRINT': 'print ',
+            'RETURN': 'return '
         }[cmdName];
 
         this._newLine();
@@ -745,18 +759,6 @@ Renderer.prototype._buildTree = function(node) {
         this._buildTree(textNode);
         this._html.endSpan();
         break;
-    case 'call':
-        // \CALL{funcName}{funcArgs}
-        // ==>
-        // funcName(funcArgs)
-        var callFuncName = node.value;
-        var argsNode = node.children[0];
-        if (node.whitespace) this._typeText(' ');
-        this._typeFuncName(callFuncName);
-        this._typeText('(');
-        this._buildTree(argsNode);
-        this._typeText(')');
-        break;
     // ------------------- Text -------------------
     case 'open-text':
         var openTextEnv = new TextEnvironment(node.children, this._globalTextStyle);
diff --git a/static/test-suite.html b/static/test-suite.html
index 4531d43..2c0ca28 100644
--- a/static/test-suite.html
+++ b/static/test-suite.html
@@ -7,15 +7,6 @@
     <title>Test suite of PseudoCode.js</title>
 </head>
 <body>
-    <pre id="test0" style="display:none">
-        \begin{algorithmic}
-            \STATE \tiny tiny \normalsize normalsize
-            \STATE font sizings:  \tiny tiny \scriptsize scriptsize \footnotesize
-            footnotesize \small small \normalsize normal \large large \Large Large
-            \LARGE LARGE \huge huge \Huge Huge \normalsize
-            \STATE should be normal size
-        \end{algorithmic}
-    </pre>
     <pre id="test-basics" style="display:none">
         \begin{algorithm}
         \caption{Test text-style}
@@ -120,7 +111,6 @@
             \ENDIF
         \ENDPROCEDURE
         \PROCEDURE{Partition}{$A, p, r$}
-            \STATE someting
             \STATE $x = A[r]$
             \STATE $i = p - 1$
             \FOR{$j = p$ \TO $r - 1$}
-- 
GitLab