From 928d10e52eebcf72162369bdecb7d650821ced2e Mon Sep 17 00:00:00 2001 From: "Tate, Hongliang Tian" <tatetian@gmail.com> Date: Wed, 11 Mar 2015 14:52:06 +0800 Subject: [PATCH] Fix broken linenum --- PseudoCode.js | 1 - src/Lexer.js | 8 +++++--- src/Renderer.js | 8 ++++---- static/pseudocode.css | 2 +- static/test-suite.html | 4 +++- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/PseudoCode.js b/PseudoCode.js index cb1838a..dc7adf1 100644 --- a/PseudoCode.js +++ b/PseudoCode.js @@ -4,7 +4,6 @@ * TODO: * * demo * * Support color - * * Support TeX comment **/ var ParseError = require('./src/ParseError'); diff --git a/src/Lexer.js b/src/Lexer.js index 8f6b926..2e9d4e1 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -66,20 +66,22 @@ var atomRegex = { ordinary: /^[^\\{}$&#%_\s]+/, math: mathPattern ///^\$.*\$/, }; -var commentRegex = /^%.*/ +var commentRegex = /^%.*/; var whitespaceRegex = /^\s+/; Lexer.prototype._skip = function(len) { this._pos += len; this._remain = this._remain.slice(len); -} +}; /* Get the next atom */ Lexer.prototype._next = function() { + var anyWhitespace = false; while (1) { // Skip whitespace (one or more) var whitespaceMatch = whitespaceRegex.exec(this._remain); if (whitespaceMatch) { + anyWhitespace = true; var whitespaceLen = whitespaceMatch[0].length; this._skip(whitespaceLen); } @@ -118,7 +120,7 @@ Lexer.prototype._next = function() { this._nextAtom = { type: type, /* special, func, open, close, ordinary, math */ text: usefulText, /* the text value of the atom */ - whitespace: whitespaceLen > 0 /* any whitespace before the atom */ + whitespace: anyWhitespace /* any whitespace before the atom */ }; this._pos += matchText.length; diff --git a/src/Renderer.js b/src/Renderer.js index cdea760..139e28a 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -309,7 +309,7 @@ HTMLBuilder.prototype.write = function(html) { HTMLBuilder.prototype.toMarkup = function() { this._flushText(); var html = this._body.join(''); - return html; + return html.trim(); }; HTMLBuilder.prototype.toDOM = function() { @@ -485,9 +485,9 @@ Renderer.prototype._newLine = function() { this._html.beginP('ps-line ps-code', this._globalTextStyle.toCSS()); if (this._options.lineNumber) { this._html.beginSpan('ps-linenum', { - 'left': - ((this._blockLevel - 1)*(indentSize - 0.2)) + 'em' + 'left': - ((this._blockLevel - 1)*(indentSize* 1.25)) + 'em' }) - .putText(this._numLOC + this._options.lineNumberPunc) + .putText(this._numLOC + this._options.lineNumberPunc + ' ') .endSpan(); } } @@ -534,7 +534,7 @@ Renderer.prototype._buildCommentsFromBlock = function(blockNode) { var commentNode = children.shift(); this._buildTree(commentNode); } -} +}; Renderer.prototype._buildTree = function(node) { var ci, child, textNode; diff --git a/static/pseudocode.css b/static/pseudocode.css index 92434f1..158a79d 100644 --- a/static/pseudocode.css +++ b/static/pseudocode.css @@ -52,7 +52,7 @@ position: relative; } .ps-root .ps-algorithmic.with-linenum .ps-line.ps-code { - text-indent: -2.2em; + text-indent: -1.6em; } .ps-root .ps-algorithmic.with-linenum .ps-line.ps-code > span{ text-indent: 0em; diff --git a/static/test-suite.html b/static/test-suite.html index 34fe8ed..4531d43 100644 --- a/static/test-suite.html +++ b/static/test-suite.html @@ -36,6 +36,7 @@ \STATE \uppercase{uppercase,} \lowercase{LOWERCASE.} \ENDPROCEDURE \PROCEDURE{Test-Colors}{} + % feature not implemented \ENDPROCEDURE \end{algorithmic} \end{algorithm} @@ -119,6 +120,7 @@ \ENDIF \ENDPROCEDURE \PROCEDURE{Partition}{$A, p, r$} + \STATE someting \STATE $x = A[r]$ \STATE $i = p - 1$ \FOR{$j = p$ \TO $r - 1$} @@ -145,7 +147,7 @@ }); var testExamples = document.getElementById("test-examples").textContent; pseudocode.render(testExamples, document.body, { - lineNumber: false, + lineNumber: true, noEnd: false }); </script> -- GitLab