/ / No Lexer per Alias ​​Found: python, parsing, beautifulsoup, pygments

Nessun Lexer per Alias ​​trovato: python, parsing, beautifulsoup, pygments

Sto tentando di utilizzare Pygments e Beautiful Soup come soluzione di evidenziazione del codice per il software del blog che sto creando per Google App Engine.

Come funziona è che i miei post HTML utilizzeranno i tag pre per identificare il codice. Come questo:

<p>Check out this cool code example<p>
<pre class="python">
import this
def demo():
pass</pre>

BeautifulSoup cattura la parte tra il pre taggalo e passa a Pygments. Si suppone che Pygments controlli il valore della classe e applichi il lexar corretto. Pygments applica quindi la formattazione e sostituisce il testo originale con il testo formattato. La soluzione è spiegata più dettagliatamente in Blog di SaltyCrane.

L'errore

ClassNotFound: no lexer for alias [u"python"] found

Forse non ho appena importato il modulo correttamente?

Il codice

from google.appengine.ext import db
import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter


def formatter(p):
soup = BeautifulSoup(p.otext)
preblocks = soup.findAll("pre")
for pre in preblocks:
if pre.has_key("class"):
code = "".join([unicode(item) for item in pre.contents])
code = unescape_html(code)
lexer = lexers.get_lexer_by_name(pre["class"])
formatter = formatters.HtmlFormatter()
code_hl = highlight(code, lexer, formatter)
pre.replaceWith(BeautifulSoup(code_hl))
else:
print "No Go"
return unicode(soup)

Il traceback

Traceback (most recent call last):
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:Usersjohnwebdevgooglelibwebapp2webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:Usersjohnwebdevworkspacebsbyviews.py", line 45, in post
p.ftext = utils.formatter(p)
File "C:Usersjohnwebdevworkspacebsbyutils.py", line 18, in formatter
lexer = lexers.get_lexer_by_name(pre["class"])
File "C:Usersjohnwebdevworkspacebsbylibpygmentslexers__init__.py", line 80, in get_lexer_by_name
raise ClassNotFound("no lexer for alias %r found" % _alias)
ClassNotFound: no lexer for alias [u"python"] found

risposte:

0 per risposta № 1

Sì, quello era il problema. Non avevo importato le librerie necessarie per l'esecuzione dello script. Ecco il codice corretto.

import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments import lexers
from pygments import formatters


def formatter(p):
soup = BeautifulSoup(p.otext)
preblocks = soup.findAll("pre")
for pre in preblocks:
if pre.has_key("class"):
code = "".join([unicode(item) for item in pre.contents])
lexer = lexers.get_lexer_by_name("python")
formatter = formatters.HtmlFormatter()
code_hl = highlight(code, lexer, formatter)
pre.replaceWith(BeautifulSoup(code_hl))
return unicode(soup)
else:
return null

L'altro è un altro problema ....

Voglio tirare il nome del lexar in modo dinamico come questo ...

lexer = lexers.get_lexer_by_name(pre["class"])

Tuttavia, quando lo faccio ottengo il seguente errore.

ClassNotFound: no lexer for alias [u"python"] found

Se lo faccio

lexer = lexers.get_lexer_by_name("python")

Funziona, ma solo per Python.