문제

I'm trying to use Pygments in Java project by including Jython.jar. In my Java project I have src/main/python source directory where I placed pygments files into pygments folder so they end up on the classpath. Now I created highlighter.py file in the src/main/python with the following content:

import sys
sys.path.append('WEB-INF/classes')
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

class Highlighter:
    def colorize(code, lexername):
        lexer = get_lexer_by_name(lexername, stripall=True)
        formatter = HtmlFormatter(linenos=True, cssclass="source")
        return highlight(code, lexer, formatter)

Then I defined Java interface Highlighter.java

public interface Highlighter {
    String colorize(String rawText, String lexer);
}

And lastly I created factory class that uses PythonInterpreter to expose highlighter.py as a Jython object as described here

Now, when I run this line of code

Highlighter hl = (Highlighter) jf.getJythonObject(Highlighter.class.getName(), 
       "WEB-INF/classes/highlighter.py");

I get PyException as follows

Traceback (most recent call last):
File "WEB-INF/classes/highlighter.py", line 3, in 
  from pygments import highlight
LookupError: no codec search functions registered: can't find encoding

This occurs if I use Jython v. 2.5.0. In 2.1 I had to copy Jython/Lib directory under src/main/python but then I end up missing IOString module

Or maybe there's a better way to achieve what I'm trying to achieve? I'm curious if anyone was able to use Pygments in Java project. I would greatly appreciate any suggestions

도움이 되었습니까?

해결책

All right. I figured it out. Made few beginner errors but if you are wondering how to make Pygments work in your Java project here's full tutorial link

Highlights:

  • Using bare jython.jar is insufficient.
  • Adding Jython/Lib to your classpath doesn't work. I ended up unjarring jython.jar and adding Lib directory + all pygments files to the resulting structure and then jarring it up again
  • Factory example form the link above is convoluted. Much better way is to use Jython book chapter 10
  • For examples of changed highlighter.py, Highlighter.java and HighlighterFactory.java see the tutorial
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top