Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
_site
tex/
build/
node_modules/
Expand Down
16 changes: 13 additions & 3 deletions docs/katex-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
\OUTPUT some outputs
\PROCEDURE{Test-Declarations}{}
\STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
\STATE font weights: {normal weight, \bfseries bold, \mdseries
\STATE font weights: {normal weight, \bfseries bold, \mdseries
medium, \lfseries lighter. }
\STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
\STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
Expand Down Expand Up @@ -107,6 +107,16 @@
\STATE $i \gets i + 1$
\UNTIL{$i>n$}
\ENDPROCEDURE
\PROCEDURE{Test-Break-Continue}{$n$}
\FOR{$i = 0$ \TO $2n$}
\IF{$i < n/2$}
\CONTINUE
\ELIF{$i > n$}
\BREAK
\ENDIF
\PRINT $i$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
Expand All @@ -131,13 +141,13 @@
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\PROCEDURE{Quicksort}{$A, p, r$}
\IF{$p < r$}
\IF{$p < r$}
\STATE $q = $ \CALL{Partition}{$A, p, r$}
\STATE \CALL{Quicksort}{$A, p, q - 1$}
\STATE \CALL{Quicksort}{$A, q + 1, r$}
Expand Down
16 changes: 13 additions & 3 deletions docs/mathjax-v2-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
\OUTPUT some outputs
\PROCEDURE{Test-Declarations}{}
\STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
\STATE font weights: {normal weight, \bfseries bold, \mdseries
\STATE font weights: {normal weight, \bfseries bold, \mdseries
medium, \lfseries lighter. }
\STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
\STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
Expand Down Expand Up @@ -117,6 +117,16 @@
\STATE $i \gets i + 1$
\UNTIL{$i>n$}
\ENDPROCEDURE
\PROCEDURE{Test-Break-Continue}{$n$}
\FOR{$i = 0$ \TO $2n$}
\IF{$i < n/2$}
\CONTINUE
\ELIF{$i > n$}
\BREAK
\ENDIF
\PRINT $i$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
Expand All @@ -141,13 +151,13 @@
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\PROCEDURE{Quicksort}{$A, p, r$}
\IF{$p < r$}
\IF{$p < r$}
\STATE $q = $ \CALL{Partition}{$A, p, r$}
\STATE \CALL{Quicksort}{$A, p, q - 1$}
\STATE \CALL{Quicksort}{$A, q + 1, r$}
Expand Down
16 changes: 13 additions & 3 deletions docs/mathjax-v3-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
\OUTPUT some outputs
\PROCEDURE{Test-Declarations}{}
\STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
\STATE font weights: {normal weight, \bfseries bold, \mdseries
\STATE font weights: {normal weight, \bfseries bold, \mdseries
medium, \lfseries lighter. }
\STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
\STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
Expand Down Expand Up @@ -117,6 +117,16 @@
\STATE $i \gets i + 1$
\UNTIL{$i>n$}
\ENDPROCEDURE
\PROCEDURE{Test-Break-Continue}{$n$}
\FOR{$i = 0$ \TO $2n$}
\IF{$i < n/2$}
\CONTINUE
\ELIF{$i > n$}
\BREAK
\ENDIF
\PRINT $i$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
Expand All @@ -141,13 +151,13 @@
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\PROCEDURE{Quicksort}{$A, p, r$}
\IF{$p < r$}
\IF{$p < r$}
\STATE $q = $ \CALL{Partition}{$A, p, r$}
\STATE \CALL{Quicksort}{$A, p, q - 1$}
\STATE \CALL{Quicksort}{$A, q + 1, r$}
Expand Down
2 changes: 1 addition & 1 deletion docs/pseudocode.js

Large diffs are not rendered by default.

72 changes: 46 additions & 26 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,47 @@
* <pseudo> :== ( <algorithm> | <algorithmic> )[0..n]
*
* <algorithm> :== \begin{algorithm}
* + ( <caption> | <algorithmic> )[0..n]
* ( <caption> | <algorithmic> )[0..n]
* \end{algorithm}
* <caption> :== \caption{ <close-text> }
*
* <algorithmic> :== \begin{algorithmic}
* + ( <ensure> | <require> | <block> )[0..n]
* + \end{algorithmic}
* <require> :== \REQUIRE + <open-text>
* <ensure> :== \ENSURE + <open-text>
* ( <ensure> | <require> | <block> )[0..n]
* \end{algorithmic}
* <require> :== \REQUIRE <open-text>
* <ensure> :== \ENSURE <open-text>
*
* <block> :== ( <control> | <function> | <statement> |
* <comment> )[0..n]
* <block> :== ( <comment> | <command> | <control> | <function> |
* <statement> )[0..n]
*
* <control> :== <if> | <for> | <while> | <repeat>
* <if> :== \IF{<cond>} + <block>
* + ( \ELIF{<cond>} <block> )[0..n]
* + ( \ELSE <block> )[0..1]
* + \ENDIF
* <for> :== \FOR{<cond>} + <block> + \ENDFOR
* <while> :== \WHILE{<cond>} + <block> + \ENDWHILE
* <repeat> :== \REPEAT + <block> + \UNTIL{<cond>}
* <if> :== \IF{<cond>} <block>
* ( \ELIF{<cond>} <block> )[0..n]
* ( \ELSE <block> )[0..1]
* \ENDIF
*
* <for> :== \FOR{<cond>} <block> \ENDFOR
* <while> :== \WHILE{<cond>} <block> \ENDWHILE
* <repeat> :== \REPEAT <block> \UNTIL{<cond>}
*
* <function> :== \FUNCTION{<name>}{<params>} <block> \ENDFUNCTION
* (same for <procedure>)
*
* <statement> :== <state> | <return> | <print>
* <state> :== \STATE + <open-text>
* <return> :== \RETURN + <open-text>
* <print> :== \PRINT + <open-text>
* <state> :== \STATE <open-text>
* <return> :== \RETURN <open-text>
* <print> :== \PRINT <open-text>
*
* <commands> :== <break> | <continue>
* <break> :== \BREAK
* <continue> :== \CONTINUE
*
* <comment> :== \COMMENT{<close-text>}
*
* <cond> :== <close-text>
* <open-text> :== ( <atom> | <call> ) + <open-text> |
* <open-text> :== ( <atom> | <call> ) <open-text> |
* { <close-text> } | <empty>
* <close-text> :== ( <atom> | <call> ) + <close-text> |
* <close-text> :== ( <atom> | <call> ) <close-text> |
* { <close-text> } | <empty>
*
* <atom> :== <ordinary>[1..n] | <special> | <symbol>
Expand All @@ -58,8 +63,8 @@
* <text-symbol> :== \textbackslash
* <quote-symbol> :== ` | `` | ' | ''
* (More LaTeX symbols can be added if necessary. See
* http://get-software.net/info/symbols/comprehensive/symbols-a4.pdf.)
* <math> :== \( + ... + \) | $ ... $
* http://tug.ctan.org/info/symbols/comprehensive/symbols-a4.pdf)
* <math> :== \( ... \) | $ ... $
* (Math are handled by KaTeX)
* <size> :== \tiny | \scriptsize | \footnotesize | \small
* | \normalsize | \large | \Large | \LARGE | \huge
Expand Down Expand Up @@ -197,7 +202,7 @@ Parser.prototype._parseAlgorithmicInner = function() {
var algmicNode = new ParseNode('algorithmic');
var node;
while (true) {
node = this._parseCommand(INPUTS_OUTPUTS_COMMANDS);
node = this._parseStatement(IO_STATEMENTS);
if (node) {
algmicNode.addChild(node);
continue;
Expand Down Expand Up @@ -236,7 +241,10 @@ Parser.prototype._parseBlock = function() {
var functionNode = this._parseFunction();
if (functionNode) { blockNode.addChild(functionNode); continue; }

var commandNode = this._parseCommand(STATEMENT_COMMANDS);
var statementNode = this._parseStatement(STATEMENTS);
if (statementNode) { blockNode.addChild(statementNode); continue; }

var commandNode = this._parseCommand(COMMANDS);
if (commandNode) { blockNode.addChild(commandNode); continue; }

var commentNode = this._parseComment();
Expand Down Expand Up @@ -353,14 +361,26 @@ Parser.prototype._parseRepeat = function() {
return repeatNode;
};

var INPUTS_OUTPUTS_COMMANDS = ['ensure', 'require', 'input', 'output'];
var STATEMENT_COMMANDS = ['state', 'print', 'return'];
var IO_STATEMENTS = ['ensure', 'require', 'input', 'output'];
var STATEMENTS = ['state', 'print', 'return'];
Parser.prototype._parseStatement = function(acceptStatements) {
if (!this._lexer.accept('func', acceptStatements)) return null;

var stmtName = this._lexer.get().text.toLowerCase();
var stmtNode = new ParseNode('statement', stmtName);

stmtNode.addChild(this._parseOpenText());

return stmtNode;
};

var COMMANDS = ['break', 'continue'];
Parser.prototype._parseCommand = function(acceptCommands) {
if (!this._lexer.accept('func', acceptCommands)) return null;

var cmdName = this._lexer.get().text.toLowerCase();
var cmdNode = new ParseNode('command', cmdName);
cmdNode.addChild(this._parseOpenText());

return cmdNode;
};

Expand Down
34 changes: 22 additions & 12 deletions src/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,22 +799,14 @@ Renderer.prototype._buildTree = function(node) {
break;
// ------------------- Lines -------------------
case 'command':
// commands: \STATE, \ENSURE, \PRINT, \RETURN, etc.
var cmdName = node.value;
var displayName = {
'state': '',
'ensure': 'Ensure: ',
'require': 'Require: ',
'input': 'Input: ',
'output': 'Output: ',
'print': 'print ',
'return': 'return ',
var displayCmdName = {
'break': 'break',
'continue': 'continue',
}[cmdName];

this._newLine();
if (displayName) this._typeKeyword(displayName);
textNode = node.children[0];
this._buildTree(textNode);
if (displayCmdName) this._typeKeyword(displayCmdName);
break;
case 'caption':
this._newLine();
Expand All @@ -830,6 +822,24 @@ Renderer.prototype._buildTree = function(node) {
this._buildTree(textNode);
this._html.endSpan();
break;
case 'statement':
// statements: \STATE, \ENSURE, \PRINT, \RETURN, etc.
var stmtName = node.value;
var displayStmtName = {
'state': '',
'ensure': 'Ensure: ',
'require': 'Require: ',
'input': 'Input: ',
'output': 'Output: ',
'print': 'print ',
'return': 'return ',
}[stmtName];

this._newLine();
if (displayStmtName) this._typeKeyword(displayStmtName);
textNode = node.children[0];
this._buildTree(textNode);
break;
// ------------------- Text -------------------
case 'open-text':
var openTextEnv = new TextEnvironment(node.children,
Expand Down
16 changes: 13 additions & 3 deletions static/body.html.part
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
\OUTPUT some outputs
\PROCEDURE{Test-Declarations}{}
\STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
\STATE font weights: {normal weight, \bfseries bold, \mdseries
\STATE font weights: {normal weight, \bfseries bold, \mdseries
medium, \lfseries lighter. }
\STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
\STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
Expand Down Expand Up @@ -92,6 +92,16 @@
\STATE $i \gets i + 1$
\UNTIL{$i>n$}
\ENDPROCEDURE
\PROCEDURE{Test-Break-Continue}{$n$}
\FOR{$i = 0$ \TO $2n$}
\IF{$i < n/2$}
\CONTINUE
\ELIF{$i > n$}
\BREAK
\ENDIF
\PRINT $i$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
Expand All @@ -116,13 +126,13 @@
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\PROCEDURE{Quicksort}{$A, p, r$}
\IF{$p < r$}
\IF{$p < r$}
\STATE $q = $ \CALL{Partition}{$A, p, r$}
\STATE \CALL{Quicksort}{$A, p, q - 1$}
\STATE \CALL{Quicksort}{$A, q + 1, r$}
Expand Down