diff options
author | Tim Hatch <tim@timhatch.com> | 2014-04-23 16:48:10 -0400 |
---|---|---|
committer | Tim Hatch <tim@timhatch.com> | 2014-04-23 16:48:10 -0400 |
commit | 690a2bdfa924f76fd374e25bd9772c2090cf6414 (patch) | |
tree | 576991298817f1055da24857df6982dc2bd37d5a /doc/docs/java.rst | |
parent | 1d9f7f4f19c49a22ecd119f58580f9bc5ccd5080 (diff) | |
parent | b69477dc22e228cde4c1d39bf11b292b88d95fe1 (diff) | |
download | pygments-690a2bdfa924f76fd374e25bd9772c2090cf6414.tar.gz |
Merged in lefticus/pygments-main (pull request #24)
Conflicts:
pygments/lexers/_mapping.py
pygments/lexers/agile.py
Diffstat (limited to 'doc/docs/java.rst')
-rw-r--r-- | doc/docs/java.rst | 70 |
1 files changed, 70 insertions, 0 deletions
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 <http://www.jython.org>`__ 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 <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 makes it a lot easier to create the Java project. + +Here is an example of a `Maven <http://www.maven.org>`__ ``pom.xml`` file for a +project running Pygments: + +.. sourcecode:: xml + + <?xml version="1.0" encoding="UTF-8"?> + + <project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>example</groupId> + <artifactId>example</artifactId> + <version>1.0-SNAPSHOT</version> + <dependencies> + <dependency> + <groupId>org.python</groupId> + <artifactId>jython-standalone</artifactId> + <version>2.5.3</version> + </dependency> + <dependency> + <groupId>org.pygments</groupId> + <artifactId>pygments</artifactId> + <version>1.5</version> + <scope>runtime</scope> + </dependency> + </dependencies> + </project> + +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 + + <div class="highlight"> + <pre><span class="k">print</span> <span class="s">"Hello World"</span></pre> + </div> |