syntax highlighting for javadoc?
The other answers here all work, but introduce additional dependencies or add additional build complexity. If you're using Maven to generate the docs, and want the simplest way to get this to work with no extra files or dependencies, then add to the maven-javadoc-plugin
config:
<additionalOptions>-html5 --allow-script-in-comments</additionalOptions>
<header><![CDATA[
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/vs.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
]]></header>
The full plugin configuration will look something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<additionalOptions>-html5 --allow-script-in-comments</additionalOptions>
<nohelp>true</nohelp>
<show>private</show>
<header><![CDATA[
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/vs.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
]]></header>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Pick your theme from here and replace the "vs" above with the theme you like (all lower case w/ dashes between words, so like mono-blue
; if the one you want isn't working, you can look here for a list of available files).
Then write your examples like this:
/**
* Does something cool.
* <pre><code class="java">{@code
// some example code here
int x = 5;
* }</code></pre>
*/
https://burningmime.gitlab.io/setmatch/javadoc/com/burningmime/setmatch/RuleDB.html
EDIT: You don't actually need the class in <pre><code class="java">
. You can modify that javascript bit so that you don't need to change the source files at all, and anything in the {@code}
parts will get highlighted, since Javadoc already adds the <code>
tag. I don't know enough JavaScript to do that, but it shouldn't be too hard. That solution is probably the least invasive of all, since it would just be a couple lines in the build config.
I'm marking this community wiki, so if someone wants to come along and add that, please do so.
Another option is to use pegdown-doclet
, which lets you use Github-style fenced code blocks.
```java
public static class Example {}
```
You can use jQuery to get it done using the beautyOfCode plugin. I'm not sure if there's an easy way to hook into the javadoc generation, but after-the-fact you can just do the following in your header:
$(function(){
$("pre").beautifyCode('java');
});
and all text inside PRE tags will be highlighted as java. Check out the links above for more info.