Hana 9e216da9ef go.mod: add go.mod and move pygments to third_party
After go1.16, go will use module mode by default,
even when the repository is checked out under GOPATH
or in a one-off directory. Add go.mod, go.sum to keep
this repo buildable without opting out of the module
mode.

> go mod init github.com/mmcgrana/gobyexample
> go mod tidy
> go mod vendor

In module mode, the 'vendor' directory is special
and its contents will be actively maintained by the
go command. pygments aren't the dependency the go will
know about, so it will delete the contents from vendor
directory. Move it to `third_party` directory now.

And, vendor the blackfriday package.

Note: the tutorial contents are not affected by the
change in go1.16 because all the examples in this
tutorial ask users to run the go command with the
explicit list of files to be compiled (e.g.
`go run hello-world.go` or `go build command-line-arguments.go`).
When the source list is provided, the go command does
not have to compute the build list and whether it's
running in GOPATH mode or module mode becomes irrelevant.
2021-02-15 16:45:26 -05:00

71 lines
2.3 KiB
ReStructuredText

=====================
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 works. You can
then look at the `Jython documentation <http://www.jython.org/docs/>`_ for more
advanced uses.
Since version 1.5, Pygments is deployed on `Maven Central
<http://repo1.maven.org/maven2/org/pygments/pygments/>`_ as a JAR, as is Jython
which makes it a lot easier to create a 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">&quot;Hello World&quot;</span></pre>
</div>