From f7b8b95674266b8cd5ffcad2ffcdec90356f26d4 Mon Sep 17 00:00:00 2001 From: tmortagne Date: Mon, 7 Jan 2013 12:00:08 +0100 Subject: Add tutorial to use Pygments in Java --- docs/src/integrate.txt | 5 +++++ docs/src/java.txt | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 docs/src/java.txt (limited to 'docs') diff --git a/docs/src/integrate.txt b/docs/src/integrate.txt index 51a3dac4..2397133b 100644 --- a/docs/src/integrate.txt +++ b/docs/src/integrate.txt @@ -41,3 +41,8 @@ Bash completion The source distribution contains a file ``external/pygments.bashcomp`` that sets up completion for the ``pygmentize`` command in bash. + +Java +---- + +`Java quickstart `_ diff --git a/docs/src/java.txt b/docs/src/java.txt new file mode 100644 index 00000000..d30e1aaf --- /dev/null +++ b/docs/src/java.txt @@ -0,0 +1,61 @@ +===================== +Use Pygments in Java +===================== + +Thanks to _Jython: http://www.jython.org it's 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 _Jython documentation: http://www.jython.org/docs/ for more advanced use. + +Since version 1.5, Pygments is deployed on _Maven Central: http://repo1.maven.org/maven2/org/pygments/pygments/ as a JAR so is Jython which make it a lot easier to create the Java project. + +Here is an example of _Maven: http://www.maven.org 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