anemadyan / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

< in code sample causes following code to not appear in prettified output #333

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. this line: for (int i=0; i<s.length(); i++) 
will truncate the output right after i (there is no space between i and <)
if you add a space and get
for (int i=0; i < s.length(); i++)
then everything works fine.

Strangely it can handle Hashtable<Integer> just fine, and '<' is no problem 
either wh

2.
3.
(Please include HTML, not just your source code)

What is the expected output?  What do you see instead?

I certainly expected it all but I only got up until the i before the <

What version are you using?  On what browser?

Safari 6.1.1 and 
https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?skin=sons
-of-obsidian

Please provide any additional information below.

Original issue reported on code.google.com by matt.b.p...@gmail.com on 26 Feb 2014 at 4:30

GoogleCodeExporter commented 8 years ago
Prettify prettifies HTML, so you need to make sure the input is valid HTML.

    for (int i = 0; i<s.length(); i++)

is equivalent to the HTML

    for (int i = 0; i
    <s junk></s>

You can either replace HTML special characters with entities

    for (int i = 0; i<s.length(); i++)

or wrap it in an XMP element ( 
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp ) instead of a 
<pre> or <code>

    <xmp class="prettyprint">for (int i = 0; i<s.length(); i++)</xmp>

Original comment by mikesamuel@gmail.com on 26 Feb 2014 at 6:17

GoogleCodeExporter commented 8 years ago
thats a no go; at least in safari it just renders like this:

import java.util.*; public class Scanner2 { // Keywords: if, then, else, begin, 
end, var, int, real, function, void // Operators: =, :=, +, -, *, /, <, <=, >, 
>=, !=, !, (, ) // Variables names: anything not a keyword. // Numbers: 
integers, doubles (XXX.XXXX) // Sigma = 
{a,..,z,A,...,Z,0,...,9,.,:,=,<,>,!,+,-,*,/,(,)} // A = {a,...,z,A,...,Z} // C 
= {0,...,9} private static HashSet Sigma = new HashSet(); private static 
HashSet letters = new HashSet(); private static HashSet digits = new HashSet(); 
private static Hashtable tokens = new Hashtable(); static String 
keywordsArray[] = new String[] { "if", "then", "else", "begin", "end", "endif", 
"var", "int", "real" }; static int tokensArray[] = new int[] { Token.IF, 
Token.THEN, Token.ELSE, Token.BEGIN, Token.END, Token.ENDIF, Token.VAR, 
Token.INT, Token.REAL }; private static Input2 input; private String lexeme; 
public Scanner2(Input2 input) { this.input = input; lexeme = ""; } private char 
consumeLookahead() { lexeme = lexeme + input.getLookahead(); input.consume(); 
return input.getLookahead(); } private char removeLookahead() { 
input.consume(); return input.getLookahead(); } private char getLookahead() { 
return input.getLookahead(); }   public Token nextToken() { char lookahead = 
getLookahead(); //System.out.println("The first lookahead is '" + lookahead + 
"'"); int state = 0; lexeme = ""; while (true) { //System.out.println("State = 
" + state + " lookahead = " + lookahead + " current lexeme: '" + lexeme + "'"); 
switch (state) { case 0: while (lookahead == ' ' || lookahead == '\n') 
lookahead = removeLookahead(); if (letters.contains(lookahead)) { lookahead = 
consumeLookahead(); state = 19; break; } else if (digits.contains(lookahead)) { 
lookahead = consumeLookahead(); state = 21; break; } else { switch(lookahead) { 
case '$': return new Token(Token.EOF, ""); case '+': lookahead = 
consumeLookahead(); state = 1; break; case '-': lookahead = consumeLookahead(); 
state = 2; break; case '*': lookahead = consumeLookahead(); state = 3; break; 
case '/': lookahead = consumeLookahead(); state = 4; break; case '(': lookahead 
= consumeLookahead(); state = 5; break; case ')': lookahead = 
consumeLookahead(); state = 6; break; case '=': lookahead = consumeLookahead(); 
state = 7; break; case '!': lookahead = consumeLookahead(); state = 8; break; 
case '<': lookahead = consumeLookahead(); state = 11; break; case ':': 
lookahead = consumeLookahead(); state = 17; break; case '>': lookahead = 
consumeLookahead(); state = 14; break; case '.': lookahead = 
consumeLookahead(); state = 22; break; default: state = -state-1; break; } } 
break; case 1: return new Token(Token.PLUS,lexeme); case 2: return new 
Token(Token.MINUS,lexeme); case 3: return new Token(Token.MULT,lexeme); case 4: 
return new Token(Token.DIV,lexeme); case 5: return new 
Token(Token.LPAREN,lexeme); case 6: return new Token(Token.RPAREN,lexeme); case 
7: return new Token(Token.EQ,lexeme); case 8: if (lookahead == '=') { lookahead 
= consumeLookahead(); state = 10; } else { state = 9; } break; case 9: return 
new Token(Token.NOT, lexeme); case 10: return new Token(Token.NOTEQ, lexeme); 
case 11: if (lookahead == '=') { lookahead = consumeLookahead(); state = 12; } 
else { state = 13; } break; case 12: return new Token(Token.LEQ, lexeme); case 
13: return new Token(Token.LT, lexeme); case 14: if (lookahead == '=') { 
lookahead = consumeLookahead(); state = 15; } else { state = 16; } break; case 
15: return new Token(Token.GEQ, lexeme); case 16: return new Token(Token.GT, 
lexeme); case 17: if (lookahead == '=') { lookahead = consumeLookahead(); state 
= 18; } else state = -state; break; case 18: return new Token(Token.ASSIGN, 
lexeme); case 19: if (letters.contains(lookahead) || 
digits.contains(lookahead)) { lookahead = consumeLookahead(); state = 19; } 
else { state = 20; } break; case 20: Integer tokenType = tokens.get(lexeme); if 
(tokenType != null) return new Token(tokenType, lexeme); else return new 
Token(Token.ID, lexeme); case 21: if (digits.contains(lookahead)) { lookahead = 
consumeLookahead(); state = 21; } else if (lookahead == '.') { lookahead = 
consumeLookahead(); state = 22; } else { state = 23; } break; case 22: if 
(digits.contains(lookahead)) { lookahead = consumeLookahead(); state = 22; } 
else { state = 24; } break; case 23: return new Token(Token.INTLIT, lexeme); 
case 24: return new Token(Token.REALLIT, lexeme); } // Handle Errors if (state 
< 0) { if (lookahead == '$') return new Token(Token.EOF, ""); else { lookahead 
= consumeLookahead(); return new Token(Token.ERROR, lexeme); } } } } public 
static void main(String args[]) { Input2 input = new Input2(args[0]); Scanner2 
scanner = new Scanner2(input); Token t = scanner.nextToken(); 
System.out.println(t); while (t.getKind() != Token.EOF) { t = 
scanner.nextToken(); System.out.println(t); } } static { String s = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i=0; i

and note that it STILL stops a i.

adding html tags like < isn't really a reasonable option if you have to go an 
HTML'ify your code every time you update it �.. I guess putting in the spacing 
is easier, but really - you guys can't fix this ??

Original comment by matt.b.p...@gmail.com on 27 Feb 2014 at 12:48

GoogleCodeExporter commented 8 years ago
What's a no go?  You haven't attached any HTML to this bug.

Original comment by mikesamuel@gmail.com on 27 Feb 2014 at 3:58

GoogleCodeExporter commented 8 years ago
I don�t have to I simply did as you guys said - change pre or code to XML but 
that didn�t render by safari!!

Original comment by matt.b.p...@gmail.com on 27 Feb 2014 at 4:00

GoogleCodeExporter commented 8 years ago
If you want help with your problem, then you have to give me enough information 
to figure out what you're doing wrong.

Original comment by mikesamuel@gmail.com on 27 Feb 2014 at 4:03

GoogleCodeExporter commented 8 years ago
I am not doing anything wrong - I did exactly as you said�.

I reported the problem that �for (i=0; i<x; �. 
stops rendering after the i in the �i<x� �.

I figured out that putting SPACE before and after <, so �i < s�
would continue the rendering, so it was caused by lack of spacing�..

It as suggested that either putting in spacing or (stupider!) to turn all < 
into <,
which of core is NOT a reasonable thing to do if you are trying to 
server source code nicely rendered on the fly - typical you are serving
from actual code in a system so changing your < to < is futile.

then you guys suggested replacing <pre �. with <xml �.

and I wrote back with the result of replacing pre with xml: no rendering of any 
kind.
that is where we are. if you want html then here you go:

<pre class="prettyprint linenums code�>
some code here
</pre>

renders correctly if you have spaces before (and perhaps after �??) your <

<xml class="prettyprint linenums code�>
some code here
</xml>

does not render anything - just give the code back on a white background;

echo '<pre class="prettyprint linenums code">';

$file = file_get_contents($_GET["file"]);
echo $file;

echo '</pre>�;

is the snippet of PHP I use to dynamically load source files and display them!

Original comment by matt.b.p...@gmail.com on 27 Feb 2014 at 4:11

GoogleCodeExporter commented 8 years ago
> then you guys suggested replacing <pre. with <xml.

What I actually said was "or wrap it in an XMP element".  Note the 'p' at the 
end is not an 'l'.

Original comment by mikesamuel@gmail.com on 27 Feb 2014 at 4:20

GoogleCodeExporter commented 8 years ago
oh so you did ;-) my apologies very much and you are right - apparently I can�t 
read!

Thanks ;-)

Original comment by matt.b.p...@gmail.com on 27 Feb 2014 at 4:24

GoogleCodeExporter commented 8 years ago
It happens to the best of us.

Original comment by mikesamuel@gmail.com on 27 Feb 2014 at 4:38

GoogleCodeExporter commented 8 years ago
Well it shouldn't happen to me.... I teach computer science and tell
at students when they can't read ;-)

Sent from my iPhone, which now again is better than Kelvin's.

On Feb 26, 2014, at 20:38, "google-code-prettify@googlecode.com"
<google-code-prettify@googlecode.com> wrote:

Original comment by matt.b.p...@gmail.com on 27 Feb 2014 at 4:48