From ff3a8dea781fb0492de4abbd4da48a5b1c110974 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 18 Jan 2014 21:16:32 +0100 Subject: New docs + website using Sphinx. --- doc/docs/java.rst | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 doc/docs/java.rst (limited to 'doc/docs/java.rst') diff --git a/doc/docs/java.rst b/doc/docs/java.rst new file mode 100644 index 00000000..5eb6196a --- /dev/null +++ b/doc/docs/java.rst @@ -0,0 +1,70 @@ +===================== +Use Pygments in Java +===================== + +Thanks to `Jython `__ it is possible to use Pygments in +Java. + +This page is a simple tutorial to get an idea of how this is working. You can +then look at the `Jython documentation `__ for more +advanced use. + +Since version 1.5, Pygments is deployed on `Maven Central +`__ as a JAR so is Jython +which makes it a lot easier to create the Java project. + +Here is an example of a `Maven `__ ``pom.xml`` file for a +project running Pygments: + +.. sourcecode:: xml + + + + + 4.0.0 + example + example + 1.0-SNAPSHOT + + + org.python + jython-standalone + 2.5.3 + + + org.pygments + pygments + 1.5 + runtime + + + + +The following Java example: + +.. sourcecode:: java + + PythonInterpreter interpreter = new PythonInterpreter(); + + // Set a variable with the content you want to work with + interpreter.set("code", code); + + // Simple use Pygments as you would in Python + interpreter.exec("from pygments import highlight\n" + + "from pygments.lexers import PythonLexer\n" + + "from pygments.formatters import HtmlFormatter\n" + + "\nresult = highlight(code, PythonLexer(), HtmlFormatter())"); + + // Get the result that has been set in a variable + System.out.println(interpreter.get("result", String.class)); + +will print something like: + +.. sourcecode:: html + +
+
print "Hello World"
+
-- cgit v1.2.1