Commit 201077e94c7d009186dc0c3fe5aac621088ddcd3

sebh 2012-08-03T17:25:09

Added Java syntax highlighter

diff --git a/code.js b/code.js
index 6123bd7..723a74c 100644
--- a/code.js
+++ b/code.js
@@ -26,7 +26,8 @@ var components = {
 		},
 		'markup': 'Markup',
 		'css': 'CSS',
-		'javascript': 'JavaScript'
+		'javascript': 'JavaScript',
+		'java' : 'Java',
 	},
 	plugins: {
 		meta: {
diff --git a/components/prism-java.js b/components/prism-java.js
new file mode 100644
index 0000000..e8f692f
--- /dev/null
+++ b/components/prism-java.js
@@ -0,0 +1,13 @@
+Prism.languages.java = {
+	'comment': {
+		pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g,
+		lookbehind: true
+	},
+	'string': /("|')(\\?.)*?\1/g,
+	'keyword': /\b(abstract|continue|for|new|switch|assert|default|goto|package|synchronized|boolean|do|if|private|this|break|double|implements|protected|throw|byte|else|import|public|throws|case|enum|instanceof|return|transient|catch|extends|int|short|try|char|final|interface|static|void|class|finally|long|strictfp|volatile|const|float|native|super|while)\b/g,
+	'boolean': /\b(true|false)\b/g,
+	'number': /\b-?(0x)?\d*\.?\d+\b/g,
+	'operator': /[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\/|%|\^|(<){2}|($gt;){2,3}|:|~/g,
+	'ignore': /&(lt|gt|amp);/gi,
+	'punctuation': /[{}[\];(),.:]/g,
+};
\ No newline at end of file
diff --git a/examples.html b/examples.html
index fdce54b..9094064 100644
--- a/examples.html
+++ b/examples.html
@@ -203,6 +203,98 @@ var comment = /\/\*[\w\W]*?\*\//g;</code></pre>
 	<pre><code>var foo = /a/, bar = 3/4;</code></pre>
 </section>
 
+<section class="language-java">
+	<h1>Java</h1>
+	
+	<h2>Java Language Keywords</h2>
+	<pre><code>
+abstract continue for        new       switch
+assert   default  goto       package   synchronized
+boolean  do       if         private   this
+break    double   implements protected throw
+byte     else     import     public    throws
+case     enum     instanceof return    transient
+catch    extends  int        short     try
+char     final    interface  static    void
+class    finally  long       strictfp  volatile
+const    float    native     super     while
+
+	</code></pre>
+	
+	<h2>Operators</h2>
+	<pre><code>// postfix
+expr++ expr--
+// unary
+++expr --expr +expr -expr ~ !
+// multiplicative
+* / %
+// additive
++ -
+// shift
+<< >> >>>
+// relational
+< > <= >= instanceof
+// equality
+== !=
+// bitwise AND
+&
+// bitwise exclusive OR
+^
+// bitwise inclusive OR
+|
+// logical AND
+&&
+// logical OR
+||
+// ternary
+? :
+// assignment
+= += -= *= /= %= &= ^= |= <<= >>= >>>=
+	</code></pre>
+	
+	<h2>Simple class example</h2>
+	
+	<pre><code>package com.prismjs;
+
+import java.io.*;
+
+@SuppressWarnings("unchecked")
+public class PrismJS {
+
+	public static Integer test = 0;
+
+	/**
+	 * Javadoc style comment
+	 * 
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		// Let's create some variables
+		int numArgs = args.length;
+		Integer a = 0x1;
+		String test = "" + "Hello" + " \"World\"!";
+		double dvalue = 1.23;
+		int optest = 0;
+		optest += 321 - 45 * 1247 / 425 % 123;
+		
+		/*
+		 * Multiline comment
+		 */
+		for (int i = 0; i < numArgs; i++) {
+			// Simple line comment
+			System.out.println("Arg value= " + args[i]);
+		}
+
+		do {
+			System.out.println("Do while ok!");
+		} while (false);
+	}
+}
+
+	</code></pre>
+	<p>As you can notice String keyword is not highlighted because it's not a Java language keyword (cf. <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html">Java Language Keywords</a>). The main reason is that String is not a primitive type such as <em>int</em> but a class type like <em>Integer</em>.</p>
+</section>
+
 <section id="failures" class="language-javascript">
 	<h1>Known failures (JavaScript)</h1>
 	<p>There are certain edge cases where Prism will fail.
diff --git a/prism.js b/prism.js
index b52f146..82c2ec1 100644
--- a/prism.js
+++ b/prism.js
@@ -425,4 +425,22 @@ if (Prism.languages.markup) {
 			}
 		}
 	});
-}
\ No newline at end of file
+}
+
+/*********************************************** 
+     Begin prism-java.js 
+***********************************************/ 
+
+Prism.languages.java = {
+	'comment': {
+		pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g,
+		lookbehind: true
+	},
+	'string': /("|')(\\?.)*?\1/g,
+	'keyword': /\b(abstract|continue|for|new|switch|assert|default|goto|package|synchronized|boolean|do|if|private|this|break|double|implements|protected|throw|byte|else|import|public|throws|case|enum|instanceof|return|transient|catch|extends|int|short|try|char|final|interface|static|void|class|finally|long|strictfp|volatile|const|float|native|super|while)\b/g,
+	'boolean': /\b(true|false)\b/g,
+	'number': /\b-?(0x)?\d*\.?\d+\b/g,
+	'operator': /[-+]{1,2}|!|=?&lt;|=?&gt;|={1,2}|(&amp;){1,2}|\|?\||\?|\*|\/|%|\^|(&lt;){2}|($gt;){2,3}|:|~/g,
+	'ignore': /&(lt|gt|amp);/gi,
+	'punctuation': /[{}[\];(),.:]/g,
+};
\ No newline at end of file