Added Java syntax highlighter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\/|%|\^|(<){2}|($gt;){2,3}|:|~/g,
+ 'ignore': /&(lt|gt|amp);/gi,
+ 'punctuation': /[{}[\];(),.:]/g,
+};
\ No newline at end of file