J'essaie d'utiliser Pygments dans un projet Java en incluant Jython.jar. Dans mon projet Java j'ai le répertoire source src/main/python
où j'ai placé les fichiers pygments dans le dossier pygments afin qu'ils finissent sur le classpath. Maintenant, je créé highlighter.py fichier dans le répertoire src/main/python avec le contenu suivant:Jython - utilisation de Pygments dans le projet Java
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)
Puis je définissais interface Java Highlighter.java
public interface Highlighter {
String colorize(String rawText, String lexer);
}
Et enfin j'ai créé la classe d'usine qui utilise PythonInterpreter
pour exposer highlighter.py
comme un objet Jython as described here
maintenant, quand je lance cette ligne de code
Highlighter hl = (Highlighter) jf.getJythonObject(Highlighter.class.getName(),
"WEB-INF/classes/highlighter.py");
je reçois PyException comme suit
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
Cela se produit si j'utilise Jython v. 2.5.0. Dans 2.1, j'ai dû copier le répertoire Jython/Lib sous src/main/python, mais je finis par manquer module IOString
Ou peut-être y at-il une meilleure façon d'atteindre ce que j'essaie d'atteindre? Je suis curieux de savoir si quelqu'un était capable d'utiliser Pygments dans le projet Java. J'apprécierais grandement toutes les suggestions
Qu'est-ce que 'sys.path' contient? –
Maintenant, quand vous l'avez mentionné, j'ai ajouté sys.path.append ('WEB-INF/classes') à highlighter.py. Test – Bostone
Maintenant, j'ai un problème différent - J'ai modifié le message original – Bostone